1、目的:解析rssp2協議2、如何使用wireshark lua插件將編寫的(假設為rssp2.lua)lua文本,放入wireshark 安裝目錄下,放哪里都行只要dofile添加了路徑.
並且在安裝目錄下找到init.lua,最后一行添加路徑代碼 :
dofile(DATA_DIR.."RSSP2.lua")
3、介紹解析由rssp2.lua、p2_data.lua、p2_parse.lua3個文件組成。如果協議內容很少,一個lua文件就能完全解決.init.lua會調用rssp.lua,rssp2.lua會加載p2_data.lua、p2_parse.lua文件.解析器dissectors介紹可以參照官網:https://wiki.wireshark.org/Lua/Dissectors
- 必須注冊為句柄
- 解析函數必須設置為 Proto對象
- call wireshark時可通過 TVB buffer(TVB object) ORZ a packet information record(pinfo object:) ORZ a tree root(TreeItem object)
- 只有當包與解析表匹配,或者用戶強制“decode as”,才能解析
4、dofile、require用法
加載一個lua文件時require會先在package.load中查找此模塊是否存在,如果存在,直接返回模塊。如果不存在加載此模塊文件。@require僅加載一次,對於模塊會按照特定的搜索規則查找文件加載。 在rssp2.lua先添加路徑:
package.path = "D:/professional program/WireShark/plugins/?.lua;;"
require("data") 那么加載路徑D:/professional program/WireShark/plugins/data/lua
- dofile和loadfile區別;dofile:讀入代碼文件並編譯執行。每調用一次dofile都會重新編譯執行一次。loadfile:編譯代碼,將整個模塊文件當成一個函數返回,但是不執行代碼。dofile是對loadfile的一次包裝。dofile使用:
dofile("D:/professionalprogram/WireShark/plugins/data.lua")
- 如果不太懂可以參考http://www.runoob.com/lua/lua-modules-packages.html中的lua模塊與包的例子.
5、wireshark 庫函數
- package.path = "D:/professional program/WireShark/plugins/?.lua;;"
添加路徑 package.path這里面是要用到的全部解析全局變量,函數沒有放里面
- require("p2_data")
func返回了rssp2.lua需要調用的所有函數
- func = dofile("D:/professional program/WireShark/plugins/p2_parse.lua")
注冊協議,函數Proto
- self_rssp2 = Proto ("RSSP2","RSSP2_Protocol")
- f_usALELen = ProtoField.uint16("rssp2.len", "Length", base.DEC)
ProtoField.uint16解析2字節的f_usALELen字段,Length在wireshark中顯示名稱,base.DE 十進制顯示
- f_usRole = ProtoField.uint8("rssp2.Role", "Role", base.HEX, { [0x10] = "Client", [0x11] = "Server"})
還可以是這樣的,不僅顯示值 [0x10] = "Client",還顯示含義.
- self_rssp2.fields = {f_usALELen, f_usALEVer, f_usAPPType, f_usSequen, f_usNRFlag}
最終將所有需要解析的字段添加到field中,如果只是定義了這個字段,而不添加到這里是解析不了的.
- function self_rssp2.dissector(buffer,pinfo,tree)
可以把函數dissector理解為我們編寫腳本的主函數,入口函數。形參是數據幀buffer,消息pinfo和 wireshark上層已經解析的tree
- pinfo.cols.protocol:set("RSSP-II")
在wireshark protocol列顯示“RSSP-II”協議字符串同理pinfo.cols.info:set("Invalid Msg")在info中顯示"Invalid Msg"
- local RSSP2Tree = tree:add(self_rssp2, buffer(offset, buffer_len), "RSSP-II Msg Structure")
添加自己的tree,"RSSP-II Msg Structure",然后就可以解析在這個tree下面的字段RSSP2Tree 下面又有ALE_tree.local ALE_tree = RSSP2Tree:add(self_rssp2,buffer(offset, 10),"ALE Layer")
- local usMsgLen = buffer(offset,2):le_uint()
offset是自己定義的局部變量,幀的偏移量,表示解析到offset字節數了.offset后面兩個字節轉化成無符號 32進制賦值給變量usMsgLen
- ALE_tree:add(f_usALELen, buffer(offset,2))
ALE_tree下面解析f_usALELen字段,該字段在幀的offset位置起始的后面兩個字節
- local tcp_port_table = DissectorTable.get("tcp.port")
將該解析函數添加到tcp_port_table ,這一步是必須的
- tcp_port_table:add(60005, self_rssp2)
該幀是通過60005端口采集,這個打開wireshark滿足幀條件,就能自動解析.實在不懂,可以到github中搜關鍵字“wireshark lua”,有很多人的lua代碼
6、注意@不同於其他語言的數組把0作為數組的初始索引,Lua里表的默認初始索引一般從1開始
@在Lua中0為true
@~= 不等於
7、wireshark中tcp幀格式# wireshark 數據解析格式#0000 70 ba ef 56 e7 cb dc 49 c9 01 01 96 08 00| 45 000010 00 28 bf 8f 00 00 40 06 a0 d8 0a 41 01 18 0a 020020 05 0e| ea 65 40 19 ca b2 100030 c3 ae 00 00 00 00 00 00分隔符| 分隔 數據鏈路層 網絡層傳輸層
參考文檔:
http://www.docin.com/p-1350594454.html wireshak使用教程
http://manual.luaer.cn/ lua在線手冊
http://blog.csdn.net/leecrest/article/details/31742419 講得比較透徹#require 、dofile區別#