# Lua ## lua HIT * local 宣告上限 60個 * parser 是以樹狀結構產生 * 每個宣告點可以是 node也可是leaf * 用陣列宣告時,在加入 protocol 定義時 ,只要加入最後的宣告點 ## 語法範例 參見 api 文件 10.4.6.5. 10.4.6.6. https://wiki.wireshark.org/LuaAPI/ ![](https://i.imgur.com/xeMYSdz.png) ![](https://i.imgur.com/cWKVG19.png) 宣告區塊 * GMac = Proto("GMac","G3 PLC MAC"); Desc : 宣告 protocol 格式 * GMac.fields = Desc : 宣告這個 proto 會用到哪些前面定義的變數當作 node 使用 :::warning 這個宣告一定要寫,否則後面 parser無法使用前面定義好的變數 ::: Parser 格式 * function GMac.dissector(mac_frame,pktinfo,root) : * Desc : parser 進入點 * parameter : mac_frame : input data 的範圍區塊 root : input data的 root點 pktinfo : packet 使用的 protocol 跟相關設定 * pktinfo.cols.protocol:set("GMac") * Desc : setting protocol * pktinfo.cols.info:set("") * Desc : setting protocol * pktlen = mac\_frame:reported\_length_remaining() * Desc : 取得 packet長度 * mac_tree = root:add(GMac, mac_frame:range(0,pktlen)) * 在 root node 下,把之前定義的 GMac變數(定義範圍是在mac_frame的 0 ~ pktlen),加入 root node下,產生新的node : mac_tree, * mhr\_famctrl\_secuenable = mac_frame:range(0,pktlen):bitfield(28,1) * 將 mac_frame 定義的範圍, 取 base = 0 , offset = pktlen的範圍,在這範圍內的第 28個 bit 開始,取1個 bit長度的值 # Wireshark 設定 ## Install package $ sudo apt-get update $ sudo apt-get install wireshark $ sudo apt-get install tcpdump ## Lua 放置位置 參考 wireshark中 help -> about wireshark 紅色框框的路徑,將 lua檔案放入此處 ![](https://i.imgur.com/EUSAA33.png) ## 將 新增的 protocol 加入 wireshark中 edit -> preferences 找到 protocols 展開 ![](https://i.imgur.com/7CYkk4c.png) 在面找到 dlt_user ![](https://i.imgur.com/hY2YYDB.png) 按下 "+" 新增一個 protocol 目前使用的是 147 並在 payload/header protocol 中打入 gmac 設定成 G3 mac protocol ![](https://i.imgur.com/XvWQVXX.png) 此時應該可以正常解譯 packet了 :::info Q : 如果無法正常解譯,需要增加設定如下 ::: :::success ![](https://i.imgur.com/WU4DSTm.png) 在 packet frame 按下右鍵,選擇protocol preferences 中的 disable frame ![](https://i.imgur.com/bNAiNe3.png) 確認裡面新增的 protocol 有被選擇 ::: 從 pipe file讀取資料 capture -> options ![](https://i.imgur.com/9fMS4Nk.png) input 處,選擇 manage interfaces ![](https://i.imgur.com/3rKrYOF.png) 新增一個 pipes 並且選定 pipe 檔案,如此就可以增加 pipe進入 wireshark ## start wireshark with pipe in interface list $ sudo wireshark -ni \/home/roy/SrcCode/VangoChip/Sniffer/scapy-2.3.2/out.pipe # 自動啟動 ## install package $ sudo apt-get install python $ sudo apt-get install python-serial ## script * 負責啟動 slip * 呼叫 python sniffer ## python sniffer * 負責把 sl0 的資料轉送到 pipe中 ## 使用方式 $ sudo ./start.sh \[start/stop/restart\] \[tty device\] ## sniffer.py解說 在 start.sh 中 呼叫 sudo python sniffer.py sl0 out.pipe & sl0 是 network interface out.pipe 是 pipe file 開啟sl0 interface ![](https://i.imgur.com/egd3dQI.png) 建立 out.pipe file ![](https://i.imgur.com/2kuBQPT.png) 開啟 out.pipe file ![](https://i.imgur.com/gYKV8iy.png) wireshark file header ![](https://i.imgur.com/LUDC1vn.png) 開始接收封包 ![](https://i.imgur.com/PIZNb0H.png) ## HIT : 1. Sniffer 資料夾內檔案位置不能變動 script 是以這個資料夾為基礎,去執行其他相關連的程式,如果要變動,需要修改相對應程式碼 2. 檔案從 windows copy到 linux 時 pipe file會被視為一般檔案,進而導致 linux 在執行的時候會無法使用 pipe 1. 所以當 wireshark 在存取 pipe file時如果有出現權限錯誤,需要修改 Sniffer 資料夾內的所有檔案權限,並且刪除 out.pipe 讓系統重新產生 2. 原因是 pipe file 跟 data file的 device node點不同,會無法存取 ![](https://i.imgur.com/hNoYikW.png) 如圖顯示, out.pipe檔案屬性是 p file,一般檔案顯示則無, 詳情請參照 linux command : mkfifo / mknode p 指令 # tcpdump(pcap) file Header ![](https://i.imgur.com/qFyfqJr.png)