-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharp_protect.py
More file actions
executable file
·142 lines (123 loc) · 4.46 KB
/
arp_protect.py
File metadata and controls
executable file
·142 lines (123 loc) · 4.46 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
import argparse, ipaddress, os, re, subprocess, time
from pathlib import Path
from typing import Dict, Set, Tuple, Optional
BASE = Path(__file__).resolve().parent
ALERT = BASE / "alerts.log"
BLOCK = BASE / "blocks.txt"
WHITE = BASE / "whitelist.txt"
IP_MAC = re.compile(r"([\d.]+)\s+dev\s+\S+\s+lladdr\s+([0-9a-f:]{17})\s+(\w+)", re.I)
observed: Dict[str, str] = {}
alerted: Set[Tuple[str, str]] = set()
# discover local subnets once
NETS = []
for line in subprocess.check_output(
"ip -o -4 addr show scope global", shell=True, text=True).splitlines():
m = re.search(r"inet (\d+\.\d+\.\d+\.\d+)/(\d+)", line)
if m:
ip, mask = m.groups()
NETS.append(ipaddress.ip_network(f"{ip}/{mask}", strict=False))
def internal(ip: str) -> bool:
ip_o = ipaddress.ip_address(ip)
return any(ip_o in n for n in NETS)
def run(rule: str) -> None:
subprocess.call(["sudo", "sh", "-c", rule],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
def persist(ip: str, mac: str) -> None:
line = f"{ip} {mac}\n"
if BLOCK.exists() and line in BLOCK.read_text():
return
with BLOCK.open("a") as f:
f.write(line)
def block(ip: str, mac: str) -> None:
rules = [
f"iptables -C INPUT -m mac --mac-source {mac} -j DROP || "
f"iptables -I INPUT -m mac --mac-source {mac} -j DROP",
f"iptables -C FORWARD -m mac --mac-source {mac} -j DROP || "
f"iptables -I FORWARD -m mac --mac-source {mac} -j DROP",
]
if not internal(ip):
rules += [
f"iptables -C INPUT -s {ip} -j DROP || "
f"iptables -I INPUT -s {ip} -j DROP",
f"iptables -C OUTPUT -d {ip} -j DROP || "
f"iptables -I OUTPUT -d {ip} -j DROP",
f"iptables -C FORWARD -s {ip} -j DROP || "
f"iptables -I FORWARD -s {ip} -j DROP",
]
for r in rules:
run(r)
persist(ip, mac)
def arp_table(iface: Optional[str] = None) -> Dict[str, str]:
tbl: Dict[str, str] = {}
cmd = "ip neigh"
if iface:
cmd += f" show dev {iface}"
out = subprocess.check_output(cmd, shell=True, text=True)
for line in out.splitlines():
m = IP_MAC.match(line)
if m and m.group(3) in ("REACHABLE", "STALE", "DELAY", "PROBE"):
tbl[m.group(1)] = m.group(2).lower()
return tbl
def whitelist() -> Dict[str, Optional[str]]:
data: Dict[str, Optional[str]] = {}
if WHITE.exists():
for raw in WHITE.read_text().splitlines():
raw = raw.split("#", 1)[0].strip()
if raw:
ip, *rest = raw.split()
data[ip] = rest[0].lower() if rest else None
return data
def blocked_ips() -> Set[str]:
if not BLOCK.exists():
return set()
return {l.split()[0] for l in BLOCK.read_text().splitlines() if l.strip()}
def log(msg: str) -> None:
with ALERT.open("a") as f:
f.write(msg + "\n")
def protect(iface: Optional[str] = None) -> None:
wl = whitelist()
blk = blocked_ips()
print("[ARP Protection] Running. Ctrl+C to stop.")
while True:
for ip, mac in arp_table(iface).items():
pair = (ip, mac)
if ip in wl and (wl[ip] in (None, mac)):
observed[ip] = mac
continue
if ip not in observed:
observed[ip] = mac
continue
if ip in blk:
if pair not in alerted:
msg = f"[ALERT] ARP spoofing | IP={ip} MAC={mac} (already blocked)"
print(msg)
log(msg)
alerted.add(pair)
continue
if observed[ip] != mac:
msg = f"[ALERT] ARP spoofing | IP={ip} MAC={mac}"
print(msg)
log(msg)
block(ip, mac)
blk.add(ip)
block_msg = f"[BLOCKED] {ip} {mac}"
print(block_msg)
log(block_msg)
alerted.add(pair)
observed[ip] = mac
time.sleep(5)
def main() -> None:
if os.geteuid() != 0:
print("Run as root.")
return
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("--loop", action="store_true")
parser.add_argument("--iface")
args = parser.parse_known_args()[0]
try:
protect(args.iface)
except KeyboardInterrupt:
print("\n[ARP Protection] stopped.")
if __name__ == "__main__":
main()