23
32

More than 5 years have passed since last update.

Wireshark で独自プロトコルを解析 (dissector with Lua)

Posted at

image.png

Wiresharkは有名なパケット解析ソフトです。
このソフト、独自プロトコルをparseする拡張をプログラミングで作ることができます!

独自のバイナリプロトコル

例として、図のようなプロトコルをWiresharkでparseできるようにします。

image.png

Wireshark拡張 Luaスクリプト

Luaというスクリプト言語で記述して、以下の場所に配置すると動きます。

環境 .lua ファイルの配置場所
Windows %APPDATA%/Wireshark/plugins
Mac/Linux ~/.config/wireshark/plugins

Luaソースコード

my_proto = Proto("my_proto","My Protocol (description)")

my_proto.fields.len = ProtoField.uint16("my_proto.len","Len")
my_proto.fields.data = ProtoField.bytes("my_proto.data","Data", base.SPACE)

function my_proto.dissector(buffer, pinfo, tree)
    pinfo.cols.protocol = "MYPROTO"

    local subtree = tree:add(my_proto, buffer(), "My Protocol")
    local len = buffer(0, 2):le_uint() -- le_ はリトルエンディアン
    local data = buffer(2, len)

    local packet_id = data(0, 4):le_uint()

    subtree:add_le(my_proto.fields.len, len) -- le_ はリトルエンディアン
    subtree:add(my_proto.fields.data, data)
end

-- TCP port 20000 を自動的にmy_protoと解釈する
tcp_table = DissectorTable.get("tcp.port")
tcp_table:add(20000, my_proto)

Wiresharkで実際にみてみる

先頭に長さ: 5 を付加した hello という5文字を送ってみました。
Protocol欄が MYPROTO になり、TCPの下に My Protocol のツリーができています!

image.png

用語

pinfo, subtree, buffer はWiresharkのUIに表示される部分を表しています。
image.png

情報の集め方

Wiresharkで独自プロトコル定義を作るなんて、そんなにメジャーなことではないと思うので情報が少ないです。
Luaから呼び出す関数などは以下の公式ドキュメントがものすごく役立ちました。

Wireshark’s Lua API Reference Manual
https://www.wireshark.org/docs/wsdg_html_chunked/wsluarm_modules.html

あとは実際に書かれた .lua ファイルの例を読むと具体的でわかりやすいと思います。
このWireshark拡張は Dissector という名前らしいので、 dissector example 等で検索すると少しあります。

以下の LLUDP Dissector はかなり複雑な解析まで書かれているので、応用できそうです。

LLUDP Dissector
http://opensimulator.org/wiki/LLUDP_Dissector

23
32
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
23
32