Wireshark中lua脚本介绍

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代码分为三部分:

  1. 创建Proto对象
  2. 创建dissector方法
  3. 注册解析器

加载解析器到wireshark

将lua文件放在wirekshark的安装目录,在wireshark的根目录中找到init.Lua,打开后将文件中的enable_lua设置为true,并在文件目录中增加我们编写的lua脚本,使用的为dofile(DATA_DIR.."DoIP.lua”)。

uploading.4e448015.gif转存失败重新上传取消

Figure 1lua脚本放置位置

uploading.4e448015.gif转存失败重新上传取消

Figure 2修改enable_lua为true

uploading.4e448015.gif转存失败重新上传取消

Figure 3注册新编写脚本

重新打开wireshark或者shift+ctrl+L快捷键进行lua加载即可进行解析。

 

Lua插件API接口

  1. 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")

  1. 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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值