wireshark 用LUA二次开发插件 解析器(Dissector)

LUA菜鸟教程:https://www.runoob.com/lua/lua-functions.html
在线编辑运行器:https://www.runoob.com/try/runcode.php?filename=HelloWorld&type=lua

【参考】
https://www.cnblogs.com/zzqcn/p/4846745.html
https://blog.csdn.net/wangquan1992/article/details/100161021
Wireshark解析器(Dissector)插件-Lua:https://blog.csdn.net/heusunduo88/article/details/129857614

一、前言

Wireshark是一款流行的网络协议分析工具,它支持多种协议的解析,并且允许用户通过编写插件来扩展其功能。Lua是一种轻量级的编程语言,它被集成到Wireshark中,允许用户编写脚本来自定义协议解析逻辑。
在这里插入图片描述

协议

Protocol: TCP 0x6(6)
UDP 0x11 (17)

===========================================================

1. 环境准备

  • 安装Wireshark:确保你的系统上已经安装了Wireshark。
  • 创建插件文件夹:Wireshark安装目录下plugins的子文件夹,用于存放Lua插件。

2. 解析器 Dissector

解析器(Dissector)是Wireshark中的概念,用于解析协议,将报文中对应的Bytes转为相应的字段值,可以简单理解为Wireshark中的解码器。

它不只能解析将相应位置的Bytes转为对应字段,还能将解析出来的字段用于报文过滤,还能自定义信息(Info)列中显示的自定义信息。

能方便开发人员调试诊断协议相关问题,比如断连后不重连,报文不正确,未发心跳。

解析器组成部分:

协议定义
字段定义
协议和字段关联
解析器主函数
协议注册

3. 编写Lua插件

  • 创建Lua文件:在plugins文件夹中创建一个新的Lua文件,例如myplugin.lua

  • 定义协议:在Lua文件中定义一个新的协议,例如:

    local myproto = Proto("myproto", "My Protocol")
    local ptype = ProtoField.uint8("myproto.ptype", "Type", base.DEC)
    local pval = ProtoField.uint16("myproto.pval", "Val", base.HEX)
    local pbyte = ProtoField.new("Bytes", "myproto.bytes", ftypes.BYTES)
    local pstr = ProtoField.new("Str", "myproto.str", ftypes.STRING)
    myproto.fields = {ptype, pval, pbyte, pstr}
    
  • 编写解析函数:定义一个函数来解析数据包,例如:

    function myproto.dissector(tvb, pinfo, tree)
      local offset = 0
      tree:add(ptype, tvb(offset, 1))
      offset = offset + 1
      tree:add(pval, tvb(offset, 2))
      offset = offset + 2
      tree:add(pbyte, tvb(offset, tvb:len() - offset))
      tree:add(pstr, tvb(offset, tvb:len() - offset))
    end
    
  • 绑定协议到端口:如果需要将你的协议绑定到一个特定的端口,可以使用以下代码:

    local udp_port = 12345
    local udp_dissector_table = DissectorTable.get("udp.port")
    local myproto_dissector = Dissector.get("myproto")
    udp_dissector_table:add(udp_port, myproto_dissector)
    

4. 调试和测试

  • 使用Lua控制台:Wireshark提供了一个Lua控制台,你可以在这里运行Lua代码来测试你的插件。

例子

-- 协议定义
foo_proto = Proto('ScoreBoard', 'ScoreBoard Protocol')

-- 协议的各个字段定义
local f_identifier = ProtoField.bytes('ScoreBoard.identifier', 'Identifier')
local f_operator = ProtoField.uint8('ScoreBoard.operator', 'Operator', base.HEX, {
    [0] = 'get-value',
    [1] = 'set-value',
    [128] = 'resp-value',
    [16] = 'get-color',
    [17] = 'set-color',
    [144] = 'resp-color'
})
local f_left = ProtoField.uint32('ScoreBoard.left', 'Value Left', base.DEC)
local f_right = ProtoField.uint32('ScoreBoard.right', 'Value Right', base.DEC)
local f_red = ProtoField.uint8('ScoreBoard.red', 'Color Red', base.DEC)
local f_green = ProtoField.uint8('ScoreBoard.green', 'Color Green', base.DEC)
local f_blue = ProtoField.uint8('ScoreBoard.blue', 'Color Blue', base.DEC)

-- 将字段添加到协议中
foo_proto.fields = {
    f_identifier,
    f_operator,
    f_left,
    f_right,
    f_red,
    f_green,
    f_blue
}

-- 获取Wireshark自带的Data解析器
local data_dis = Dissector.get('data')

