如何在 Wireshark 中向字段添加过滤器

How to add filter to field in Wireshark

我正在尝试向 Wireshak 中的 字段 添加过滤器。
我的解析器名称是:"basic".
它有 3 个字段 - 字段 1、字段 2、字段 3。
每个字段可以有一个字符串值。
我希望在 Wireshark 上能够按特定字段进行过滤,例如:basic.field1。 (就像你寻找 tcp.len 一样)

我该怎么做?

您必须声明这些字段,将它们分配给您的协议,并在适当的时候将它们添加到树中。目前 Lua 支持 2 种不同类型的字符串,ftypes.STRING 类型用于已知固定长度的字符串,ftypes.STRINGZ 类型是 NULL (零)终止的字符串,所以你如何声明字段将取决于它们是 2 种类型中的哪一种。

不幸的是,尽管documentation listing ftypes.UINT_STRING as a supported type, it isn't as can be seen in the source code for wslua_proto_field.c。当长度字段位于字符串之前以指示字符串的字节长度时,这种类型的字符串适用。无论如何,它目前不适用于 Lua-based 解剖器。

因此,举个例子,假设您的协议使用 UDP/33333 作为其传输和端口号,并且其 3 个字段由上述 3 种类型的字符串中的每一种组成,即:

  • field1:fixed-length 12 字节的字符串。
  • field2: NULL-terminated 任意长度的字符串。
  • field3:一个计数字符串,前面是 big-endian(网络)字节顺序的 2 字节长度字段。

鉴于这些假设,下面将剖析数据包:

-- Protocol
local p_basic = Proto("basic", "Basic Protocol")

-- Fields
local f_basic_field1 = ProtoField.string("basic.field1", "Field1")
local f_basic_field2 = ProtoField.stringz("basic.field2", "Field2")
local f_basic_field3 = ProtoField.string("basic.field3", "Field3")

p_basic.fields = { f_basic_field1, f_basic_field2, f_basic_field3 }

-- Dissection
function p_basic.dissector(buf, pinfo, tree)
    local basic_tree = tree:add(p_basic, buf(0,-1))

    pinfo.cols.protocol:set("BASIC")

    basic_tree:add(f_basic_field1, buf(0, 12))

    local strz = buf(12):stringz()
    local field2_len = string.len(strz) + 1
    basic_tree:add(f_basic_field2, buf(12, field2_len))

    local field3_len = buf:range(12 + field2_len, 2):uint()
    basic_tree:add(f_basic_field3, buf(12 + field2_len + 2, field3_len))
end

-- Registration
local udp_table = DissectorTable.get("udp.port")
udp_table:add(33333, p_basic)

如果你想测试这个,首先将上面的 lua 代码保存到你的个人插件目录(通过 Help -> About Wireshark -> Folders -> Personal Plugins 找到)中的文件,例如 basic.lua。然后您可以使用以下十六进制字节对其进行测试:

0000  00 0e b6 00 00 02 00 0e b6 00 00 01 08 00 45 00
0010  00 37 00 00 40 00 40 11 b5 ea c0 00 02 65 c0 00
0020  02 66 82 35 82 35 00 23 22 32 48 65 6c 6c 6f 20
0030  77 6f 72 6c 64 21 48 69 20 74 68 65 72 65 00 00
0040  04 42 79 65 21

将这些字节保存到文本文件中,例如 basic.txt。启动 Wireshark 并通过 File -> Import from Hex Dump... -> Filename:basic.txt -> OK 导入文件。您应该看到 3 个字段被分解为 "Basic Protocol".

的一部分

有关 Lua 解析器的进一步帮助,您可能需要参考以下一项或多项: