在Lua Wireshark解剖器中重新组装PDU

在一个系统中,我有一个自定义协议,我希望实现一个 Wireshark 分析器,这样我就可以使用 Wireshark 分析通信。

  • 对象通过协议发送,我们称它们为“消息”。每个消息可以很大,例如100MB,它们也可以非常小,例如50字节。

  • 每个消息被分成大约1KB的块,并标记有序列号和GUID消息ID,这些可以在另一端用于重新组装消息。

到目前为止,我已经成功地制作了一个分析器,可以将所有块单独记录到 Wireshark 中,但我想更进一步,也将所有消息(组装成消息的块)记录在 Wireshark 中。这可行吗?如何实现?也许可以在下面开发的分析器之上实现一个分析器?

如果可能在下面的分析器之上实现一个分析器,那么我怎样才能确保它仅分析 myproto PDUs?下面的分析器在特定的tcp端口上触发,但对于第二个阶段的分析器,这不起作用...

myproto_proto = Proto("myproto", "My Protocol")

function myproto_proto.dissector(buffer, pinfo, tree)
    pinfo.cols.protocol = "myproto"

    local message_length = buffer(0, 4):le_uint()+4

    if message_length>pinfo.len then
        pinfo.desegment_len = message_length
        pinfo.desegment_offset = 0
        return;
    end

    local subtree = tree:add(myproto_proto, buffer(), "My Protocol Data")
    local packet = subtree:add(buffer(0, message_length), "Chunk")
    packet:add(buffer(0, 4), "Packet length: " .. buffer(0, 4):le_uint())
    packet:add(buffer(32, 16), "Message ID")
    packet:add(buffer(48, 4), "Block ID: " .. buffer(48, 4):le_uint())
    packet:add(buffer(52, 4), "Max Block ID: " .. buffer(52, 4):le_uint())
    packet:add(buffer(68, message_length-68-20), "Data")

    pinfo.desegment_len = 0
    pinfo.desegment_offset = message_length
    return

end

tcp_table = DissectorTable.get("tcp.port")
tcp_table:add(1234, myproto_proto)
点赞
用户2888103
用户2888103

假设您已经创建了第二个解析器 msgproto。由于您之间似乎没有多路复用,因此您不需要设置解析器表。相反,在myproto_proto.dissector的末尾,您可以执行以下操作:

msgproto.dissector:call(buffer(68, message_length-68-20):tvb, pinfo, tree)

这将传递所有块数据给您的 msgproto。在消息协议解析器中,您可以使用块协议的字段,当然还有只包含一个块数据的 tvb。现在,您需要将块组合到一个光辉的巨大 tvb 中。使 msgproto 具有状态:

local stateMap = {}
function msgproto.init()
    stateMap = {}
end

将您的 tvb 转换为 ByteArray,并与来自其他调用到您的解析器的数组一起存储到 stateMap 中。当您将所有数据组装成一个数组,称之为 oarr 时,可以从中创建一个 tvb:

local otvb = ByteArray.tvb(oarr, "message data")
-- 从0开始,需要传递tvb范围而不是tvb。
stree:add(msgproto.fields.payload, otvb(0))

假设您的 payload 字段类型为 Protofield.bytes。此代码将在Wireshark窗口底部,您常规的“Frame”窗格旁边创建一个名为“message data”的新数据窗格。

我不确定Wireshark会对您的超大缓冲区作何反应。除此之外,我非常确信上面概述的方法会起作用。我没有按照此确切顺序使用所述的技术,但我已经使用了所有所述的技术,例如根据纯Lua中的数据deflated创建一个新的填充了数据的字节窗格。

2014-10-08 19:22:31