trproof 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- trproof/__init__.py +1 -0
- trproof/cli.py +342 -0
- trproof/collectors/__init__.py +30 -0
- trproof/collectors/ad.py +873 -0
- trproof/collectors/entra.py +762 -0
- trproof/graph/__init__.py +1 -0
- trproof/graph/edges.py +219 -0
- trproof/keys/default_ed25519_priv.pem +3 -0
- trproof/keys/default_ed25519_pub.pem +3 -0
- trproof/models.py +76 -0
- trproof/report/__init__.py +1 -0
- trproof/report/html.py +222 -0
- trproof/report/signing.py +121 -0
- trproof/scoring/__init__.py +1 -0
- trproof/scoring/engine.py +50 -0
- trproof-0.1.0.dist-info/METADATA +165 -0
- trproof-0.1.0.dist-info/RECORD +21 -0
- trproof-0.1.0.dist-info/WHEEL +5 -0
- trproof-0.1.0.dist-info/entry_points.txt +3 -0
- trproof-0.1.0.dist-info/licenses/LICENSE +21 -0
- trproof-0.1.0.dist-info/top_level.txt +1 -0
trproof/collectors/ad.py
ADDED
|
@@ -0,0 +1,873 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import datetime
|
|
4
|
+
from typing import Any, Callable, List, Optional
|
|
5
|
+
|
|
6
|
+
from ldap3 import ALL, Connection, Server, SUBTREE
|
|
7
|
+
|
|
8
|
+
from trproof.models import (
|
|
9
|
+
CollectionWarning,
|
|
10
|
+
Finding,
|
|
11
|
+
IdentityEdge,
|
|
12
|
+
PrincipalNode,
|
|
13
|
+
ScanBundle,
|
|
14
|
+
Severity,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# ---------------------------------------------------------------------------
|
|
19
|
+
# LDAP attribute helpers
|
|
20
|
+
# ---------------------------------------------------------------------------
|
|
21
|
+
|
|
22
|
+
def _raw(entry: Any, attr: str) -> Any:
|
|
23
|
+
obj = getattr(entry, attr, None)
|
|
24
|
+
if obj is None and hasattr(entry, "__getitem__"):
|
|
25
|
+
try:
|
|
26
|
+
obj = entry[attr]
|
|
27
|
+
except Exception:
|
|
28
|
+
obj = None
|
|
29
|
+
if obj is None:
|
|
30
|
+
return None
|
|
31
|
+
if hasattr(obj, "value"):
|
|
32
|
+
try:
|
|
33
|
+
obj = obj.value
|
|
34
|
+
except Exception:
|
|
35
|
+
pass
|
|
36
|
+
if isinstance(obj, (list, tuple)):
|
|
37
|
+
return list(obj)
|
|
38
|
+
return obj
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _str(entry: Any, attr: str) -> Optional[str]:
|
|
42
|
+
value = _raw(entry, attr)
|
|
43
|
+
if value is None:
|
|
44
|
+
return None
|
|
45
|
+
if isinstance(value, (list, tuple)):
|
|
46
|
+
value = value[0] if value else None
|
|
47
|
+
if isinstance(value, (bytes, bytearray)):
|
|
48
|
+
try:
|
|
49
|
+
return value.decode("utf-8", "ignore")
|
|
50
|
+
except Exception:
|
|
51
|
+
return None
|
|
52
|
+
return value if isinstance(value, str) else None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _int(entry: Any, attr: str) -> Optional[int]:
|
|
56
|
+
value = _raw(entry, attr)
|
|
57
|
+
if value is None:
|
|
58
|
+
return None
|
|
59
|
+
if isinstance(value, (list, tuple)):
|
|
60
|
+
value = value[0] if value else None
|
|
61
|
+
if isinstance(value, bool):
|
|
62
|
+
return int(value)
|
|
63
|
+
if isinstance(value, int):
|
|
64
|
+
return value
|
|
65
|
+
if isinstance(value, str):
|
|
66
|
+
try:
|
|
67
|
+
return int(value)
|
|
68
|
+
except ValueError:
|
|
69
|
+
return None
|
|
70
|
+
return None
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _list_str(entry: Any, attr: str) -> List[str]:
|
|
74
|
+
value = _raw(entry, attr)
|
|
75
|
+
if value is None:
|
|
76
|
+
return []
|
|
77
|
+
if not isinstance(value, (list, tuple)):
|
|
78
|
+
value = [value]
|
|
79
|
+
result: List[str] = []
|
|
80
|
+
for item in value:
|
|
81
|
+
if isinstance(item, (bytes, bytearray)):
|
|
82
|
+
try:
|
|
83
|
+
result.append(item.decode("utf-8", "ignore"))
|
|
84
|
+
except Exception:
|
|
85
|
+
pass
|
|
86
|
+
elif isinstance(item, str):
|
|
87
|
+
result.append(item)
|
|
88
|
+
return result
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _dn(entry: Any) -> str:
|
|
92
|
+
for attr in ("distinguishedName", "distinguished_name"):
|
|
93
|
+
value = _str(entry, attr)
|
|
94
|
+
if value:
|
|
95
|
+
return value
|
|
96
|
+
entry_dn = getattr(entry, "entry_dn", None)
|
|
97
|
+
if isinstance(entry_dn, str) and entry_dn:
|
|
98
|
+
return entry_dn
|
|
99
|
+
return str(entry)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _account_name(entry: Any) -> str:
|
|
103
|
+
for attr in ("sAMAccountName", "sam_account_name", "cn", "name"):
|
|
104
|
+
value = _str(entry, attr)
|
|
105
|
+
if value:
|
|
106
|
+
return value
|
|
107
|
+
dn = _dn(entry)
|
|
108
|
+
first = dn.split(",", 1)[0]
|
|
109
|
+
if first.lower().startswith("cn="):
|
|
110
|
+
return first[3:]
|
|
111
|
+
return dn
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _uac(entry: Any) -> int:
|
|
115
|
+
return _int(entry, "userAccountControl") or 0
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _entries(conn: Any) -> List[Any]:
|
|
119
|
+
entries = getattr(conn, "entries", None)
|
|
120
|
+
if isinstance(entries, (list, tuple)):
|
|
121
|
+
return list(entries)
|
|
122
|
+
return []
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _name_from_member_dn(member_dn: str) -> str:
|
|
126
|
+
first = member_dn.split(",", 1)[0]
|
|
127
|
+
if first.lower().startswith("cn="):
|
|
128
|
+
return first[3:]
|
|
129
|
+
return member_dn
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
# ---------------------------------------------------------------------------
|
|
133
|
+
# Collector
|
|
134
|
+
# ---------------------------------------------------------------------------
|
|
135
|
+
|
|
136
|
+
class ADCollector:
|
|
137
|
+
"""Read-only Active Directory collector (Phase 2)."""
|
|
138
|
+
|
|
139
|
+
UAC_ACCOUNTDISABLE = 0x0002
|
|
140
|
+
UAC_DONT_EXPIRE_PASSWORD = 0x10000
|
|
141
|
+
UAC_TRUSTED_FOR_DELEGATION = 0x80000
|
|
142
|
+
UAC_TRUSTED_TO_AUTH_FOR_DELEGATION = 0x100000
|
|
143
|
+
UAC_DONT_REQ_PREAUTH = 0x400000
|
|
144
|
+
UAC_SERVER_TRUST_ACCOUNT = 0x2000
|
|
145
|
+
|
|
146
|
+
CHECK_LABELS = {
|
|
147
|
+
"_check_tier0_admins": "Tier-0 administrators (_check_tier0_admins)",
|
|
148
|
+
"_check_stale_or_nonexpiring_admincount": "Stale/non-expiring adminCount accounts (_check_stale_or_nonexpiring_admincount)",
|
|
149
|
+
"_check_kerberoastable_accounts": "Kerberoastable accounts (_check_kerberoastable_accounts)",
|
|
150
|
+
"_check_asrep_roastable_accounts": "AS-REP roastable accounts (_check_asrep_roastable_accounts)",
|
|
151
|
+
"_check_kerberos_delegation": "Kerberos delegation (_check_kerberos_delegation)",
|
|
152
|
+
"_check_local_admin_sprawl_proxies": "Local admin sprawl proxies (_check_local_admin_sprawl_proxies)",
|
|
153
|
+
"_check_password_and_compat_hygiene": "Password and compatibility hygiene (_check_password_and_compat_hygiene)",
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
def __init__(
|
|
157
|
+
self,
|
|
158
|
+
server: str,
|
|
159
|
+
username: str,
|
|
160
|
+
password: str,
|
|
161
|
+
port: int = 636,
|
|
162
|
+
use_ssl: bool = True,
|
|
163
|
+
base_dn: Optional[str] = None,
|
|
164
|
+
timeout: int = 10,
|
|
165
|
+
) -> None:
|
|
166
|
+
self.server = server
|
|
167
|
+
self.username = username
|
|
168
|
+
self.password = password
|
|
169
|
+
self.port = port
|
|
170
|
+
self.use_ssl = use_ssl
|
|
171
|
+
self.base_dn = base_dn
|
|
172
|
+
self.timeout = timeout
|
|
173
|
+
|
|
174
|
+
self.conn: Optional[Any] = None
|
|
175
|
+
self.nodes: List[PrincipalNode] = []
|
|
176
|
+
self.edges: List[IdentityEdge] = []
|
|
177
|
+
self.findings: List[Finding] = []
|
|
178
|
+
self.warnings: List[CollectionWarning] = []
|
|
179
|
+
|
|
180
|
+
self._tier0_users: set = set()
|
|
181
|
+
self._direct_da_count: int = 0
|
|
182
|
+
|
|
183
|
+
# ---------------------------------------------------------------- connect
|
|
184
|
+
|
|
185
|
+
def connect(self) -> Any:
|
|
186
|
+
try:
|
|
187
|
+
server = Server(
|
|
188
|
+
self.server,
|
|
189
|
+
port=self.port,
|
|
190
|
+
use_ssl=self.use_ssl,
|
|
191
|
+
get_info=ALL,
|
|
192
|
+
connect_timeout=self.timeout,
|
|
193
|
+
)
|
|
194
|
+
conn = Connection(
|
|
195
|
+
server,
|
|
196
|
+
user=self.username,
|
|
197
|
+
password=self.password,
|
|
198
|
+
auto_bind=True,
|
|
199
|
+
read_only=True,
|
|
200
|
+
receive_timeout=self.timeout,
|
|
201
|
+
)
|
|
202
|
+
except Exception as exc:
|
|
203
|
+
raise ConnectionError(
|
|
204
|
+
f"Failed to connect/bind to LDAP server {self.server}:{self.port}: {exc}"
|
|
205
|
+
) from exc
|
|
206
|
+
|
|
207
|
+
if self.base_dn is None:
|
|
208
|
+
dnc = None
|
|
209
|
+
try:
|
|
210
|
+
server_info = getattr(conn, "server", None)
|
|
211
|
+
info = getattr(server_info, "info", None)
|
|
212
|
+
other = getattr(info, "other", None)
|
|
213
|
+
if isinstance(other, dict):
|
|
214
|
+
dnc = other.get("defaultNamingContext")
|
|
215
|
+
if not dnc:
|
|
216
|
+
naming_contexts = getattr(info, "naming_contexts", None)
|
|
217
|
+
if isinstance(naming_contexts, (list, tuple)) and naming_contexts:
|
|
218
|
+
dnc = naming_contexts[0]
|
|
219
|
+
if isinstance(dnc, (list, tuple)):
|
|
220
|
+
dnc = dnc[0] if dnc else None
|
|
221
|
+
except Exception:
|
|
222
|
+
dnc = None
|
|
223
|
+
|
|
224
|
+
if not dnc:
|
|
225
|
+
try:
|
|
226
|
+
conn.search(
|
|
227
|
+
search_base="",
|
|
228
|
+
search_filter="(objectClass=*)",
|
|
229
|
+
search_scope=0, # BASE
|
|
230
|
+
attributes=["defaultNamingContext"],
|
|
231
|
+
)
|
|
232
|
+
for entry in _entries(conn):
|
|
233
|
+
dnc = _str(entry, "defaultNamingContext")
|
|
234
|
+
if dnc:
|
|
235
|
+
break
|
|
236
|
+
except Exception:
|
|
237
|
+
dnc = None
|
|
238
|
+
|
|
239
|
+
if not dnc:
|
|
240
|
+
try:
|
|
241
|
+
conn.unbind()
|
|
242
|
+
except Exception:
|
|
243
|
+
pass
|
|
244
|
+
raise ConnectionError(
|
|
245
|
+
f"Could not discover base DN from RootDSE on {self.server}:{self.port}."
|
|
246
|
+
)
|
|
247
|
+
self.base_dn = dnc
|
|
248
|
+
|
|
249
|
+
self.conn = conn
|
|
250
|
+
return conn
|
|
251
|
+
|
|
252
|
+
# ---------------------------------------------------------------- helpers
|
|
253
|
+
|
|
254
|
+
def _search(
|
|
255
|
+
self,
|
|
256
|
+
conn: Any,
|
|
257
|
+
search_filter: str,
|
|
258
|
+
attributes: List[str],
|
|
259
|
+
search_base: Optional[str] = None,
|
|
260
|
+
) -> List[Any]:
|
|
261
|
+
base = search_base if search_base is not None else self.base_dn
|
|
262
|
+
conn.search(
|
|
263
|
+
search_base=base,
|
|
264
|
+
search_filter=search_filter,
|
|
265
|
+
search_scope=SUBTREE,
|
|
266
|
+
attributes=attributes,
|
|
267
|
+
)
|
|
268
|
+
return _entries(conn)
|
|
269
|
+
|
|
270
|
+
def _add_node(self, node: PrincipalNode) -> None:
|
|
271
|
+
if not any(existing.id == node.id for existing in self.nodes):
|
|
272
|
+
self.nodes.append(node)
|
|
273
|
+
|
|
274
|
+
def _add_edge(self, edge: IdentityEdge) -> None:
|
|
275
|
+
duplicate = any(
|
|
276
|
+
existing.source_id == edge.source_id
|
|
277
|
+
and existing.target_id == edge.target_id
|
|
278
|
+
and existing.relation == edge.relation
|
|
279
|
+
for existing in self.edges
|
|
280
|
+
)
|
|
281
|
+
if not duplicate:
|
|
282
|
+
self.edges.append(edge)
|
|
283
|
+
|
|
284
|
+
# ---------------------------------------------------------------- collect
|
|
285
|
+
|
|
286
|
+
def collect(self, progress_callback: Callable[[str], None] | None = None) -> ScanBundle:
|
|
287
|
+
conn = None
|
|
288
|
+
checks = [
|
|
289
|
+
(self.CHECK_LABELS["_check_tier0_admins"], self._check_tier0_admins),
|
|
290
|
+
(self.CHECK_LABELS["_check_stale_or_nonexpiring_admincount"], self._check_stale_or_nonexpiring_admincount),
|
|
291
|
+
(self.CHECK_LABELS["_check_kerberoastable_accounts"], self._check_kerberoastable_accounts),
|
|
292
|
+
(self.CHECK_LABELS["_check_asrep_roastable_accounts"], self._check_asrep_roastable_accounts),
|
|
293
|
+
(self.CHECK_LABELS["_check_kerberos_delegation"], self._check_kerberos_delegation),
|
|
294
|
+
(self.CHECK_LABELS["_check_local_admin_sprawl_proxies"], self._check_local_admin_sprawl_proxies),
|
|
295
|
+
(self.CHECK_LABELS["_check_password_and_compat_hygiene"], self._check_password_and_compat_hygiene),
|
|
296
|
+
]
|
|
297
|
+
try:
|
|
298
|
+
conn = self.connect()
|
|
299
|
+
for label, check in checks:
|
|
300
|
+
if progress_callback is not None:
|
|
301
|
+
progress_callback(label)
|
|
302
|
+
try:
|
|
303
|
+
check(conn)
|
|
304
|
+
except Exception as exc: # never crash on a single failed check
|
|
305
|
+
self.warnings.append(
|
|
306
|
+
CollectionWarning(check_name=label, source="ad", reason=str(exc))
|
|
307
|
+
)
|
|
308
|
+
finally:
|
|
309
|
+
if conn is not None:
|
|
310
|
+
try:
|
|
311
|
+
conn.unbind()
|
|
312
|
+
except Exception:
|
|
313
|
+
pass
|
|
314
|
+
self.conn = None
|
|
315
|
+
|
|
316
|
+
return ScanBundle(
|
|
317
|
+
nodes=self.nodes,
|
|
318
|
+
edges=self.edges,
|
|
319
|
+
findings=self.findings,
|
|
320
|
+
warnings=self.warnings,
|
|
321
|
+
metadata={
|
|
322
|
+
"source": "ad",
|
|
323
|
+
"server": self.server,
|
|
324
|
+
"base_dn": self.base_dn,
|
|
325
|
+
},
|
|
326
|
+
)
|
|
327
|
+
|
|
328
|
+
# ---------------------------------------------------------------- checks
|
|
329
|
+
|
|
330
|
+
def _check_tier0_admins(self, conn: Any) -> None:
|
|
331
|
+
group_names = ["Domain Admins", "Enterprise Admins", "Administrators"]
|
|
332
|
+
group_dns: dict = {}
|
|
333
|
+
for gname in group_names:
|
|
334
|
+
entries = self._search(conn, f"(&(objectClass=group)(cn={gname}))", ["distinguishedName"])
|
|
335
|
+
for entry in entries:
|
|
336
|
+
dn = _dn(entry)
|
|
337
|
+
if dn and gname not in group_dns:
|
|
338
|
+
group_dns[gname] = dn
|
|
339
|
+
|
|
340
|
+
direct_by_group: dict = {g: set() for g in group_names}
|
|
341
|
+
recursive_by_group: dict = {g: set() for g in group_names}
|
|
342
|
+
|
|
343
|
+
for gname in group_names:
|
|
344
|
+
group_dn = group_dns.get(gname)
|
|
345
|
+
if not group_dn:
|
|
346
|
+
continue
|
|
347
|
+
entries = self._search(conn, f"(distinguishedName={group_dn})", ["member"])
|
|
348
|
+
for entry in entries:
|
|
349
|
+
for member in _list_str(entry, "member"):
|
|
350
|
+
direct_by_group[gname].add(member)
|
|
351
|
+
|
|
352
|
+
entries = self._search(
|
|
353
|
+
conn,
|
|
354
|
+
f"(&(objectCategory=person)(objectClass=user)(memberOf:1.2.840.113556.1.4.1941:={group_dn}))",
|
|
355
|
+
["sAMAccountName", "distinguishedName", "cn"],
|
|
356
|
+
)
|
|
357
|
+
for entry in entries:
|
|
358
|
+
recursive_by_group[gname].add(_account_name(entry))
|
|
359
|
+
|
|
360
|
+
all_tier0: set = set()
|
|
361
|
+
for gname in group_names:
|
|
362
|
+
all_tier0 |= recursive_by_group[gname]
|
|
363
|
+
self._tier0_users = all_tier0
|
|
364
|
+
|
|
365
|
+
for gname in group_names:
|
|
366
|
+
for member_dn in direct_by_group[gname]:
|
|
367
|
+
member_name = _name_from_member_dn(member_dn)
|
|
368
|
+
node = PrincipalNode(
|
|
369
|
+
id=member_name,
|
|
370
|
+
name=member_name,
|
|
371
|
+
principal_type="user",
|
|
372
|
+
source="ad",
|
|
373
|
+
is_privileged=True,
|
|
374
|
+
metadata={"tier0_group": gname, "direct_member": True},
|
|
375
|
+
)
|
|
376
|
+
self._add_node(node)
|
|
377
|
+
self._add_edge(
|
|
378
|
+
IdentityEdge(
|
|
379
|
+
source_id=member_name,
|
|
380
|
+
target_id=gname,
|
|
381
|
+
relation="member_of",
|
|
382
|
+
description=f"{member_name} is a direct member of {gname}.",
|
|
383
|
+
)
|
|
384
|
+
)
|
|
385
|
+
|
|
386
|
+
direct_da_count = len(direct_by_group.get("Domain Admins", set()))
|
|
387
|
+
self._direct_da_count = direct_da_count
|
|
388
|
+
total_tier0 = len(all_tier0)
|
|
389
|
+
|
|
390
|
+
if direct_da_count > 5 or total_tier0 > 10:
|
|
391
|
+
severity = Severity.HIGH
|
|
392
|
+
elif total_tier0 > 5:
|
|
393
|
+
severity = Severity.MEDIUM
|
|
394
|
+
else:
|
|
395
|
+
severity = Severity.LOW
|
|
396
|
+
|
|
397
|
+
self.findings.append(
|
|
398
|
+
Finding(
|
|
399
|
+
id="AD-001",
|
|
400
|
+
title="Excessive Tier-0 (Domain/Enterprise/Builtin Admins) membership",
|
|
401
|
+
severity=severity,
|
|
402
|
+
source="ad",
|
|
403
|
+
affected_count=total_tier0,
|
|
404
|
+
affected_entities=sorted(all_tier0),
|
|
405
|
+
why_it_matters=(
|
|
406
|
+
"Too many accounts can perform full-domain administration, so one stolen "
|
|
407
|
+
"password can compromise the whole business network."
|
|
408
|
+
),
|
|
409
|
+
how_to_fix=(
|
|
410
|
+
"Keep only a few named admin accounts in Domain, Enterprise, and Builtin "
|
|
411
|
+
"Administrators and remove nested group memberships."
|
|
412
|
+
),
|
|
413
|
+
)
|
|
414
|
+
)
|
|
415
|
+
|
|
416
|
+
def _check_stale_or_nonexpiring_admincount(self, conn: Any) -> None:
|
|
417
|
+
entries = self._search(
|
|
418
|
+
conn,
|
|
419
|
+
"(adminCount=1)",
|
|
420
|
+
["sAMAccountName", "lastLogonTimestamp", "userAccountControl", "distinguishedName", "objectClass"],
|
|
421
|
+
)
|
|
422
|
+
|
|
423
|
+
flagged: List[str] = []
|
|
424
|
+
now_epoch = datetime.datetime.now(datetime.timezone.utc).timestamp()
|
|
425
|
+
cutoff = now_epoch - 90 * 86400
|
|
426
|
+
|
|
427
|
+
for entry in entries:
|
|
428
|
+
object_classes = _list_str(entry, "objectClass")
|
|
429
|
+
if object_classes and not any("user" in oc.lower() for oc in object_classes):
|
|
430
|
+
if not _str(entry, "sAMAccountName"):
|
|
431
|
+
continue
|
|
432
|
+
name = _account_name(entry)
|
|
433
|
+
uac = _uac(entry)
|
|
434
|
+
dont_expire = bool(uac & self.UAC_DONT_EXPIRE_PASSWORD)
|
|
435
|
+
last_logon_raw = _int(entry, "lastLogonTimestamp")
|
|
436
|
+
stale = False
|
|
437
|
+
if last_logon_raw:
|
|
438
|
+
try:
|
|
439
|
+
epoch = (last_logon_raw - 116444736000000000) / 10000000
|
|
440
|
+
stale = 0 < epoch < cutoff
|
|
441
|
+
except Exception:
|
|
442
|
+
stale = False
|
|
443
|
+
if stale or dont_expire:
|
|
444
|
+
flagged.append(name)
|
|
445
|
+
self._add_node(
|
|
446
|
+
PrincipalNode(
|
|
447
|
+
id=name,
|
|
448
|
+
name=name,
|
|
449
|
+
principal_type="user",
|
|
450
|
+
source="ad",
|
|
451
|
+
is_privileged=True,
|
|
452
|
+
metadata={
|
|
453
|
+
"admin_count": 1,
|
|
454
|
+
"stale_logon": stale,
|
|
455
|
+
"password_never_expires": dont_expire,
|
|
456
|
+
},
|
|
457
|
+
)
|
|
458
|
+
)
|
|
459
|
+
|
|
460
|
+
if flagged:
|
|
461
|
+
self.findings.append(
|
|
462
|
+
Finding(
|
|
463
|
+
id="AD-002",
|
|
464
|
+
title="Stale or non-expiring privileged (adminCount=1) accounts",
|
|
465
|
+
severity=Severity.HIGH,
|
|
466
|
+
source="ad",
|
|
467
|
+
affected_count=len(flagged),
|
|
468
|
+
affected_entities=flagged,
|
|
469
|
+
why_it_matters=(
|
|
470
|
+
"Privileged accounts that never expire or have not logged in for months "
|
|
471
|
+
"give attackers a long-lived, unnoticed way into your network."
|
|
472
|
+
),
|
|
473
|
+
how_to_fix=(
|
|
474
|
+
"Disable unused privileged accounts and turn off 'password never expires' "
|
|
475
|
+
"on the rest, with regular password rotation."
|
|
476
|
+
),
|
|
477
|
+
)
|
|
478
|
+
)
|
|
479
|
+
|
|
480
|
+
def _check_kerberoastable_accounts(self, conn: Any) -> None:
|
|
481
|
+
search_filter = (
|
|
482
|
+
"(&(objectCategory=person)(objectClass=user)(servicePrincipalName=*)"
|
|
483
|
+
"(!(userAccountControl:1.2.840.113556.1.4.803:=2)))"
|
|
484
|
+
)
|
|
485
|
+
entries = self._search(
|
|
486
|
+
conn,
|
|
487
|
+
search_filter,
|
|
488
|
+
["sAMAccountName", "servicePrincipalName", "adminCount", "userAccountControl", "distinguishedName"],
|
|
489
|
+
)
|
|
490
|
+
|
|
491
|
+
kerberoastable: List[str] = []
|
|
492
|
+
privileged: List[str] = []
|
|
493
|
+
for entry in entries:
|
|
494
|
+
name = _account_name(entry)
|
|
495
|
+
if name.lower() == "krbtgt":
|
|
496
|
+
continue
|
|
497
|
+
spns = _list_str(entry, "servicePrincipalName")
|
|
498
|
+
if not spns:
|
|
499
|
+
continue
|
|
500
|
+
is_privileged = (_int(entry, "adminCount") == 1) or (name in self._tier0_users)
|
|
501
|
+
kerberoastable.append(name)
|
|
502
|
+
if is_privileged:
|
|
503
|
+
privileged.append(name)
|
|
504
|
+
self._add_node(
|
|
505
|
+
PrincipalNode(
|
|
506
|
+
id=name,
|
|
507
|
+
name=name,
|
|
508
|
+
principal_type="user",
|
|
509
|
+
source="ad",
|
|
510
|
+
is_privileged=is_privileged,
|
|
511
|
+
metadata={"spns": spns, "kerberoastable": True},
|
|
512
|
+
)
|
|
513
|
+
)
|
|
514
|
+
target = "Domain Admins" if is_privileged else name
|
|
515
|
+
relation = "is_kerberoastable_with_admin_equiv" if is_privileged else "is_kerberoastable"
|
|
516
|
+
self._add_edge(
|
|
517
|
+
IdentityEdge(
|
|
518
|
+
source_id=name,
|
|
519
|
+
target_id=target,
|
|
520
|
+
relation=relation,
|
|
521
|
+
description=f"{name} has an SPN and can be Kerberoasted"
|
|
522
|
+
+ (" with admin-equivalent rights." if is_privileged else "."),
|
|
523
|
+
)
|
|
524
|
+
)
|
|
525
|
+
|
|
526
|
+
if kerberoastable:
|
|
527
|
+
self.findings.append(
|
|
528
|
+
Finding(
|
|
529
|
+
id="AD-003",
|
|
530
|
+
title="Kerberoastable service accounts",
|
|
531
|
+
severity=Severity.CRITICAL if privileged else Severity.HIGH,
|
|
532
|
+
source="ad",
|
|
533
|
+
affected_count=len(kerberoastable),
|
|
534
|
+
affected_entities=kerberoastable,
|
|
535
|
+
why_it_matters=(
|
|
536
|
+
"Any logged-in user can request an offline-crackable ticket for these "
|
|
537
|
+
"accounts, and privileged ones hand over the whole domain."
|
|
538
|
+
),
|
|
539
|
+
how_to_fix=(
|
|
540
|
+
"Remove unnecessary SPNs, move services to group managed service accounts "
|
|
541
|
+
"(gMSA), and rotate passwords on the rest."
|
|
542
|
+
),
|
|
543
|
+
)
|
|
544
|
+
)
|
|
545
|
+
|
|
546
|
+
def _check_asrep_roastable_accounts(self, conn: Any) -> None:
|
|
547
|
+
search_filter = (
|
|
548
|
+
"(&(objectCategory=person)(objectClass=user)"
|
|
549
|
+
"(userAccountControl:1.2.840.113556.1.4.803:=4194304)"
|
|
550
|
+
"(!(userAccountControl:1.2.840.113556.1.4.803:=2)))"
|
|
551
|
+
)
|
|
552
|
+
entries = self._search(
|
|
553
|
+
conn,
|
|
554
|
+
search_filter,
|
|
555
|
+
["sAMAccountName", "adminCount", "userAccountControl", "distinguishedName"],
|
|
556
|
+
)
|
|
557
|
+
|
|
558
|
+
asrep: List[str] = []
|
|
559
|
+
privileged: List[str] = []
|
|
560
|
+
for entry in entries:
|
|
561
|
+
name = _account_name(entry)
|
|
562
|
+
is_privileged = (_int(entry, "adminCount") == 1) or (name in self._tier0_users)
|
|
563
|
+
asrep.append(name)
|
|
564
|
+
if is_privileged:
|
|
565
|
+
privileged.append(name)
|
|
566
|
+
self._add_node(
|
|
567
|
+
PrincipalNode(
|
|
568
|
+
id=name,
|
|
569
|
+
name=name,
|
|
570
|
+
principal_type="user",
|
|
571
|
+
source="ad",
|
|
572
|
+
is_privileged=is_privileged,
|
|
573
|
+
metadata={"asrep_roastable": True, "dont_req_preauth": True},
|
|
574
|
+
)
|
|
575
|
+
)
|
|
576
|
+
target = "Domain Admins" if is_privileged else name
|
|
577
|
+
relation = "is_asrep_roastable_with_admin_equiv" if is_privileged else "is_asrep_roastable"
|
|
578
|
+
self._add_edge(
|
|
579
|
+
IdentityEdge(
|
|
580
|
+
source_id=name,
|
|
581
|
+
target_id=target,
|
|
582
|
+
relation=relation,
|
|
583
|
+
description=(
|
|
584
|
+
f"{name} does not require Kerberos pre-authentication and can be AS-REP roasted."
|
|
585
|
+
),
|
|
586
|
+
)
|
|
587
|
+
)
|
|
588
|
+
|
|
589
|
+
if asrep:
|
|
590
|
+
self.findings.append(
|
|
591
|
+
Finding(
|
|
592
|
+
id="AD-004",
|
|
593
|
+
title="AS-REP roastable accounts (no pre-authentication)",
|
|
594
|
+
severity=Severity.CRITICAL if privileged else Severity.HIGH,
|
|
595
|
+
source="ad",
|
|
596
|
+
affected_count=len(asrep),
|
|
597
|
+
affected_entities=asrep,
|
|
598
|
+
why_it_matters=(
|
|
599
|
+
"Attackers can request an encrypted login reply for these accounts without "
|
|
600
|
+
"any password and crack it offline."
|
|
601
|
+
),
|
|
602
|
+
how_to_fix=(
|
|
603
|
+
"Clear the 'Do not require Kerberos preauthentication' flag and set strong "
|
|
604
|
+
"unique passwords on the affected accounts."
|
|
605
|
+
),
|
|
606
|
+
)
|
|
607
|
+
)
|
|
608
|
+
|
|
609
|
+
def _check_kerberos_delegation(self, conn: Any) -> None:
|
|
610
|
+
unconstrained_filter = (
|
|
611
|
+
"(&(objectClass=*)(userAccountControl:1.2.840.113556.1.4.803:=524288)"
|
|
612
|
+
"(!(userAccountControl:1.2.840.113556.1.4.803:=8192)))"
|
|
613
|
+
)
|
|
614
|
+
constrained_filter = "(userAccountControl:1.2.840.113556.1.4.803:=1048576)"
|
|
615
|
+
rbcd_filter = "(msDS-AllowedToActOnBehalfOfOtherIdentity=*)"
|
|
616
|
+
|
|
617
|
+
unconstrained: List[str] = []
|
|
618
|
+
constrained: List[str] = []
|
|
619
|
+
rbcd: List[str] = []
|
|
620
|
+
|
|
621
|
+
entries = self._search(
|
|
622
|
+
conn,
|
|
623
|
+
unconstrained_filter,
|
|
624
|
+
["sAMAccountName", "userAccountControl", "distinguishedName", "objectClass"],
|
|
625
|
+
)
|
|
626
|
+
for entry in entries:
|
|
627
|
+
if _uac(entry) & self.UAC_SERVER_TRUST_ACCOUNT:
|
|
628
|
+
continue
|
|
629
|
+
name = _account_name(entry)
|
|
630
|
+
if name not in unconstrained:
|
|
631
|
+
unconstrained.append(name)
|
|
632
|
+
self._add_edge(
|
|
633
|
+
IdentityEdge(
|
|
634
|
+
source_id=name,
|
|
635
|
+
target_id="Domain Admins" if name in self._tier0_users else name,
|
|
636
|
+
relation="has_weak_delegation",
|
|
637
|
+
description=f"{name} allows unconstrained Kerberos delegation.",
|
|
638
|
+
)
|
|
639
|
+
)
|
|
640
|
+
|
|
641
|
+
entries = self._search(
|
|
642
|
+
conn,
|
|
643
|
+
constrained_filter,
|
|
644
|
+
["sAMAccountName", "userAccountControl", "distinguishedName"],
|
|
645
|
+
)
|
|
646
|
+
for entry in entries:
|
|
647
|
+
uac = _uac(entry)
|
|
648
|
+
if uac & self.UAC_SERVER_TRUST_ACCOUNT:
|
|
649
|
+
continue
|
|
650
|
+
if not (uac & self.UAC_TRUSTED_TO_AUTH_FOR_DELEGATION):
|
|
651
|
+
continue
|
|
652
|
+
name = _account_name(entry)
|
|
653
|
+
if name not in constrained:
|
|
654
|
+
constrained.append(name)
|
|
655
|
+
self._add_edge(
|
|
656
|
+
IdentityEdge(
|
|
657
|
+
source_id=name,
|
|
658
|
+
target_id="Domain Admins" if name in self._tier0_users else name,
|
|
659
|
+
relation="has_weak_delegation",
|
|
660
|
+
description=f"{name} allows constrained delegation with protocol transition.",
|
|
661
|
+
)
|
|
662
|
+
)
|
|
663
|
+
|
|
664
|
+
entries = self._search(
|
|
665
|
+
conn,
|
|
666
|
+
rbcd_filter,
|
|
667
|
+
["sAMAccountName", "msDS-AllowedToActOnBehalfOfOtherIdentity", "distinguishedName"],
|
|
668
|
+
)
|
|
669
|
+
for entry in entries:
|
|
670
|
+
if not _raw(entry, "msDS-AllowedToActOnBehalfOfOtherIdentity"):
|
|
671
|
+
continue
|
|
672
|
+
name = _account_name(entry)
|
|
673
|
+
if name not in rbcd:
|
|
674
|
+
rbcd.append(name)
|
|
675
|
+
self._add_edge(
|
|
676
|
+
IdentityEdge(
|
|
677
|
+
source_id=name,
|
|
678
|
+
target_id="Domain Admins" if name in self._tier0_users else name,
|
|
679
|
+
relation="has_weak_delegation",
|
|
680
|
+
description=f"{name} has Resource-Based Constrained Delegation configured.",
|
|
681
|
+
)
|
|
682
|
+
)
|
|
683
|
+
|
|
684
|
+
all_weak = unconstrained + [x for x in constrained if x not in unconstrained]
|
|
685
|
+
all_weak += [x for x in rbcd if x not in all_weak]
|
|
686
|
+
if all_weak:
|
|
687
|
+
self.findings.append(
|
|
688
|
+
Finding(
|
|
689
|
+
id="AD-005",
|
|
690
|
+
title="Weak Kerberos delegation settings",
|
|
691
|
+
severity=Severity.CRITICAL if unconstrained else Severity.HIGH,
|
|
692
|
+
source="ad",
|
|
693
|
+
affected_count=len(all_weak),
|
|
694
|
+
affected_entities=all_weak,
|
|
695
|
+
why_it_matters=(
|
|
696
|
+
"Delegation settings let a compromised service impersonate any user, "
|
|
697
|
+
"including administrators, to other services."
|
|
698
|
+
),
|
|
699
|
+
how_to_fix=(
|
|
700
|
+
"Disable unconstrained delegation, prefer constrained delegation to specific "
|
|
701
|
+
"services, and review RBCD permissions regularly."
|
|
702
|
+
),
|
|
703
|
+
)
|
|
704
|
+
)
|
|
705
|
+
|
|
706
|
+
def _check_local_admin_sprawl_proxies(self, conn: Any) -> None:
|
|
707
|
+
operator_groups = ["HelpDesk", "IT Support", "Account Operators", "Server Operators"]
|
|
708
|
+
proxy_found = False
|
|
709
|
+
affected_groups: List[str] = []
|
|
710
|
+
|
|
711
|
+
for operator_name in operator_groups:
|
|
712
|
+
entries = self._search(
|
|
713
|
+
conn,
|
|
714
|
+
f"(&(objectClass=group)(cn={operator_name}))",
|
|
715
|
+
["member", "distinguishedName"],
|
|
716
|
+
)
|
|
717
|
+
for entry in entries:
|
|
718
|
+
for member in _list_str(entry, "member"):
|
|
719
|
+
member_lower = member.lower()
|
|
720
|
+
privileged_target = next(
|
|
721
|
+
(target for target in ("Domain Admins", "Enterprise Admins", "Administrators")
|
|
722
|
+
if target.lower() in member_lower),
|
|
723
|
+
None,
|
|
724
|
+
)
|
|
725
|
+
if privileged_target:
|
|
726
|
+
proxy_found = True
|
|
727
|
+
affected_groups.append(operator_name)
|
|
728
|
+
self._add_edge(
|
|
729
|
+
IdentityEdge(
|
|
730
|
+
source_id=operator_name,
|
|
731
|
+
target_id=privileged_target,
|
|
732
|
+
relation="member_of",
|
|
733
|
+
description=f"{operator_name} is nested into {privileged_target}.",
|
|
734
|
+
)
|
|
735
|
+
)
|
|
736
|
+
|
|
737
|
+
direct_da = self._direct_da_count
|
|
738
|
+
nesting_depth = 3 if direct_da >= 3 else (2 if direct_da else 1)
|
|
739
|
+
severity = Severity.HIGH if (nesting_depth >= 3 or proxy_found or direct_da >= 5) else Severity.MEDIUM
|
|
740
|
+
|
|
741
|
+
affected = affected_groups or (["Domain Admins"] if direct_da else ["No direct Domain Admins members"])
|
|
742
|
+
self.findings.append(
|
|
743
|
+
Finding(
|
|
744
|
+
id="AD-006",
|
|
745
|
+
title="Local administrator sprawl and proxy delegation",
|
|
746
|
+
severity=severity,
|
|
747
|
+
source="ad",
|
|
748
|
+
affected_count=direct_da if direct_da else len(affected_groups),
|
|
749
|
+
affected_entities=affected,
|
|
750
|
+
why_it_matters=(
|
|
751
|
+
"Help-desk or operator groups that can reset or join privileged accounts let "
|
|
752
|
+
"a normal support login snowball into full domain control."
|
|
753
|
+
),
|
|
754
|
+
how_to_fix=(
|
|
755
|
+
"Tier administration so support groups cannot reset or modify privileged "
|
|
756
|
+
"accounts, and flatten nested group memberships."
|
|
757
|
+
),
|
|
758
|
+
)
|
|
759
|
+
)
|
|
760
|
+
|
|
761
|
+
def _check_password_and_compat_hygiene(self, conn: Any) -> None:
|
|
762
|
+
# (a) Enabled users with DONT_EXPIRE_PASSWORD
|
|
763
|
+
entries = self._search(
|
|
764
|
+
conn,
|
|
765
|
+
"(&(objectCategory=person)(objectClass=user)"
|
|
766
|
+
"(userAccountControl:1.2.840.113556.1.4.803:=65536)"
|
|
767
|
+
"(!(userAccountControl:1.2.840.113556.1.4.803:=2)))",
|
|
768
|
+
["sAMAccountName", "userAccountControl"],
|
|
769
|
+
)
|
|
770
|
+
nonexpiring = [_account_name(entry) for entry in entries]
|
|
771
|
+
if nonexpiring:
|
|
772
|
+
self.findings.append(
|
|
773
|
+
Finding(
|
|
774
|
+
id="AD-007A",
|
|
775
|
+
title="Enabled accounts with 'password never expires'",
|
|
776
|
+
severity=Severity.MEDIUM,
|
|
777
|
+
source="ad",
|
|
778
|
+
affected_count=len(nonexpiring),
|
|
779
|
+
affected_entities=nonexpiring,
|
|
780
|
+
why_it_matters=(
|
|
781
|
+
"Accounts whose passwords never expire give attackers an unlimited window "
|
|
782
|
+
"to guess or reuse a stolen password."
|
|
783
|
+
),
|
|
784
|
+
how_to_fix=(
|
|
785
|
+
"Clear 'password never expires' and require periodic password changes with "
|
|
786
|
+
"strong complexity."
|
|
787
|
+
),
|
|
788
|
+
)
|
|
789
|
+
)
|
|
790
|
+
|
|
791
|
+
# (b) Disabled accounts that are still privileged
|
|
792
|
+
entries = self._search(
|
|
793
|
+
conn,
|
|
794
|
+
"(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=2))",
|
|
795
|
+
["sAMAccountName", "userAccountControl", "adminCount", "memberOf"],
|
|
796
|
+
)
|
|
797
|
+
disabled_privileged: List[str] = []
|
|
798
|
+
for entry in entries:
|
|
799
|
+
name = _account_name(entry)
|
|
800
|
+
is_privileged = (_int(entry, "adminCount") == 1) or (name in self._tier0_users)
|
|
801
|
+
member_of = _list_str(entry, "memberOf")
|
|
802
|
+
if is_privileged or any(
|
|
803
|
+
("Domain Admins" in m or "Enterprise Admins" in m) for m in member_of
|
|
804
|
+
):
|
|
805
|
+
if name not in disabled_privileged:
|
|
806
|
+
disabled_privileged.append(name)
|
|
807
|
+
if disabled_privileged:
|
|
808
|
+
self.findings.append(
|
|
809
|
+
Finding(
|
|
810
|
+
id="AD-007B",
|
|
811
|
+
title="Disabled accounts still holding privileged access",
|
|
812
|
+
severity=Severity.MEDIUM,
|
|
813
|
+
source="ad",
|
|
814
|
+
affected_count=len(disabled_privileged),
|
|
815
|
+
affected_entities=disabled_privileged,
|
|
816
|
+
why_it_matters=(
|
|
817
|
+
"Disabled but still-privileged accounts can be re-enabled or misused and "
|
|
818
|
+
"keep unnecessary admin rights in the directory."
|
|
819
|
+
),
|
|
820
|
+
how_to_fix=(
|
|
821
|
+
"Remove privileged group memberships and adminCount from disabled accounts."
|
|
822
|
+
),
|
|
823
|
+
)
|
|
824
|
+
)
|
|
825
|
+
|
|
826
|
+
# (c) Pre-Windows 2000 Compatible Access
|
|
827
|
+
entries = self._search(
|
|
828
|
+
conn,
|
|
829
|
+
"(&(objectClass=group)(|(cn=Pre-Windows 2000 Compatible Access)(objectSid=S-1-5-32-554)))",
|
|
830
|
+
["member", "distinguishedName", "cn"],
|
|
831
|
+
)
|
|
832
|
+
prewin2k_members: List[str] = []
|
|
833
|
+
for entry in entries:
|
|
834
|
+
for member in _list_str(entry, "member"):
|
|
835
|
+
member_name = _name_from_member_dn(member)
|
|
836
|
+
if member_name and member_name not in prewin2k_members:
|
|
837
|
+
prewin2k_members.append(member_name)
|
|
838
|
+
|
|
839
|
+
if not prewin2k_members:
|
|
840
|
+
entries = self._search(
|
|
841
|
+
conn,
|
|
842
|
+
"(distinguishedName=CN=Pre-Windows 2000 Compatible Access,CN=Builtin,*)",
|
|
843
|
+
["member"],
|
|
844
|
+
)
|
|
845
|
+
for entry in entries:
|
|
846
|
+
for member in _list_str(entry, "member"):
|
|
847
|
+
member_name = _name_from_member_dn(member)
|
|
848
|
+
if member_name and member_name not in prewin2k_members:
|
|
849
|
+
prewin2k_members.append(member_name)
|
|
850
|
+
|
|
851
|
+
if prewin2k_members:
|
|
852
|
+
high_risk = any(
|
|
853
|
+
member.lower() in ("everyone", "authenticated users", "anonymous logon")
|
|
854
|
+
for member in prewin2k_members
|
|
855
|
+
)
|
|
856
|
+
self.findings.append(
|
|
857
|
+
Finding(
|
|
858
|
+
id="AD-007C",
|
|
859
|
+
title="Broad membership in Pre-Windows 2000 Compatible Access",
|
|
860
|
+
severity=Severity.HIGH if high_risk else Severity.MEDIUM,
|
|
861
|
+
source="ad",
|
|
862
|
+
affected_count=len(prewin2k_members),
|
|
863
|
+
affected_entities=prewin2k_members,
|
|
864
|
+
why_it_matters=(
|
|
865
|
+
"This legacy group can expose account details to everyone or all logged-in "
|
|
866
|
+
"users, weakening privacy and access controls."
|
|
867
|
+
),
|
|
868
|
+
how_to_fix=(
|
|
869
|
+
"Remove Everyone/Authenticated Users from Pre-Windows 2000 Compatible "
|
|
870
|
+
"Access and grant only the specific legacy apps that need it."
|
|
871
|
+
),
|
|
872
|
+
)
|
|
873
|
+
)
|