Repository navigation
Expand file tree
/
Copy pathwatcher.py
More file actions
1433 lines (1257 loc) · 55.2 KB
/
Copy pathwatcher.py
File metadata and controls
1433 lines (1257 loc) · 55.2 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
"""Watch Security log for reg.exe hive exports and selected firewall events.
This is a merged watcher that handles registry hive exports (reg.exe save)
and the firewall policy events (4950, 4946, 4947, 4948). Detected incidents
are serialized to JSON (using `utils.json_formatter`) and uploaded via the
MongoDB uploader.
"""
from __future__ import annotations
import ctypes
import os
import shlex
import time
import sys
from ctypes import wintypes
from datetime import datetime, timezone, timedelta
from pathlib import Path
from typing import Optional, List, Dict, Any
import pywintypes
import win32evtlog
import win32com.client
import pythoncom
import argparse
import re
from utils.xml_parser import parse_event_xml
from utils.json_formatter import (
build_registry_hive_json,
build_firewall_json,
build_priv_esc_json,
build_generic_command_json,
write_json_file,
)
from utils.firewall_parser import parse_firewall_event, format_firewall_summary
from utils.firewall_watcher import build_firewall_context
from utils.reg_parser import parse_reg_event
from utils.reg_parser import parse_reg_event
from utils.killchain import build_killchain
from utils.priv_esc_parser import parse_priv_esc_event
from uploaders.mongodb_uploader import upload_json_report
import subprocess
import hashlib
import http.client
import json
host_id = "67b3dba8bc6778101892eb77249db32e"
agent_id = "c4ca4238a0b923820dcc509a6f75849b"
# Event selection: include reg.exe 4688 events and firewall rule events
EVENT_FILTER = "Event[System[EventID=4688 or EventID=4950 or EventID=4946 or EventID=4947 or EventID=4948]]"
SECURITY_CHANNEL = "Security"
SYSMON_CHANNEL = "Microsoft-Windows-Sysmon/Operational"
POWER_SHELL_CHANNEL = "Microsoft-Windows-PowerShell/Operational"
TARGET_PROCESS = "reg.exe"
TRIGGER_KEYWORD = "save"
WATCH_INTERVAL_SECONDS = 1
FIREWALL_EVENT_IDS = {4950, 4946, 4947, 4948}
# Runtime flags
DEBUG = False
# Mapping and helpers adapted from network_attack.py
TARGET_EVENTS = {
4950: "SETTING CHANGED",
4946: "RULE ADDED",
4947: "RULE MODIFIED",
4948: "RULE DELETED",
}
def _should_upload_firewall_event(profile: Optional[str], new_state: Optional[bool]) -> bool:
"""Determine if a firewall state change should trigger an upload.
Only upload if:
1. This is a profile ON/OFF state change (not a rule modification)
2. The state is actually different from the last recorded state
3. Profile is one of the tracked profiles (Domain, Private, Public)
Returns True if upload should proceed, False otherwise.
"""
if not profile or profile not in g_firewall_state:
return False
if new_state is None:
return False
previous_state = g_firewall_state[profile]
# If we haven't seen this profile before, record the state but don't upload
if previous_state is None:
g_firewall_state[profile] = new_state
print(f"[DEBUG] Initialized firewall state for {profile}: {new_state}")
return False
# Check if state actually changed
if previous_state == new_state:
print(f"[DEBUG] Firewall state for {profile} unchanged: {new_state}")
return False
# State changed! Update and allow upload
g_firewall_state[profile] = new_state
print(f"[DEBUG] Firewall state change detected for {profile}: {previous_state} -> {new_state}")
return True
def _extract_firewall_profile_state(context: dict) -> tuple[Optional[str], Optional[bool]]:
"""Extract the profile name and enabled/disabled state from firewall event context.
Returns (profile_name, is_enabled) or (None, None) if not detectable.
"""
automated_response = context.get("automated_response", {})
profile = automated_response.get("profile")
value = automated_response.get("value")
if not profile:
return None, None
# Check if this is a profile enabled/disabled change
if value is not None:
value_lower = str(value).lower()
if "disable" in value_lower or "false" in value_lower or value_lower == "0":
return profile, False
elif "enable" in value_lower or "true" in value_lower or value_lower == "1":
return profile, True
return None, None
def _is_profile_state_event(context: dict) -> bool:
"""Check if this firewall event is about profile state (on/off), not rule changes."""
automated_response = context.get("automated_response", {})
event_type = automated_response.get("type", "")
# Only process "SETTING CHANGED" events for profile state changes
if "SETTING CHANGED" not in event_type:
return False
setting = automated_response.get("setting", "")
return "enabled" in str(setting).lower() or "state" in str(setting).lower()
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except Exception:
return False
def check_audit_policy() -> None:
try:
output = subprocess.check_output(
'auditpol /get /subcategory:"MPSSVC Rule-Level Policy Change"',
shell=True,
).decode("utf-8", errors="ignore")
if "No Auditing" in output:
print("[WARN] 'Audit MPSSVC Rule-Level Policy Change' appears disabled.")
print(
' Run as Admin to enable: auditpol /set /subcategory:"MPSSVC Rule-Level Policy Change" /success:enable /failure:enable'
)
else:
print("[INFO] Firewall audit policy appears active.")
except Exception as e:
print(f"[INFO] Could not verify audit policy automatically: {e}")
def _get_firewall_message(evt: dict) -> str:
"""Try to extract a concise human-readable message for firewall events from parsed XML."""
system = evt.get("Event", {}).get("System", {})
data = evt.get("Event", {}).get("EventData", {})
try:
eid = int(system.get("EventID"))
except Exception:
return "Unknown firewall event"
# Build a lowercase-key lookup for fuzzy matching
lower = {k.lower(): v for k, v in (data or {}).items()}
def find_by_substr(subs: list[str]) -> Optional[str]:
for s in subs:
for k, v in lower.items():
if s in k and v:
return v
return None
# 4950 typically signals a profile/setting change; look for keys containing
# 'profile', 'setting', 'value', 'type', etc.
if eid == 4950:
profile = (
find_by_substr(["profile"]) or find_by_substr(["profilechanged"]) or ""
)
# Setting field names vary: SettingType, SettingName, etc.
setting = find_by_substr(["setting", "settingtype"]) or ""
value = find_by_substr(["value", "settingvalue"]) or ""
if profile or setting or value:
return f"Profile: {profile} | Setting: {setting} | Value: {value}"
# Rule-add/modify/delete events — try to find anything with 'rule' or 'name'
if eid in (4946, 4947, 4948):
rule = find_by_substr(["rule", "name"]) or ""
if rule:
return f"Rule Name: {rule}"
# Generic fallback — include up to 3 non-empty values from EventData
if data:
vals = [v for v in data.values() if v]
if vals:
return f"Details: {' | '.join(vals[:3])}"
return "No details available"
# Mitigation Constants
PROCESS_TERMINATE = 0x0001
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
kernel32.OpenProcess.restype = wintypes.HANDLE
kernel32.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]
kernel32.TerminateProcess.restype = wintypes.BOOL
kernel32.TerminateProcess.argtypes = [wintypes.HANDLE, wintypes.UINT]
kernel32.CloseHandle.restype = wintypes.BOOL
kernel32.CloseHandle.argtypes = [wintypes.HANDLE]
_WMI_SERVICE = None
_WMI_FAILED = False
# Global state
g_subscription_handles: list = []
g_min_record_id: Optional[int] = None
# Simple cache of recent process metadata keyed by PID to capture short-lived CLI commands
g_process_cache: dict[int, dict] = {}
# Track processed event record IDs to prevent duplicate uploads
g_processed_event_ids: set = set()
# Track firewall state (profile -> on/off status) for 3 profiles: Domain, Private, Public
# Scenarios: each profile can be on or off (2^3 = 8 possible states, but we focus on transitions)
g_firewall_state: Dict[str, bool] = {
"Domain": None, # None = unknown, True = on, False = off
"Private": None,
"Public": None,
}
def _safe_evt_close(handle) -> None:
close_fn = getattr(win32evtlog, "EvtClose", None)
if not close_fn or not handle:
return
try:
close_fn(handle)
except pywintypes.error:
pass
def _utc_now_iso() -> str:
return (
datetime.now(timezone.utc).isoformat(timespec="seconds").replace("+00:00", "Z")
)
def _parse_pid(pid_hex: Optional[str]) -> Optional[int]:
if not pid_hex:
return None
try:
return int(pid_hex, 0)
except (TypeError, ValueError):
return None
def _ensure_wmi_service():
global _WMI_SERVICE, _WMI_FAILED
if _WMI_FAILED:
return None
if _WMI_SERVICE is not None:
return _WMI_SERVICE
try:
try:
pythoncom.CoInitialize()
except pythoncom.error:
pass
locator = win32com.client.Dispatch("WbemScripting.SWbemLocator")
_WMI_SERVICE = locator.ConnectServer(".", "root\\cimv2")
return _WMI_SERVICE
except pywintypes.com_error as exc:
print(f" [WARN] Unable to connect to WMI for process lookup: {exc}")
_WMI_FAILED = True
return None
def _lookup_process_metadata(pid: Optional[int]) -> dict[str, str]:
empty = {"command_line": "", "creation_time": "", "owner": ""}
if pid is None:
return empty
# Check recent-process cache first (helpful for short-lived processes like netsh)
try:
cached = g_process_cache.get(int(pid))
if cached:
# treat entries younger than 5 minutes as valid
if (time.time() - cached.get("cached_at", 0)) < 300:
return {
"command_line": cached.get("command_line", ""),
"creation_time": cached.get("creation_time", ""),
"owner": cached.get("owner", ""),
"parent_pid": cached.get("parent_pid", None),
"executable": cached.get("executable", ""),
}
except Exception:
pass
service = _ensure_wmi_service()
if not service:
return empty
try:
results = service.ExecQuery(
f"SELECT CommandLine, CreationDate, ParentProcessId, ExecutablePath FROM Win32_Process WHERE ProcessId = {pid}"
)
except pywintypes.com_error:
return empty
for proc in results:
cmd = getattr(proc, "CommandLine", None) or ""
creation_raw = getattr(proc, "CreationDate", None)
creation_iso = _wmi_datetime_to_iso(creation_raw) if creation_raw else ""
owner = ""
try:
owner_info = proc.GetOwner()
if owner_info:
username = owner_info[0]
domain = owner_info[1]
if username:
owner = f"{domain}\\{username}" if domain else username
except (pywintypes.com_error, AttributeError, TypeError):
owner = ""
parent_pid = getattr(proc, "ParentProcessId", None)
exe = getattr(proc, "ExecutablePath", None) or ""
return {
"command_line": cmd,
"creation_time": creation_iso,
"owner": owner,
"parent_pid": parent_pid,
"executable": exe,
}
return empty
def _cache_process_creation(data: dict, evt: dict) -> None:
"""Cache short-lived process creation metadata from a 4688 event.
This extracts command line, parent PID, creation time and executable
and stores a small entry in the global `g_process_cache` keyed by PID.
"""
try:
new_proc_hex = data.get("NewProcessId")
new_pid = _parse_pid(new_proc_hex)
if not new_pid:
return
cmdline = data.get("CommandLine") or ""
raw_parent = data.get("ParentProcessId") or None
try:
parent_pid_val = int(raw_parent, 0) if raw_parent not in (None, "", "-") else None
except Exception:
parent_pid_val = None
g_process_cache[int(new_pid)] = {
"command_line": cmdline,
"creation_time": (evt.get("Event", {}).get("System", {}).get("TimeCreated") or ""),
"owner": "",
"parent_pid": parent_pid_val,
"executable": (data.get("NewProcessName") or "").lower(),
"cached_at": time.time(),
}
except Exception:
# Swallow any errors to avoid impacting event processing
pass
def _resolve_parent_meta(data: dict, child_meta: dict) -> Optional[dict]:
"""Resolve parent process metadata given event data and child metadata.
Returns a metadata dict for the parent process or None if not found.
"""
parent_meta = None
try:
parent_pid_hex = data.get("ProcessId") or data.get("ParentProcessId")
parent_pid_int = _parse_pid(parent_pid_hex)
if not parent_pid_int:
parent_pid_int = child_meta.get("parent_pid")
try:
parent_pid_int = int(parent_pid_int) if parent_pid_int not in (None, "", "-") else None
except Exception:
parent_pid_int = None
if parent_pid_int:
parent_meta = _lookup_process_metadata(parent_pid_int)
except Exception:
parent_meta = None
return parent_meta
# NOTE: historical Security log polling removed by request. Only subscribed 4688 events
# populate `g_process_cache` and are used for correlation with firewall events.
def _wmi_datetime_to_iso(raw: Optional[str]) -> str:
if not raw or len(raw) < 22:
return ""
try:
base = raw[:14]
micros = raw[15:21]
offset_sign = raw[21]
offset_minutes = int(raw[22:]) if len(raw) > 22 else 0
dt = datetime.strptime(base, "%Y%m%d%H%M%S")
if micros.isdigit():
dt = dt.replace(microsecond=int(micros))
minutes = offset_minutes if offset_sign == "+" else -offset_minutes
tz = timezone(timedelta(minutes=minutes))
return (
dt.replace(tzinfo=tz)
.astimezone(timezone.utc)
.isoformat()
.replace("+00:00", "Z")
)
except Exception:
return ""
def _query_log_channel(channel: str, query: str) -> List[Dict]:
"""
Query a specific Event Log channel with an XPath query and return parsed events.
Returns list of parsed event dicts (via `parse_event_xml`).
"""
results: List[Dict] = []
try:
handle = win32evtlog.EvtQuery(
channel,
win32evtlog.EvtQueryChannelPath | win32evtlog.EvtQueryReverseDirection,
query,
)
except pywintypes.error:
return []
try:
while True:
events = win32evtlog.EvtNext(handle, 10)
if not events:
break
for event_h in events:
try:
xml = win32evtlog.EvtRender(event_h, win32evtlog.EvtRenderEventXml)
parsed = parse_event_xml(xml)
if parsed:
results.append(parsed)
except Exception:
continue
finally:
_safe_evt_close(handle)
return results
def _build_process_ancestry(
start_pid: Optional[int], max_depth: int = 5
) -> List[Dict[str, Any]]:
"""
Trace backwards through Security 4688 events to build a parent chain for `start_pid`.
Returns a list of process metadata dicts (parent-first, closest ancestor earliest),
each containing: pid (int), executable (path), command_line, creation_time, parent_pid.
"""
if not start_pid:
return []
chain: List[Dict[str, Any]] = []
trace_pid = start_pid
for depth in range(max_depth):
if not trace_pid:
break
pid_hex = hex(int(trace_pid)).lower()
# XPath: find 4688 events where NewProcessId equals our pid hex
query = f"*[System[EventID=4688]] and *[EventData[Data[@Name='NewProcessId']='{pid_hex}']]"
events = _query_log_channel(SECURITY_CHANNEL, query)
if not events:
break
evt = events[0]
evt_data = evt.get("Event", {}).get("EventData", {})
system = evt.get("Event", {}).get("System", {})
new_proc_hex = evt_data.get("NewProcessId")
parent_pid_hex = evt_data.get("ProcessId")
try:
new_pid = int(new_proc_hex, 0) if new_proc_hex else None
except Exception:
new_pid = None
try:
parent_pid = int(parent_pid_hex, 0) if parent_pid_hex else None
except Exception:
parent_pid = None
chain.append(
{
"pid": new_pid or trace_pid,
"executable": evt_data.get("NewProcessName") or "",
"command_line": evt_data.get("CommandLine") or "",
"creation_time": system.get("TimeCreated") or "",
"parent_pid": parent_pid,
}
)
trace_pid = parent_pid
# Reverse chain so parent-most appears first (optional, but consistent)
try:
chain.reverse()
except Exception:
pass
return chain
def _terminate_process(pid_hex: Optional[str], label: str) -> bool:
if not pid_hex:
return False
try:
pid = int(pid_hex, 0)
except (TypeError, ValueError):
print(f" [WARN] Invalid PID value for {label}: {pid_hex}")
return False
handle = kernel32.OpenProcess(PROCESS_TERMINATE, False, pid)
if not handle:
err = ctypes.get_last_error()
if err == 87:
print(
f" [INFO] OpenProcess indicates {label} PID {pid} already exited (error 87)"
)
return True
print(f" [WARN] OpenProcess failed for PID {pid} ({label}), error {err}")
return False
try:
if not kernel32.TerminateProcess(handle, 1):
err = ctypes.get_last_error()
print(f" ⚠️ TerminateProcess failed for PID {pid} ({label}), error {err}")
return False
return True
finally:
kernel32.CloseHandle(handle)
def _add_termination_action(
pid_hex: Optional[str], label: str, actions: list, timeline: list, event_label: Optional[str] = None
) -> None:
"""Attempt to terminate a process and record the action and timeline entries.
pid_hex: hex string like '0x141c'
label: human label used in prints and action names
event_label: optional explicit timeline event string; if None, a sensible default is used.
"""
result = _terminate_process(pid_hex, label)
action_time = _utc_now_iso()
actions.append(
{
"name": f"Terminate {label}",
"status": "Success" if result else "Failed",
"timestamp": action_time,
"details": f"PID {pid_hex}",
}
)
timeline.append(
{
"timestamp": action_time,
"event": (event_label or (f"Terminated {label}" if result else f"Failed to terminate {label}")),
"details": f"PID {pid_hex}",
}
)
print(f" [{'OK' if result else 'WARN'}] {'Terminated' if result else 'Failed to terminate'} {label} (PID {pid_hex})")
def _attempt_corrupt_output(output_path: Optional[str], actions: list, timeline: list) -> tuple[str, str]:
"""Attempt to corrupt the output path and record action/timeline entries.
Returns a tuple of (corruption_status, corruption_detail).
"""
if not output_path:
return "Skipped", "No output path detected"
success, detail = _corrupt_file(output_path)
action_time = _utc_now_iso()
actions.append(
{
"name": "Corrupt hive dump",
"status": "Success" if success else "Failed",
"timestamp": action_time,
"details": detail,
}
)
timeline.append(
{
"timestamp": action_time,
"event": ("Corrupted hive dump" if success else "Failed to corrupt hive dump"),
"details": detail,
}
)
print(f" [{'CORRUPTED' if success else 'WARN'}] Corruption attempt: {detail}")
return ("Success" if success else "Failed", detail)
def _extract_output_path(command_line: str) -> Optional[str]:
if not command_line:
return None
try:
tokens = shlex.split(command_line, posix=False)
except ValueError:
tokens = command_line.split()
if not tokens:
return None
lowered = [token.lower() for token in tokens]
try:
idx = lowered.index(TRIGGER_KEYWORD)
except ValueError:
idx = -1
if idx != -1:
if len(tokens) > idx + 2:
return tokens[idx + 2].strip('"')
if len(tokens) > idx + 1:
return tokens[idx + 1].strip('"')
return tokens[-1].strip('"') if tokens else None
def _corrupt_file(path_str: str) -> tuple[bool, str]:
"""Overwrite a file with random data to render it unusable."""
if not path_str:
return False, "No output path supplied"
if path_str.startswith("\\"):
return False, "Remote path detected; skipping corruption"
file_path = Path(path_str)
try:
if not file_path.exists():
return False, "File not found"
if file_path.is_dir():
return False, "Target is a directory"
size = file_path.stat().st_size
bytes_to_write = size if size > 0 else 1024
chunk_size = 1024 * 1024
with file_path.open("r+b") as handle:
handle.seek(0)
remaining = bytes_to_write
while remaining > 0:
this_chunk = min(chunk_size, remaining)
handle.write(os.urandom(this_chunk))
remaining -= this_chunk
if bytes_to_write < size:
handle.truncate(bytes_to_write)
return True, f"Overwrote {file_path} with {bytes_to_write} bytes of random data"
except FileNotFoundError:
return False, "File not found"
except PermissionError as exc:
return False, f"Permission denied: {exc}"
except OSError as exc:
return False, f"Corruption failed: {exc}"
# parsing logic for reg and firewall moved to utils.reg_parser and utils.firewall_parser
def _execute_mitigation_actions(context: dict) -> tuple[list[dict], list[dict]]:
reg_pid = context["reg_pid"]
parent_pid = context["parent_pid"]
output_path = context["output_path"]
raw_command = context["command_line"]
timeline: list[dict[str, str]] = [
{
"timestamp": context["process_creation_time"],
"event": "reg.exe save command detected",
"details": raw_command or "-",
}
]
actions: list[dict[str, str]] = []
# Terminate reg.exe
_add_termination_action(reg_pid, "reg.exe", actions, timeline, event_label="Terminated reg.exe")
# Terminate parent process
if parent_pid and parent_pid != reg_pid:
_add_termination_action(parent_pid, "parent process", actions, timeline, event_label="Terminated parent process")
# Corrupt output file (if present)
if output_path:
corruption_status, corruption_detail = _attempt_corrupt_output(output_path, actions, timeline)
else:
print(" [WARN] Unable to determine dump output path for corruption")
corruption_status, corruption_detail = "Skipped", "No output path detected"
context["corruption_status"] = corruption_status
context["corruption_detail"] = corruption_detail
# Build a kill_chain object using available lookup/ancestry helpers.
try:
context["kill_chain"] = build_killchain(context, _lookup_process_metadata, _build_process_ancestry, primary_detection="T1003.002")
except Exception:
context.setdefault("kill_chain", {})
return actions, timeline
def _report_incident(context: dict, event_type: str = "reg") -> None:
"""
Serialize incident `context` to JSON using the appropriate builder and upload it.
event_type: 'reg' or 'firewall'
"""
ts = datetime.now(timezone.utc).strftime("%Y%m%d%H%M%S")
json_dir = Path("json_data")
json_dir.mkdir(parents=True, exist_ok=True)
# Choose builder and filename based on event type
if event_type == "reg":
filename = f"registry_hive_attempt_{ts}.json"
payload = build_registry_hive_json(context)
elif event_type == "firewall":
record_id = context.get("event_record_id") or ts
filename = f"firewall_event_{record_id}.json"
payload = build_firewall_json(context)
elif event_type == "priv":
# Privilege escalation report
record_id = context.get("event_record_id") or ts
filename = f"priv_esc_{record_id}.json"
payload = build_priv_esc_json(context)
elif event_type == "generic":
record_id = context.get("event_record_id") or ts
filename = f"incident_{record_id}.json"
# Use generic builder; allow caller to pass desired attack type in context['attack_type']
attack_type = context.get("attack_type") or "Generic Command"
payload = build_generic_command_json(context, attack_type=attack_type)
elif event_type == "priv":
# Privilege escalation report
record_id = context.get("event_record_id") or ts
filename = f"priv_esc_{record_id}.json"
payload = build_priv_esc_json(context)
else:
# Fallback to generic firewall builder if unknown
record_id = context.get("event_record_id") or ts
filename = f"incident_{record_id}.json"
payload = build_firewall_json(context)
json_path = json_dir / filename
# Build agent_data block and prepend it to the payload so it appears
# before `incident_summary` in the written JSON.
# Use a randomly generated MD5 for event_id (unique per uploaded event).
try:
rnd = os.urandom(16)
except Exception:
# fallback to time-based randomness
rnd = f"{ts}-{time.time()}".encode("utf-8")
md5sum = hashlib.md5(rnd).hexdigest()
agent_data = {
"event_id": md5sum,
"host_id": host_id,
"agent_id": agent_id,
}
# Prepend agent_data and kill_chain to the payload (preserve insertion order)
kc = context.get("kill_chain") or {}
final_payload = {"agent_data": agent_data, "kill_chain": kc, **payload}
# Extract command from incident_summary (or from context/process details) and ask classifier endpoint
try:
incident_summary = final_payload.get("incident_summary", {})
cmd_to_send = ""
if isinstance(incident_summary, dict):
# try common keys
cmd_to_send = incident_summary.get("Command") or incident_summary.get("Command Line") or ""
# If no command in summary, try to extract from context (firewall events often don't include 'Command')
if not cmd_to_send:
# direct context keys
cmd_to_send = context.get("command_line") or context.get("command") or ""
if not cmd_to_send:
# firewall initiator block
fw = context.get("firewall") or {}
if isinstance(fw, dict):
initiator = fw.get("initiator")
if isinstance(initiator, dict):
cmd_to_send = initiator.get("command_line") or initiator.get("command") or ""
if not cmd_to_send:
# inspect processes list from context
procs = context.get("processes") or []
if isinstance(procs, list):
for p in procs:
if not isinstance(p, dict):
continue
cmd = p.get("command") or p.get("Command Line") or p.get("command_line")
if cmd:
cmd_to_send = cmd
break
if not cmd_to_send:
# inspect final_payload process_details (could be dict or list)
pd = final_payload.get("process_details") or []
if isinstance(pd, dict):
for role, rec in pd.items():
if not isinstance(rec, dict):
continue
cmd = rec.get("command_line") or rec.get("Command Line") or rec.get("command")
if cmd:
cmd_to_send = cmd
break
elif isinstance(pd, list):
for rec in pd:
if not isinstance(rec, dict):
continue
cmd = rec.get("Command Line") or rec.get("Command") or rec.get("command_line")
if cmd:
cmd_to_send = cmd
break
if cmd_to_send:
try:
conn = http.client.HTTPConnection("138.197.166.142", 80, timeout=5)
# Send the command as the raw request body (plain text)
conn.request("POST", "/predict", body=str(cmd_to_send), headers={"Content-Type": "text/plain"})
resp = conn.getresponse()
resp_body = resp.read().decode("utf-8", errors="ignore")
try:
classifier_json = json.loads(resp_body)
# Response expected like: {"confidence score":0.999,"label":"Benign"}
label = classifier_json.get("label") or classifier_json.get("Label") or ""
confidence = (
classifier_json.get("confidence score")
if "confidence score" in classifier_json
else classifier_json.get("confidence")
)
# coerce confidence to a number when possible
try:
confidence = float(confidence)
except Exception:
# keep as-is (likely 0) if coercion fails
pass
# Inject results into the incident summary mapping
if isinstance(final_payload.get("incident_summary"), dict):
final_payload["incident_summary"]["MITRE ATT&CK"] = label
final_payload["incident_summary"]["Confidence"] = confidence
except Exception:
print(" [WARN] Failed to parse classifier response")
except Exception as exc:
print(f" [WARN] Failed to contact classifier endpoint: {exc}")
finally:
try:
conn.close()
except Exception:
pass
except Exception:
# Ensure that any errors here do not prevent the report from being written/uploaded
pass
write_json_file(final_payload, json_path)
print(f" [INFO] JSON report generated: {json_path}")
# If running in debug mode, skip the upload step so the user can inspect
# generated JSON files without attempting to contact MongoDB.
try:
if DEBUG:
print(" [DEBUG] Debug mode enabled - skipping upload")
return
except Exception:
# If DEBUG isn't available for any reason, fall back to uploading.
pass
uploaded_id = upload_json_report(str(json_path))
if uploaded_id:
print(" [OK] JSON report uploaded to MongoDB (id=", uploaded_id, ")")
else:
print(" [WARN] JSON report upload failed")
def _handle_firewall_event(evt: dict) -> None:
# Delegate building the firewall incident context to the dedicated parser
try:
context = build_firewall_context(
evt, _lookup_process_metadata, g_process_cache, _build_process_ancestry
)
except Exception as exc:
print(f"[ERROR] Failed to build firewall context: {exc}")
return
# Debug: show that we're about to report a firewall incident and a summary
try:
sys = evt.get("Event", {}).get("System", {})
print(f"[DEBUG] Handling firewall event id={sys.get('EventID')} record={sys.get('EventRecordID')}")
except Exception:
pass
# Check if this is a profile state change event and enforce state-change policy
if _is_profile_state_event(context):
profile, new_state = _extract_firewall_profile_state(context)
if profile and new_state is not None:
if not _should_upload_firewall_event(profile, new_state):
print(f"[DEBUG] Firewall state event for {profile} does not meet upload criteria; skipping upload")
return
# Persist JSON and upload via centralized reporter
_report_incident(context, event_type="firewall")
def _handle_event_callback(action, _context, event) -> None:
if action == win32evtlog.EvtSubscribeActionError:
print("[WARN] Subscription error while receiving events")
return
if action != win32evtlog.EvtSubscribeActionDeliver:
return
try:
xml = win32evtlog.EvtRender(event, win32evtlog.EvtRenderEventXml)
except pywintypes.error as exc:
print("[ERROR] Failed to render event:", exc)
return
# Parse XML just to get EventRecordID for deduplication
try:
evt_temp = parse_event_xml(xml)
system_temp = evt_temp.get("Event", {}).get("System", {})
record_id = system_temp.get("EventRecordID")
if record_id:
if record_id in g_processed_event_ids:
print(f"[DEBUG] Skipping duplicate event (EventRecordID={record_id})")
return
g_processed_event_ids.add(record_id)
except Exception:
pass
# Defensive parsing: catch parsing errors and log the parsed System/EventData
try:
evt = parse_event_xml(xml)
except Exception as exc:
# Log a short excerpt of the XML to help debugging
try:
snippet = (xml[:200] + ""
"...") if xml and len(xml) > 200 else xml
except Exception:
snippet = "<unprintable xml>"
print(f"[ERROR] parse_event_xml failed: {exc} -- xml snippet: {snippet}")
return
if not evt:
print("[WARN] parse_event_xml returned no event")
return
system = evt.get("Event", {}).get("System", {})
eventdata = evt.get("Event", {}).get("EventData", {})
event_id = None
try:
event_id = int(system.get("EventID"))
except Exception:
# Try to extract from EventData if missing
pass
channel = system.get("Channel") or ""
is_firewall_event = event_id in FIREWALL_EVENT_IDS or (
isinstance(channel, str) and "firewall" in channel.lower()
)
# Only print debug info for firewall events
if is_firewall_event:
try:
print(f"[DEBUG] Parsed System fields: EventID={system.get('EventID')} EventRecordID={system.get('EventRecordID')} Channel={system.get('Channel')}")
# Also show a short summary of EventData for troubleshooting
if eventdata:
sample = ", ".join([f"{k}={v}" for k, v in list(eventdata.items())[:4]])
print(f"[DEBUG] EventData sample: {sample}")
except Exception:
pass
# Delegate to specialized processors
if event_id == 4688:
print(f"[DEBUG] Calling _process_4688_event for EventID 4688")
_process_4688_event(evt)
return
# Check for "reg save" detection across ALL event types and channels
# This catches reg.exe from any parent process (cmd, PowerShell, etc.)
try:
eventdata_vals = []
if isinstance(eventdata, dict):
for v in eventdata.values():
if v:
eventdata_vals.append(str(v))
combined = " ".join(eventdata_vals).lower()
# reg save - detect "reg save" or "reg.exe save" anywhere in eventdata
if re.search(r'\breg(?:\.exe)?\s+save\b', combined, re.IGNORECASE):
print(f"[DEBUG] Found 'reg save' in non-4688 event - combined text: {combined[:300]}")
detection_time = system.get("TimeCreated") or _utc_now_iso()
proc_id = None
try:
proc_id = int(system.get("ProcessID")) if system.get("ProcessID") else None
except Exception:
proc_id = None
child_meta = _lookup_process_metadata(proc_id) if proc_id else {}
# Extract output path - look for arguments after "reg save"
path_match = re.search(r'\breg(?:\.exe)?\s+save\s+([A-Za-z0-9\\:\\_\\.-/]+\s+)?([A-Za-z0-9\\:\\_\\.-/:\\"]+)', combined, re.IGNORECASE)
out_path = path_match.group(2) if path_match else None
m = re.search(r'\breg(?:\.exe)?\s+save\b.*', combined, re.IGNORECASE)