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報文,填寫我的本機ip172.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。如下:

查詢mac為什么設備

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

例如:

進階

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM