Wireshark:自定义类型帧解析
文章目录
- 1. 前言
- 2. 背景
- 3. 开发 Lua 插件
1. 前言
限于作者能力水平,本文可能存在谬误,因此而给读者带来的损失,作者不做任何承诺。
2. 背景
Wireshark
不认识用 tcpdump
抓取的数据帧,仔细分析相关代码和数据帧后,发现是交换芯片在以太网帧头
和 IP 帧头
之间插入了 6 个字节
。由于要做对这些数据帧做分析工作,同时又想利用 Wireshark 的便利,于是想到 Wireshark
的 Lua
插件功能。
3. 开发 Lua 插件
在应用 Lua
插件解析数据帧前,Wireshark
的解析如下图:
可见 Wireshark
无法正常解析数据帧。接下来编写 Lua
脚本 edsa.lua
如下:
-- 定义协议
local edsap = Proto("edsa", "EDSA Protocol")
-- 添加字段
local f_edsa_field1 = ProtoField.uint8("edsa.field1", "Field 1", base.DEC)
local f_edsa_field2 = ProtoField.uint8("edsa.field2", "Field 2", base.DEC)
local f_edsa_field3 = ProtoField.uint16("edsa.field3", "Index", base.DEC)
local f_edsa_field4 = ProtoField.uint8("edsa.field4", "Field 4", base.DEC)
local f_edsa_field5 = ProtoField.uint8("edsa.field5", "Field 5", base.DEC)
edsap.fields = {f_edsa_field1, f_edsa_field2, f_edsa_field3, f_edsa_field4, f_edsa_field5}
-- 定义 dissector 函数
-- 注意,这里 tvbuf 的数据,不包含以太网帧头的 14 字节
function edsap.dissector(tvbuf, pinfo, tree)
if tvbuf:len() < 6 then return end
pinfo.cols.protocol = edsap.name
local edsa_tree = tree:add(edsap, tvbuf(0,6), "EDSA Protocol")
edsa_tree:add(f_edsa_field1, tvbuf(0,1):uint())
edsa_tree:add(f_edsa_field2, tvbuf(1,1):uint())
edsa_tree:add(f_edsa_field3, tvbuf(2,2):uint())
edsa_tree:add(f_edsa_field4, tvbuf(3,1):uint())
edsa_tree:add(f_edsa_field5, tvbuf(4,1):uint())
-- 处理剩余的数据
local new_tvbuf = tvbuf(6+2):tvb()
Dissector.get("ip"):call(new_tvbuf, pinfo, tree)
end
-- 注册到以太网帧类型 0xdada :
-- 当 wireshark 发现以太网帧的 EtherType 字段为 0xdada 时,
-- 则调用 dsap.dissector()
local edsa_type = DissectorTable.get("ethertype")
edsa_type:add(0xdada, edsap)
然后把 edsa.lua
放到下图 Wireshark
设定的路径中:
然后重启 Wireshark
加载 tcpdump
抓取的数据包:
可以看到,已经可以正常解析了。
在开发 Lua
脚本的过程中,可以开启 Wireshark
的控制台
,Lua
的 print()
信息会输出到控制台,帮助定位开发过程中遇到的问题:
调试完成后,记得关闭它。