31. 31
postdissector例
TCPの場合に送信元/先IPアドレス
とポートの組み合わせをPacket
Detailsに追加。
http://wiki.wireshark.org/Lua/Dissectors より
-- trivial postdissector example
-- declare some Fields to be read
ip_src_f = Field.new("ip.src")
ip_dst_f = Field.new("ip.dst")
tcp_src_f = Field.new("tcp.srcport")
tcp_dst_f = Field.new("tcp.dstport")
-- declare our (pseudo) protocol
trivial_proto = Proto("trivial","Trivial Postdissector")
-- create the fields for our "protocol"
src_F = ProtoField.string("trivial.src","Source")
dst_F = ProtoField.string("trivial.dst","Destination")
conv_F = ProtoField.string("trivial.conv","Conversation","A Conversation")
-- add the field to the protocol
trivial_proto.fields = {src_F, dst_F, conv_F}
-- create a function to "postdissect" each frame
function trivial_proto.dissector(buffer,pinfo,tree)
-- obtain the current values the protocol fields
local tcp_src = tcp_src_f()
local tcp_dst = tcp_dst_f()
local ip_src = ip_src_f()
local ip_dst = ip_dst_f()
if tcp_src then
local subtree = tree:add(trivial_proto,"Trivial Protocol Data")
local src = tostring(ip_src) .. ":" .. tostring(tcp_src)
local dst = tostring(ip_dst) .. ":" .. tostring(tcp_dst)
local conv = src .. "->" .. dst
subtree:add(src_F,src)
subtree:add(dst_F,dst)
subtree:add(conv_F,conv)
end
end
-- register our protocol as a postdissector
register_postdissector(trivial_proto)
パケット内で読み取るフィー
ルドを宣言
新しいプロトコルを宣言
宣言したプロトコルで使用
するフィールドを定義
postdissectorを定義
postdissectorを登録
39. 39
TAP例
do
local function menuable_tap()
-- Declare the window we will use
local tw = TextWindow.new("Address Counter")
-- This will contain a hash of counters of appearances of a certain address
local ips = {}
-- this is our tap
local tap = Listener.new();
function remove()
-- this way we remove the listener than otherwise will remain running indifinitelly
tap:remove();
end
-- we tell the window to call the remove() function when closed
tw:set_atclose(remove)
-- this function will be called once for each packet
function tap.packet(pinfo,tvb)
local src = ips[tostring(pinfo.src)] or 0
local dst = ips[tostring(pinfo.dst)] or 0
ips[tostring(pinfo.src)] = src + 1
ips[tostring(pinfo.dst)] = dst + 1
end
-- this function will be called once every few seconds to update our window
function tap.draw(t)
tw:clear()
for ip,num in pairs(ips) do
tw:append(ip .. "t" .. num .. "n");
end
end
-- this function will be called whenever a reset is needed
-- e.g. when reloading the capture file
function tap.reset()
tw:clear()
ips = {}
end
end
-- using this function we register our fuction
-- to be called when the user selects the Tools->Test->Packets menu
register_menu("Test/Packets", menuable_tap, MENU_TOOLS_UNSORTED)
end
Listener.new(“frame”, “ip.addr == 10.0.0.0/8”)
のようにフィルタリングをすることも可能。
TAPを生成
送信元または送信先ごとにパケット数をカウント
パケットを受け取るたびに
呼び出される関数
テキストウィンドウに
結果を表示
Toolsメニューにスクリプトを
実行するメニューを追加
http://www.wireshark.org/docs/wsug_html_chunked/wslua_tap_example.html より