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='x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00' |>>>)]

#可以看到,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

原文地址:https://www.cnblogs.com/maskerk/p/10011923.html