2015-09-27 241 views
0

我發送ARP分組廣播與此行:的Python Scapy的--arp請求和響應

send(ARP(op=ARP.who_has, psrc="192.168.5.51", pdst=the_ip)) 

我的問題是:如何查看響應(在這種情況下:遠程IP的MAC) ?我知道我可以做:

pkt = sniff(filter=arp , count=10) 
print (pkt.summary()) 

但我不想算數據包,因爲我不知道什麼時候會被打印(可能是在未來10個或100包)

是否有嗅探的方式,打印摘要,因此,看到我正在尋找的Mac地址?

編輯:我有一個想法,我可以嗅探10個數據包,如果有數據包中的IP打印MAC地址,否則嗅探10個數據包......這種技術似乎不是一個好的方法...

回答

2

Scapy's user manual建議使用用於發送數據包和接收答案sr()sr1()功能:

The sr() function is for sending packets and receiving answers. The function returns a couple of packet and answers, and the unanswered packets. The function sr1() is a variant that only returns one packet that answered the packet (or the packet set) sent. The packets must be layer 3 packets (IP, ARP, etc.). The function srp() does the same for layer 2 packets (Ethernet, 802.3, etc.)

The official API documentation指定其完整的簽名。這些似乎是這個用例的相關參數:

retry : if positive, how many times to resend unanswered packets. if negative, how many consecutive unanswered probes before giving up. Only the negative value is really useful.
timeout : how much time to wait after the last packet has been sent. By default, sr will wait forever and the user will have to interrupt (Ctrl-C) it when he expects no more answers.
inter : time in seconds to wait between each packet sent.

這裏是與sr()功能執行例子:

In [1]: from scapy.all import * 
WARNING: No route found for IPv6 destination :: (no default route?) 

In [2]: results, unanswered = sr(ARP(op=ARP.who_has, psrc='192.168.1.2', pdst='192.168.1.1')) 
Begin emission: 
.....*Finished to send 1 packets. 

Received 6 packets, got 1 answers, remaining 0 packets 

In [3]: results 
Out[3]: <Results: TCP:0 UDP:0 ICMP:0 Other:1> 

In [4]: result = results[0] 

In [5]: result 
Out[5]: 
(<ARP op=who-has psrc=192.168.1.2 pdst=192.168.1.1 |>, 
<ARP hwtype=0x1 ptype=0x800 hwlen=6 plen=4 op=is-at hwsrc=XX:XX:XX:XX:XX:XX psrc=192.168.1.1 hwdst=XX:XX:XX:XX:XX:XX pdst=192.168.1.2 |>) 

In [6]: original_packet, answer = result 

In [7]: original_packet 
Out[7]: <ARP op=who-has psrc=192.168.1.2 pdst=192.168.1.1 |> 

In [8]: answer 
Out[8]: <ARP hwtype=0x1 ptype=0x800 hwlen=6 plen=4 op=is-at hwsrc=XX:XX:XX:XX:XX:XX psrc=192.168.1.1 hwdst=XX:XX:XX:XX:XX:XX pdst=192.168.1.2 |> 

這裏與sr1()功能執行例子:

In [9]: result = sr1(ARP(op=ARP.who_has, psrc='192.168.1.2', pdst='192.168.1.1')) 
Begin emission: 
.....Finished to send 1 packets. 
* 
Received 6 packets, got 1 answers, remaining 0 packets 

In [10]: result 
Out[10]: <ARP hwtype=0x1 ptype=0x800 hwlen=6 plen=4 op=is-at hwsrc=XX:XX:XX:XX:XX:XX psrc=192.168.1.1 hwdst=XX:XX:XX:XX:XX:XX pdst=192.168.1.2 |> 
+0

我無法打印結果(必須使用字節打印)。我嘗試瞭解碼(「UTF-8」),這是行不通的:AttributeError:decode –

+0

數據包的十六進制表示可能通過語句str(packet).encode(「HEX」)打印出來。 – Yoel

+0

這是輸出:pkt = str(pkt).encode(「HEX」) LookupError:'HEX'不是文本編碼;使用codecs.encode()來處理任意的編解碼器 –