2011-02-09 7 views
23

나는 파이썬을위한 pcap 라이브러리의 추천을 요청하고 싶다. .pcap 파일 구문 분석에 대한 프로젝트를 수행하고 있습니다. 나는 Google을 검색하여 pylibpcap을 발견했습니다. 거기 밖에 다른 것이 있습니까? 어느 lib를 선호합니까?pcap 파이썬 라이브러리?

감사합니다.

+0

안녕하세요 PSS, 그래서 프로젝트를 완성하셨습니까? 너에게 접근 했니? –

답변

16

scapy을 시도합니다. 패킷 검사, 조작 및 생성을위한 매우 강력한 프로그램입니다.

build your own tools으로 사용할 수 있습니다.

+0

http://github.com/phaethon/scapy에서 scapy의 새 버전을 확인하십시오. 그것은 python3과 호환되며 새로운 기능을 포함합니다. –

+4

파이썬 라이브러리의 경우 비정상적으로 scapy가 GPLv2라는 점에 유의하십시오. 라이브러리 면제는 아닙니다. 따라서 import scapy를 입력하면 코드가 GPLv2가됩니다. 괜찮 으면 이동하십시오. – Hal

14

나는 그것을 시도한 다음 pcapy를 시도했다. 내 사용법이 내가 찾은 사례와 비슷하기 때문에 나는 pcapy를 선택한다.

http://snipplr.com/view/3579/live-packet-capture-in-python-with-pcapy/

(이하 복사 동일한 코드 참조)

import pcapy 
from impacket.ImpactDecoder import * 

# list all the network devices 
pcapy.findalldevs() 

max_bytes = 1024 
promiscuous = False 
read_timeout = 100 # in milliseconds 
pc = pcapy.open_live("name of network device to capture from", max_bytes, 
    promiscuous, read_timeout) 

pc.setfilter('tcp') 

# callback for received packets 
def recv_pkts(hdr, data): 
    packet = EthDecoder().decode(data) 
    print packet 

packet_limit = -1 # infinite 
pc.loop(packet_limit, recv_pkts) # capture packets 
5

Pyshark를 사용하는 것이 좋습니다. 이것은 tshark 래퍼입니다. 그것은 또한 모든 tshark 필터, 디코더 lib를 지원하며 ... 사용하기 쉽습니다!

import pyshark 
cap = pyshark.FileCapture('/root/log.cap') 
cap 
>>> <FileCapture /root/log.cap> 
print cap[0] 
Packet (Length: 698) 
Layer ETH: 
     Destination: BLANKED 
     Source: BLANKED 
     Type: IP (0x0800) 
Layer IP: 
     Version: 4 
     Header Length: 20 bytes 
     Differentiated Services Field: 0x00 (DSCP 0x00: Default; ECN: 0x00: Not-ECT (Not ECN-Capable Transport)) 
     Total Length: 684s 
     Identification: 0x254f (9551) 
     Flags: 0x00 
     Fragment offset: 0 
     Time to live: 1 
     Protocol: UDP (17) 
     Header checksum: 0xe148 [correct] 
     Source: BLANKED 
     Destination: BLANKED 
    ... 
dir(cap[0]) 
['__class__', '__contains__', '__delattr__', '__dict__', '__dir__', '__doc__', '__format__', '__getattr__', '__getattribute__', '__getitem__', '__getstate__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_packet_string', 'bssgp', 'captured_length', 'eth', 'frame_info', 'gprs-ns', 'highest_layer', 'interface_captured', 'ip', 'layers', 'length', 'number', 'pretty_print', 'sniff_time', 'sniff_timestamp', 'transport_layer', 'udp'] 
cap[0].layers 
[<ETH Layer>, <IP Layer>, <UDP Layer>, <GPRS-NS Layer>, <BSSGP Layer>] 
....