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”)。

转存失败重新上传取消

Figure 1lua脚本放置位置

转存失败重新上传取消

Figure 2修改enable_lua为true

转存失败重新上传取消

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 = ProtoField.uint8 (NAME1 .. ".M", "M",base.HEX,Payload_type,0x80)

fields_pt = ProtoField.uint8 (NAME1 .. ".PT", "PT", base.DEC,Payload_type,0x7F)

fields_seqno   = ProtoField.uint16(NAME1 .. ".seqno", "Sequence number")

fields_h264bytes = ProtoField.bytes(NAME1 .. ".bytes", "H264Data")

fields_fec        = ProtoField.bytes(NAME1 .. ".fec", "FEC Payload")

这些字段添加到结构树种后显示如下:

转存失败重新上传取消

  1. Tvb

Testy vitual Buffer表示报文缓存,也就是实际报文数据,可以通过一下方法将报文数据中解析出信息,接口如下:

tvb:__tostring()

将报文数据转化为字符串,可用于调试

tvb:reported_len()

get tvb的(not captured)长度

tvb:len()

get tvb的(captured)长度

tvb:reported_length_remaining()

获取当前tvb的剩余长度,如果偏移值大于报文长度,则返回-1

tvb:offset()

返回原始偏移

常用的字段是tvb:len()

  1. Pinfo

报文信息(packet information),接口如下:

接口

说明

pinfo.len pinfo.caplen

get报文长度

pinfo.abs_ts

get报文捕获时间

pinfo.number

get报文编号

pinfo.src pinfo.dst

get/set报文的源地址、目的地址

pinfo.columns pinfo.cols

get报文列表列(界面)

获得列表信息后就可以设备该列文本,比如

-- show protocol name in protocol column

pinfo.cols.protocol = “RED”

如上,可以将协议类型显示为RED

  1. Treeitem

解析树,用来展示字段的树形结构

接口

说明

tree_item:add_packet_field(protofield, [tvbrange], encoding, [label])

使用协议字段创建一个子树,可以根据入参调整大小字节序ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN

treeitem:add([protofield], [tvbrange], [value], [label])

增加一个子树,并返回该子树,使用大字节序解析

treeitem:add_le([protofield], [tvbrange], [value], [label])

增加一个子树,并返回该子树,使用小字节序解析

treeitem:append_text

附加文本到这个表

treeitem:prepend_text(text)

在表前增加文本

treeitem:add_proto_expert_info(expert, [text])

设置树项的专家标志并将专家信息添加到包中。

  1. DissectorTable

这个是一个具体协议解析表,比如TCP的解析表tcp.port包括http,SMTP,ftp等

此对象的接口如下:

接口

说明

DissectorTable.get(name)

get名为name的解析表的引用

dissectortable:add(pattern, dissector)

将Proto或Dissector对象添加到解析表,即注册。pattern可以是整型值,整型值范围或字符串,这取决于当前解析表的类型

dissectortable:remove(pattern, dissector)

将满足pattern的一个或一组Proto、Dissector对象从解析表中删除

  1. 其他API接口

https://www.wireshark.org/docs/wsdg_html_chunked/lua_module_Proto.html

  1. 样例:

local version_str = string.match(_VERSION, "%d+[.]%d*")

local version_num = version_str and tonumber(version_str) or 5.1

local bit = (version_num >= 5.2) and require("bit32") or require("bit")

-- create a new dissector to decode rtp private payload

local NAME1          = "red"

local PORT           = 5004

local RTP_PROTO_TYPE = 106

local red            = Proto(NAME1, "Red Protocol")

-- create fields of red

fields_M             = ProtoField.uint8 (NAME1 .. ".M", "M", base.HEX,Payload_type,0x80)

fields_pt            = ProtoField.uint8 (NAME1 .. ".PT", "PT", base.DEC,Payload_type,0x7F)

fields_seqno         = ProtoField.uint16(NAME1 .. ".seqno", "Sequence number")

fields_h264bytes     = ProtoField.bytes(NAME1 .. ".bytes", "H264Data")

fields_fec           = ProtoField.bytes(NAME1 .. ".fec", "FEC Payload")

red.fields           = { fields_M, fields_pt, fields_seqno, fields_h264bytes,fields_fec }

local RTP_dis        = Dissector.get("rtp")

local H264_dis       = Dissector.get("h264")

local Data_dis       = Dissector.get("data")

-- dissect packet

function red.dissector(tvb, pinfo, tree)

length = tvb:len()

if length == 0 then return end

-- decode private header

local subtree = tree:add(red, tvb(0,3))

subtree:add(fields_M, tvb(0,1))

subtree:add(fields_pt, tvb(0,1))

subtree:add(fields_seqno, tvb(1,2))

-- show protocol name in protocol column

pinfo.cols.protocol = red.name

local fec_id = tvb(0,1):uint()

local fec_type = bit.band(fec_id,0x7F)

if fec_type == 109 then

tree:add(fields_fec,tvb(3))

else

H264_dis:call(tvb(3):tvb(), pinfo, tree)

end

end

--decode first layer  as rtp

local udp_dissector_table = DissectorTable.get("udp.port")

udp_dissector_table:set(PORT,RTP_dis)

-- register this dissector

-- DissectorTable.get("rtp.pt"):add(PORT, red)

