小工具知识网

当前位置: 首页 > 行业知识 > wireshark 用LUA二次开发插件 解析器(Dissector)

行业知识

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

2026-12-05 04:49:00 来源:互联网转载

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 = 12345local 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 trueend-- 解析器主函数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)    endend-- 注册协议插件local tcp_port_table = DissectorTable.get('tcp.port')tcp_port_table:add(80, foo_proto)

上一篇:轻松获取蛋仔照片,超简单下载教程!

下一篇:怎么选亚马逊服务器?