读pcap报文

import os
import re
import codecs
import struct
‘’’
一、pcap文件的格式为:
文件头 24字节
数据报头 + 数据报 数据包头为16字节,后面紧跟数据报
数据报头 + 数据报 …
pcap.h里定义了文件头的格式
二、文件头字段
struct pcap_file_header
{
bpf_u_int32 magic;
u_short version_major;
u_short version_minor;
bpf_int32 thiszone;
bpf_u_int32 sigfigs;
bpf_u_int32 snaplen;
bpf_u_int32 linktype;
}
说明:

1、标识位:32位的,这个标识位的值是16进制的 0xa1b2c3d4。
a 32-bit magic number ,The magic number has the value hex a1b2c3d4.
2、主版本号:16位, 默认值为0x2。
a 16-bit major version number,The major version number should have the value 2.
3、副版本号:16位,默认值为0x04。
a 16-bit minor version number,The minor version number should have the value 4.
4、区域时间:32位,实际上该值并未使用,因此可以将该位设置为0。
a 32-bit time zone offset field that actually not used, so you can (and probably should) just make it 0;
5、精确时间戳:32位,实际上该值并未使用,因此可以将该值设置为0。
a 32-bit time stamp accuracy field tha not actually used,so you can (and probably should) just make it 0;
6、数据包最大长度:32位,该值设置所抓获的数据包的最大长度,如果所有数据包都要抓获,将该值设置为65535;例如:想获取数据包的前64字节,可将该值设置为64。
a 32-bit snapshot length" field;The snapshot length field should be the maximum number of bytes perpacket that will be captured. If the entire packet is captured, make it 65535; if you only capture, for example, the first 64 bytes of the packet, make it 64.
7、链路层类型:32位, 数据包的链路层包头决定了链路层的类型。
a 32-bit link layer type field.The link-layer type depends on the type of link-layer header that the
packets in the capture file have:

三、packet数据包头:

struct pcap_pkthdr
{
struct tim ts;
DWORD caplen;
DWORD len;
}

struct tim
{
DWORD GMTtime;
DWORD microTime
}
1、时间戳,包括:
秒计时:32位,一个UNIX格式的精确到秒时间值,用来记录数据包抓获的时间,记录方式是记录从格林尼治时间的1970年1月1日 00:00:00
到抓包时经过的秒数;
微秒计时:32位, 抓取数据包时的微秒值。
a time stamp, consisting of:
a UNIX-format time-in-seconds when the packet was captured, i.e. the number of seconds since January 1,
1970, 00:00:00 GMT (that GMT, NOT local time!);
the number of microseconds since that second when the packet was captured;

2、数据包长度:32位 ,标识所抓获的数据包保存在pcap文件中的实际长度,以字节为单位。
a 32-bit value giving the number of bytes of packet data that were captured;

