首页 > 解决方案 > Wireshark lua 脚本剖析 CoAP 选项

问题描述

我正在编写 lua 脚本来剖析 coap 协议。但是,如果有几个相同的选项,我无法获得第二个或更高版本的 coap 选项(URI-Path)。

do
 local test_proto = Proto("test_proto", "Test Protocol")
 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 。如何获得第二个或更高版本的相同选项字段?

标签: luawiresharkcoap

解决方案


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

do
    local test_proto = Proto("test_proto", "Test Protocol")
    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-带有 lua 的值


推荐阅读