Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions examples/hardening-report-sample.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/aditor/hardening/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
underneath it.
"""

SCAN_ENGINE_VERSION = "1.6.0"
SCAN_ENGINE_VERSION = "1.6.1"
31 changes: 25 additions & 6 deletions src/aditor/hardening/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,29 +284,48 @@ def _read_scope(self) -> Optional[Scope]:
"""
try:
entries = self._search("(objectCategory=computer)",
["userAccountControl"])
["userAccountControl", "operatingSystem"])
except Exception as exc: # noqa: BLE001 - fall back, and say so
self.scope_notes.append(
f"Computer accounts couldn't be read ({exc}), so each GPO is "
f"judged by its links, not by the computers it reaches.")
return None
containers: Dict[str, List[int]] = {}
labels: Dict[str, str] = {}
not_windows = 0
for entry in entries:
dn = _norm_dn(entry.get("dn") if isinstance(entry, dict) else "")
raw = str(entry.get("dn") or "") if isinstance(entry, dict) else ""
dn = _norm_dn(raw)
if "," not in dn:
continue
uac = _as_int(_attr(entry.get("attributes", {}) or {},
"userAccountControl"))
attributes = entry.get("attributes", {}) or {}
uac = _as_int(_attr(attributes, "userAccountControl"))
if uac & _UAC_ACCOUNTDISABLE:
continue
counts = containers.setdefault(dn.split(",", 1)[1], [0, 0])
# Only Windows machines apply Group Policy. A computer account with
# no Windows operating system (an appliance, a cluster or listener
# name, AZUREADSSOACC) never does, so it isn't a machine a GPO
# failed to reach.
if not _text(_attr(attributes, "operatingSystem", "")).lower() \
.startswith("windows"):
not_windows += 1
continue
container = dn.split(",", 1)[1]
labels.setdefault(container, raw.split(",", 1)[1].strip())
counts = containers.setdefault(container, [0, 0])
counts[0] += 1
if uac & (_UAC_SERVER_TRUST_ACCOUNT | _UAC_PARTIAL_SECRETS_ACCOUNT):
counts[1] += 1
if not_windows:
self.scope_notes.append(
f"{not_windows} enabled computer account(s) have no Windows "
f"operating system (appliances, cluster or listener names, "
f"AZUREADSSOACC and the like) and aren't counted: they don't "
f"apply Group Policy.")
if not containers:
return None
return Scope(containers={dn: (c[0], c[1]) for dn, c in containers.items()},
blocked=frozenset(self.blocked_containers))
blocked=frozenset(self.blocked_containers), labels=labels)

def _read_gpo_snapshots(self, links_by_guid: Dict[str, List[GpoLink]]
) -> Tuple[List[GpoSnapshot], List[Dict[str, str]]]:
Expand Down
19 changes: 17 additions & 2 deletions src/aditor/hardening/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ class Scope:

containers: Dict[str, Tuple[int, int]]
blocked: frozenset = frozenset()
#: Normalised container DN -> the DN as the directory spells it, for display.
labels: Dict[str, str] = field(default_factory=dict)

def targets(self, control: Control) -> Dict[str, int]:
"""The machines a control is about, per container."""
Expand Down Expand Up @@ -854,7 +856,15 @@ def evaluate_control(control: Control,
covered = set().union(*(reach.get(m["gpo_dn"], set()) for m in matches))
total = sum(targets.values())
got = sum(targets[c] for c in covered)
coverage = {"covered": got, "total": total, "unit": unit}
missed = sorted(((c, n) for c, n in targets.items() if c not in covered),
key=lambda item: (-item[1], item[0]))
hit = sorted(((c, targets[c]) for c in covered),
key=lambda item: (-item[1], item[0]))
coverage = {"covered": got, "total": total, "unit": unit,
"reached": [{"container": scope.labels.get(c, c),
"count": n} for c, n in hit[:8]],
"not_reached": [{"container": scope.labels.get(c, c),
"count": n} for c, n in missed[:8]]}
if got < total:
rest = total - got
if control.os_default is not None:
Expand All @@ -869,9 +879,14 @@ def evaluate_control(control: Control,
else:
worst = STATE_NOT_STARTED
outcome = "aren't configured"
where = ", ".join(f"{scope.labels.get(c, c)} ({n})"
for c, n in missed[:8])
extra_notes.append(
f"Group Policy sets this on {got} of {total} {unit}. The "
f"other {rest} get no value from it, so they {outcome}.")
f"other {rest} get no value from it, so they {outcome}. "
f"Not reached: {where}"
+ (f" and {len(missed) - 8} more places." if len(missed) > 8
else "."))

if control.operator == "present":
result = RESULT_PASS
Expand Down
46 changes: 45 additions & 1 deletion src/aditor/hardening/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,15 @@ def _next_step(finding: Dict[str, Any], section_id: str) -> str:
if _is_directory(finding):
count = len((finding.get("evidence") or {}).get("found") or [])
return f"fix the {count} listed on the card"
coverage = (finding.get("evidence") or {}).get("coverage")
if isinstance(coverage, dict) and coverage.get("covered") and \
coverage.get("covered") != coverage.get("total") and all(
m.get("rollout_state") == "enforced" for m in
(finding.get("evidence") or {}).get("found") or []
if isinstance(m, dict)):
missing = coverage["total"] - coverage["covered"]
return (f"apply the same setting to the {missing} "
f"{_esc(coverage.get('unit'))} it doesn't reach yet")
expected = (finding.get("evidence") or {}).get("expected") or {}
if not isinstance(expected, dict) or expected.get("operator") in (
"present", "absent"):
Expand Down Expand Up @@ -1262,6 +1271,37 @@ def _render_first_step(finding: Dict[str, Any], section_id: str) -> str:
_NTLM_VERDICT_CSS = {"blocked": "danger", "not-yet": "warn", "clear": "ok-banner"}


def _places(entries: Any) -> str:
return ", ".join(f'{_esc(m.get("container"))} ({_esc(m.get("count"))})'
for m in entries or [] if isinstance(m, dict))


def _render_partial(finding: Dict[str, Any]) -> str:
"""Done here, not there, and what that leaves open there.

A setting applied to some machines is progress, and the card should say
so before saying where it's missing and what the gap means.
"""
coverage = (finding.get("evidence") or {}).get("coverage")
if not isinstance(coverage, dict) or not coverage.get("total") \
or coverage.get("covered") == coverage.get("total"):
return ""
covered, total = coverage.get("covered") or 0, coverage.get("total")
unit = _esc(coverage.get("unit"))
lines = []
if covered:
lines.append(f'<p class="done-here">&#10003; <strong>Done on {covered} '
f'of {total} {unit}:</strong> '
f'{_places(coverage.get("reached"))}.</p>')
risk = finding.get("why_it_matters")
lines.append(f'<p class="not-here">&#10007; <strong>Not yet on '
f'{total - covered}:</strong> '
f'{_places(coverage.get("not_reached"))}.'
+ (f' What that leaves open: {_esc(risk)}' if risk else "")
+ ' Link the GPO there too, or to a container above them.</p>')
return f'<div class="block partial">{"".join(lines)}</div>'


def _render_ntlm_evidence(finding: Dict[str, Any]) -> str:
"""Who still uses NTLMv1, from an exported event log, on the NTLMv2 card."""
if finding.get("control_id") != _LM_CONTROL:
Expand Down Expand Up @@ -1517,7 +1557,8 @@ def _render_card(finding: Dict[str, Any], section_id: str) -> str:
anchor = control_id.lower().replace(" ", "-")

body: List[str] = []
if finding.get("why_it_matters"):
# With partial coverage the risk is said once, where the gap is.
if finding.get("why_it_matters") and not _render_partial(finding):
body.append(f'<p class="why">{_esc(finding.get("why_it_matters"))}</p>')
body.append(_deadline_line(finding, _SCAN_DATE.get("date", "")))
body.append(_render_glance(finding))
Expand All @@ -1533,6 +1574,7 @@ def _render_card(finding: Dict[str, Any], section_id: str) -> str:
body.append(_render_unknown_reason(finding))
notes_shown = True
body.append(_render_conflict(finding))
body.append(_render_partial(finding))
body.append(_render_ntlm_evidence(finding))
if section_id in (SECTION_FAIL, SECTION_UNKNOWN, SECTION_OPPORTUNITIES):
body.append(_render_first_step(finding, section_id))
Expand Down Expand Up @@ -1963,6 +2005,8 @@ def _render_toc(grouped: Dict[str, List[Dict[str, Any]]],
details.group>summary::before{content:"\\25B8 ";color:var(--muted)}
details.group[open]>summary::before{content:"\\25BE "}
.method{margin-top:2rem}
.partial .done-here{color:var(--ok)}
.partial .not-here{color:var(--bad)}
.danger{background:var(--bad-bg);border-left:4px solid var(--bad);
padding:.5rem .7rem;margin:.4rem 0}
details.howto>summary{cursor:pointer;font-weight:600}
Expand Down
12 changes: 8 additions & 4 deletions tests/test_hardening_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2974,8 +2974,10 @@ def test_a_test_ou_link_is_not_domain_wide_enforcement(self, lm_control):
scope=domain())

assert finding["result"] == RESULT_FAIL
assert finding["evidence"]["coverage"] == {
"covered": 3, "total": 15, "unit": "computers"}
coverage = finding["evidence"]["coverage"]
assert (coverage["covered"], coverage["total"], coverage["unit"]) == (
3, 15, "computers")
assert [m["count"] for m in coverage["not_reached"]] == [10, 2]
assert any("3 of 15 computers" in n for n in finding["evidence"]["notes"])

def test_a_root_link_covers_every_computer(self, lm_control):
Expand Down Expand Up @@ -3017,8 +3019,10 @@ def test_a_dc_moved_out_of_the_dc_ou_is_still_a_dc(self, signing):
finding = evaluate_control(signing, [gpo], scope=domain(moved_dc=True))

assert finding["result"] == RESULT_FAIL
assert finding["evidence"]["coverage"] == {
"covered": 1, "total": 2, "unit": "domain controllers"}
coverage = finding["evidence"]["coverage"]
assert (coverage["covered"], coverage["total"], coverage["unit"]) == (
1, 2, "domain controllers")
assert coverage["not_reached"] == [{"container": LAB_OU, "count": 1}]

def test_uncovered_machines_with_a_windows_default_use_the_default(self):
client = load_catalog().by_id("DEVORE-03-LDAP-CLIENT-SIGNING")
Expand Down
58 changes: 57 additions & 1 deletion tests/test_hardening_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
GUID_A = "11111111-1111-1111-1111-111111111111"
GUID_B = "22222222-2222-2222-2222-222222222222"
GUID_C = "33333333-3333-3333-3333-333333333333"
GUID_D = "44444444-4444-4444-4444-444444444444"

# A GPO display name an attacker who can rename a GPO would choose. Directory
# content is untrusted input to this renderer.
Expand Down Expand Up @@ -1021,12 +1022,23 @@ def sample_scan(first_gpo=HOSTILE_NAME):
snapshot(GUID_C, "Unreadable Sample GPO",
links=[GpoLink(target_dn=BASE_DN)],
read_error="SMB read failed: STATUS_ACCESS_DENIED"),
# LLMNR off on the workstations only, so the sample shows partial
# coverage: done here, not on the servers.
snapshot(GUID_D, "Sample Workstation LLMNR GPO",
links=[GpoLink(target_dn=f"OU=Workstations,{BASE_DN}")],
pol_entries=[{"key": r"Software\Policies\Microsoft\Windows NT"
r"\DNSClient",
"value": "EnableMulticast", "type": "REG_DWORD",
"data": 0}]),
], catalog=catalog, read_errors=read_errors, scope=Scope(containers={
# A small made-up fleet, so the sample shows "Applies to: N of M".
f"ou=domain controllers,{BASE_DN}".lower(): (2, 2),
f"ou=workstations,{BASE_DN}".lower(): (40, 0),
f"ou=servers,{BASE_DN}".lower(): (8, 0),
}), directory={
}, labels={f"ou={ou},{BASE_DN}".lower(): f"OU={OU},{BASE_DN}"
for ou, OU in (("domain controllers", "Domain Controllers"),
("workstations", "Workstations"),
("servers", "Servers"))}), directory={
# One directory-state failure and two passes, so the sample shows
# what a directory finding looks like.
"DEVORE-07-UNCONSTRAINED-DELEGATION": {"objects": [{
Expand Down Expand Up @@ -1948,3 +1960,47 @@ def test_without_directory_data_the_report_says_so(self, catalog):
def test_the_sample_shows_coverage(self):
document = render_report(sample_scan("Sample Workstation Baseline GPO"))
assert "Applies to:</strong>" in document


class TestPartialCoverageReadsAsProgressThenGap:
"""Done here, not there, and what that means there."""

def document(self, covered):
payload = sample_scan("Sample Workstation Baseline GPO")
finding = next(f for f in payload["findings"]
if f["control_id"] == "DEVORE-06-LLMNR-DISABLE")
finding["evidence"]["coverage"] = {
"covered": covered, "total": 150, "unit": "computers",
"reached": [{"container": "OU=Workstations,DC=example,DC=com",
"count": covered}] if covered else [],
"not_reached": [{"container": "OU=Servers,DC=example,DC=com",
"count": 150 - covered}]}
document = render_report(payload)
card = document[document.index('id="devore-06-llmnr-disable"'):]
return card[:card.index("</article>")]

def test_it_credits_what_is_done_then_names_the_gap_and_its_risk(self):
card = self.document(130)
assert "Done on 130 of 150 computers:</strong> " \
"OU=Workstations,DC=example,DC=com (130)" in card
assert "Not yet on 20:</strong> OU=Servers,DC=example,DC=com (20)" in card
assert "What that leaves open: On the local network, an attacker" in card
assert card.index("Done on") < card.index("Not yet on")

def test_nothing_done_says_only_the_gap(self):
card = self.document(0)
assert "Done on" not in card
assert "Not yet on 150" in card


class TestPartialCoverageNextStep:

def test_a_setting_done_where_it_applies_says_to_extend_it(self):
document = render_report(sample_scan("Sample Workstation Baseline GPO"))
card = document[document.index('id="devore-06-llmnr-disable"'):]
card = card[:card.index("</article>")]
assert "Next:</strong> apply the same setting to the 10 computers it " \
"doesn&#x27;t reach yet" in card or \
"apply the same setting to the 10 computers it doesn't reach yet" in card
assert card.count("collect password hashes") == 1
assert "OU=Servers,DC=test,DC=local (8)" in card
27 changes: 23 additions & 4 deletions tests/test_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,9 +812,10 @@ class TestScopingThroughTheScan:
WS = f"OU=Workstations,{BASE_DN}"

@staticmethod
def computer(name, ou, uac=0x1000):
def computer(name, ou, uac=0x1000, os="Windows 11 Pro"):
return {"dn": f"CN={name},{ou}",
"attributes": {"userAccountControl": uac}}
"attributes": {"userAccountControl": uac,
"operatingSystem": os}}

def wire(self, mock_ldap_manager, link_entries, computers, extra=()):
wire_ldap(mock_ldap_manager, [gpo_entry(GUID_SIGNING, "LM Policy")],
Expand Down Expand Up @@ -854,8 +855,9 @@ def test_a_lab_only_link_fails_and_says_how_much_it_covers(
finding = response["findings"][0]

assert finding["result"] == "fail"
assert finding["evidence"]["coverage"] == {
"covered": 1, "total": 10, "unit": "computers"}
coverage = finding["evidence"]["coverage"]
assert (coverage["covered"], coverage["total"]) == (1, 10)
assert coverage["not_reached"][0] == {"container": self.WS, "count": 8}
assert response["scan"]["computers_scoped"] == 10

def test_block_inheritance_on_an_ou_without_links_is_read(
Expand Down Expand Up @@ -883,3 +885,20 @@ def test_a_site_link_to_another_domains_gpo_is_not_credited(

assert finding["result"] != "pass"
assert finding["evidence"]["found"] == []

def test_computer_accounts_without_windows_are_not_counted(
self, tools, mock_ldap_manager):
"""Appliances, cluster names and AZUREADSSOACC never apply Group
Policy, so a GPO not reaching them isn't a gap."""
fleet = ([self.computer("WS1", self.WS)]
+ [self.computer("NAS1", self.WS, os=""),
self.computer("AZUREADSSOACC", f"CN=Computers,{BASE_DN}",
os="")])
self.wire(mock_ldap_manager, [link_entry(self.WS, GUID_SIGNING)], fleet)

response = self.scan_lm(tools)

assert response["findings"][0]["result"] == "pass"
assert response["scan"]["computers_scoped"] == 1
assert any("2 enabled computer account(s) have no Windows" in note
for note in response["scan"]["scope_notes"])