--decode private protocol layer  3-bytes private datas + standard h264

local rtp_dissector_table = DissectorTable.get("rtp.pt")

rtp_dissector_table:set(RTP_PROTO_TYPE,red)

附录:

red.dissector(tvb, pinfo, tree)

参考:https://blog.csdn.net/qq_40421919/article/details/103516694

Wireshark中lua脚本介绍相关推荐

  1. Redis 中 Lua 脚本的应用和实践

    引言 前段时间组内有个投票的产品,上线前考虑欠缺,导致被刷票严重.后来,通过研究,发现可以通过 redis lua 脚本实现限流,这里将 redis lua 脚本相关的知识分享出来,讲的不到位的地方还 ...

  2. SOMv3.3.3二次开发中LUA脚本对机基础操作指南

    前言 先感谢亲爱的学长,没有他们,我一个人根本无法完成这篇博客 顿首,顿首,再顿首!!! 本篇博客属于实验记录,由于LUA脚本较为简单,所以本博客不多做深入探讨,基本上是把官方的用法更为详细地记录一下 ...

  3. Lua脚本介绍以及编辑器的介绍

    编辑器软件LuaStudio 一:Lua脚本 说来也巧,redis的大老板给了你解决这种问题的方法,那就是Lua脚本,而且redis的最新版本也支持Lua Script debug,这应该也是未来Re ...

  4. lua table insert_超详细的sysbench oltp-数据库性能测试中lua脚本解剖

    概述 在sysbench0.4的后期版本中sysbench已经取消了test中的oltp模式,换而代之的是oltp的lua脚本.这一改变大大的提升了sysbench的灵活性.用户可以结合业务来定制lu ...

  5. cryengine3中lua脚本模块集成笔记

    公司上个仿真项目使用了cryengine3来制作,所以有机会接触和分析世界顶尖的引擎.作为一个通用引擎,那么必须具备很好的扩展性,让用户能够自定义开发,其中脚本是不可缺少的一个模块,与unreal e ...

  6. hiredis中lua脚本调用

    想要直接用hiredis调用lua脚本,研究了好久发现hiredis源码好像不支持调用eval,因为hiredis中是用空格来分割各个参数的,但是lua的return和结果之间一定会有空格. 改了一下 ...

  7. 深入理解redis中的lua脚本

    本文来说下redis中的lua脚本 文章目录 概述 Lua简介 使用Lua脚本的好处 Redis+Lua实现限流 本文小结 概述 今天讲一些redis和lua脚本的相关的东西,lua这个脚本是一个好东 ...

  8. Redis Lua脚本的详细介绍以及使用入门

    Redis Lua脚本的详细介绍以及使用入门. 文章目录 Redis Lua脚本的引入 开源软件的可扩展性 Redis的扩展性脚本 Redis Lua脚本的基本使用 通过EVAL命令执行Lua脚本 通 ...

  9. redis中的事务、lua脚本和管道的使用场景

    https://blog.csdn.net/fangjian1204/article/details/50585080 事务 redis中的事务并不像mysql中那么完美,只是简单的保证了原子性.re ...

  10. 添加lua_非关系型数据库Redis之Lua脚本

    [本文详细介绍了非关系型数据库Redis中Lua脚本的基本概念和使用方法,欢迎读者朋友们阅读.转发和收藏!] 1 Lua 简介 Lua 是一个小巧的脚本语言,其设计目的是为了嵌入应用程序中,从而为应用 ...

最新文章

  1. Windows Mobile 开发环境搭建(1)
  2. strtus2改成springboot_jdk1.6环境下struts2改spring boot方案
  3. 详解 javascript中offsetleft属性的用法(转)
  4. python连接sqlite数据库的代码_Python3实现连接SQLite数据库的方法
  5. 【HihoCoder - 1502】最大子矩阵(二维前缀和,尺取)
  6. eBay和PayPal公布分拆细节:双方还将紧密合作。
  7. Hamcrest匹配器框架
  8. 数字 IC 技能拓展(18)如何快速上手 FPGA 开发板呢
  9. 如何查看同一服务器上挂有多少个网站
  10. 判断一个整数能否同时被3和5整除
  11. Chrome 93 版本新特性 Chrome 93 版本发行说明
  12. P2197 nim博弈
  13. npm scripts
  14. Shang Nicht mehr Hut Wird Bambus Faser Un
  15. 爆笑的评论,真是不能相信看起来义正辞严的话啊。
  16. 康耐视visionpro工具-数据分析CogDataAnalysisTool
  17. Unity 开发 Hololens2 MR应用程序
  18. element ui 日期选择器 选择日期范围 添加默认值
  19. BBS论坛系统的需求
  20. 遥感IDL二次开发(大气校正)

热门文章

  1. JDK自带的命令行工具
  2. Vue页面跳转动画效果实现
  3. 发票查验API给财务工作带来了哪些便利?
  4. 重装系统后计算机无法联网,韩博士重装系统后电脑无法上网怎么办?
  5. Windows安装证书
  6. 新买笔记本屏幕缝隙有灰尘_如何清除笔记本电脑上的灰尘
  7. 数据是一把双刃剑,IPFS/FIL存储助力发展,合理应用技术中
  8. 值钱的木头——前缀和思想
  9. 大数据分析应用的九大领域
  10. EfficientDet介绍