python wireshark_用python编写脚本从wireshark导出的数据文件中提取数据
上篇文章搭建了一個UDP多播程序的基礎,所謂基礎,就是看著它,我可以寫簡單的多播程序了,可以在這個基礎上面開始工作了。
會多播了,多播的內容從哪里來,播出什么內容呢?呵呵,有個設備,沒有通訊協議,用wireshark抓包,分析協議,編程實現之,這就是此次多播的任務。
啟動wireshark,抓取數據包,導出為文本文件,三五十兆的文件,ultraedit搜索,觀察,眼睛都看直了,有設備通訊數據,還有上網瀏覽的數據,有QQ的數據,有MSN的數據,還有些不知所以的數據,看了個暈頭轉向,感覺設備的通訊數據的結構有點規律,有門兒。咱是程序員,有啥問題看看能不能編程解決呢,上python,提取數據,這樣子會讓分析數據方便一些,還能用提取的數據做測試。
看看wireshark導出數據格式,每個數據段都是“NO.”開頭的,接下來的一行是源IP和目標IP,后面還有一段“Data:”標識的數據段,有規律,我用python提取這些數據沒問題。如果規律不明顯我就沒轍了。
Python程序要完成的功能:
l提取指定ip地址的數據,把數據包部分保存到一個文件A中,這就方便觀察設備發送的數據,定義出相關的結構
l然后從文件A中,提取出數據部分,保存為文件B,我在VC程序中讀取這個文件B,嘗試解析這些數據,如果能夠成功解析,我的工作就基本完成了
看程序。
#coding=gb18030
import fileinput
# 本程序的bug:因為導出二進制數據時,字符串匹配以"0000 "為
# 開始標志,這與wireshark導出文件的第一個包的時間0.000000相
# 同,所以第一個你要手工把0.000000改為0.000001。
# 本程序就是為了導出數據加以分析,不必為了修正上面的bug繞半天
#
# 在介紹本程序功能之前,先了解一下wireshark文件的格式。
# wireshark導出文件格式,"No."是開始標志,開始標志的
# 下一行是ip地址和協議信息,接下來是它協議分析的一些輸出
# 最后是Data。
#
#No. Time Source Destination Protocol Info
# 16 58.5 192.168.0.66 234.5.6.7 UDP Source port: 1024 Destination port: synchronet-db
#
# 這中間是wireshark分析的輸出
#
#
#Data (40 bytes)
#
#0000 e9 24 00 00 ff ff 01 00 02 00 00 00 d2 04 00 00 .$..............
#0010 14 00 00 00 42 03 01 00 00 00 00 00 05 00 00 00 ....B...........
#0020 02 00 00 00 33 00 00 00 ....3...
#
#-------------------------------------------------------
# 本程序的功能是讀取wireshark的導出文件,過濾出指定ip的數據
# 1. Print_Data_Text導出從一個"No."到下一個"No."之間的內容
# 2. Print_Data_bin 導出"Data "部分的內容,格式如下:
#e9 24 00 00 ff ff 01 00 02 00 00 00 d2 04 00 00
#14 00 00 00 42 03 01 00 00 00 00 00 05 00 00 00
#02 00 00 00 33 00 00 00
#----
#e9 24 00 00 ff ff 01 00 02 00 00 00 d2 04 00 00
#14 00 00 00 42 03 01 00 00 00 00 00 05 00 00 00
#02 00 00 00 33 00 00 00
#在兩個數據包之間插入了一樣"----",
# 3. Print_IP_Line 僅導出包含ip的行,就是"No."下面的那行
#-------------------------------------------------------
# 如何設置導出數據的條件:
# 導出數據的條件
# and字段的內容必須全部滿足
# or字段的內容,至少滿足一個
# and 字段和 or 字段都滿足
# 如果and字段不存在,則認為條件滿足
# 如果or字段不存在,則認為條件滿足
#No. Time Source Destination Protocol Info
#14 9.949685 192.168.0.66 234.5.6.7 UDP Source port: onehome-remote Destination port: synchronet-db
# 舉例:
# 導出滿足下面條件的數據:
# No.下面一行的數據,包含"234.5.6.7"和"UDP",
# 并且包含"192.168.0.202"和"192.168.0.22"之一
# cond = []
# cond.append({
# "and":["234.5.6.7", "UDP"],
# "or":["192.168.0.202", "192.168.0.22"],
# })
#
#-------------------------------------------------------
# 導出滿足條件的數據, 為了在導出的文件中能看到數據是從
# 源文件的那個地方來的,導出的數據可以帶有行號信息,行號
# 添加在每行數據的前面
# fileName: 原始數據文件名稱
# saveto : 導出數據保存文件名稱
# cond : 數據需要滿足的條件
# with_ln_number : 導出數據時是否帶行號
# 只有帶行號的數據,才能被Print_Data_bin函數使用
def Print_Data_Text(fileName, saveto, cond, with_ln_number=True):
f1 = open(saveto, 'w+')
ln = []
myfile = fileinput.input(fileName)
for x in myfile:
ln.append(x)
start_flag = 0
start_ln = 0
end_ln = 0
end_flag = 0
find_count = 0
print " "
for i in range(0,len(ln)):
if ln[i].find("No.") == 0 and start_flag == 1:
start_flag = 0
if ln[i].find("No.") == 0 and start_flag == 0:
if Check_Expr(ln[i+1], cond)==True:
start_ln = i
start_flag = 1
find_count = find_count + 1
msg = ""
msg = ("/b/b/b/b/b/b/b%02d")%(find_count)
print msg,
if start_flag == 1:
msg = ""
if with_ln_number == True:
msg = ("%08d:/t%s")%(i+1,ln[i])
else:
msg = ("%s")%(ln[i])
f1.write(msg)
f1.close()
# 導出數據包中的Data字段的內容
# 通過,查找 "0000 "作為開始標志
# 查找"Data:"作為結束標志
def Print_Data_bin(fileName, saveto, data_start, data_end, with_ln_number=True):
f1 = open(saveto, 'w+')
ln = []
myfile = fileinput.input(fileName)
for x in myfile:
ln.append(x)
start_flag = 0
start_ln = 0
end_ln = 0
end_flag = 0
find_count = 0
print " "
for i in range(0,len(ln)):
if start_flag == 0 and Check_Expr(ln[i], data_start)==True:
start_flag = 1
msg = ("%s/n")%(ln[i][16:63])
f1.write("----/n")
if start_flag == 1 and Check_Expr(ln[i], data_end)==True:
start_flag = 0
if start_flag == 1:
msg = ""
if with_ln_number == True:
msg = ("%s/n")%(ln[i][16:63])
else:
msg = ("%s/n")%(ln[i][6:53])
f1.write(msg)
# if i > 100:
# break
f1.close()
def Check_Expr(ln, expr):
expr_flag = False
for x in expr:
and_flag = True
or_flag = False
if 'and' in x:
for y in x["and"]:
if ln.find(y) == -1:
and_flag = False
break
if 'or' in x:
for y in x["or"]:
if ln.find(y) != -1:
or_flag = True
break
else:
or_flag = True
if and_flag == True and or_flag == True:
expr_flag = True
break
return expr_flag
#
# 為了方便查看數據網來,只
# 打印帶有IP地址的那行數據
#
def Print_IP_Line(fileName, saveto,cond):
f1 = open(saveto, 'w+')
ln = []
myfile = fileinput.input(fileName)
for x in myfile:
ln.append(x)
start_flag = 0
start_ln = 0
end_ln = 0
end_flag = 0
find_count = 0
print " ",
for i in range(0,len(ln)):
if ln[i].find("No.") == 0:
if Check_Expr(ln[i+1], cond) == True:
start_ln = i+1
start_flag = 1
find_count = find_count + 1
msg = ""
msg = ("/b/b/b/b/b/b/b/b%02d")%(find_count)
print msg,
if start_flag == 1:
msg = ""
msg = ("%08d:/t%s")%(start_ln+1,ln[start_ln])
f1.write(msg)
start_flag = 0
f1.close()
if __name__ == '__main__':
src_data_file = "c://ws.txt"
txt_data_file_11 = "d://RE_11.txt"
bin_data_file_11 = "d://bin_11.txt"
txt_data_file_66 = "d://RE_66.txt"
bin_data_file_66 = "d://bin_66.txt"
#
# 導出數據的條件
# and字段的內容必須全部滿足
# or字段的內容,至少滿足一個
# and 字段和 or 字段都滿足
# 如果and字段不存在,則認為條件滿足
# 如果or字段不存在,則認為條件滿足
# 舉例:
#No. Time Source Destination Protocol Info
#14 9.949685 192.168.0.202 234.5.6.7 UDP Source port: onehome-remote Destination port: synchronet-db
# 導出滿足下面條件的數據:
# No.下面一行的數據,包含"234.5.6.7"和"UDP",并且并且包含"192.168.0.202"和"192.168.0.22"之一
# cond = []
# cond.append({
# "and":["234.5.6.7", "UDP"],
# "or":["192.168.0.202", "192.168.0.22"],
# })
#
cond = []
cond.append({
"and":["234.5.6.7", "UDP"],
"or":["192.168.0.202", "192.168.0.22"],
})
getdata_start = []
getdata_start.append({
"and":["0000 "],
})
getdata_end = []
getdata_end.append({
"and":["Data: "],
})
# print getdata_start
# Print_IP_Line('c://RE.txt',cond)
filter_data_11 = []
filter_data_11.append({
"and":["192.168.0.22", "234.5.6.7", "UDP"],
})
filter_data_66 = []
filter_data_66.append({
"and":["192.168.0.202", "234.5.6.7", "UDP"],
})
Print_Data_Text(src_data_file, txt_data_file_11, filter_data_11)
Print_Data_bin(txt_data_file_11, bin_data_file_11,getdata_start, getdata_end)
Print_Data_Text(src_data_file, txt_data_file_66, filter_data_66)
Print_Data_bin(txt_data_file_66, bin_data_file_66,getdata_start, getdata_end)
Print_Data_bin(src_data_file, "d://ttxx1.txt",getdata_start, getdata_end, 0)
Print_IP_Line(src_data_file,"d://ttxx2.txt", cond)
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python wireshark_用python编写脚本从wireshark导出的数据文件中提取数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android开发app初始化,安卓快速
- 下一篇: python 输入正方形_Python打