Wireshark的Lua解析器

首先,我完全是Lua的新手,这是我编写Wireshark分解器的第一次尝试。

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

当我通过Lua控制台运行代码时,一切正常。

当代码添加到Wireshark插件目录中时,我收到错误消息

Lua Error: [string "C:\Users...\AppData\Roaming\Wireshark..."]:15: calling 'add' on bad self (number expected, got string)

第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

原文链接 https://stackoverflow.com/questions/10401569

点赞
stackoverflow用户622215
stackoverflow用户622215

你的解剖器代码非常接近正确,但你正在进行界面不接受的额外工作。如果你像下面这样更改你的 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)) --让 Wireshark 来做困难的工作
        t:add(f_text,buf(2,len)) --你已经定义过他们的标签等等
end

你将得到所期望的行为。 "Text" 和 "Length" 标签已经为你的字段定义,因此无需在第15和16行再次提供它们。

2012-05-02 14:42:31