wireshark lua脚本

2023-06-05,,

1、目的:解析rssp2协议

 
2、如何使用wireshark lua插件

  将编写的(假设为rssp2.lua)lua文本,放入wireshark 安装目录下,放哪里都行只要dofile添加了路径.

并且在安装目录下找到init.lua,最后一行添加路径代码 :

dofile(DATA_DIR.."RSSP2.lua") 

 
3、介绍

  解析由rssp2.lua、p2_data.lua、p2_parse.lua3个文件组成。如果协议内容很少,一个lua文件就能完全解决.init.lua会调用rssp.lua,rssp2.lua会加载p2_data.lua、p2_parse.lua文件.解析器dissectors介绍可以参照官网:https://wiki.wireshark.org/Lua/Dissectors

必须注册为句柄
解析函数必须设置为 Proto对象
call wireshark时可通过 TVB buffer(TVB object) ORZ a packet information record(pinfo object:) ORZ a tree root(TreeItem object)
只有当包与解析表匹配,或者用户强制“decode as”,才能解析

4、dofile、require用法

http://blog.csdn.net/leecrest/article/details/31742419讲得比较透彻

加载一个lua文件时require会先在package.load中查找此模块是否存在,如果存在,直接返回模块。如果不存在加载此模块文件。

  @require仅加载一次,对于模块会按照特定的搜索规则查找文件加载。 在rssp2.lua先添加路径:

      package.path = "D:/professional program/WireShark/plugins/?.lua;;"

  require("data") 那么加载路径
  D:/professional program/WireShark/plugins/data/lua

dofile和loadfile区别;dofile:读入代码文件并编译执行。每调用一次dofile都会重新编译执行一次。loadfile:编译代码,将整个模块文件当成一个函数返回,但是不执行代码。dofile是对loadfile的一次包装。dofile使用:

  dofile("D:/professionalprogram/WireShark/plugins/data.lua")

如果不太懂可以参考http://www.runoob.com/lua/lua-modules-packages.html中的lua模块与包的例子.

5、wireshark 库函数
     https://www.wireshark.org/docs/wsdg_html_chunked/wsluarm_modules.html
package.path = "D:/professional program/WireShark/plugins/?.lua;;"
          添加路径package.path

require("p2_data")

这里面是要用到的全部解析全局变量,函数没有放里面

func = dofile("D:/professional program/WireShark/plugins/p2_parse.lua")

         func返回了rssp2.lua需要调用的所有函数

self_rssp2 = Proto ("RSSP2","RSSP2_Protocol")

注册协议,函数Proto

f_usALELen = ProtoField.uint16("rssp2.len", "Length", base.DEC)

ProtoField.uint16解析2字节的f_usALELen字段,Length在wireshark中显示名称,base.DE 十进制显示

f_usRole = ProtoField.uint8("rssp2.Role", "Role", base.HEX, { [0x10] = "Client", [0x11] = "Server"})
          还可以是这样的,不仅显示值 [0x10] = "Client",还显示含义.
self_rssp2.fields = {f_usALELen, f_usALEVer, f_usAPPType, f_usSequen, f_usNRFlag}
          最终将所有需要解析的字段添加到field中,如果只是定义了这个字段,而不添加到这里是解析不了的.
function self_rssp2.dissector(buffer,pinfo,tree)

可以把函数dissector理解为我们编写脚本的主函数,入口函数。形参是数据帧buffer,消息pinfo和 wireshark上层已经解析的tree

pinfo.cols.protocol:set("RSSP-II")
         在wireshark protocol列显示“RSSP-II”协议字符串
         同理pinfo.cols.info:set("Invalid Msg")
         在info中显示"Invalid Msg"

local RSSP2Tree = tree:add(self_rssp2, buffer(offset, buffer_len),
"RSSP-II Msg Structure")

添加自己的tree,"RSSP-II Msg
Structure",然后就可以解析在这个tree下面的字段
RSSP2Tree 下面又有ALE_tree.
local ALE_tree = RSSP2Tree:add(self_rssp2,buffer(offset, 10),"ALE
Layer")

local usMsgLen = buffer(offset,2):le_uint()

offset是自己定义的局部变量,帧的偏移量,表示解析到offset字节数了.offset后面两个字节转化成无符号 32进制赋值给变量usMsgLen 

ALE_tree:add(f_usALELen, buffer(offset,2))

ALE_tree下面解析f_usALELen字段,该字段在帧的offset位置起始的后面两个字节

local tcp_port_table = DissectorTable.get("tcp.port")

将该解析函数添加到tcp_port_table ,这一步是必须的

tcp_port_table:add(60005, self_rssp2)

该帧是通过60005端口采集,这个打开wireshark满足帧条件,就能自动解析.
 
实在不懂,可以到github中搜关键字“wireshark
lua”,有很多人的lua代码

 

6、注意

  @不同于其他语言的数组把0作为数组的初始索引,Lua里表的默认初始索引一般从1开始

  @在Lua中0为true

  @~= 不等于

 

7、wireshark中tcp帧格式
  # wireshark
数据解析格式#
  http://note.youdao.com/noteshare?id=b2c354990472154833321e1d7b316df1
  0000   70 ba ef 56 e7 cb dc 49 c9 01 01 96 08 00| 45 00
  0010   00 28 bf 8f 00 00 40 06 a0 d8 0a 41 01 18 0a 02
  0020   05 0e| ea 65 40 19 ca b2 10
  0030   c3 ae 00 00 00 00 00 00
  分隔符| 分隔 数据链路层 网络层传输层

 
参考文档:
http://www.docin.com/p-1350594454.html wireshak使用教程
http://blog.tianya.cn/blogger/post_show.asp?BlogID=2338564&PostID=19226926 Wireshak 插件编写
http://www.codingnow.com/2000/download/lua_manual.html lua中文教程
http://manual.luaer.cn/  lua在线手册
https://wiki.wireshark.org/Lua/Dissectors 参考
https://www.wireshark.org/docs/wsdg_html_chunked/wsluarm_modules.html 
wireshark
lua库函数
http://blog.csdn.net/leecrest/article/details/31742419 讲得比较透彻#require
、dofile区别#

http://blog.csdn.net/birdflyto206/article/details/49403801 #VC 生成lua源代码#

wireshark lua脚本的相关教程结束。

《wireshark lua脚本.doc》

下载本文的Word格式文档,以方便收藏与打印。