-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
107 lines (85 loc) · 2.48 KB
/
Copy pathutils.py
File metadata and controls
107 lines (85 loc) · 2.48 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
import ipaddress
import threading
IS_LOCALHOST = False
CHOICE_START_STOP_IDS = "START_STOP_IDS"
CHOICE_VIEW_TRAFFIC = "VIEW_TRAFFIC"
CHOICE_VIEW_LOGS = "VIEW_LOGS"
CHOICE_VIEW_BLOCKED_IPS = "VIEW_BLOCKED_IPS"
CHOICE_CLEAR_BLOCKED_LIST = "CLEAR_BLOCKED_LIST"
CHOICE_UNBLOCK_IP = "UNBLOCK_IP"
CHOICE_EXIT = "EXIT"
CHOICES = {
1: CHOICE_START_STOP_IDS,
2: CHOICE_VIEW_TRAFFIC,
3: CHOICE_VIEW_LOGS,
4: CHOICE_VIEW_BLOCKED_IPS,
5: CHOICE_CLEAR_BLOCKED_LIST,
6: CHOICE_UNBLOCK_IP,
7: CHOICE_EXIT,
}
LOG_FILE_NAME = "ids.log"
LOGS_DF_ORDER = [
"Timestamp",
"Date",
"Time",
"Intrusion Type",
"Attacker IP",
"Targeted Ports/Flags",
"Time Span Of Attack",
]
IP_SET_LOCK = threading.Lock()
STDOUT_LOCK = threading.Lock()
BLOCKED_IP_SET = set()
IDS_THREAD = None
VIEW_TRAFFIC_THREAD = None
FIREWALL_UPDATE_THREAD = None
def thread_safe_print(message: str) -> None:
"""Print a message in a thread-safe manner"""
with STDOUT_LOCK:
print(message)
def is_valid_ip(ip_str: str) -> bool:
"""Validate if a string is a valid IPv4 or IPv6 address."""
try:
ipaddress.ip_address(ip_str)
return True
except ValueError as err:
thread_safe_print(f"[ERROR] {repr(err)}")
return False
def is_ip_blocked(ip: str) -> bool:
"""Check if an IP is already blocked"""
with IP_SET_LOCK:
res = ip in BLOCKED_IP_SET
return res
def add_to_blocked_ip_set(ip: str) -> bool:
"""Add an IP address to the blocked set in a thread-safe manner"""
if not is_valid_ip(ip):
return False
with IP_SET_LOCK:
try:
BLOCKED_IP_SET.add(ip)
except Exception as err:
thread_safe_print(f"[ERROR] {repr(err)}")
return False
return True
def remove_ip_from_blocked_set(ip: str) -> bool:
"""Remove an IP from the blocked set in a thread-safe manner"""
with IP_SET_LOCK:
try:
BLOCKED_IP_SET.remove(ip)
except Exception as err:
thread_safe_print(f"[ERROR] {repr(err)}")
return False
return True
def clear_blocked_ip_set() -> bool:
"""Clear the blocked set"""
with IP_SET_LOCK:
try:
BLOCKED_IP_SET.clear()
except Exception as err:
thread_safe_print(f"[ERROR] {repr(err)}")
return False
return True
def write_to_log_file(line: str) -> None:
"""Write a line to the log file"""
with open(LOG_FILE_NAME, "a") as fp:
fp.write(line)