Wireshark Lua Dissector - 如何使用 TAP?

我想在我的 Lua Dissector 中解析自定义协议的数据,然后进行一些分析。我试图这样做:

myproto_proto = Proto("myproto", "Myproto Protocol")
m_dest = ProtoField.uint16("myproto.dest", "Destination", base.HEX)
m_src = ProtoField.uint16("myproto.src", "Source", base.HEX)
myproto_proto.fields = { sm_dest, sm_src }

dofile(MYPROTO_PROTO_PATH.."parser.lua")

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

   local subtree = tree:add(myproto_proto, buffer(), "Myproto Protocol Data")
   parse_msg(buffer, pinfo, subtree) -- does the actual parsing and sets the fields
end

udp_table = DissectorTable.get("udp.port")
udp_table:add(9000,myproto_proto)

-- LISTENER / TAP

f_test = Field.new("myproto.dest") -- fails because "field does not exist"
local function my_tap()
   local window = TextWindow.new("Myproto Tap")
   local tap = Listener.new(nil, "myproto")

   local counter = 0
   function remove()
      tap:remove()
   end

   window:set_atclose(remove)

   function tap.packet(pinfo, buffer)
      counter = counter + 1
   end

   function tap.draw(t)
      window:append("Counter: \t" .. counter .. "\n")
   end

   function tap.reset()
      window:clear()
      counter = 0
   end
   retap_packets()
end

register_menu("My Tap", my_tap, MENU_TOOLS_UNSORTED)

我的问题是,我无法使用字段提取器访问解析的数据。那么我该如何在我的 Lua Tap 中获得解析的数据呢?

预先感谢您的帮助。

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

点赞
stackoverflow用户88888888
stackoverflow用户88888888

这是一个已知的问题,即自定义Lua Field对象在OSX上无法使用(它似乎在Windows XP中可以工作但是不适用于Windows 7)。

有几种方法可以从你的dissector中传递数据到你的tap。


选项1:使用共享Lua表格

  1. 创建一个全局字典,以分组编号为键(从pinfo.number,它对dissector和 tap都可见)。
--我们省略了“local”关键字,以便将“dict”作为全局变量
dict = {}
  1. 在你的dissector中,将数据包数据添加到字典中:
dict [pinfo.number] = { dest = m_dest, src = m_src }
  1. 在你的tap中,你可以通过简单的查找来访问数据。
print('dest', dict[pinfo.number].dest )

XXX:需要一个全局变量; 重复存储已经在协议树中保存的变量(应该从tap中访问)。


选项2:使用pinfo.private

这是在开发版本(1.7.0)中添加的。它类似于上面的解决方案。pinfo.private是一个PrivateTable,它是一个只存储字符串的哈希表。

  1. 在你的dissector中,将你的数据添加到数据包的私有表中:
pinfo.private["src"] = tostring(m_src)
pinfo.private["dest"] = tostring(m_dest)
  1. 在您的tap中,从pinfo对象中访问数据:
print('dest', pinfo.private["dest"] )

XXX:只能存储字符串值


选项3:重新解析缓冲区

  1. 在你的tap中,调用你的解析器(即parser.lua)以重新解析传递给tap的数据中的buffer

XXX:重复解析器已经完成的工作(可以将X大型捕获文件的处理时间翻倍)。

2012-02-11 20:54:45