3、数据包实际长度: 所抓获的数据包的真实长度,如果文件中保存不是完整的数据包,那么这个值可能要比前面的数据包长度的值大。
a 32-bit value giving the actual length of the packet, in bytes (which may be greater than the previous number,
if you are not saving the entire packet).
‘’’
global search_word
search_word = b’\x01\x01\x01\x0D\x00\x0A\x00\x00\x00\x01\x00\x04\x00\x00\x00\x00\x00\x03\x00\x04\xC0\xA8\x65\x1D\x00\x04\x00\x04\xC0\xA8\x64\x1F\x00\x05\x00\x02\xCA\xEC\x00\x06\x00\x02\x05\xF1\x00\x07\x00\x04\x5E\x78\x14\x1A\x00\x5B\x00\x01\x02\x00\x5D\x00\x02\x00\x02\x00\x11\x00\x02\x00\x03\x00\x43\x00\x78\x28\x20\x3A\x70\x61\x72\x61\x6D\x36\x20\x2C\x31\x2C\x31\x30\x30\x30\x29\x2C\x20\x3A\x70\x61\x72\x61\x6D\x37\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x38\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x39\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x30\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x31\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x32\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x33\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x34\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x35\x20\x2C\x31\x29\x00\x00\x00\x00\x44\x00\x18\x32\x31\x38\x36\x35\x30\x39\x36\x36\x35\x31\x35\x38\x34\x39\x32\x36\x39\x37\x38\x00\x00\x00\x00\x00\x45\x00\x08\x53\x55\x43\x43\x45\x53\x53\x00\x00\x5C\x00\x18\x30\x32\x30\x35\x30\x30\x32\x39\x64\x31\x34\x31\x30\x30\x30\x35\x31\x36\x39\x30\x30\x35\x00\x00\x00\x71\x00\x04\x31\x00\x00\x00’
class pcap():
def init(self, file):
self.file = file
self.i=self.file.split(’/’)[-1].split(’.’)[0]# 取路径中的文件名
self.file_pcap=open(‘D:/python/pcap1/target_{}.pcap’.format(self.i),‘ab+’)
def db_log(self):
#search_word = b’\x01\x01\x01\x0D\x00\x0A\x00\x00\x00\x01\x00\x04\x00\x00\x00\x00\x00\x03\x00\x04\xC0\xA8\x65\x1D\x00\x04\x00\x04\xC0\xA8\x64\x1F\x00\x05\x00\x02\xCA\xEC\x00\x06\x00\x02\x05\xF1\x00\x07\x00\x04\x5E\x78\x14\x1A\x00\x5B\x00\x01\x02\x00\x5D\x00\x02\x00\x02\x00\x11\x00\x02\x00\x03\x00\x43\x00\x78\x28\x20\x3A\x70\x61\x72\x61\x6D\x36\x20\x2C\x31\x2C\x31\x30\x30\x30\x29\x2C\x20\x3A\x70\x61\x72\x61\x6D\x37\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x38\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x39\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x30\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x31\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x32\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x33\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x34\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x35\x20\x2C\x31\x29\x00\x00\x00\x00\x44\x00\x18\x32\x31\x38\x36\x35\x30\x39\x36\x36\x35\x31\x35\x38\x34\x39\x32\x36\x39\x37\x38\x00\x00\x00\x00\x00\x45\x00\x08\x53\x55\x43\x43\x45\x53\x53\x00\x00\x5C\x00\x18\x30\x32\x30\x35\x30\x30\x32\x39\x64\x31\x34\x31\x30\x30\x30\x35\x31\x36\x39\x30\x30\x35\x00\x00\x00\x71\x00\x04\x31\x00\x00\x00’
#b’\x61\x6d\x31\x30\x20\x2c\x20\x3a\x70\x61\x72\x61\x6d\x31\x31\x20’
#35 33 39 2D 65 63 34 63 2D 34 31 36 31 2D 39 35 66 31
magic_number = b’\xd4\xc3\xb2\xa1’
f=open(self.file,‘rb’)
pcap_file = f.read()

	pcap_file_header=pcap_file[0:24]
	self.file_pcap.write(pcap_file_header)
	#print(srcbytearr.find(magic_number,0))
	#print(srcbytearr)
	#print(srcbytearr[0:36])
	#print(srcbytearr[16:20])
	#packet_len=struct.unpack('I',pcap_file[32:36])[0]
	#print(srcbytearr[36:40])

	#startindex=0
	j=24
	len1=24
	len2=0
	#print(len(pcap_file))
	while (j<len(pcap_file)) :
		packet_len=struct.unpack('I',pcap_file[j+12:j+16])[0]
		print(packet_len)
		len2=len1+16+packet_len
		print(len1)
		print(len2)
		s=pcap_file[len1:len2]
		#print(s.hex())
		targetindex_time1 = s.find(search_word,0)
		if targetindex_time1 == -1:
			print('00000000000')
			len1=len2
			j=len2 
			continue
		else:
			self.file_pcap.write(s)					
		len1=len2
		j=len2
		print(len2)
		print('1111111111111')

class file():
def init(self, file):
self.file = file
def search_file(self):
#search_word = b’\x01\x01\x01\x0D\x00\x0A\x00\x00\x00\x01\x00\x04\x00\x00\x00\x00\x00\x03\x00\x04\xC0\xA8\x65\x1D\x00\x04\x00\x04\xC0\xA8\x64\x1F\x00\x05\x00\x02\xCA\xEC\x00\x06\x00\x02\x05\xF1\x00\x07\x00\x04\x5E\x78\x14\x1A\x00\x5B\x00\x01\x02\x00\x5D\x00\x02\x00\x02\x00\x11\x00\x02\x00\x03\x00\x43\x00\x78\x28\x20\x3A\x70\x61\x72\x61\x6D\x36\x20\x2C\x31\x2C\x31\x30\x30\x30\x29\x2C\x20\x3A\x70\x61\x72\x61\x6D\x37\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x38\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x39\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x30\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x31\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x32\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x33\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x34\x20\x2C\x20\x3A\x70\x61\x72\x61\x6D\x31\x35\x20\x2C\x31\x29\x00\x00\x00\x00\x44\x00\x18\x32\x31\x38\x36\x35\x30\x39\x36\x36\x35\x31\x35\x38\x34\x39\x32\x36\x39\x37\x38\x00\x00\x00\x00\x00\x45\x00\x08\x53\x55\x43\x43\x45\x53\x53\x00\x00\x5C\x00\x18\x30\x32\x30\x35\x30\x30\x32\x39\x64\x31\x34\x31\x30\x30\x30\x35\x31\x36\x39\x30\x30\x35\x00\x00\x00\x71\x00\x04\x31\x00\x00\x00’
#list_file=[]
f1=open(self.file,‘rb’)
srcbytearr = f1.read()
startindex=0
while True :
targetindex_time = srcbytearr.find(search_word,startindex)
if targetindex_time == -1:
return -1
break
#startindex=targetindex_time+21
#targetindex_time2 = srcbytearr.find(indxbytearr_time2,startindex)
# break
else:
return self.file
break

if name == ‘main’:
list1=[]
#第一次循环查找匹配的文件
for fpathe,dirs,fs in os.walk(‘D:/python/pcap/’):#路径可修改
for fl in fs:
print(fl)
s=file(os.path.join(fpathe,fl))
if s.search_file() != -1:
list1.append(s.search_file())
print(list1)
#根据第一次查找的文件,针对文件中的每个报文匹配查找,并把找到的报文保存
for fl in list1:
print(fl)
s=pcap(os.path.join(fpathe,fl))
s.db_log()
写自定义目录标题)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值