前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >wireshark解析自定义的protobuf协议

wireshark解析自定义的protobuf协议

作者头像
meteoric
发布2018-11-20 11:05:23
4.3K0
发布2018-11-20 11:05:23
举报
文章被收录于专栏:游戏杂谈

先看最终效果

image
image

wireshark是开源的,而且在Windows下安装时用的是64位,所以相应的库文件需要使用64位。

一个Lua插件的Dissector结构大致如下:

代码语言:javascript
复制
do  
    -- 协议名称为 m_MeteoricProto,在Packet Details窗格显示为 XXX Protocol   
    local struct = Struct
    local data_dis = Dissector.get("data")
    local m_MeteoricProto = Proto("meteoric_proto","XXX Protocol")
 

    function m_MeteoricProto.dissector(buffer, pinfo, tree) 
        --在主窗口的 Protocol 字段显示的名称为 XX_Protobuf
        pinfo.cols.protocol:set("XX_Protobuf")

        if Meteoric_dissector(buffer, pinfo, tree) then            

        else
            -- data 这个 dissector 几乎是必不可少的; 当发现不是我的协议时, 就应该调用data
            data_dis:call(buffer, pinfo, tree)
        end
    end

    DissectorTable.get("tcp.port"):add(tcp_port, m_MeteoricProto)
end

剩下的就是对Buffer的解析了。注意的几个坑点:

1、wireshark自带lua版本是5.2,安装目录下有lua52.dll;

2、wireshark自带zlib库文件,名字叫zlib1.dll;

在编写插件时,将编译生成好的*.dll文件放到wireshark安装目录下,在lua中直接require(“xx”)即可,如果报错,在系统的环境变量中添加 LUA_CPATH,值为dll所有目录位置。

项目的protobuf用的是lua-protobuf,https://github.com/starwing/lua-protobuf 。编译64的lua-protobuf时,我下载了lua 5.2.4的源码,然后进行的编译。新建一个项目,用来导出lua-protobuf.dll文件。

image
image

注意要引用lua52.dll,配置附加库目录、附加包含目录。

用到的另外一个库是lua-zlib https://github.com/brimworks/lua-zlib

我先下载了zlib的源码,版本为 1.2.11。使用cmake进行编译,之后将cmake生成的zconf.h文件复制到zlib-1.2.11目录下,然后配置lua-zlib工程。

image
image
image
image

同样配置附加包含目录、附加包含库的路径,最终生成lua_zlib.dll文件,然后将其改名为zlib.dll。复制到wireshark安装目录,lua中直接require(“zlib”)

使用Dependency Walker查看生成的dll是否正确

image
image
image
image

在解析消息的过程中,我使用了递归的方法来展开所有数据。

代码语言:javascript
复制
local function AddTreeNode(nodeTree, msgTable)
        for k,v in pairs(msgTable) do
            if type(v) == "table" then
                AddTreeNode(nodeTree:add(k), v)
            else 
                nodeTree:add(k..":", v)
            end
        end
    end

目前客户端  -> 服务器,服务器 –> 客户端的数据都可以正常解析出来。我定义了本机的ip,然后通过 pinfo.src 是否与本机 ip 相等来判断是否当前消息为客户端发给服务端的数据。

参考:

https://wiki.wireshark.org/Lua/Dissectors https://www.wireshark.org/docs/wsdg_html_chunked/lua_module_Struct.html#lua_class_Struct https://www.wireshark.org/docs/wsdg_html_chunked/index.html https://www.wireshark.org/docs/wsdg_html_chunked/wsluarm_modules.html https://www.wireshark.org/docs/wsdg_html_chunked/wslua_tap_example.html https://wiki.wireshark.org/LuaAPI https://wiki.wireshark.org/LuaAPI/Pinfo

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档