Wireshark中lua脚本介绍
概述
Wireshark是非常强大的报文解析工具,是网络定位中不可缺的使用工具,在物联网中很多为自定义协议,wireshark无法解析,此时lua脚本就有了用武之地。Lua是一个脚本语言,不需要编译可以直接调用,完美解决了自定义报文解析。
代码框架
-- create a new dissector
local NAME = "Doip"
local PORT = 13400
local Doip = Proto(NAME, " Doip Protocol")
-- dissect packet
function Doip.dissector (tvb, pinfo, tree)
end
-- register this dissector
DissectorTable.get("udp.port"):add(PORT, Doip)
如上一个简单的lua代码分为三部分:
- 创建Proto对象
- 创建dissector方法
- 注册解析器
加载解析器到wireshark
将lua文件放在wirekshark的安装目录,在wireshark的根目录中找到init.Lua,打开后将文件中的enable_lua设置为true,并在文件目录中增加我们编写的lua脚本,使用的为dofile(DATA_DIR.."DoIP.lua”)。
Figure 1lua脚本放置位置
Figure 2修改enable_lua为true
Figure 3注册新编写脚本
重新打开wireshark或者shift+ctrl+L快捷键进行lua加载即可进行解析。
Lua插件API接口
- Proto对象
表示一个新的protocol,使用
接口 |
说明 |
proto:__call (name,desc) |
创建Proto对象。name和desc分别是对象的名称和描述,前者可用于过滤器等 |
proto.name |
get名称 |
proto.fields |
get/set字段 |
proto.prefs |
get配置项 |
proto.init |
初始化,无参数 |
proto.dissector |
解析函数,3个参数tvb,pinfo,tree,分别是报文内容,报文信息和解析树结构 |
proto:register_heuristic (listname, func) |
为Proto注册一个启发式解析器,被调用时,参数func将被传入与dissector方法相同的3个参数 |
常用使用:
local NAME1 = "red"
local PORT = 5004
local RTP_PROTO_TYPE = 106
local red = Proto(NAME1, "Red Protocol")
- Protofield
此对象为协议字段,用于解析字段后在描述字上添加节点,根据接口不同可以分成两大类:
整型:
ProtoFiled.{type}(abbr,[name],desc,base,valuestring,mask)
Type包括:uint8,uint16,uint24,uint32,uint64,framenum
Abbr:过滤器的名字
Name:在解析树中的名字
Base:One of base.DEC, base.HEX or base.OCT, base.DEC_HEX, base.HEX_DEC, base.UNIT_STRING or base.RANGE_STRING.
Valuestring:可以用表的形式来进行解析,也可以理解为switch语句
Mask:类型掩码
Desc:字段描述
其他类型:
ProtoField.{type},(abbr,[bane],[desc])
Type包括:float,double,string,stringz,bytes,bool,ipv4,ipv6,ether,oid,guid.
[]内的事可选字段,{}中的事可替换字段。
示例如下:
-- create fields of red
fields_M