資源簡介
基于Tkinter的Python GUI界面設計,能分條展示數據包的概要信息(summary()),分層解析數據包,可顯示數據包的十六進制編碼值(hexdump());在抓包的同時解析數據包(不能等抓包停止后才解析),可判斷IP、TCP或UDP數據包的校驗和是否正確;支持BPF過濾器,抓包過程可以暫停和停止;可將數據包存儲在pcap文件中,以供wireshark或其它數據包解析工具分析;可以在退出時提示用戶進行保存未保存的數據包,進行保存工作;可以在再次開始新的抓包前提示用戶保存未保存的數據包。
代碼片段和文件信息
#?coding=utf-8
import?datetime
import?threading
import?tkinter
from?tkinter?import?*
from?tkinter?import?font?filedialog
from?tkinter.constants?import?*
from?tkinter.messagebox?import?askyesno
from?tkinter.scrolledtext?import?ScrolledText
from?tkinter.ttk?import?Treeview
from?scapy.layers.inet?import?*
from?scapy.layers.l2?import?*
#?用來終止抓包線程的線程事件
stop_sending?=?threading.Event()
#?用來給抓到扥數據包編號
packet_id?=?1
#?用來存放抓到的數據包
packet_list?=[]
#?暫停抓包的標志位
pause_flag?=?False
#?保存文件標志位
save_flag?=?False
#?停止抓包標志位
stop_flag=False
#?狀態欄類
class?StatusBar(frame):
????def?__init__(self?master):
????????frame.__init__(self?master)
????????self.label?=?Label(self?bd=1?relief=SUNKEN?anchor=W)
????????self.label.pack(fill=X)
????def?set(self?fmt?*args):
????????self.label.config(text=fmt?%?args)
????????self.label.update_idletasks()
????def?clear(self):
????????self.label.config(text=““)
????????self.label.update_idletasks()
#?時間戳轉為格式化的時間字符串
def?timestamp2time(timestamp):
????time_array?=?time.localtime(timestamp)
????mytime?=?time.strftime(“%Y-%m-%d?%H:%M:%S“?time_array)
????return?mytime
“““
數據包列表單擊事件響應函數,在數據包列表單擊某數據包時,在協議解析區解析此數據包,并在hexdump區顯示此數據包的十六進制內容
:param?event:?TreeView單擊事件
:return:?None
“““
def?on_click_packet_list_tree(event):
????#?event.widget獲取Treeview對象,調用selection獲取選擇對象名稱返回結果為字符型元祖
????selected_item?=?event.widget.selection()
????#?清空packet_dissect_tree上現有的內容------------------------
????packet_dissect_tree.delete(*packet_dissect_tree.get_children())
????#?設置協議解析區的寬度
????packet_dissect_tree.column(‘Dissect‘?width=packet_list_frame.winfo_width())
????#?轉換為整型
????packet_id?=?int(selected_item[0])-1
????#?取出要分析的數據包
????packet?=?packet_list[packet_id]
????#packet.show()
????lines?=?(packet.show(dump=True)).split(‘\n‘)??#?dump=True返回字符串,不打出,\n換行符
????last_tree_entry?=?None
????for?line?in?lines:
????????if?line.startswith(‘#‘):
????????????line?=?line.strip(‘#?‘)??#?刪除#
????????????last_tree_entry?=?packet_dissect_tree.insert(‘‘?‘end‘?text=line)??#?第一個參數為空表示根節點
????????else:
????????????packet_dissect_tree.insert(last_tree_entry?‘end‘?text=line)
????????col_width?=?font.Font().measure(line)
????????#?根據新插入數據項的長度動態調整協議解析區的寬度
????????if?packet_dissect_tree.column(‘Dissect‘?width=None)?????????????packet_dissect_tree.column(‘Dissect‘?width=col_width)
????if?IP?in?packet:
????????ip?=?packet[IP]
????????ip_chksum?=?ip.chksum
????????#?ip.show()#抓到的IP報文
????????ip.chksum?=?None
????????#?ip.show()
????????ip_check?=?IP(raw(ip)).chksum
????????ip.chksum?=?ip_chksum
????????print(ip_chksum?“計算出的IP首部校驗和:“?ip_check)
????????if?TCP?in?packet:
????????????tcp?=?packet[TCP]
????????????tcp_chksum?=?tcp.chksum
????????????tcp.chksum?=?None
????????????tcp_check?=?TCP(raw(tcp)).chksum
????????????tcp.chksum?=?tcp_chksum
????????????print(tcp_chksum?“計算出的TCP檢驗和:“?tcp_check)
????????????information?=?“IP與TCP的校驗和檢查通過\r\nIP的校驗和為:{chksum_ip}\r\nT
- 上一篇:Fisher算法線性判別分析python實現
- 下一篇:Python實時顯示數據
評論
共有 條評論