tcp.stream 在wireshark
以下是wireshrak 为获取tcp.stream 所做的工作。Tcp 解析器有一个全局变量guint32 tcp_stream_index;
,然后每个数据包与会话相关联。每个对话数据都存储在一个哈希表中(Wireshark 使用GHashTable)。他们使用 5 元组作为键。如果他们得到新的 5 元组,他们会启动新的对话并在那里增加 tcp_stream_index:
init_tcp_conversation_data(packet_info *pifo)
{
...
tcpd->stream = tcp_stream_index++;
...
}
还有他们如何获得哈希:
/*
* Hash an address into a hash value (which must already have been set).
*/
#define ADD_ADDRESS_TO_HASH(hash_val, addr) { \
const guint8 *ADD_ADDRESS_TO_HASH_data; \
int ADD_ADDRESS_TO_HASH_index; \
ADD_ADDRESS_TO_HASH_data = (addr)->data; \
for (ADD_ADDRESS_TO_HASH_index = 0; \
ADD_ADDRESS_TO_HASH_index < (addr)->len; \
ADD_ADDRESS_TO_HASH_index++) \
hash_val += ADD_ADDRESS_TO_HASH_data[ADD_ADDRESS_TO_HASH_index]; \
}
...
hash_val = 0;
ADD_ADDRESS_TO_HASH(hash_val, &key->addr1);
hash_val += key->port1;
ADD_ADDRESS_TO_HASH(hash_val, &key->addr2);
hash_val += key->port2;
...
在数据包中添加 flowid
这是一个用lua编写的wireshark监听器的简单示例。但是你需要函数 mk_flowid、update_conversation_data、show_gathered_statics。
local tap
local conversations = {}
local function packet(pinfo, tvb, userdata)
local id = mk_flowid(pinfo.src, pinfo.src_port,
pinfo.dst, pinfo.dst_port, pinfo.ipproto)
local conv = converstaion[id]
update_conversation_data(conv)
-- Also you can output to a file
-- to_file(pinfo.number, id)
end
local function draw(userdata)
print_gathered_statistics(conversations)
end
local function reset(userdata)
conversations = {}
end
local function show_myconv()
tap = Listener.new()
tap.packet = packet
tap.draw = draw
tap.reset = reset
end
register_stat_cmd_arg('myconv', show_myconv)
并启动 tshark:
tshark -X lua_script:myconv.lua -z myconv -r in.pcap