2015-05-05 33 views
0

我在Scapy中構建了一個網絡嗅探器,但它無法處理我正在嗅探的數據包的速率(它增加了15-20分鐘的延遲,這是不可接受的)。在過去,我曾經以成功的速度使用Pcapy,但這次爲了節省我不得不重寫所有使用Scapy的解析代碼,我想將Pcapy收到的數據包轉換爲Scapy IP對象。問題是,當我嘗試這樣做時,我得到的IP和協議號碼是亂碼/不可用的,就像Scapy正在讀取數據包的錯誤部分。Python - Pcapy到Scapy的SPAN端口,奇怪的行爲

一些示例代碼如下:

#!/usr/bin/python 
from pcapy import findalldevs, open_live 
from impacket import ImpactDecoder, ImpactPacket 
from scapy.all import * 

def sniff(): 
    interface = "eth3" 

    print "Listening on: %s" % interface 

    # Open a live capture 
    reader = open_live(interface, 65535, 1, 100) 

    # Set a filter to be notified only for TCP packets 
    reader.setfilter('ip proto \\tcp') 

    # Run the packet capture loop 
    reader.loop(0, callback) 

def callback(hdr, data): 

    pkt = IP(data) 
    if IP in pkt: 

     print pkt[IP].dst 

    # Parse the Ethernet packet 
    #decoder = ImpactDecoder.EthDecoder() 
    #ether = decoder.decode(data) 

    # Parse the IP packet inside the Ethernet packet 
    #iphdr = ether.child() 

    # Parse the TCP packet inside the IP packet 
    #tcphdr = iphdr.child() 

    # Only process SYN packets 
    #if tcphdr.get_SYN() and not tcphdr.get_ACK(): 

    # # Get the source and destination IP addresses 
    # src_ip = iphdr.get_ip_src() 
    # dst_ip = iphdr.get_ip_dst() 

    # # Print the results 
    # print "Connection attempt %s -> %s" % (src_ip, dst_ip) 

def main(): 
    sniff() 

if __name__ == "__main__": 
    main() 

和輸出的例子:

30.184.113.84 
0.120.231.205 
30.184.113.91 
5.64.113.97 
0.120.231.206 
21.248.113.98 
0.120.231.207 
0.120.231.208 
0.120.231.209 
0.120.231.210 
0.120.231.211 
0.48.243.73 

正如你可以看到這些IP的不道理,你在哪裏想到,我錯了。 Eth3連接到NetGear鏡像端口。

感謝您的時間。

回答

0

沒關係,只是我是一個白癡,我怪銀行假期星期一。我試圖從錯誤的層檢測數據包。將原料轉換爲醚,Scapy爲我完成剩下的工作。

def callback(hdr, data): 

    pkt = Ether(data) 

    if IP in pkt: 

     print pkt[IP].dst 
    else: 

     print list(pkt) 

乾杯