Lua中的Wireshark解析器

8

首先,我对Lua完全不熟悉,这是我第一次尝试编写Wireshark分析器。

我的协议很简单 - 2字节长度字段,后跟该长度的字符串。

当我通过Lua控制台运行代码时,一切都按预期工作。

当将代码添加到Wireshark插件目录中时,出现错误

Lua错误:[string "C:\ Users ... \ AppData \ Roaming \ Wireshark ..."]:15:在错误的self上调用'add'(期望数字,得到字符串)

第15行对应于t:add(f_text ...行。

有人能解释执行方法之间的差异吗?

do
    local p_multi = Proto("aggregator","Aggregator");

    local f_len = ProtoField.int16("aggregator.length","Length",base.DEC)
    local f_text = ProtoField.string("aggregator.text","Text")

    p_multi.fields = { f_len, f_text }

    local data_dis = Dissector.get("data")

    function p_multi.dissector(buf,pkt,root)
            pkt.cols.protocol = "Aggregator"
            local len = buf(0,2):int()
            local t = root:add(p_multi,buf(0,len+2))
            t:add(f_len,buf(0,2),"Length: " .. buf(0,2):int())
            t:add(f_text,buf(2,len),"Text: " .. buf(2,len):string())
    end

    local tcp_encap_table = DissectorTable.get("tcp.port")
    tcp_encap_table:add(4321,p_multi)
end

我要注意一下,我使用了http://www.wireshark.org/docs/wsug_html_chunked/wslua_dissector_example.html和http://wiki.wireshark.org/Lua/Dissectors作为灵感来源。是否有任何好的API文档来源? - IgnoredAmbience
用户指南的第11章是Lua接口的API文档。第11.10、11.11和11.12节是功能接口。除此之外,实际上没有任何文档可用。看起来你的分析器应该可以正常工作。你的代码显示你获取了数据分析器的引用(local data_dis = Dissector.get("data")),但你没有使用它。这是你完整的分析器代码吗?如果不是,你可能会在这里未显示的某个地方意外地改变了t - multipleinterfaces
这是我的完整解析器代码,data_dis是与链接的示例相关的遗留问题。 - IgnoredAmbience
1个回答

7
你的分析器代码非常接近正确,但你做了额外的工作,接口不会接受。如果你按以下方式更改你的dissector函数,就可以解决问题:
function p_multi.dissector(buf,pkt,root)
        pkt.cols.protocol = "Aggregator"
        local len = buf(0,2):int()
        local t = root:add(p_multi,buf(0,len+2))
        t:add(f_len,buf(0,2)) --let Wireshark do the hard work
        t:add(f_text,buf(2,len)) --you've already defined their labels etc.
end

您将获得所需的行为。 "文本"和"长度"标签已经为您的字段定义,因此无需在第15行和第16行再次提供它们。


谢谢。我在下班前匆忙发布了这个问题,现在花时间更多地思考它,它现在更有意义了。尽管如此,我仍然困惑为什么它可以通过Lua控制台工作! - IgnoredAmbience

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接