wireshark解析自定義的protobuf協議

先看最終效果html

image

 

wireshark是開源的,並且在Windows下安裝時用的是64位,因此相應的庫文件須要使用64位。node

一個Lua插件的Dissector結構大體以下:git

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的解析了。注意的幾個坑點:github

一、wireshark自帶lua版本是5.2,安裝目錄下有lua52.dll;服務器

二、wireshark自帶zlib庫文件,名字叫zlib1.dll;tcp

 

在編寫插件時,將編譯生成好的*.dll文件放到wireshark安裝目錄下,在lua中直接require(「xx」)便可,若是報錯,在系統的環境變量中添加 LUA_CPATH,值爲dll全部目錄位置。ui

項目的protobuf用的是lua-protobuf,https://github.com/starwing/lua-protobuf 。編譯64的lua-protobuf時,我下載了lua 5.2.4的源碼,而後進行的編譯。新建一個項目,用來導出lua-protobuf.dll文件。lua

image

 

注意要引用lua52.dll,配置附加庫目錄、附加包含目錄。spa

 

用到的另一個庫是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

相關文章
相關標籤/搜索