在 Lua Wireshark 解析器中重新组装数据包。

我正在尝试为基于bplists的Safari远程调试协议编写解析器,并且已经有了一定的成功(当前代码在此处:https://github.com/andydavies/bplist-dissector)。

但是我在重新组装数据包方面遇到了困难。

通常,该协议会发送一个四字节的数据包,其中包含下一个数据包的长度,然后是带有bplist的数据包。

不幸的是,来自iOS模拟器的某些数据包不遵循此约定,四个字节要么附加到bplist数据包的前面,要么附加到前一个bplist数据包的末尾,或者数据包含有多个bplists。

我已经尝试使用desegment_lendesegment_offset重新组装它们,如下所示:

  function p_bplist.dissector(buf, pkt, root)

    -- length of data packet
    local dataPacketLength = tonumber(buf(0, 4):uint())
    local desiredPacketLength = dataPacketLength + 4

    -- if not enough data indicate how much more we need
    if desiredPacketLen > buf:len() then
      pkt.desegment_len = dataPacketLength
      pkt.desegment_offset = 0
      return
    end

    -- have more than needed so set offset for next dissection
    if buf:len() > desiredPacketLength then
      pkt.desegment_len = DESEGMENT_ONE_MORE_SEGMENT
      pkt.desegment_offset = desiredPacketLength
    end

    -- copy data needed
    buffer = buf:range(4, dataPacketLen)

    ...

我在此尝试的是始终将大小字节强制为要解析的数据包的前四个字节,但它不起作用,我仍然会看到一个4字节的数据包,然后是一个_x_字节的数据包。

我可以想到其他管理前面的额外四个字节的方法,但是该协议包含一个查找表距离数据包末尾32个字节,因此需要一种准确地将数据包拆分为bplists的方法。

这是一个示例捕获:http://www.cloudshark.org/captures/2a826ee6045b # 338是一个示例,其中bplist大小在数据的开头,数据中有多个plists。

我是否做得对(在SO上查看其他问题和网络上的示例看起来是这样),或者是否有更好的方法?

点赞
用户238077
用户238077

TCP解析器packet-tcp.ctcp_dissect_pdus()函数,它用于循环解剖TCP流中的PDU;该函数假定PDU由包含足够信息以确定PDU长度的固定长度数据块,以及其余PDU组成。

Lua API中没有这种函数,但这是一个很好的例子可以参考。

另一个例子。我在一年前的测试中使用了这个函数:

local slicer = Proto("slicer","Slicer")
function slicer.dissector(tvb, pinfo, tree)
    local offset = pinfo.desegment_offset or 0

    local len = get_len() -- for tests i used a constant, but can be taken from tvb

    while true do
        local nxtpdu = offset + len

        if nxtpdu > tvb:len() then
            pinfo.desegment_len = nxtpdu - tvb:len()
            pinfo.desegment_offset = offset
            return
        end

        tree:add(slicer, tvb(offset, len))

        offset = nxtpdu

        if nxtpdu == tvb:len() then
             return
        end
    end
end
local tcp_table = DissectorTable.get("tcp.port")
tcp_table:add(2506, slicer)
2013-01-18 11:44:17