-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.py
More file actions
231 lines (193 loc) · 8.3 KB
/
rules.py
File metadata and controls
231 lines (193 loc) · 8.3 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
import datetime
class DFIRRuleEngine:
def __init__(self, sessions):
self.sessions = sessions
self.results = []
def _fmt(self, t):
"""Format timestamps safely for reporting."""
if not t:
return "Unknown Time"
return str(t)
def _correlation_note(self, e):
"""Explain how an event was correlated to the session."""
c = e.get("_correlation")
if c == "in_session":
return "Correlation: in-session activity"
elif c == "grace_before":
return "Correlation: pre-session (grace window)"
elif c == "grace_after":
return "Correlation: post-session (grace window)"
return "Correlation: unknown"
def _add_finding(self, session, severity, rule_name, description):
"""Add a structured DFIR finding linked to a session."""
self.results.append({
"user": session.get("user"),
"ip": session.get("source_ip"),
"start": session.get("start_time"),
"end": session.get("end_time"),
"severity": severity,
"rule": rule_name,
"description": description
})
# Rule categories:
# * Authentication abuse
# * Session anomalies
# * Persistence
# * Privilege escalation
# * Anti-forensics
def run_rules(self):
# Collect failed authentication attempts across all sessions
global_failed = []
for s in self.sessions:
for e in s.get("events", []):
if e["event_id"] == "4625" and e.get("parsed_time"):
global_failed.append(e)
# Global brute-force indicator (cross-session behavior)
if len(global_failed) >= 2:
ips = set()
users = set()
timestamps = []
for f in global_failed:
ips.add(f["details"].get("IpAddress") or "UNKNOWN_IP")
users.add(f["details"].get("TargetUserName") or "UNKNOWN_USER")
timestamps.append(self._fmt(f.get("parsed_time") or f.get("timestamp")))
self.results.append({
"user": ", ".join(u for u in users if u != "UNKNOWN_USER") or None,
"ip": ", ".join(i for i in ips if i != "UNKNOWN_IP") or None,
"start": None,
"end": None,
"severity": "High",
"rule": "Strong Brute Force Indicator",
"description":
f"{len(global_failed)} failed RDP authentication attempts detected\n"
f"Users: {', '.join(users)}\n"
f"IPs: {', '.join(ips)}\n"
f"Timestamps:\n " + "\n ".join(timestamps)
})
# Process rules per reconstructed session
for session in self.sessions:
events = session.get("events", [])
session_seen = {
"tasks": set(),
"services": set(),
"user_created": False,
"admin_add": False,
"log_clear": False
}
# Flag unusually short or long sessions
if session.get("start_time") and session.get("end_time"):
duration = (session["end_time"] - session["start_time"]).total_seconds()
if duration < 10:
self._add_finding(
session,
"Medium",
"Very Short RDP Session",
f"Duration: {duration} seconds | "
f"Start: {self._fmt(session['start_time'])} | "
f"End: {self._fmt(session['end_time'])}"
)
if duration > 1800:
self._add_finding(
session,
"Medium",
"Very Long RDP Session",
f"Duration: {duration/60:.1f} minutes | "
f"Start: {self._fmt(session['start_time'])} | "
f"End: {self._fmt(session['end_time'])}"
)
# Detect sessions without clean termination
if session.get("end_time") is None:
self._add_finding(
session,
"Low",
"Unclosed RDP Session",
f"No clean logoff detected | "
f"Session Start: {self._fmt(session['start_time'])}"
)
# Analyze DFIR-relevant events within the session
for e in events:
eid = e["event_id"]
ts = self._fmt(e.get("parsed_time") or e.get("timestamp"))
corr = self._correlation_note(e)
# Local account creation
if eid == "4720" and not session_seen["user_created"]:
session_seen["user_created"] = True
self._add_finding(
session,
"High",
"User Account Created",
f"Local account creation detected\n"
f"Event Time: {ts}\n{corr}"
)
# Privilege escalation via group membership
elif eid == "4732" and not session_seen["admin_add"]:
session_seen["admin_add"] = True
self._add_finding(
session,
"Critical",
"User Added To Administrators",
f"Privilege escalation detected\n"
f"Event Time: {ts}\n{corr}"
)
# Scheduled task persistence
elif eid in ("4698", "129"):
task_name = (
e["details"].get("TaskName")
or e["details"].get("Task")
or e["details"].get("TaskPath")
or e["details"].get("Name")
or "UnknownTask"
)
if task_name in session_seen["tasks"]:
continue
session_seen["tasks"].add(task_name)
KNOWN_SAFE = [
"\\Microsoft\\Windows\\", "Office", "Defrag",
"Idle Maintenance", "WindowsUpdate",
"Time Synchronization", "Customer Experience",
"Servicing", "Telemetry"
]
if any(k.lower() in task_name.lower() for k in KNOWN_SAFE):
continue
self._add_finding(
session,
"High",
"Scheduled Task Persistence",
f"Task Created: {task_name}\n"
f"Event Time: {ts}\n{corr}"
)
# Service-based persistence
elif eid == "7045":
service = (
e["details"].get("ServiceName")
or e["details"].get("Service")
or "Unknown Service"
)
if service in session_seen["services"]:
continue
session_seen["services"].add(service)
binary = (
e["details"].get("ImagePath")
or e["details"].get("BinaryPath")
or "Unknown Binary"
)
self._add_finding(
session,
"Critical",
"Service Installed",
f"Service: {service}\n"
f"Binary: {binary}\n"
f"Event Time: {ts}\n{corr}"
)
# Log clearing as anti-forensics
elif eid == "1102" and not session_seen["log_clear"]:
session_seen["log_clear"] = True
self._add_finding(
session,
"Critical",
"Security Logs Cleared",
f"Log wipe detected\n"
f"Event Time: {ts}\n{corr}"
)
print(f"[+] DFIR analysis completed. Findings: {len(self.results)}")
return self.results