資源簡介
本代碼能對抓包工具抓下來的pcap包各個字段進行精確的解析,包括文件頭,報文頭,協議頭,數據內容等的解析。。
代碼片段和文件信息
#!/usr/bin/env?python
#?coding:?utf-8
‘‘‘
Created?on?2016年8月3日
@author:?dxl
‘‘‘
import?dpkt
import?os
import?socket
import?struct
#from?autotest.configure?import?filepath
tcp_or_udp?={}
tcp_or_udp[6]?=?‘tcp‘
tcp_or_udp[17]?=?‘udp‘
tcp_or_udp[1]?=?‘icmp‘
tcp_or_udp[2]?=?‘igmp‘
def?parseRadius(filepath):
????fpcap?=?open(filepath‘rb‘)
????source_pcap_name?=?os.path.basename(filepath).split(‘.‘)[0]
????string_data?=?fpcap.read()
????print?len(string_data)
????#pcap文件包頭解析
????pcap_header?=?{}
????pcap_header[‘magic_number‘]?=?string_data[0:4]
????pcap_header[‘version_major‘]?=?string_data[4:6]
????pcap_header[‘version_minor‘]?=?string_data[6:8]
????pcap_header[‘thiszone‘]?=?string_data[8:12]
????pcap_header[‘sigfigs‘]?=?string_data[12:16]
????pcap_header[‘snaplen‘]?=?string_data[16:20]
????pcap_header[‘linktype‘]?=?string_data[20:24]
????#pcap頭部長度?24字節????
????pcap_header_str?=?string_data[:24]
????print?pcap_header_str
????
????#pcap文件的數據包解析
?
評論
共有 條評論