lua - 如何在 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).
解决方案
查看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 增强错误请求。
推荐阅读
- ios - Xcode 尝试在未从模拟器卸载的情况下运行应用程序时显示错误(多配置文件)
- latex - 使用 gnuplot,如何获取具有画布大小的 PDF,包括文本?
- javascript - JavaScript 中的函数是如何实现固有异步的
- json - 使用 powershell 编辑 Json
- python - Python 日志记录失败
- python - 根据 NumPy 中的非标准化对数概率从集合中采样
- python - 网页抓取时如何登录网站
- python - Python 中的粗体注释。相当于 Python 中的 MATLAB '%%'
- java - 将我的文件打印到控制台时遇到问题
- java - Android Room 属性在保存到数据库之前验证?