0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

謎のバイナリプロトコルを手動で喋りたい時の小技

Posted at

TL;DR

tail -f hoge.bin | nc localhost 8000 | hexdump -C

hoge.bin に向かって printf "\x01\x02" とか打つと捗る

前置き

未知のプロトコルを実装したいとき、手元で作ったバイナリを投げて実験しながら実装していきたいですよね。

そこで丹精込めて作ったお手製のバイナリhoge.binを、特定の TCP サーバーに投げようとすると

cat hoge.bin | nc localhost 8000 | hexdump -C

みたいなことをすると思います。

出力は hexdump に渡すと、サーバーから返ってきたバイナリをギリ人間が解読できる状態にできてとても便利です。

ただ、nc は入力が EOF になるとすぐにコネクションを閉じてしまいます。サーバーからのレスポンスを受け取り切る前に終了したり、1コネクションが1ラリーで終了しないインタラクティブなプロトコルの実験ができなかったりします。

困りましたね。

銀の弾丸

要は EOF を送らなければ良いので、cat の代わりに tail -f でファイルを開いてあげましょう。

tail -f hoge.bin | nc localhost 8000 | hexdump -C

これで hoge.bin の内容を出力し終えた後も nc が終了することはありません。
しかも tail -f のおかげで hoge.bin に追記すればそれも nc で送信してくれます。最高ですね。

あとは出力結果を見ながら、printf "\x01\x02" >> hoge.bin みたいな感じで追記してあげれば、いつでもどこでもオレオレプロトコルが喋れちゃいます。

余談

こんなことしなくても nc のオプションをよく調べたり、やりたいことにぴったりなスマートなコマンドを探したりすれば解決できると思いますが、そんな使用頻度の低い知識はどうせすぐ忘れるので、よく使うコマンドとよく使うオプションで代替できるのならそれが一番です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?