使用 Wireshark lua 脚本解析 CoAP 选项

我正在编写 Lua 脚本来解析 CoAP 协议。然而,如果有多个相同的选项,我无法获取第二个或之后的 CoAP 选项(URI-Path)。

do
local test_proto = Proto("test_proto", "测试协议")
local test_uripath = ProtoField.string("test.uripath", "Uri-Path")
test_proto.fields = {test_uripath}
local coap_uripath = Field.new("coap.opt.uri_path")
function test_proto.dissector(tvbuffer, pinfo, treeitem)
  local subtree = treeitem:add(test_proto)
  subtree:add(test_uripath, tostring(coap_uripath().value))
end
register_postdissector(test_proto)
end

即使 CoAP URI-Path 选项具有多个值(如下所示),子树中仅显示第一个 URI-Path。

Opt Name: #1: URI-Path: XXX
Opt Name: #2: URI-Path: YYY

我只能使用 coap.opt.uri_path 获取 XXX。如何获取第二个或之后相同的选项字段?

点赞
用户2755698
用户2755698

如果您对所有字段都感兴趣而不仅仅是第一个字段,那么您需要处理整个表。例如:

do
    local test_proto = Proto("test_proto", "测试协议")
    local test_uripath = ProtoField.string("test.uripath", "Uri-Path")
    test_proto.fields = {test_uripath}

    local coap_uripath = Field.new("coap.opt.uri_path")

    function test_proto.dissector(tvbuffer, pinfo, treeitem)
        local subtree = treeitem:add(test_proto)
        local coap_uripath_table = { coap_uripath() }

        for i,uripath in ipairs(coap_uripath_table) do
            subtree:add(test_uripath, tostring(uripath.value))
        end
    end

    register_postdissector(test_proto)
end

另请参见:

https://osqa-ask.wireshark.org/questions/35682/lua-accessing-multiple-smb2msg_id-values

https://osqa-ask.wireshark.org/questions/1579/fetching-multiple-named-values-with-lua

2019-06-12 21:02:00