最終效果如下:
參考文檔:https://mika-s.github.io/topics/
此參考文檔中7個例子教我們如何編寫lua腳本去識別我們自定義的協議
安裝Wireshark
https://www.wireshark.org/上下載安裝包安裝即可。我的安裝路徑是D:\Install\Wireshark
,在Wireshark的菜單:幫助->關于->文件夾中有安裝位置:
要解析的協議
本次要解析的協議是UDP協議,在UDP基礎上封裝了一層應用層協議。協議明細如下:
編寫腳本前需要明確的幾個對象
-
Proto:協議對象,有一個
name
的屬性,在構造函數第一個參數時傳入,決定了這里顯示什么(配合函數中的賦值)
-
ProtoField:協議字段,有如下方法
- uint8/int8(filter_string,display_name,display_type)
- uint16/int16(filter_string,display_name,display_type)
- uint32/int32(filter_string,display_name,display_type)
編寫插件腳本
lua文件的位置有兩種位置可以放:
- 任意位置,此時需要在
init.lua
中添加dofile
把lua文件添加進去 - plugins文件夾,此時wireshark啟動時自動執行,按
Ctr+Shift+L
會重新加載
我們采用第2中方式
在D:\Install\Wireshark\plugins
中新建一個test.lua
文件
在lua腳本中添加如下代碼:
--協議對象 構造函數第一個參數:顯示在協議列,第二個參數:協議描述
local my_request=Proto('myrequst','my custom request')-- 要顯示的字段,構造函數第一個參數:用于上方搜索欄過濾的 第二個參數:顯示在下方協議中的字段 第三個字段:顯示十進制還是十六進制
local time_second=ProtoField.uint8("myrequst.time_second","秒",base.HEX)
local time_minute=ProtoField.uint8("myrequst.time_minute","分",base.HEX)
local time_hour=ProtoField.uint8("myrequst.time_hour","時",base.HEX)
local time_day=ProtoField.uint8("myrequst.time_day","天",base.HEX)
local time_month=ProtoField.uint8("myrequst.time_month","月",base.HEX)
local time_year=ProtoField.uint8("myrequst.time_year","年",base.HEX)local group=ProtoField.uint8("myrequst.group","組",base.HEX)local cmd=ProtoField.uint16("myrequst.cmd","命令",base.DEC)local len=ProtoField.uint32("myrequst.length","body長度",base.DEC)-- 將字段添加到協議對象
my_request.fields={time_second,time_minute,time_hour,time_day,time_month,time_year,group,cmd,len}-- 此方法返回bool,返回true表示自定義的協議驗證通過,會傳入三個參數
-- buffer:包,去掉繼承協議之后的內容。比如繼承udp,那udp的報文就被去掉了,buffer只表示udp的應用層內容
-- pinfo:顯示抓包內容列表的信息。賦值協議名稱時會用到
-- tree:下方的樹結構
local function checker(buffer,pinfo,tree)local length=buffer:len()if length<26 thenreturn falseend-- 頭判斷if buffer(0,1):uint()~=0x48 orbuffer(1,1):uint()~=0x54 orbuffer(2,1):uint()~=0x56 orbuffer(3,1):uint()~=0x58 orbuffer(4,1):uint()~=0x41 orbuffer(5,1):uint()~=0x58 orbuffer(6,1):uint()~=0x42 orbuffer(7,1):uint()~=0x58 orbuffer(8,1):uint()~=0x49 orbuffer(9,1):uint()~=0x58 thenreturn falseend-- 賦值協議列pinfo.cols.protocol=my_request.name--字段解析local subtree=tree:add(my_request,buffer(),"我自定義的請求")subtree:add(time_second,buffer(10,1))subtree:add(time_minute,buffer(11,1))subtree:add(time_hour,buffer(12,1))subtree:add(time_day,buffer(13,1))subtree:add(time_month,buffer(15,1))subtree:add(time_year,buffer(16,1))subtree:add(group,buffer(18,1))subtree:add_le(cmd,buffer(19,1))subtree:add(len,buffer(21,4))return true
end
-- 注冊,讓wireshark解析包的時候會調用checker
my_request:register_heuristic("udp",checker)
保存lua腳本之后,按Ctr+Shift+L
就可以看到效果啦