OMCI LOG to dot PCAP file(Wireshark)

接觸GPON多年,一直是土法煉鋼的去解每個bit的定義.
從沒想過寫個parser去解析.
原來早就有高手拿Wireshark來做這件事.

裝上這個插件,Wireshark就具有解譯OMCI的能力了
OMCI Wireshark Dissector
再來就是怎樣把OMCI Log餵給Wireshark,
之前在某個博客看到可以把.pcap轉成hexdump
猜想應該也有反轉的方式,果然WireShark就有內建的方法.
於是利用這個小工具text2pcap應該是解法.

參考他的範例需要把假的乙太包head給加回去,
所以先規劃pseudo code:

1. log file convert to UNIX format
2. Separate OMCC message raw data from log file
3. Reorganize Raw data to hexdump type(Add head)
4. Converst to .pcap

藉Linux上強大的字串處裡工具用bash Script是最快實現的方式,
也是我第一次嘗試用AWK來重組變成所需要的FORMAT,果然好用ㄚ.
需要事先安裝的是dos2unix和wireshark-commom這些套件,
而我的平台是Ubuntu 10.04,apt-get 直接install即可.
比較需要注意的是Ubuntu上採用了tofrodos取代了dos2unix,安裝後再alias就可以了.

範例程式不完全和我實際上完成的一樣,不過主要的處裡都在這邊了.

#!/bin/bash
#Converst OMCI debug log file to wireshark .pcap file
 
#1. dos2unix
dos2unix $1
 
#2. Filter OMCI Hex
cat $1 | grep '[[:xdigit:]] [[:xdigit:]][[:xdigit:]]$' |grep '^ [[:xdigit:]][[:xdigit:]] [[:xdigit:]]'| \
 
#3. Reorganize Raw data to hexdump type
 #-- Merge OMCI line to one
sed 'N;N;s/\n//g'| \
 #-- Insert OMCI Head for pcap 
sed 's/^/00 00 00 00 00 01 00 00 00 00 00 02 88 b5/i'| \
 #-- Out put hexdump type format 
awk '{
	for(i=0;i<=NF;i++){
		if((i%16 ==0)) printf "\n%03x0 ",(i/16);
        	printf "%s ",$(i+1);
	}
	print "\n"
}' > temp.txt
 
#4. converst to .pcap
text2pcap temp.txt $1.pcap -q
rm -rf temp.txt

不同平台只需要把不同的戳記找出就可以用一樣的方式輸出供WireShark讀取的格式.
以後有空再把不同的platform的filter補齊option就可以跨平台使用了.

refer:
http://www.kroosec.com/2011/08/hex-dump-to-pcap.html
https://www.wireshark.org/docs/wsug_html_chunked/AppToolstext2pcap.html
http://www.zhaocs.info/wireshark-text2pcap.html
http://tw.perlmaven.com/filtering-values-with-perl-grep
http://serverfault.com/questions/189498/convert-txt-file-to-cap-to-open-it-by-wireshark
http://stackoverflow.com/questions/7353673/using-text2pcap-or-equivilent-to-merge-multiple-plain-text-packets-into-one-pc

Facebook Comments
Scottj Written by:

史考特 喜歡3C 愛拍照