首页 > 解决方案 > 如何在 Wireshark Lua 解析器中使用 add_packet_field?

问题描述

我在 Lua 中为我们的自定义协议编写解析器时磕磕绊绊。虽然我有基本的字段提取工作,但我们的许多字段都有与之相关的比例因子。除了原始提取值之外,我还想呈现缩放值。

在我看来,这tree_item:add_packet_field是为这个目的量身定做的。除了我无法让它工作。

我发现Mika 的博客非常有用,并按照他的模式将我的解剖器分成不同的文件等。这一切都有效。

给定一个数据包类型“my_packet”,我有一个 14 位有符号整数“AOA”,我可以很好地提取它

local pref = "my_packet"
local m = {
   aoa        = ProtoField.new("AOA", pref .. ".aoa", ftypes.INT16, nil, base.DEC, 0x3FFF, "angle of arrival measurement"),
}

local option=2
local aoa_scale = 0.1

function m.parse(tree_arg, buffer)
   if option == 1 then
      -- basic field extraction. This works just fine. The field is extracted and added to the tree
      tree_arg:add(m.aoa, buffer)

   elseif option == 2 then
      -- This parses and runs. The item is decoded and added to the tree,
      -- but the value of 'v' is always nil
      local c,v = tree_arg:add_packet_field(m.aoa, buffer, ENC_BIG_ENDIAN)

      -- this results in an error, doing arithmetic on 'nil'
      c:append_text(" (scaled= " .. tostring(v*aoa_scale) .. ")")

   end
end

(为了在声明我的字段时保持一致性,我使用ProtoField.new而不是任何特定于类型的变体)

文档add_packet_field说编码参数是强制性的。

源代码中有一个自述文件说 ENC_BIG_ENDIAN 应该为网络字节顺序数据指定(我的是)。proto_tree_add_item我知道该部分是add_packet_field针对proto_tree_add_item.

基本上,在这一点上,我迷路了。我确实发现这篇2014 年的帖子建议对有限的支持,add_packet_field但现在肯定支持像整数值这样基本的东西?

另外,我确实知道如何声明 a并在解析Field后提取值;tree:add最坏的情况我会回到那个,但肯定有一种更方便的方法来访问刚刚解析的添加到树的值?

Wireshark 版本

3.2.4 (v3.2.4-0-g893b5a5e1e3e)

Compiled (64-bit) with Qt 5.12.8, with WinPcap SDK (WpdPack) 4.1.2, with GLib
2.52.3, with zlib 1.2.11, with SMI 0.4.8, with c-ares 1.15.0, with Lua 5.2.4,
with GnuTLS 3.6.3 and PKCS #11 support, with Gcrypt 1.8.3, with MIT Kerberos,
with MaxMind DB resolver, with nghttp2 1.39.2, with brotli, with LZ4, with
Zstandard, with Snappy, with libxml2 2.9.9, with QtMultimedia, with automatic
updates using WinSparkle 0.5.7, with AirPcap, with SpeexDSP (using bundled
resampler), with SBC, with SpanDSP, with bcg729.

Running on 64-bit Windows 10 (1803), build 17134, with Intel(R) Xeon(R) CPU
E3-1505M v6 @ 3.00GHz (with SSE4.2), with 32558 MB of physical memory, with
locale English_United States.1252, with light display mode, without HiDPI, with
Npcap version 0.9991, based on libpcap version 1.9.1, with GnuTLS 3.6.3, with
Gcrypt 1.8.3, with brotli 1.0.2, without AirPcap, binary plugins supported (19
loaded).

Built using Microsoft Visual Studio 2019 (VC++ 14.25, build 28614).

标签: luawiresharkwireshark-dissector

解决方案


查看try_add_packet_field()源代码,仅FT_支持某些类型,即:

  • FT_BYTES
  • FT_UINT_BYTES
  • FT_OID
  • FT_REL_OID
  • FT_SYSTEM_ID
  • FT_ABSOLUTE_TIME
  • FT_RELATIVE_TIME

[尚未] 支持任何其他FT_类型,包括FT_UINT16,这是您在这里感兴趣的类型,即,其他任何事情都只需要以老式方式完成

如果您希望实现此功能,我建议您在Wireshark Bug Tracker提交 Wireshark 增强错误请求。


推荐阅读