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

And*_*ies 7 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)

    ...
Run Code Online (Sandbox Code Playgroud)

我在这里尝试做的总是强制大小字节是要解析的数据包的前四个字节,但它不起作用我仍然看到一个4字节的数据包,后面跟着一个x字节数据包.

我可以想到在前面管理额外的四个字节的其他方法,但协议包含一个查找表,该数据包从数据包的末尾开始是32个字节,因此需要一种方法将数据包准确地拼接到bplists中.

以下是一个示例上限:http://www.cloudshark.org/captures/2a826ee6045b#338是一个数据包的示例,其中bplist大小位于数据的开头,并且数据中有多个plist.

我这样做是对的吗(关于SO的其他问题,以及我似乎在网上的例子)还是有更好的方法?

gra*_*ite 5

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)
Run Code Online (Sandbox Code Playgroud)

  • 尽管帖子很老,但对于将来,我认为值得一提的是,从Wireshark 1.99.2开始,有一个** tcp_dissect_pdus的Lua API。在这里看看:[wireshark.org/docs](https://www.wireshark.org/docs/wsdg_html_chunked/lua_module_Proto.html#lua_fn_dissect_tcp_pdus_tvb__tree__size__func__func___desegment__) (4认同)