1. 程式人生 > >python scapy讀取pcap包

python scapy讀取pcap包

廢話不說,直接上程式碼.相對來說還是比較簡單的

import scapy_http.http
try:
    import scapy.all as scapy
except ImportError:
    import scapy


def parse_http_pcap(pcap_path):
    pcap_infos = list()
    packets = scapy.rdpcap(pcap_path)
    for p in packets:
        print "----"
        # 判斷是否包含某一層,用haslayer
        if
p.haslayer("IP"): src_ip = p["IP"].src dst_ip = p["IP"].dst print "sip: %s" % src_ip print "dip: %s" % dst_ip if p.haslayer("TCP"): # 獲取某一層的原始負載用.payload.original raw_http = p["TCP"].payload.original sport = p["TCP"
].sport dport = p["TCP"].dport print "sport: %s" % sport print "dport: %s" % dport print "raw_http:\n%s" % raw_http if p.haslayer("HTTPRequest"): host = p["HTTPRequest"].Host uri = p["HTTPRequest"].Path # 直接獲取提取好的字典形式的http資料用fields
http_fields = p["HTTPRequest"].fields http_payload = p["HTTPRequest"].payload.fields print "host: %s" % host print "uri: %s" % uri print "http_fields:\n%s" % http_fields print "http_payload:\n%s" % http_payload parse_http_pcap("test.pcap")