天天看點

Scapy之ARP詢問

引言

校園網中,有同學遭受永恒之藍攻擊,但是被防毒軟體查下,并知道了攻擊者的ip也是校園網。是以我想看一下,這個ip是PC,還是路由器。

在ip視角,路由器和pc沒什麼差别。

實作

首先是構造arp封包,進行廣播

send.py

from scapy.all import *

myarp = ARP()

myarp.psrc = '172.17.132.176'
myarp.pdst = '172.17.174.73'

myarp.op = 1

while True:
	send(myarp)
           

構造arp封包,填寫我的本機ip

172.17.132.176

,釋放永恒之藍的ip

172.17.174.73

,op 為1代表查詢,為2代表回應,這裡我們是查詢。

攻擊者終端收到arp請求後,會相應arp,裡面攜帶有攻擊者的mac

receive.py

from scapy.all import *

while True:
	PTKS = sniff(store = 1,timeout = 0.1)
	PTKS.show()
           

可以從終端看到列印的arp回應,攜帶有mac。如下:

Scapy之ARP詢問

查詢mac為什麼裝置

将以上mac在 https://mac.51240.com/ 輸入查詢,可得到廠商,基本就知道終端為路由器或者PC

例如:

Scapy之ARP詢問

進階

import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *

def scapy_arp_request(ip_address , ifname = 'eth0',queue = None):
    result_raw = srp(Ether(dst = 'FF:FF:FF:FF:FF:FF')#srp  二層幀
        /ARP(op = 1,hwdst = '00:00:00:00:00:00',pdst = ip_address),#ARP詢問操作,op置1
        timeout = 1,#等待1s
        iface = ifname,#二層一定要填寫接口
        verbose = False)#關閉發送資料提示資訊
#result_raw接收到的資料如:(<Results: TCP:0 UDP:0 ICMP:0 Other:1>, <Unanswered: TCP:0 UDP:0 ICMP:0 Other:0>)
#[0]為相應的資料,[1]為未相應的資料(等待1s,是以有可能會産生未響應的資料)
    try:
        result_list = result_raw[0].res #把響應的資料包對,産生為清單
#result_list資料為展開的資訊,如:[(<Ether  dst=FF:FF:FF:FF:FF:FF type=0x806 |<ARP  op=who-has hwdst=00:00:00:00:00:00 pdst=172.17.174.73 |>>, <Ether  dst=e0:3f:49:a1:99:6c src=58:69:6c:5e:70:ec type=0x806 |<ARP  hwtype=0x1 ptype=0x800 hwlen=6 plen=4 op=is-at hwsrc=58:69:6c:5e:70:ec psrc=172.17.174.73 hwdst=e0:3f:49:a1:99:6c pdst=172.17.171.178 |<Padding  load='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |>>>)]

#可以看到,result_list中隻有一組資料,下标為0。在這一組裡,[1]代表接收到的包,[0]代表發送的資料包
#[2]ARP頭部字段的['hwsrc']字段,作為傳回值傳回

        if queue == None:
            #return result_list[0][1][1].fields['hwsrc']
            return result_list[0][1].getlayer(ARP).fields['hwsrc']

        else:
            queue.put((ip_address,result_list[0][1].getlayer(ARP).fields['hwsrc']))

    except:
        return 

if __name__ == "__main__":
    import sys
    print(scapy_arp_request(sys.argv[1],sys.argv[2]))
           

執行程式,後面跟着參數

需要查詢mac的ip

網口名稱

,即可列印目标mac

send()函數将會在第3層發送資料包。也就是說它會為你處理路由和第2層的資料。sendp()函數将會工作在第2層。選擇合适的接口和正确的鍊路層協定都取決于你。

sr()函數是用來發送資料包和接收應答。該函數傳回一對資料包及其應答,還有無應答的資料包。srp()則是使用第2層封包(以太網,802.3等)。

參考

推薦:https://www.jianshu.com/p/8eab70118fad

https://zhuanlan.zhihu.com/p/34843290

https://github.com/Larryxi/Scapy_zh-cn