wireshark解析自定义的protobuf协议

先看最终效果

image

 

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

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

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

 

注意要引用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

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

 

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

image

image

 

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

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

posted @ 2017-09-11 20:31  meteoric_cry  阅读(1838)  评论(0编辑  收藏  举报