-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocklist.py
96 lines (85 loc) · 3.18 KB
/
blocklist.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import requests
import ipaddress
import logging
import subprocess
import json
# Logging einrichten
def setup_logging():
logging.basicConfig(
filename="blocklist.log",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
)
# Blocklist herunterladen
def download_blocklist(url, timeout):
try:
response = requests.get(url, timeout=timeout)
response.raise_for_status()
return response.text.splitlines()
except requests.RequestException as e:
logging.error(f"Fehler beim Herunterladen der Blockliste von {url}: {e}")
return []
# Blockliste verarbeiten und validieren
def parse_blocklist(blocklist):
ipv4_addresses = []
ipv6_addresses = []
for line in blocklist:
line = line.strip()
if not line or line.startswith("#"): # Leere Zeilen und Kommentare ignorieren
continue
try:
ip = ipaddress.ip_network(line, strict=False)
if ip.version == 4:
ipv4_addresses.append(str(ip))
elif ip.version == 6:
ipv6_addresses.append(str(ip))
except ValueError:
logging.warning(f"Ungültige IP/Subnetz gefunden und übersprungen: {line}")
return ipv4_addresses, ipv6_addresses
# IPs zu Regeln hinzufügen
def add_ips_to_iptables(ipv4_list, ipv6_list):
for ip in ipv4_list:
try:
subprocess.run(
["iptables", "-I", "INPUT", "-s", ip, "-j", "DROP"],
check=True,
capture_output=True,
)
logging.info(f"IPv4-Adresse/Subnetz erfolgreich hinzugefügt: {ip}")
except subprocess.CalledProcessError as e:
logging.error(f"Fehler beim Hinzufügen von IPv4 {ip}: {e.stderr.decode()}")
for ip in ipv6_list:
try:
subprocess.run(
["ip6tables", "-I", "INPUT", "-s", ip, "-j", "DROP"],
check=True,
capture_output=True,
)
logging.info(f"IPv6-Adresse/Subnetz erfolgreich hinzugefügt: {ip}")
except subprocess.CalledProcessError as e:
logging.error(f"Fehler beim Hinzufügen von IPv6 {ip}: {e.stderr.decode()}")
# URLs aus Konfigurationsdatei laden
def load_urls_from_config(config_file="blocklist.json"):
try:
with open(config_file, "r") as file:
config = json.load(file)
return config.get("urls", [])
except (FileNotFoundError, json.JSONDecodeError) as e:
logging.error(f"Fehler beim Laden der Konfigurationsdatei: {e}")
return []
# Hauptfunktion
def main():
setup_logging()
urls = load_urls_from_config()
if not urls:
logging.error("Keine gültigen URLs in der Konfigurationsdatei gefunden.")
return
timeout = 10 # Timeout für HTTP-Anfragen in Sekunden
for url in urls:
logging.info(f"Blockliste von {url} wird verarbeitet...")
blocklist = download_blocklist(url, timeout)
ipv4_list, ipv6_list = parse_blocklist(blocklist)
add_ips_to_iptables(ipv4_list, ipv6_list)
logging.info(f"Verarbeitung der Blockliste von {url} abgeschlossen.")
if __name__ == "__main__":
main()