2012-04-23 4 views
1

Wireshark 프로그램에서 덤프 된 wireshark.bin 데이터 파일을 읽고 패킷 시간을 선택합니다. 머리글을 건너 뛰고 처음으로 어떻게 찾을 수 있는지 잘 모릅니다.패킷 시간에 대한 Wireshark 덤프 파일 읽기

""" 
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)) 

답변

1

한 번에 전체 파일을 읽고, 다음 목록으로 접근 해보십시오 :

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) 바이트 크기의 청크의 데이터를 통해 루프 상쇄 할 수 각 청크에서 타임 스탬프를 찾습니다.

마법 번호는 4 바이트 또는 2 단어를 사용하는 입니다. , 우리가 할 수있는 지금 우리는 순서를 가지고

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() 

(그리고 그것이 와이어 샤크 파일 알고) : 우리는 struct 모듈을 사용하여 이러한 처음 4 바이트를 검사하여 (빅 endian 또는 little endian) 순서를 결정할 수 있습니다 패킷을 반복 :이 숙제이지만, 그 field? 이름 타임 스탬프와 다음 패킷 데이터의 길이를 포함하는 패킷 헤더에 저장된 정보를 나타내는 때문에

field0, field1, field2, field3 = \ 
     struct.unpack('%sllll' % order, data[:16]) 
payload = data[16 : 16 + field?] 
data = data[16 + field?] 

난 모호 이름을 떠났다.

이 코드는 불완전하지만 잘하면 갈 수있을 것입니다.

+0

전체 파일을 한 번에 읽으려고하면 오류가 발생합니다. 역 추적 (가장 최근의 호출 마지막) : 파일 "V : \ 작업 \ Python3_Homework08 \ SRC \ wiretimes.py" 데이터 라인 8 = 공개 ("wireshark.bin")) (읽기 파일 "C. : \ 0 \UnicodeDecodeError : 'charmap'코덱이 28 위치의 바이트 0x8d를 디코딩 할 수 없습니다. (예 : \ Python \ lib \ encodings \ cp1252.py), 디코드시 return codecs.charmap_decode (input, self.errors, decoding_table) [0] : 문자가

+0

@Python_Maybe : woops, 'rb'- 편집 답변을 잊어 버렸습니다. –

+0

감사합니다. 이제 전체 파일을 읽을 수 있습니다. 패킷 길이를 어떻게 알 수 있습니까? 모든 패킷이 동일한 크기는 아닙니다. –

관련 문제