我的指令是读取从Wireshark程序中转储的wirehark.bin数据文件,并选择数据包时间。我不知道如何跳过标题并找到第一次。
"""
reads the wireshark.bin data file dumped from the wireshark program
"""
from datetime import datetime
import struct
import datetime
#file = r"V:\workspace\Python3_Homework08\src\wireshark.bin"
file = open("wireshark.bin", "rb")
idList = [ ]
with open("wireshark.bin", "rb") as f:
while True:
bytes_read = file.read(struct.calcsize("=l"))
if not bytes_read:
break
else:
if len(bytes_read) > 3:
idList.append(struct.unpack("=l", bytes_read)[0])
o = struct.unpack("=l111", bytes_read)[0]
print( datetime.date.fromtimestamp(o))
最佳答案
尝试一次读取整个文件,然后以列表形式访问它:
data = open("wireshark.bin", "rb").read() # let Python automatically close file
magic = data[:4] # magic wireshark number (also reveals byte order)
gmt_correction = data[8:12] # GMT offset
data = data[24:] # actual packets
现在,您可以遍历(16?)个字节大小的块中的数据,查看时间戳中每个块中的适当偏移量。
幻数是
0xa1b2c3d4
,它需要四个字节或两个字。我们可以通过使用struct
模块检查前四个字节来确定顺序(大端或小端):magic = struct.unpack('>L', data[0:4])[0] # before the data = data[24:] line above
if magic == 0xa1b2c3d4:
order = '>'
elif magic == 0xd4c3b2a1:
order = '<'
else:
raise NotWireSharkFile()
现在我们有了订单(并且知道它是一个wireshark文件),我们可以遍历数据包:
field0, field1, field2, field3 = \
struct.unpack('%sllll' % order, data[:16])
payload = data[16 : 16 + field?]
data = data[16 + field?]
由于这是家庭作业,所以我留下了模糊的名称,但是那些
field?
名称代表存储在数据包头中的信息,其中包括时间戳和后续数据包的长度。这段代码是不完整的,但是希望足以使您继续前进。