-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonSIEMscript.py
More file actions
1078 lines (949 loc) · 43.8 KB
/
pythonSIEMscript.py
File metadata and controls
1078 lines (949 loc) · 43.8 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import socket
import re
import os
import ipaddress
from datetime import datetime
import random
import argparse
import subprocess
import platform
import sys
import time
import threading
import queue
from collections import defaultdict, deque
# Attempt to import tkinter for the GUI (Traffic Visualizer).
try:
import tkinter as tk
except ImportError:
tk = None
# ==========================================
# CONFIGURATION
# ==========================================
# Paths to common log files (Adjust as needed)
NGINX_LOG_PATH = "/var/log/nginx/access.log"
APACHE_LOG_PATH = "/var/log/apache2/access.log"
LINUX_NETWORK_LOG = "/var/log/kern.log" # Often contains UFW/IPTables logs
# [CENTRALIZED LOGGING CONFIG]
SIEM_HQ_IP = None
SIEM_PORT = 5555
# [PHP API CONFIG]
PHP_API_ENABLED = True # Send events to PHP API
PHP_API_URL = "http://localhost/SIEMproject/api.php/security-events"
# [LOGSTASH SYSLOG CONFIG]
LOGSTASH_HOST = "127.0.0.1"
LOGSTASH_PORT = 10514
# ==========================================
# BASE CLASS: LOGS
# ==========================================
class Logs:
"""
Base class responsible for OS detection and initializing the Log Queue.
"""
def __init__(self):
self.os_type = self.detect_os()
# A thread-safe queue to hold logs from all sources
# Items in queue are tuples: (source_type, log_data_string)
self.log_queue = queue.Queue()
self.stop_event = threading.Event()
def detect_os(self):
"""Determines if we are running on Linux, Windows, or macOS."""
system = platform.system()
if system == 'Windows':
return 'Windows'
elif system == 'Linux':
return 'Linux'
elif system == 'Darwin':
return 'macOS'
else:
return 'Unknown'
def start_tailing_thread(self, target, args=()):
"""Helper to start a daemon thread."""
t = threading.Thread(target=target, args=args, daemon=True)
t.start()
return t
# ==========================================
# CLASS: READ_LOGS (COLLECTOR)
# ==========================================
class Read_logs(Logs):
"""
Spins up background threads to 'tail' various log sources.
Pushes incoming lines to the central self.log_queue.
"""
def __init__(self):
super().__init__()
self.log_dir = "captured_logs"
if not os.path.exists(self.log_dir):
os.makedirs(self.log_dir)
# Start the background listeners immediately
self.start_log_streams()
def start_log_streams(self):
print("[Read_logs] Initializing continuous log streams...")
# 1. System Logs (OS specific)
if self.os_type == 'Linux':
# journalctl -f -o json (Follows system logs in JSON format)
self.start_tailing_thread(self._tail_command, (['journalctl', '-f', '-o', 'json'], 'linux_system'))
# Tail Network/Kernel logs
if os.path.exists(LINUX_NETWORK_LOG):
self.start_tailing_thread(self._tail_file, (LINUX_NETWORK_LOG, 'linux_network'))
elif self.os_type == 'macOS':
# log stream --style json (Follows system logs in JSON)
self.start_tailing_thread(self._tail_command, (['log', 'stream', '--style', 'json'], 'macos_system'))
elif self.os_type == 'Windows':
# Windows Event Logs don't "tail" natively. We run a polling loop in a thread.
self.start_tailing_thread(self._poll_windows_events)
# 2. Web Server Logs (File Tailing)
# Nginx
if os.path.exists(NGINX_LOG_PATH):
print(f"[Read_logs] Found Nginx at {NGINX_LOG_PATH}, tailing...")
self.start_tailing_thread(self._tail_file, (NGINX_LOG_PATH, 'nginx_access'))
# Apache
if os.path.exists(APACHE_LOG_PATH):
print(f"[Read_logs] Found Apache at {APACHE_LOG_PATH}, tailing...")
self.start_tailing_thread(self._tail_file, (APACHE_LOG_PATH, 'apache_access'))
# --- WORKER: Tail a File (Linux/macOS/Windows) ---
def _tail_file(self, filepath, source_tag):
"""
Equivalent to 'tail -f'. Opens file, seeks to end, and reads new lines.
"""
try:
# Use 'subprocess' with tail on Unix for robustness, or python file seek
# Using subprocess tail -f is often more robust against log rotation on Linux.
if self.os_type in ['Linux', 'macOS']:
cmd = ['tail', '-f', filepath]
self._tail_command(cmd, source_tag)
else:
# Fallback for pure Python implementation (Windows mostly)
with open(filepath, 'r') as f:
f.seek(0, 2) # Go to end
while not self.stop_event.is_set():
line = f.readline()
if not line:
time.sleep(0.1)
continue
self.log_queue.put((source_tag, line.strip()))
except Exception as e:
print(f"[Error] Tailing file {filepath}: {e}")
def forward_to_syslog(self, source, data):
"""Broadcasts fetched logs to the Logstash Syslog server."""
try:
# Standard Syslog format: <Priority>Timestamp Hostname Tag: Message
timestamp = datetime.now().strftime("%b %d %H:%M:%S")
hostname = socket.gethostname()
message = json.dumps(data) if isinstance(data, dict) else str(data)
syslog_msg = f"<34>{timestamp} {hostname} {source}: {message}"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(syslog_msg.encode('utf-8'), (LOGSTASH_HOST, LOGSTASH_PORT))
sock.close()
except Exception:
pass # Don't let a network blip crash your script
# --- WORKER: Tail a Command (Linux/macOS) ---
def _tail_command(self, cmd_list, source_tag):
"""
Runs a shell command (like journalctl -f) and reads stdout line-by-line.
"""
try:
# subprocess.Popen allows us to read stdout as a stream
process = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, bufsize=1)
# Read line by line as they arrive
for line in iter(process.stdout.readline, ''):
if self.stop_event.is_set():
break
if line:
self.log_queue.put((source_tag, line.strip()))
process.stdout.close()
process.wait()
except Exception as e:
print(f"[Error] Running command {cmd_list}: {e}")
# --- WORKER: Windows Event Poll (Windows Only) ---
def _poll_windows_events(self):
"""
Simulates 'tail -f' for Windows Event Logs by polling for new events.
"""
last_index = 0
try:
while not self.stop_event.is_set():
# Get events after the last one we saw
# Note: This is a simplified approach. In prod, use 'Get-WinEvent' with bookmarks.
ps_cmd = "Get-EventLog -LogName System -Newest 1 | Select-Object -Property Index, Source, Message, EntryType, InstanceId | ConvertTo-Json -Depth 1"
try:
output = subprocess.check_output(["powershell", "-Command", ps_cmd], text=True)
entry = json.loads(output)
current_index = entry.get('Index', 0)
if current_index != last_index:
last_index = current_index
self.log_queue.put(('windows_system', entry)) # It's already dict/json
except:
pass
time.sleep(1) # Poll every second
except Exception as e:
print(f"[Error] Windows polling: {e}")
def process_and_store_logs(self):
"""
Main Thread calls this to 'drain' the queue and forward to Logstash.
"""
parsed_data = []
while not self.log_queue.empty():
try:
# Use get_nowait() inside a try block to prevent crashes
source, raw_data = self.log_queue.get_nowait()
log_entry = None
# --- [START] YOUR EXISTING PARSING LOGIC ---
if source in ['linux_system', 'macos_system', 'windows_system']:
log_entry = raw_data if isinstance(raw_data, dict) else json.loads(raw_data)
elif source == 'nginx_access':
log_entry = self._parse_nginx(raw_data)
elif source == 'apache_access':
log_entry = self._parse_apache(raw_data)
elif source == 'linux_network':
log_entry = {'message': raw_data}
# --- [END] YOUR EXISTING PARSING LOGIC ---
if log_entry:
# FORWARD TO LOGSTASH (SYSLOG)
self.forward_to_syslog(source, log_entry)
parsed_data.append({'source': source, 'data': log_entry})
except queue.Empty:
break # Queue became empty unexpectedly, exit loop safely
except Exception as e:
print(f"[Error] Processing log from {source}: {e}")
continue # Skip this one log and keep the script running
# Save to local files as well
if parsed_data:
self.store_to_files(parsed_data)
return parsed_data
def _parse_nginx(self, line):
# Nginx Combined Format - Capture full request to handle payloads with spaces
regex = r'(?P<ip>[\d\.]+) - - \[(?P<date>[^\]]+)\] "(?P<request>[^"]*)" (?P<status>\d+)'
match = re.search(regex, line)
if match:
data = match.groupdict()
# Parse the request into method, path, and protocol
request_parts = data['request'].split(' ')
if len(request_parts) >= 2:
data['method'] = request_parts[0]
data['path'] = ' '.join(request_parts[1:-1]) if len(request_parts) > 2 else request_parts[1]
else:
data['method'] = request_parts[0] if request_parts else 'UNKNOWN'
data['path'] = request_parts[1] if len(request_parts) > 1 else ''
return data
return None
def _parse_apache(self, line):
# Apache Common/Combined - Capture full request to handle payloads with spaces
regex = r'(?P<ip>[\d\.]+) - - \[(?P<date>[^\]]+)\] "(?P<request>[^"]*)" (?P<status>\d+)'
match = re.search(regex, line)
if match:
data = match.groupdict()
# Parse the request into method, path, and protocol
request_parts = data['request'].split(' ')
if len(request_parts) >= 2:
data['method'] = request_parts[0]
data['path'] = ' '.join(request_parts[1:-1]) if len(request_parts) > 2 else request_parts[1]
else:
data['method'] = request_parts[0] if request_parts else 'UNKNOWN'
data['path'] = request_parts[1] if len(request_parts) > 1 else ''
return data
return None
def store_to_files(self, parsed_data):
"""Separates logs into files based on their source (e.g., sshd.json, nginx.json)."""
for item in parsed_data:
source_name = self.sanitize_filename(item['source'])
filename = os.path.join(self.log_dir, f"{source_name}.json")
content = []
if os.path.exists(filename):
try:
with open(filename, 'r') as f:
content = json.load(f)
except: content = []
# Simple append (no dedup needed for streaming usually, but good practice)
content.append(item['data'])
if len(content) > 100: content = content[-100:]
with open(filename, 'w') as f:
json.dump(content, f, indent=4)
def sanitize_filename(self, name):
"""Removes illegal characters from filenames."""
return re.sub(r'[\\/*?:"<>|]', "", str(name)).strip().replace(' ', '_')
# ==========================================
# CLASS: SORT_EVENT (CLASSIFIER)
# ==========================================
class Sort_event(Logs):
def __init__(self):
super().__init__()
self.output_file = "captured_logs/security_events.json"
self.event_counter = 1
if not os.path.exists(self.output_file):
with open(self.output_file, 'w') as f:
json.dump([], f)
else:
try:
with open(self.output_file, 'r') as f:
data = json.load(f)
self.event_counter = len(data) + 1
except: pass
def _event_fingerprint(self, event):
"""Build a stable fingerprint used to suppress duplicate suspicious events."""
attack_type = str(event.get("attack_type", "")).strip().lower()
source = str(event.get("source", "")).strip().lower()
target = str(event.get("target", "")).strip().lower()
details = str(event.get("details", "")).strip().lower()
return f"{attack_type}|{source}|{target}|{details}"
def _is_duplicate_recent_event(self, new_event, window_seconds=60):
"""
Return True if an equivalent event already exists in recent history.
This helps prevent repeated suspicious events from being persisted/displayed.
"""
try:
if not os.path.exists(self.output_file):
return False
with open(self.output_file, 'r') as f:
existing = json.load(f)
if not isinstance(existing, list) or not existing:
return False
new_fp = self._event_fingerprint(new_event)
new_ts = datetime.strptime(new_event.get("timestamp", ""), "%Y-%m-%d %H:%M:%S")
for event in reversed(existing[-200:]): # check recent tail only
if not isinstance(event, dict):
continue
if self._event_fingerprint(event) != new_fp:
continue
ts_str = event.get("timestamp")
if not ts_str:
return True
try:
old_ts = datetime.strptime(ts_str, "%Y-%m-%d %H:%M:%S")
if abs((new_ts - old_ts).total_seconds()) <= window_seconds:
return True
except Exception:
return True
except Exception:
return False
return False
def classify_severity(self, attack_type):
severity_map = {
"DDoS Attack": "Critical 🔴",
"Privilege Escalation": "Critical 🔴",
"Privilege Escalation Risk": "Critical 🔴",
"Potential Malware": "Critical 🔴",
"SQL Injection": "High 🟠",
"XSS Attack": "High 🟠",
"Brute Force": "High 🟠",
"Directory Traversal": "Medium 🟡",
"Suspicious Activity": "Low 🔵"
}
return severity_map.get(attack_type, "Low 🔵")
def send_to_logstash_alert(self, log_object):
"""
Sends the security alert to Logstash Syslog with high priority.
"""
try:
# Change priority based on severity
priority = "<32>" if "Critical" in log_object["severity_sticker"] else "<33>"
timestamp = datetime.now().strftime("%b %d %H:%M:%S")
hostname = socket.gethostname()
# Create a structured message
message = json.dumps(log_object)
syslog_msg = f"{priority}{timestamp} {hostname} SIEM_ALERT: {message}"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(syslog_msg.encode('utf-8'), ("127.0.0.1", 10514))
sock.close()
except Exception:
pass
def log_security_event(self, attack_type, source_ip, target_ip, details, remote_reporter_ip=None):
severity = self.classify_severity(attack_type)
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
entry_id = f"sim-log-entry-{self.event_counter}"
action = "BLOCKED" if "Critical" in severity or "High" in severity else "FLAGGED"
if remote_reporter_ip:
formatted_message = (
f"{entry_id}: [REMOTE REPORT FROM {remote_reporter_ip}] {attack_type} detected. Source: {source_ip}\n"
f"{entry_id}: Action: {action} (Reported by Agent)"
)
target_ip = f"{target_ip} (Reported by {remote_reporter_ip})"
else:
formatted_message = (
f"{entry_id}: {attack_type} from {source_ip} targeting {target_ip}\n"
f"{entry_id}: Action: {action}"
)
log_object = {
"id": entry_id,
"timestamp": timestamp,
"severity_sticker": severity,
"attack_type": attack_type,
"source": source_ip,
"target": target_ip,
"formatted_log": formatted_message,
"details": details
}
if self._is_duplicate_recent_event(log_object):
return log_object
self.save_to_json(log_object)
self.send_to_logstash_alert(log_object)
print(f"\n[{severity}] {entry_id} {'(REMOTE)' if remote_reporter_ip else ''}")
print(formatted_message)
self.event_counter += 1
return log_object
def save_to_json(self, log_object):
data = []
if os.path.exists(self.output_file):
try:
with open(self.output_file, 'r') as f:
data = json.load(f)
except: pass
data.append(log_object)
with open(self.output_file, 'w') as f:
json.dump(data, f, indent=4)
# Also send to PHP API if enabled
if PHP_API_ENABLED:
self.send_to_php_api(log_object)
def send_to_php_api(self, log_object):
"""
Send security event to PHP API for real-time dashboard updates
"""
try:
import urllib.request
import urllib.error
# Convert Python object to JSON payload
payload = json.dumps({
'id': log_object.get('id'),
'timestamp': log_object.get('timestamp'),
'severity_sticker': log_object.get('severity_sticker'),
'attack_type': log_object.get('attack_type'),
'source': log_object.get('source'),
'target': log_object.get('target'),
'details': log_object.get('details'),
'formatted_log': log_object.get('formatted_log')
}).encode('utf-8')
# Send POST request to PHP API
request = urllib.request.Request(
PHP_API_URL,
data=payload,
headers={'Content-Type': 'application/json'},
method='POST'
)
response = urllib.request.urlopen(request, timeout=2)
response.read()
response.close()
except Exception as e:
# Silently fail - don't block log processing if API is unavailable
pass
# ==========================================
# CLASS: CONTAINMENT (NETWORK COMMS)
# ==========================================
class Containment(Logs):
def __init__(self, sort_engine):
super().__init__()
self.sort_engine = sort_engine
self.port = SIEM_PORT
def start_listener(self):
server_thread = threading.Thread(target=self._server_loop, daemon=True)
server_thread.start()
print(f"[Containment] Central Log Server Listening on Port {self.port}...")
def _server_loop(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', self.port))
s.listen(5)
while True:
conn, addr = s.accept()
sender_ip = addr[0]
try:
data = conn.recv(8192).decode('utf-8')
if data:
event_data = json.loads(data)
self.sort_engine.log_security_event(
attack_type=event_data.get('attack_type', 'Unknown Remote'),
source_ip=event_data.get('source', 'Unknown'),
target_ip=event_data.get('target', 'Remote Agent'),
details=event_data.get('details', ''),
remote_reporter_ip=sender_ip
)
except: pass
finally: conn.close()
except Exception as e:
print(f"[Containment] Server Error: {e}")
def forward_log_to_hq(self, log_object):
if not SIEM_HQ_IP: return
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
s.connect((SIEM_HQ_IP, self.port))
s.send(json.dumps(log_object).encode('utf-8'))
s.close()
print(f" -> [Sent to HQ: {SIEM_HQ_IP}]")
except Exception as e:
print(f" -> [Failed to send to HQ: {e}]")
# ==========================================
# CLASS: PARSE_LOGS (DETECTION CORE)
# ==========================================
class Parse_logs(Logs):
def __init__(self, sort_engine, network_engine, containment_engine):
super().__init__()
self.sort_engine = sort_engine
self.network_engine = network_engine
self.containment_engine = containment_engine
self.brute_force_registry = defaultdict(deque)
self.ddos_registry = defaultdict(deque)
self.BRUTE_FORCE_LIMIT = 5
self.BRUTE_FORCE_WINDOW = 60
self.DDOS_LIMIT = 20
self.DDOS_WINDOW = 2
def analyze_logs(self, log_batch):
for log in log_batch:
source = log['source']
data = log['data']
if 'nginx' in source or 'apache' in source:
self.detect_web_attacks(data)
self.detect_ddos(data)
else:
self.detect_brute_force(data)
self.detect_privilege_escalation(data)
self.detect_malware_indicators(data)
def detect_ddos(self, data):
ip = data.get('ip')
if not ip: return
now = time.time()
self.ddos_registry[ip].append(now)
while self.ddos_registry[ip] and (now - self.ddos_registry[ip][0] > self.DDOS_WINDOW):
self.ddos_registry[ip].popleft()
if len(self.ddos_registry[ip]) > self.DDOS_LIMIT:
self.trigger_alert("DDoS Attack", ip, f"High traffic: {len(self.ddos_registry[ip])} reqs in {self.DDOS_WINDOW}s")
self.ddos_registry[ip].clear()
def detect_web_attacks(self, data):
path = data.get('path', '')
try:
from urllib.parse import unquote
path = unquote(path)
except: pass
sqli_pattern = r"(union\s+select|select\s+.*\s+from|\bOR\b\s+\d+=\d+|--|;\s*DROP)"
xss_pattern = r"(<script>|javascript:|onerror=|onload=|alert\()"
traversal_pattern = r"(\.\./|\.\.\\|/etc/passwd|c:\\windows)"
if re.search(sqli_pattern, path, re.IGNORECASE):
self.trigger_alert("SQL Injection", data.get('ip'), f"Payload found: {path}")
if re.search(xss_pattern, path, re.IGNORECASE):
self.trigger_alert("XSS Attack", data.get('ip'), f"Payload found: {path}")
if re.search(traversal_pattern, path, re.IGNORECASE):
self.trigger_alert("Directory Traversal", data.get('ip'), f"Payload found: {path}")
def detect_brute_force(self, data):
is_failure = False
identifier = "Unknown"
message = ""
# Handle JSON dicts vs Raw Strings
if isinstance(data, dict):
message = data.get('MESSAGE', str(data))
else:
message = str(data)
if self.os_type == 'Linux':
if re.search(r'(Failed password|authentication failure)', message, re.IGNORECASE):
is_failure = True
if isinstance(data, dict):
identifier = data.get('SYSLOG_IDENTIFIER', 'system')
elif self.os_type == 'Windows':
if isinstance(data, dict):
if str(data.get('InstanceId')) == '4625' or re.search(r'Audit Failure', str(data), re.IGNORECASE):
is_failure = True
identifier = "Windows User"
if is_failure:
now = time.time()
self.brute_force_registry[identifier].append(now)
while self.brute_force_registry[identifier] and (now - self.brute_force_registry[identifier][0] > self.BRUTE_FORCE_WINDOW):
self.brute_force_registry[identifier].popleft()
if len(self.brute_force_registry[identifier]) >= self.BRUTE_FORCE_LIMIT:
self.trigger_alert("Brute Force", identifier, f"{len(self.brute_force_registry[identifier])} failures detected")
self.brute_force_registry[identifier].clear()
def detect_privilege_escalation(self, data):
message = ""
if isinstance(data, dict):
message = data.get('MESSAGE', str(data))
else:
message = str(data)
if self.os_type == 'Linux':
if 'sudo' in message or ('SYSLOG_IDENTIFIER' in data and 'sudo' in data['SYSLOG_IDENTIFIER']):
if 'COMMAND=' in message:
user = re.search(r'user=([a-zA-Z0-9_-]+)', message)
cmd = re.search(r'COMMAND=(.+)', message)
user_str = user.group(1) if user else "Unknown"
cmd_str = cmd.group(1) if cmd else "Unknown"
if any(x in cmd_str for x in ['/bin/bash', '/bin/sh', 'visudo', 'shadow']):
self.trigger_alert("Privilege Escalation Risk", user_str, f"Sensitive SUDO command: {cmd_str}")
elif self.os_type == 'Windows':
if isinstance(data, dict) and str(data.get('InstanceId')) == '4732':
self.trigger_alert("Privilege Escalation", "Windows", "User added to Admin group (Event 4732)")
def detect_malware_indicators(self, data):
message = str(data)
suspicious_keywords = [
r'nc\s+.*-e\s+/bin/sh', r'bash\s+-i\s+>&', r'curl\s+.*\|\s*bash',
r'wget\s+.*\|\s*bash', r'/etc/shadow', r'trojan', r'miner'
]
for pattern in suspicious_keywords:
if re.search(pattern, message, re.IGNORECASE):
self.trigger_alert("Potential Malware", "System", f"Suspicious pattern found: {pattern}")
def trigger_alert(self, detection_type, source_ip, details):
target_ip = self.network_engine.private_ip
log_obj = self.sort_engine.log_security_event(detection_type, source_ip, target_ip, details)
if SIEM_HQ_IP:
self.containment_engine.forward_log_to_hq(log_obj)
# ==========================================
# CLASS: NETWORK (VISUALIZATION)
# ==========================================
class Network(Logs):
def __init__(self):
super().__init__()
self._private_ip = self.get_private_ip()
self._last_ip_check = time.time()
self.persist_server_status() # Initial persistence
@property
def private_ip(self):
"""Get the current private IP, refreshing if it's been more than 5 minutes since last check."""
if time.time() - self._last_ip_check > 300: # 5 minutes
self.update_private_ip()
return self._private_ip
def get_private_ip(self):
"""Fetch the most likely LAN private IP (avoid tunnel/VPN interfaces)."""
candidates = []
avoided_prefixes = ('utun', 'tun', 'tap', 'wg', 'tailscale', 'ppp', 'docker', 'veth', 'br-', 'lo')
preferred_prefixes = ('wlan', 'wl', 'eth', 'en', 'eno', 'enp', 'wlp')
def add_candidate(ip, iface=''):
try:
ip_obj = ipaddress.ip_address(ip)
except Exception:
return
if ip_obj.version != 4:
return
if not ip_obj.is_private or ip_obj.is_loopback or ip_obj.is_link_local:
return
iface_l = (iface or '').lower()
score = 0
if iface_l.startswith(preferred_prefixes):
score += 3
if any(iface_l.startswith(prefix) for prefix in avoided_prefixes):
score -= 5
# Prefer common LAN blocks over carrier-grade NAT/tunnel ranges.
if ip.startswith('192.168.') or ip.startswith('10.'):
score += 2
if ip.startswith('100.64.') or ip.startswith('100.'):
score -= 2
candidates.append((score, ip))
# Linux: reliable interface + IPv4 list
if self.os_type == 'Linux':
try:
output = subprocess.check_output(['ip', '-4', '-o', 'addr', 'show', 'up'], text=True)
for line in output.splitlines():
# Example: "2: wlan0 inet 192.168.1.10/24 ..."
parts = line.split()
if len(parts) >= 4:
iface = parts[1]
inet_cidr = parts[3]
ip = inet_cidr.split('/')[0]
add_candidate(ip, iface)
except Exception:
pass
# macOS/BSD fallback
elif self.os_type == 'macOS':
try:
output = subprocess.check_output(['ifconfig'], text=True)
current_iface = ''
for line in output.splitlines():
if line and not line.startswith('\t') and ':' in line:
current_iface = line.split(':', 1)[0].strip()
continue
m = re.search(r'inet (\d+\.\d+\.\d+\.\d+)', line)
if m:
add_candidate(m.group(1), current_iface)
except Exception:
pass
# Generic fallback
try:
host_ips = socket.gethostbyname_ex(socket.gethostname())[2]
for ip in host_ips:
add_candidate(ip)
except Exception:
pass
# Last resort from outbound route (can pick tunnel, low score mitigates this if others exist).
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
outbound_ip = s.getsockname()[0]
s.close()
add_candidate(outbound_ip, 'route')
except Exception:
pass
if candidates:
candidates.sort(key=lambda x: x[0], reverse=True)
return candidates[0][1]
return "127.0.0.1"
def update_private_ip(self):
"""Update the stored private IP address with the current value."""
old_ip = self._private_ip
self._private_ip = self.get_private_ip()
self._last_ip_check = time.time()
if old_ip != self._private_ip:
print(f"[Network] Private IP updated: {old_ip} -> {self._private_ip}")
self.persist_server_status()
return self._private_ip
def force_refresh_private_ip(self):
"""Force refresh the private IP immediately, regardless of timing."""
return self.update_private_ip()
def persist_server_status(self):
"""Persist the current server status including private IP to server_status.json"""
try:
status = {
'private_ip': self.private_ip,
'last_update': datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')
}
with open('server_status.json', 'w') as f:
json.dump(status, f, indent=4)
print(f"[Network] Server status updated: {status}")
except Exception as e:
print(f"[Network] Failed to persist server status: {e}")
def scan_network(self):
print("\n--- Network Device Scan (ARP) ---")
devices = []
try:
output = subprocess.check_output(['arp', '-a'], text=True)
for line in output.splitlines():
ip_match = re.search(r'\(?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\)?', line)
if ip_match:
devices.append(ip_match.group(1))
print(f"Device found: {ip_match.group(1)} | Raw: {line.strip()}")
except Exception as e:
print(f"Error scanning network: {e}")
print("---------------------------------")
return devices
def persist_network_scan(self, devices):
"""
Persist the latest network scan to captured_logs/network_scan.json
"""
try:
out = {
'scanned_at': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'private_ip': self.private_ip,
'devices': devices
}
path = os.path.join('captured_logs', 'network_scan.json')
with open(path, 'w') as f:
json.dump(out, f, indent=4)
except Exception as e:
print(f"[Network] Failed to persist scan: {e}")
def persist_traffic_stats(self, bytes_sent, bytes_recv):
try:
out = {
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'private_ip': self.private_ip,
'bytes_sent': bytes_sent,
'bytes_recv': bytes_recv
}
path = os.path.join('captured_logs', 'network_stats.json')
with open(path, 'w') as f:
json.dump(out, f, indent=4)
# Also append to history (trim to last 120 entries)
try:
hist_path = os.path.join('captured_logs', 'network_stats_history.json')
history = []
if os.path.exists(hist_path):
try:
with open(hist_path, 'r') as hf:
history = json.load(hf)
except: history = []
history.append(out)
if len(history) > 120:
history = history[-120:]
with open(hist_path, 'w') as hf:
json.dump(history, hf, indent=4)
except Exception:
pass
except Exception as e:
print(f"[Network] Failed to persist traffic stats: {e}")
def get_traffic_stats(self):
bytes_sent = 0
bytes_recv = 0
if self.os_type == 'Linux':
try:
with open('/proc/net/dev', 'r') as f:
lines = f.readlines()[2:]
for line in lines:
data = line.split()
if len(data) > 9:
bytes_recv += int(data[1])
bytes_sent += int(data[9])
except: pass
elif self.os_type == 'Windows':
try:
output = subprocess.check_output(['netstat', '-e'], text=True)
for line in output.splitlines():
if "Bytes" in line:
parts = line.split()
if len(parts) >= 3:
bytes_recv = int(parts[1])
bytes_sent = int(parts[2])
except: pass
return bytes_sent, bytes_recv
def check_for_containment_actions(self, enabled=False):
"""
Check for containment actions written by the web UI.
Actions should be written to captured_logs/containment_actions.json as a list of objects.
This function will mark actions as executed in containment_executed.json and optionally
attempt to apply a local block (only when enabled and running as root on Linux).
"""
try:
actions_path = os.path.join('captured_logs', 'containment_actions.json')
executed_path = os.path.join('captured_logs', 'containment_executed.json')
if not os.path.exists(actions_path):
return
try:
with open(actions_path, 'r') as f:
actions = json.load(f)
except Exception:
return
remaining = []
executed = []
if os.path.exists(executed_path):
try:
with open(executed_path, 'r') as f:
executed = json.load(f)
except: executed = []
for action in actions:
# Expected action shape: { 'ip': '1.2.3.4', 'command': 'block', 'requested_at': '...' }
ip = action.get('ip')
command = action.get('command')
action_id = action.get('id') or f"action-{int(time.time())}"
result = {'id': action_id, 'ip': ip, 'command': command, 'requested_at': action.get('requested_at'), 'executed_at': datetime.now().strftime('%Y-%m-%d %H:%M:%S'), 'status': 'skipped'}
if not ip or not command:
result['status'] = 'invalid'
executed.append(result)
continue
# Safe default: do not run destructive commands unless explicitly enabled
if enabled and self.os_type == 'Linux' and os.geteuid() == 0:
try:
if command == 'block':
# Attempt to add an iptables DROP rule for the IP
subprocess.check_call(['iptables', '-I', 'INPUT', '-s', ip, '-j', 'DROP'])
result['status'] = 'blocked'
else:
result['status'] = 'unknown-command'
except Exception as e:
result['status'] = 'failed'
result['error'] = str(e)
else:
# Log the intended action for manual review
result['status'] = 'logged'
executed.append(result)
# Write executed log and clear input actions file
try:
with open(executed_path, 'w') as f:
json.dump(executed, f, indent=4)
except Exception:
pass
# Remove containment_actions.json so actions are not reprocessed
try:
os.remove(actions_path)
except Exception:
pass
except Exception as e:
print(f"[Network] containment check failed: {e}")
def visualize_traffic(self):
if not tk:
print("[Error] Tkinter not installed. Cannot show diagram.")
return
root = tk.Tk()
root.title(f"Network Monitor - {self.private_ip}")
root.geometry("400x300")
lbl_sent = tk.Label(root, text="Upload: 0 KB/s", fg="blue", font=("Arial", 12))
lbl_sent.pack(pady=5)
lbl_recv = tk.Label(root, text="Download: 0 KB/s", fg="green", font=("Arial", 12))
lbl_recv.pack(pady=5)
canvas = tk.Canvas(root, width=300, height=150, bg="white")
canvas.pack(pady=10)
canvas.create_line(10, 140, 290, 140, width=2)
bar_up = canvas.create_rectangle(50, 140, 100, 140, fill="blue")
bar_down = canvas.create_rectangle(200, 140, 250, 140, fill="green")
canvas.create_text(75, 155, text="Upload")
canvas.create_text(225, 155, text="Download")
self.last_sent, self.last_recv = self.get_traffic_stats()
self.last_time = time.time()
def update_graph():
current_sent, current_recv = self.get_traffic_stats()
current_time = time.time()
time_diff = current_time - self.last_time
if time_diff == 0: time_diff = 1
sent_speed = (current_sent - self.last_sent) / time_diff
recv_speed = (current_recv - self.last_recv) / time_diff
self.last_sent = current_sent
self.last_recv = current_recv
self.last_time = current_time
lbl_sent.config(text=f"Upload: {sent_speed/1024:.1f} KB/s")
lbl_recv.config(text=f"Download: {recv_speed/1024:.1f} KB/s")
scale_factor = 0.001
h_up = min(130, sent_speed * scale_factor)
h_down = min(130, recv_speed * scale_factor)
canvas.coords(bar_up, 50, 140 - h_up, 100, 140)
canvas.coords(bar_down, 200, 140 - h_down, 250, 140)