Wireshark(Ethereal)で独自パケットフォーマットを解析する方法

Luaプラグインを使うと、独自パケットフォーマットのパーサーを簡単に定義することができます。
Lua - The Wireshark Wiki

プラグインの有効化

Wiresharkのインストールディレクトリ → init.luaを編集

disable_lua = true; do return end;
をコメントアウト
-- disable_lua = true; do return end;
run_user_scripts_when_superuser = falseを
run_user_scripts_when_superuser = trueに

一番最下行に実行したいluaスクリプトを記述したファイルをWiresharkのインストールディレクトリに置き、dofileで指定する。

dofile("hoge.lua")

以下の例は、プロトコル名をTRIVIALとし、UDPの7777番にきたパケットをサブツリーで分解する例です。
hoge.lua

-- trivial protocol example
-- プロトコルの定義
trivial_proto = Proto("trivial","TRIVIAL","Trivial Protocol")
-- パース用の関数定義
function trivial_proto.dissector(buffer,pinfo,tree)
    pinfo.cols.protocol = "TRIVIAL"
    local subtree = tree:add(trivial_proto,buffer(),"Trivial Protocol Data")
    subtree:add(buffer(0,2),"The first two bytes: " .. buffer(0,2):uint())
    subtree = subtree:add(buffer(2,2),"The next two bytes")
    subtree:add(buffer(2,1),"The 3rd byte: " .. buffer(2,1):uint())
    subtree:add(buffer(3,1),"The 4th byte: " .. buffer(3,1):uint())
end
-- udp.portテーブルのロード
udp_table = DissectorTable.get("udp.port")
-- ポート7777番とプロトコルの紐付けをする
udp_table:add(7777,trivial_proto)

他にも本家にいくつかサンプルがあります。
Lua/Examples - The Wireshark Wiki

この中でも、下記のスクリプトは結構利用場面は多いのではないでしょうか。

-- HTTPを 4888-4891番に割り当てる
do
        local tcp_port_table = DissectorTable.get("tcp.port")
        local http_dissector = tcp_port_table:get_dissector(80)
        for port in {4888,4889,4890,4891} do
                tcp_port_table:add(port,http_dissector)
        end
end
  • パケットカウンター
-- 10.0.0.0/8の通信のパケット数を計測する
do
    packets = 0;
    local function init_listener()
        local tap = Listener.new("frame","ip.addr == 10.0.0.0/8")
        function tap.reset()
            packets = 0;
        end
        function tap.packet(pinfo,tvb,ip)
            packets = packets + 1
        end
        function tap.draw()
            print("Packets to/from 10.0.0./8",packets)
        end
    end
    init_listener()
end