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

时间:2013-01-17 20:19:13

标签: lua wireshark wireshark-dissector

我正在尝试为基于bplists的Safari远程调试协议编写一个解剖器并且已经相当成功(当前代码在这里:https://github.com/andydavies/bplist-dissector)。

我在重新组装数据包时遇到了困难。

通常,协议发送一个包含4个字节的数据包,其中包含下一个数据包的长度,然后是包含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大小位于数据的开头,并且数据中有多个plist。

我是否正确行事(关于SO的其他问题以及我似乎在网上的例子)还是有更好的方法?

1 个答案:

答案 0 :(得分:4)

TCP Dissector packet-tcp.c有tcp_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)
相关问题