-- 解析函数
local function ScoreBoard_dissector(buf, pkt, root)
    local buf_len = buf:len()
    if buf_len > 17 then
        return false
    end
    local v_identifier = buf(0, 16)
    if (
        (buf(0, 1):uint() ~= 226) or (buf(1, 1):uint() ~= 203) or (buf(2, 1):uint() ~= 181) or
        (buf(3, 1):uint() ~= 128) or (buf(4, 1):uint() ~= 203) or (buf(5, 1):uint() ~= 9) or
        (buf(6, 1):uint() ~= 78) or (buf(7, 1):uint() ~= 186) or (buf(8, 1):uint() ~= 163) or
        (buf(9, 1):uint() ~= 107) or (buf(10, 1):uint() ~= 246) or (buf(11, 1):uint() ~= 7) or
        (buf(12, 1):uint() ~= 206) or (buf(13, 1):uint() ~= 149) or (buf(14, 1):uint() ~= 63) or
        (buf(15, 1):uint() ~= 43)
    ) then
        return false
    end
    local v_operator = buf(16, 1)
    local i_operator = v_operator:uint()

    local t = root:add(foo_proto, buf)
    pinfo.cols.protocol = 'ScoreBoard'
    t:add(f_identifier, v_identifier)
    t:add(f_operator, v_operator)

    if ((i_operator == 1) or (i_operator == 128)) and (buf_len >= 25) then
        t:add(f_left, buf(17, 4))
        t:add(f_right, buf(21, 4))
    elseif ((i_operator == 17) or (i_operator == 144)) and (buf_len >= 20) then
        t:add(f_red, buf(17, 1))
        t:add(f_green, buf(18, 1))
        t:add(f_blue, buf(19, 1))
    end
    return true
end

-- 解析器主函数
function foo_proto.dissector(tvb, pinfo, root)
    pinfo.cols.protocol:set('FOW')
    pinfo.cols.info:set('FoF Protocol')

    if ScoreBoard_dissector(tvb, pinfo, root) then
        -- 如果是有效的报文
    else
        -- 当发现不是我的协议时,就使用Wireshark自带的Data解析器显示
        data_dis:call(tvb, pinfo, root)
    end
end

-- 注册协议插件
local tcp_port_table = DissectorTable.get('tcp.port')
tcp_port_table:add(80, foo_proto)

二、解析OMCI协议报文详细信息

Wireshark+lua插件方式抓包及解析OMCI协议报文详细信息:https://blog.csdn.net/binfeng1987/article/details/129944022

下载 omci 和 BinDecHex 文件

在这里插入图片描述

然后 Wireshark 重新加载 【分析 - 重新载入LUA插件】

在这里插入图片描述

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Wireshark支持使用Lua脚本进行协议解析。要添加Lua解析器,请按照以下步骤操作: 1. 安装Lua环境。Wireshark需要使用Lua 5.1或更高版本。可以从Lua的官方网站下载安装程序并按照说明进行安装。 2. 找到Wireshark插件目录。在Wireshark的菜单中,选择“帮助”->“关于Wireshark”,然后单击“文件夹”按钮,即可打开插件目录。 3. 创建一个新的Lua插件。在插件目录中创建一个新的目录,例如“my_lua_plugin”。 4. 创建一个Lua脚本。在新创建的目录中创建一个名为“my_protocol.lua”的文件,并将以下代码复制到该文件中: ``` -- my_protocol.lua -- declare our protocol my_protocol = Proto("my_protocol", "My Protocol") -- create a function to dissect it function my_protocol.dissector(buffer, pinfo, tree) -- add protocol name to protocol column pinfo.cols.protocol = "MY_PROTO" -- create a subtree for our protocol subtree = tree:add(my_protocol, buffer(), "My Protocol Data") -- add fields to the subtree subtree:add(buffer(0,1), "Field 1") subtree:add(buffer(1,1), "Field 2") end -- register our protocol tcp_table = DissectorTable.get("tcp.port") tcp_table:add(1234, my_protocol) ``` 这个脚本定义了一个名为“my_protocol”的协议,并使用“tcp.port”表将其注册到TCP端口1234上。在协议分析器中,它将显示为“My Protocol”。 5. 启用Lua插件。在Wireshark的菜单中,选择“编辑”->“首选项”,然后选择“协议”->“Lua”。单击“+”按钮,然后选择新创建的插件目录。确保选中“启用”复选框,然后单击“应用”和“确定”。 6. 使用Lua解析器。现在,当Wireshark捕获到使用TCP端口1234发送的数据包时,它将调用my_protocol.dissector()函数来解析该协议,并显示解析结果。 这只是一个简单的示例,可以根据需要进行修改和扩展。更多信息,请参阅WiresharkLua API文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值