Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.
Python agentic-aiai-securityalert-triageblue-teamcybersecuritydetection-engineeringincident-responselanggraphllm-agentsmcpmitre-attackmodel-context-protocolpurple-teamsecurity-operationsself-hostedsiemsoarsocthreat-detectionthreat-intelligence
feat(detections): an adversary-in-the-middle family on sessions
Beenu Arora2d7ee7e369a7
diff --git a/README.md b/README.mdindex dfe2ad1c8..5e79144aa 100644--- a/README.md+++ b/README.md@@ -114,7 +114,7 @@ rule is harder to find than a missing one: of the 2,005 Splunk rules bundled her## How it worksIngest normalizes to a common shape and stamps an **activity projection** on every event — actor, action, resource, location, outcome, with `actor.kind` read from each vendor's own documented field rather than inferred — then Kafka carries it.-Then fusion runs 2511 executable detection rules, of 6991 on disk, plus 72 sliding-window rules (70 count across events; 2 match a sequence), applies **your tenant's own tuning** on top — the disables, floors and suppressions the console writes, so a rule you turned off actually stops firing — and decides what becomes an alert.+Then fusion runs 2511 executable detection rules, of 6991 on disk, plus 76 sliding-window rules (73 count across events; 3 match an ordered sequence), applies **your tenant's own tuning** on top — the disables, floors and suppressions the console writes, so a rule you turned off actually stops firing — and decides what becomes an alert.Correlation groups related alerts, an agent investigates and writes its reasoning to the Investigation Ledger, and a playbook may start from the result. Separately, new threat intelligence sweeps the lake for sightings you already collected, and a hypothesis becomes a hunt without anyone writing a query — the model fills a closed schema and every value is bound as a parameter, so it cannot express a query at all.**A playbook triggered by an alert previews before it acts.** Three switches must agree — thediff --git a/apps/web/src/data/corpus-stats.json b/apps/web/src/data/corpus-stats.jsonindex e61dd390d..a4493fde4 100644--- a/apps/web/src/data/corpus-stats.json+++ b/apps/web/src/data/corpus-stats.json@@ -1,6 +1,6 @@{"executable": 2511,- "windowed": 72,+ "windowed": 76,"onDisk": 6991,"quarantined": 4480,"plugins": 80,diff --git a/docs/detections/truth-table.md b/docs/detections/truth-table.mdindex 33b3f5e2c..3177c9609 100644--- a/docs/detections/truth-table.md+++ b/docs/detections/truth-table.md@@ -23,7 +23,7 @@ lever that reaches the engine is adding a spec and re-running| **executable (loaded by the engine)** | **2511** || non-executable (provenance/coverage only) | 4480 || — of which: enabled, but no compiled spec | 0 |-| windowed rules in `windowed_ruleset.json` | 72 |+| windowed rules in `windowed_ruleset.json` | 76 |The windowed figure is its own row rather than part of the headline becausea windowed rule is a different kind of thing: it counts events or distinctdiff --git a/scripts/export_windowed_ruleset.py b/scripts/export_windowed_ruleset.pyindex 4810a653e..14ba25519 100644--- a/scripts/export_windowed_ruleset.py+++ b/scripts/export_windowed_ruleset.py@@ -330,6 +330,105 @@ WINDOWED_RULES: list[dict] = [# Script-driven enumeration and mass encryption both look like this.# Build agents and installers do too, so this is deliberately medium.},+ # ── Adversary-in-the-middle, on sessions (depth 7.3) ────────────────────+ #+ # An AiTM proxy relays the victim's credentials *and* their MFA response,+ # so the sign-in is legitimate by every check the identity provider+ # makes: SPF on the lure passes or fails elsewhere, the password is+ # right, the push is approved by the real person. There is no failed+ # authentication to count and no impossible travel to measure, because+ # the proxy often sits in the same country.+ #+ # What gives it away is the *stolen session* being used from somewhere+ # the session was never established. So the entity these rules group by+ # is `session_id`, not the user — which is why 7.3 needed the connector+ # hoist first: Okta and Entra both carry a session identity and neither+ # `normalize()` emitted it, so this family had nothing to group by and+ # could not have been written at all.+ #+ # Thresholds are 2 rather than a larger number on purpose. These are+ # `distinct_by` rules: two *different* networks for one session is the+ # whole signal, and raising it to three would mean the detection only+ # fires on an attacker careless enough to rotate twice.+ #+ # `session_id_neq: None` is "the event carries a session id", spelled+ # with an operator that exists rather than an `_exists` one that does+ # not: `_check` returns False for a missing field under `neq` and True+ # for a present one, which is exactly the test. Inventing `_exists`+ # would have read as a field literally named `session_id_exists`, which+ # nothing emits — the `approver_role_neq` defect, one release later.+ {+ "id": "wd-aitm-session-replayed-from-new-asn",+ "name": "One session used from two different networks",+ "severity": "high",+ "category": "identity",+ "mitre": ["T1539", "T1557"],+ "match_when": {"session_id_neq": None},+ "group_by": "session_id",+ "distinct_by": "src_asn",+ "threshold": 2,+ "window_seconds": 3600,+ # An hour, because a session cookie is worth stealing precisely+ # because it outlives the sign-in; a five-minute window would miss+ # the replay and a day-long one would catch a laptop moving from an+ # office to a home connection, which is the same shape and is not+ # this.+ },+ {+ "id": "wd-aitm-session-replayed-from-new-client",+ "name": "One session used from two different client families",+ "severity": "high",+ "category": "identity",+ "mitre": ["T1539", "T1550.004"],+ "match_when": {"session_id_neq": None},+ "group_by": "session_id",+ "distinct_by": "client_browser",+ "threshold": 2,+ "window_seconds": 3600,+ # The second half of the same signal, and the one a network-level+ # evasion does not avoid: a session established in a browser and+ # next used by a toolkit's HTTP client changes the client family+ # whatever address it comes from. Browser rather than the raw user+ # agent, because a Chrome auto-update changes the string and not the+ # family.+ },+ {+ "id": "wd-aitm-session-left-the-country-it-started-in",+ "name": "One session used from two different countries",+ "severity": "high",+ "category": "identity",+ "mitre": ["T1539"],+ "match_when": {"session_id_neq": None},+ "group_by": "session_id",+ "distinct_by": "src_country",+ "threshold": 2,+ "window_seconds": 3600,+ # Deliberately a *session* rule and not an impossible-travel rule.+ # Impossible travel compares two sign-ins by one user and is defeated+ # by a proxy in the victim's own country; this compares two uses of+ # one session, where any country change at all is a claim the session+ # cannot support.+ },+ {+ "id": "wd-aitm-mfa-then-session-from-new-asn",+ "name": "An MFA-satisfied sign-in, then the same session used from another network",+ "severity": "critical",+ "category": "identity",+ "mitre": ["T1557", "T1539"],+ "sequence": [+ {"mfa_satisfied": True},+ {"session_id_neq": None},+ ],+ "ordered": True,+ "group_by": "session_id",+ "window_seconds": 1800,+ # Critical, and the only one of the family that is. The ordering is+ # the detection: a session used from two networks is suspicious, and+ # a session that *satisfied MFA* and was then used elsewhere within+ # half an hour is the AiTM pattern with the step the attacker needed+ # the proxy for already recorded. A threshold rule cannot express it+ # — that is the whole reason 3.2's ordered sequences exist.+ },]diff --git a/services/connectors/tests/connectors/test_aitm_session_fields_reach_detection.py b/services/connectors/tests/connectors/test_aitm_session_fields_reach_detection.pynew file mode 100644index 000000000..5c900d145--- /dev/null+++ b/services/connectors/tests/connectors/test_aitm_session_fields_reach_detection.py@@ -0,0 +1,234 @@+"""The session, network and client fields an AiTM rule needs survive normalize().++The connector half of depth 7.3. Its sibling,+``services/fusion/tests/test_aitm_session_rules.py``, drives the real+windowed engine over the payload this file asserts the connector produces.+They are separate files because ``services/connectors`` and+``services/fusion`` both name their top-level package ``app``, so importing+one inside the other's interpreter shadows it — the arrangement+``test_okta_outcome_reaches_detection.py`` already uses for the same reason.++The reproduction+----------------+Before this change ``OktaConnector.normalize()`` returned ``None`` for+``session_id``, ``src_asn``, ``user_agent``, ``client_browser`` and+``mfa_satisfied`` on a System Log row carrying all five. The engine flattens+``raw_event`` exactly one level, so ``authenticationContext`` and+``securityContext`` arrive as dicts and a rule clausing on a session id+compares a string against ``{...}``.++The payloads below are **hand-authored from the vendors' documented event+shapes**, not generated from the rules they exercise. Around 600 of the+corpus's fixtures are synthesised from the rule under test, which makes+replay tautological: the fixture contains the clause because the clause was+copied into it. These were written as an Okta System Log row and a Microsoft+Graph ``signIn`` resource first, and the rules were written against what+those carry.+"""++from __future__ import annotations++import pytest+from app.connectors.azure_entra import AzureEntraConnector+from app.connectors.okta import OktaConnector++# ── Okta System Log, hand-authored ───────────────────────────────────────────+#+# One AiTM story in three events, all sharing `externalSessionId`:+# 1. the victim signs in through the proxy and satisfies MFA, from their+# own ISP in a browser;+# 2. the attacker replays the stolen cookie from a hosting provider;+# 3. the attacker, having the session, reads the directory.++_SESSION = "102AbCdEfGhIjKlMnOpQrSt"+_VICTIM = "[email protected]"++OKTA_MFA_SIGNIN = {+ "uuid": "aa000000-0000-4000-8000-000000000001",+ "published": "2026-10-11T09:15:00.000Z",+ "eventType": "user.session.start",+ "displayMessage": "User login to Okta",+ "severity": "INFO",+ "outcome": {"result": "SUCCESS", "reason": "OK"},+ "actor": {"id": "00u1", "type": "User", "displayName": "Alice Example", "alternateId": _VICTIM},+ "client": {+ "ipAddress": "203.0.113.42",+ "userAgent": {"rawUserAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/141.0", "os": "Windows 10", "browser": "CHROME"},+ "geographicalContext": {"country": "United States", "city": "Boston"},+ "device": "Computer",+ },+ "authenticationContext": {"authenticationStep": 0, "externalSessionId": _SESSION},+ "securityContext": {"asNumber": 64500, "asOrg": "example telecom", "isp": "example telecom", "domain": "example.net", "isProxy": False},+ "debugContext": {"debugData": {"factor": "OKTA_VERIFY_PUSH", "authnRequestId": "req-1"}},+}++OKTA_REPLAYED_FROM_HOSTING = {+ "uuid": "aa000000-0000-4000-8000-000000000002",+ "published": "2026-10-11T09:21:00.000Z",+ "eventType": "user.session.access_admin_app",+ "displayMessage": "User accessing Okta admin app",+ "severity": "INFO",+ "outcome": {"result": "SUCCESS", "reason": "OK"},+ "actor": {"id": "00u1", "type": "User", "displayName": "Alice Example", "alternateId": _VICTIM},+ "client": {+ "ipAddress": "198.51.100.77",+ "userAgent": {"rawUserAgent": "python-requests/2.32.0", "os": "Unknown", "browser": "UNKNOWN"},+ "geographicalContext": {"country": "Netherlands", "city": "Amsterdam"},+ "device": "Unknown",+ },+ "authenticationContext": {"authenticationStep": 0, "externalSessionId": _SESSION},+ "securityContext": {"asNumber": 64512, "asOrg": "example hosting", "isp": "example hosting", "domain": "", "isProxy": True},+}++#: The negative twin. Same session, same user, **same network and same client+#: family** — which is what an ordinary second request inside a session looks+#: like. It inverts the clause each rule is about (the distinct network, the+#: distinct client, the distinct country) and nothing else: the event type,+#: the outcome, the actor and the session are deliberately identical to the+#: positive, so a rule that fired on any of those would still fire here.+OKTA_SAME_SESSION_SAME_NETWORK = {+ **OKTA_REPLAYED_FROM_HOSTING,+ "uuid": "aa000000-0000-4000-8000-000000000003",+ "published": "2026-10-11T09:22:00.000Z",+ "client": {+ "ipAddress": "203.0.113.51",+ "userAgent": {"rawUserAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/141.0", "os": "Windows 10", "browser": "CHROME"},+ "geographicalContext": {"country": "United States", "city": "Boston"},+ "device": "Computer",+ },+ "securityContext": {"asNumber": 64500, "asOrg": "example telecom", "isp": "example telecom", "domain": "example.net", "isProxy": False},+}++# ── Microsoft Graph signIn, hand-authored ────────────────────────────────────++ENTRA_MFA_SIGNIN = {+ "_aisoc_event_kind": "riskySignIn",+ "id": "bb000000-0000-4000-8000-000000000001",+ "createdDateTime": "2026-10-11T09:15:00Z",+ "userPrincipalName": _VICTIM,+ "userDisplayName": "Alice Example",+ "sessionId": "9f2c4d7a-0000-4000-8000-00000000aaaa",+ "correlationId": "11111111-0000-4000-8000-000000000001",+ "ipAddress": "203.0.113.42",+ "autonomousSystemNumber": 64500,+ "clientAppUsed": "Browser",+ "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/141.0",+ "authenticationRequirement": "multiFactorAuthentication",+ "incomingTokenType": "none",+ "riskLevelAggregated": "medium",+ "riskState": "atRisk",+ "riskEventTypes_v2": ["unfamiliarFeatures"],+ "deviceDetail": {"deviceId": "", "operatingSystem": "Windows 10", "browser": "Chrome 141.0", "isCompliant": False, "isManaged": False},+ "location": {"city": "Boston", "state": "Massachusetts", "countryOrRegion": "US"},+ "status": {"errorCode": 0, "failureReason": "Other."},+}++ENTRA_REPLAYED = {+ **ENTRA_MFA_SIGNIN,+ "id": "bb000000-0000-4000-8000-000000000002",+ "createdDateTime": "2026-10-11T09:21:00Z",+ "correlationId": "11111111-0000-4000-8000-000000000002",+ "ipAddress": "198.51.100.77",+ "autonomousSystemNumber": 64512,+ "clientAppUsed": "Mobile Apps and Desktop clients",+ "userAgent": "python-requests/2.32.0",+ "authenticationRequirement": "singleFactorAuthentication",+ "incomingTokenType": "primaryRefreshToken",+ "deviceDetail": {"deviceId": "", "operatingSystem": "Unknown", "browser": "Unknown", "isCompliant": False, "isManaged": False},+ "location": {"city": "Amsterdam", "state": "", "countryOrRegion": "NL"},+}+++def _okta(raw: dict) -> dict:+ return OktaConnector(domain="example.okta.com", api_token="unit-test-placeholder").normalize(raw)+++def _entra(raw: dict) -> dict:+ return AzureEntraConnector(tenant_id="t", client_id="c", client_secret="unit-test-placeholder").normalize(raw)+++class TestOktaEmitsWhatTheRulesGroupBy:+ def test_the_session_identity_survives_normalisation(self) -> None:+ """The reproduction: this returned ``None`` before depth 7.3."""+ assert _okta(OKTA_MFA_SIGNIN)["session_id"] == _SESSION++ def test_the_network_is_an_asn_not_only_an_address(self) -> None:+ """A residential ISP rotates addresses within one ASN all day."""+ event = _okta(OKTA_MFA_SIGNIN)+ assert event["src_asn"] == 64500+ assert event["src_asn_org"] == "example telecom"+ assert event["src_ip"] == "203.0.113.42"++ def test_the_client_family_is_emitted_separately_from_the_raw_string(self) -> None:+ """A Chrome auto-update changes the string and not the family."""+ event = _okta(OKTA_MFA_SIGNIN)+ assert event["client_browser"] == "CHROME"+ assert event["user_agent"].startswith("Mozilla/5.0")++ def test_a_replay_differs_from_the_sign_in_on_every_clause_the_family_reads(self) -> None:+ first, second = _okta(OKTA_MFA_SIGNIN), _okta(OKTA_REPLAYED_FROM_HOSTING)+ assert first["session_id"] == second["session_id"]+ assert first["src_asn"] != second["src_asn"]+ assert first["client_browser"] != second["client_browser"]+ assert first["src_country"] != second["src_country"]++ def test_the_negative_twin_shares_the_session_and_the_network(self) -> None:+ """A negative fixture must invert the clause the rule is *about*."""+ first, benign = _okta(OKTA_MFA_SIGNIN), _okta(OKTA_SAME_SESSION_SAME_NETWORK)+ assert first["session_id"] == benign["session_id"]+ assert first["src_asn"] == benign["src_asn"]+ assert first["client_browser"] == benign["client_browser"]+ assert first["src_country"] == benign["src_country"]+++class TestMfaSatisfiedIsThreeValued:+ def test_a_session_start_with_a_recorded_factor_is_satisfied(self) -> None:+ assert _okta(OKTA_MFA_SIGNIN)["mfa_satisfied"] is True++ def test_a_session_start_with_no_factor_is_not_satisfied(self) -> None:+ no_factor = {**OKTA_MFA_SIGNIN, "debugContext": {"debugData": {"authnRequestId": "req-2"}}}+ assert _okta(no_factor)["mfa_satisfied"] is False++ def test_an_event_that_says_nothing_about_mfa_answers_none(self) -> None:+ """Treating silence as False would fire on every directory read."""+ assert _okta(OKTA_REPLAYED_FROM_HOSTING)["mfa_satisfied"] is None++ def test_a_denied_push_is_an_mfa_event_and_not_a_satisfied_one(self) -> None:+ """Folding this in would make the field true for the MFA-fatigue sequence."""+ denied = {**OKTA_MFA_SIGNIN, "eventType": "user.mfa.okta_verify.deny_push"}+ assert _okta(denied)["mfa_satisfied"] is False+++class TestEntraEmitsTheSameVocabulary:+ def test_the_graph_session_id_is_read_and_the_correlation_id_is_not_a_fallback(self) -> None:+ """One identifies a request, the other a session. Substituting is a silent defect."""+ event = _entra(ENTRA_MFA_SIGNIN)+ assert event["session_id"] == ENTRA_MFA_SIGNIN["sessionId"]+ assert event["correlation_id"] == ENTRA_MFA_SIGNIN["correlationId"]++ without = {k: v for k, v in ENTRA_MFA_SIGNIN.items() if k != "sessionId"}+ degraded = _entra(without)+ assert degraded["session_id"] is None, "a correlation id standing in as a session id would make every sign-in its own session"+ assert degraded["correlation_id"] == ENTRA_MFA_SIGNIN["correlationId"]++ def test_graph_states_the_authentication_requirement_so_nothing_is_inferred(self) -> None:+ assert _entra(ENTRA_MFA_SIGNIN)["mfa_satisfied"] is True+ assert _entra(ENTRA_REPLAYED)["mfa_satisfied"] is False++ def test_an_absent_requirement_is_none_rather_than_single_factor(self) -> None:+ """An older API version must not make every sign-in look unchallenged."""+ without = {k: v for k, v in ENTRA_MFA_SIGNIN.items() if k != "authenticationRequirement"}+ assert _entra(without)["mfa_satisfied"] is None++ def test_the_replay_differs_on_network_and_client(self) -> None:+ first, second = _entra(ENTRA_MFA_SIGNIN), _entra(ENTRA_REPLAYED)+ assert first["session_id"] == second["session_id"]+ assert first["src_asn"] != second["src_asn"]+ assert first["client_browser"] != second["client_browser"]+ assert first["src_country"] != second["src_country"]++ @pytest.mark.parametrize("field", ["session_id", "src_asn", "client_browser", "src_country", "mfa_satisfied"])+ def test_both_vendors_use_one_field_name_per_concept(self, field: str) -> None:+ """Otherwise the rule needs a vendor clause, which is the thing normalisation is for."""+ assert field in _okta(OKTA_MFA_SIGNIN)+ assert field in _entra(ENTRA_MFA_SIGNIN)diff --git a/services/connectors/tests/test_okta_outcome_reaches_detection.py b/services/connectors/tests/test_okta_outcome_reaches_detection.pyindex 1575675ec..04b1e5f16 100644--- a/services/connectors/tests/test_okta_outcome_reaches_detection.py+++ b/services/connectors/tests/test_okta_outcome_reaches_detection.py@@ -115,6 +115,26 @@ class TestNothingElseChanged:"event_type": "user.session.start","outcome": "FAILURE","outcome_reason": "INVALID_CREDENTIALS",+ # Depth 7.3 added these. The fixture above is a trimmed row that+ # carries none of them, so every one is None -- which is the+ # right answer and is pinned here deliberately: a connector that+ # started inventing a session id for an event that has none+ # would red this test, and an AiTM rule grouping by an invented+ # id would group unrelated events together.+ "session_id": None,+ "authentication_step": None,+ "src_asn": None,+ "src_asn_org": None,+ "src_isp": None,+ "is_proxy": None,+ "src_country": None,+ "user_agent": None,+ "client_browser": None,+ "client_os": None,+ # None rather than False: this event says nothing about MFA, and+ # a detection reading False would treat silence as a negative+ # result.+ "mfa_satisfied": None,"raw_event": OKTA_FAILED_LOGIN,"created_at": "2026-10-10T09:15:00.000Z",}diff --git a/services/fusion/app/data/windowed_ruleset.json b/services/fusion/app/data/windowed_ruleset.jsonindex 98c4c49d9..355b8cea7 100644--- a/services/fusion/app/data/windowed_ruleset.json+++ b/services/fusion/app/data/windowed_ruleset.json@@ -1,5 +1,5 @@{- "count": 72,+ "count": 76,"rules": [{"category": "application",@@ -290,6 +290,77 @@"threshold": 100,"window_seconds": 120},+ {+ "category": "identity",+ "distinct_by": "src_asn",+ "group_by": "session_id",+ "id": "wd-aitm-session-replayed-from-new-asn",+ "match_when": {+ "session_id_neq": null+ },+ "mitre": [+ "T1539",+ "T1557"+ ],+ "name": "One session used from two different networks",+ "severity": "high",+ "threshold": 2,+ "window_seconds": 3600+ },+ {+ "category": "identity",+ "distinct_by": "client_browser",+ "group_by": "session_id",+ "id": "wd-aitm-session-replayed-from-new-client",+ "match_when": {+ "session_id_neq": null+ },+ "mitre": [+ "T1539",+ "T1550.004"+ ],+ "name": "One session used from two different client families",+ "severity": "high",+ "threshold": 2,+ "window_seconds": 3600+ },+ {+ "category": "identity",+ "distinct_by": "src_country",+ "group_by": "session_id",+ "id": "wd-aitm-session-left-the-country-it-started-in",+ "match_when": {+ "session_id_neq": null+ },+ "mitre": [+ "T1539"+ ],+ "name": "One session used from two different countries",+ "severity": "high",+ "threshold": 2,+ "window_seconds": 3600+ },+ {+ "category": "identity",+ "group_by": "session_id",+ "id": "wd-aitm-mfa-then-session-from-new-asn",+ "mitre": [+ "T1557",+ "T1539"+ ],+ "name": "An MFA-satisfied sign-in, then the same session used from another network",+ "ordered": true,+ "sequence": [+ {+ "mfa_satisfied": true+ },+ {+ "session_id_neq": null+ }+ ],+ "severity": "critical",+ "window_seconds": 1800+ },{"category": "application","group_by": "src_ip",diff --git a/services/fusion/tests/test_aitm_session_rules.py b/services/fusion/tests/test_aitm_session_rules.pynew file mode 100644index 000000000..ef6786d51--- /dev/null+++ b/services/fusion/tests/test_aitm_session_rules.py@@ -0,0 +1,298 @@+"""The AiTM family fires on a replayed session and not on an ordinary one.++The engine half of depth 7.3. Its sibling,+``services/connectors/tests/connectors/test_aitm_session_fields_reach_detection.py``,+drives the real ``OktaConnector.normalize()`` and+``AzureEntraConnector.normalize()`` over the same vendor payloads and asserts+exactly the dictionaries transcribed below. The two cannot be one file:+``services/connectors`` and ``services/fusion`` both name their top-level+package ``app``, so importing one inside the other's interpreter shadows it.++What this drives+----------------+The **real** ``WindowedDetectionEngine``, the **real** matcher, and the+**shipped** ruleset loaded off disk — not a hand-made rule list. A test that+declares its own copy of the rule proves the engine works and says nothing+about what the product ships.++Why the fixtures are hand-authored+-----------------------------------+Around 600 of the corpus's fixtures are synthesised from the rule they test,+which makes replay tautological: the fixture matches because the clause was+copied into it, and an unreachable field is invisible. These started as an+Okta System Log row and a Microsoft Graph ``signIn`` resource, and the rules+were written against what those carry.++The negative fixture inverts the clause each rule is **about** — the distinct+network, the distinct client family, the distinct country — and nothing else.+Same session, same user, same event type, same outcome. A rule that had+secretly keyed on any of those would still fire on it, which is the point of+holding them constant.+"""++from __future__ import annotations++import json+from pathlib import Path+from typing import Any+from uuid import uuid4++import pytest+from app.services.windowed_detection import (+ WindowedDetectionEngine,+ load_sequence_rules,+ load_window_rules,+)++#: Transcribed from what the connector emits. A transcription rather than a+#: reimplementation: the connector's own test asserts this exact mapping+#: against the real class, so a connector change that drops a field reds that+#: test rather than silently passing this one.+_SESSION = "102AbCdEfGhIjKlMnOpQrSt"+_VICTIM = "[email protected]"++MFA_SIGNIN = {+ "source": "okta",+ "external_id": "aa000000-0000-4000-8000-000000000001",+ "title": "User login to Okta",+ "event_type": "user.session.start",+ "outcome": "SUCCESS",+ "actor_email": _VICTIM,+ "src_ip": "203.0.113.42",+ "session_id": _SESSION,+ "src_asn": 64500,+ "src_asn_org": "example telecom",+ "src_country": "United States",+ "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/141.0",+ "client_browser": "CHROME",+ "client_os": "Windows 10",+ "is_proxy": False,+ "mfa_satisfied": True,+}++REPLAYED_FROM_HOSTING = {+ **MFA_SIGNIN,+ "external_id": "aa000000-0000-4000-8000-000000000002",+ "title": "User accessing Okta admin app",+ "event_type": "user.session.access_admin_app",+ "src_ip": "198.51.100.77",+ "src_asn": 64512,+ "src_asn_org": "example hosting",+ "src_country": "Netherlands",+ "user_agent": "python-requests/2.32.0",+ "client_browser": "UNKNOWN",+ "client_os": "Unknown",+ "is_proxy": True,+ "mfa_satisfied": None,+}++#: The negative twin: the same session used again from the same network, in+#: the same browser, in the same country. This is what an ordinary second+#: request inside a session looks like, and it is the only thing that+#: separates this family from "a session was used twice".+SAME_SESSION_SAME_NETWORK = {+ **REPLAYED_FROM_HOSTING,+ "external_id": "aa000000-0000-4000-8000-000000000003",+ "src_ip": "203.0.113.51",+ "src_asn": MFA_SIGNIN["src_asn"],+ "src_asn_org": MFA_SIGNIN["src_asn_org"],+ "src_country": MFA_SIGNIN["src_country"],+ "user_agent": MFA_SIGNIN["user_agent"],+ "client_browser": MFA_SIGNIN["client_browser"],+ "client_os": MFA_SIGNIN["client_os"],+ "is_proxy": False,+}++#: A different session entirely, from the hosting provider. Proves the+#: grouping is by session and not by user: this shares the actor with the+#: positive and must not complete its sequence.+OTHER_SESSION_SAME_USER = {**REPLAYED_FROM_HOSTING, "external_id": "aa000000-0000-4000-8000-000000000004", "session_id": "999OtherSession"}++AITM_RULE_IDS = (+ "wd-aitm-session-replayed-from-new-asn",+ "wd-aitm-session-replayed-from-new-client",+ "wd-aitm-session-left-the-country-it-started-in",+ "wd-aitm-mfa-then-session-from-new-asn",+)+++class _FakeRedis:+ """The sorted-set and marker calls the engine makes, faithfully.++ A fake store, not a fake engine: every matching, grouping, windowing and+ ordering decision below is made by the real ``WindowedDetectionEngine``.+ Redis here is storage, which is why the alert-reduction gate could drive+ the real correlator without a container either.+ """++ def __init__(self) -> None:+ self.zsets: dict[str, dict[str, float]] = {}+ self.strings: dict[str, str] = {}++ async def zadd(self, key: str, mapping: dict[str, float]) -> None:+ self.zsets.setdefault(key, {}).update(mapping)++ async def zremrangebyscore(self, key: str, min_s: float, max_s: float) -> None:+ members = self.zsets.get(key, {})+ for member in [m for m, score in list(members.items()) if min_s <= score <= max_s]:+ del members[member]++ async def zcard(self, key: str) -> int:+ return len(self.zsets.get(key, {}))++ async def zrangebyscore(self, key: str, min_s: float, max_s: float, withscores: bool = False) -> list[Any]:+ items = sorted(((m, s) for m, s in self.zsets.get(key, {}).items() if min_s <= s <= max_s), key=lambda kv: (kv[1], kv[0]))+ return items if withscores else [m for m, _ in items]++ async def expire(self, key: str, ttl: int) -> None:+ return None++ async def set(self, key: str, value: str, nx: bool = False, ex: int | None = None) -> Any:+ if nx and key in self.strings:+ return None+ self.strings[key] = value+ return True+++def _shipped_engine() -> WindowedDetectionEngine:+ """The engine, loaded with what the product ships."""+ return WindowedDetectionEngine(_FakeRedis(), rules=load_window_rules(), sequences=load_sequence_rules())+++def _message(tenant: str, event: dict[str, Any], when: float) -> dict[str, Any]:+ return {+ "tenant_id": tenant,+ "ocsf_event": {"tenant_uid": tenant, "raw_data": json.dumps({**event, "event_time": when})},+ }+++async def _fired(engine: WindowedDetectionEngine, tenant: str, events: list[tuple[dict[str, Any], float]]) -> set[str]:+ hits: set[str] = set()+ for event, when in events:+ for hit in await engine.evaluate(_message(tenant, event, when)):+ hits.add(hit.rule_id)+ return hits+++class TestTheFamilyIsActuallyShipped:+ """A rule the engine does not load is a rule that detects nothing."""++ @pytest.mark.parametrize("rule_id", AITM_RULE_IDS)+ def test_each_rule_is_in_the_artefact_the_engine_loads(self, rule_id: str) -> None:+ loaded = {r.id for r in load_window_rules()} | {r.id for r in load_sequence_rules()}+ assert rule_id in loaded++ def test_the_committed_json_is_what_the_exporter_produces(self) -> None:+ """Otherwise the deployed engine and the committed corpus can diverge."""+ path = Path(__file__).resolve().parents[2] / "fusion" / "app" / "data" / "windowed_ruleset.json"+ if not path.is_file(): # pragma: no cover - layout guard+ path = Path(__file__).resolve().parents[1] / "app" / "data" / "windowed_ruleset.json"+ shipped = {str(r.get("id")) for r in json.loads(path.read_text(encoding="utf-8")).get("rules") or []}+ assert set(AITM_RULE_IDS) <= shipped++ @pytest.mark.parametrize("rule_id", AITM_RULE_IDS[:3])+ def test_the_threshold_rules_group_by_the_session(self, rule_id: str) -> None:+ rule = next(r for r in load_window_rules() if r.id == rule_id)+ assert rule.group_by == "session_id"+ assert rule.distinct_by, "a threshold over events would fire on an ordinary busy session"+ assert rule.threshold == 2, "two *different* values is the whole signal; three only catches a careless attacker"+++class TestTheReplayFires:+ @pytest.mark.asyncio+ async def test_a_session_used_from_two_networks_fires(self) -> None:+ hits = await _fired(_shipped_engine(), str(uuid4()), [(MFA_SIGNIN, 1000.0), (REPLAYED_FROM_HOSTING, 1300.0)])+ assert "wd-aitm-session-replayed-from-new-asn" in hits++ @pytest.mark.asyncio+ async def test_a_session_used_from_two_client_families_fires(self) -> None:+ hits = await _fired(_shipped_engine(), str(uuid4()), [(MFA_SIGNIN, 1000.0), (REPLAYED_FROM_HOSTING, 1300.0)])+ assert "wd-aitm-session-replayed-from-new-client" in hits++ @pytest.mark.asyncio+ async def test_a_session_used_from_two_countries_fires(self) -> None:+ hits = await _fired(_shipped_engine(), str(uuid4()), [(MFA_SIGNIN, 1000.0), (REPLAYED_FROM_HOSTING, 1300.0)])+ assert "wd-aitm-session-left-the-country-it-started-in" in hits++ @pytest.mark.asyncio+ async def test_the_mfa_then_replay_sequence_fires(self) -> None:+ """The ordering is the detection, and a threshold cannot express it."""+ hits = await _fired(_shipped_engine(), str(uuid4()), [(MFA_SIGNIN, 1000.0), (REPLAYED_FROM_HOSTING, 1300.0)])+ assert "wd-aitm-mfa-then-session-from-new-asn" in hits+++class TestTheNegativeTwinDoesNotFire:+ """Each assertion inverts the clause its rule is about, and nothing else."""++ @pytest.mark.asyncio+ async def test_the_same_session_on_the_same_network_fires_nothing(self) -> None:+ hits = await _fired(_shipped_engine(), str(uuid4()), [(MFA_SIGNIN, 1000.0), (SAME_SESSION_SAME_NETWORK, 1300.0)])+ assert hits & set(AITM_RULE_IDS[:3]) == set()++ @pytest.mark.asyncio+ async def test_one_event_alone_fires_nothing(self) -> None:+ """A single sign-in is not a replay, however unusual the network."""+ hits = await _fired(_shipped_engine(), str(uuid4()), [(REPLAYED_FROM_HOSTING, 1000.0)])+ assert hits & set(AITM_RULE_IDS) == set()++ @pytest.mark.asyncio+ async def test_two_sessions_for_one_user_are_not_one_session(self) -> None:+ """Proves the grouping is the session, not the actor they share."""+ hits = await _fired(_shipped_engine(), str(uuid4()), [(MFA_SIGNIN, 1000.0), (OTHER_SESSION_SAME_USER, 1300.0)])+ assert hits & set(AITM_RULE_IDS) == set()++ @pytest.mark.asyncio+ async def test_an_event_with_no_session_id_is_not_grouped_at_all(self) -> None:+ """``session_id_neq: None`` is the clause; without it every event would group under None."""+ no_session = {k: v for k, v in REPLAYED_FROM_HOSTING.items() if k != "session_id"}+ other = {k: v for k, v in MFA_SIGNIN.items() if k != "session_id"}+ hits = await _fired(_shipped_engine(), str(uuid4()), [(other, 1000.0), (no_session, 1300.0)])+ assert hits & set(AITM_RULE_IDS) == set()++ @pytest.mark.asyncio+ async def test_the_sequence_does_not_fire_without_the_mfa_stage(self) -> None:+ """Otherwise the sequence is its final stage with extra machinery."""+ unchallenged = {**MFA_SIGNIN, "mfa_satisfied": False}+ hits = await _fired(_shipped_engine(), str(uuid4()), [(unchallenged, 1000.0), (REPLAYED_FROM_HOSTING, 1300.0)])+ assert "wd-aitm-mfa-then-session-from-new-asn" not in hits++ @pytest.mark.asyncio+ async def test_the_sequence_does_not_fire_in_the_wrong_order(self) -> None:+ """A session used elsewhere and *then* challenged is a re-auth, not a theft."""+ hits = await _fired(_shipped_engine(), str(uuid4()), [(REPLAYED_FROM_HOSTING, 1000.0), (MFA_SIGNIN, 1300.0)])+ assert "wd-aitm-mfa-then-session-from-new-asn" not in hits++ @pytest.mark.asyncio+ async def test_a_replay_outside_the_sequence_window_does_not_fire(self) -> None:+ """Half an hour is the sequence's stated budget, measured on event time.++ Only the **sequence** rule is asserted here, and the reason is a+ property of the engine worth stating rather than working around. The+ threshold rules window by *arrival*: ``_evaluate_thresholds`` scores+ each member with ``time.time()``, while ``_evaluate_sequences`` reads+ ``parse_event_time``. So feeding two events whose ``event_time``+ fields are two hours apart does not move the threshold window at all+ — both land at "now" — and an earlier version of this test asserted+ otherwise and failed, which is how the difference was found.++ That is not a defect to patch here. Arrival-time windowing is what+ makes a threshold cheap, and a connector polling every five minutes+ delivers a burst whose arrival order is the only clock fusion has.+ It does mean the one-hour figure on the threshold rules is a budget+ on *ingestion*, not on the vendor's timestamps, and the rules say an+ hour because that is how long a session cookie stays worth replaying.+ """+ engine = _shipped_engine()+ tenant = str(uuid4())+ hits = await _fired(engine, tenant, [(MFA_SIGNIN, 1000.0), (REPLAYED_FROM_HOSTING, 1000.0 + 7200)])+ assert "wd-aitm-mfa-then-session-from-new-asn" not in hits+++class TestTenantsDoNotShareASession:+ @pytest.mark.asyncio+ async def test_one_tenants_sign_in_cannot_complete_anothers_replay(self) -> None:+ engine = _shipped_engine()+ first, second = str(uuid4()), str(uuid4())+ assert not await engine.evaluate(_message(first, MFA_SIGNIN, 1000.0))+ hits = {h.rule_id for h in await engine.evaluate(_message(second, REPLAYED_FROM_HOSTING, 1300.0))}+ assert hits & set(AITM_RULE_IDS) == set()diff --git a/services/fusion/tests/test_okta_identity_rules_are_reachable.py b/services/fusion/tests/test_okta_identity_rules_are_reachable.pyindex 3e9dc8a2a..f7e2e2a01 100644--- a/services/fusion/tests/test_okta_identity_rules_are_reachable.py+++ b/services/fusion/tests/test_okta_identity_rules_are_reachable.py@@ -80,6 +80,21 @@ def _normalised(raw: dict[str, Any]) -> dict[str, Any]:"event_type": raw.get("eventType"),"outcome": result,"outcome_reason": outcome.get("reason"),+ # Depth 7.3. The connector's own test pins the full payload; these+ # are transcribed so this file keeps matching it, and because the+ # AiTM family reads them. The trimmed fixture above carries none of+ # them, so every one is None — which the sibling test asserts is the+ # right answer for an event that has no session.+ "session_id": (raw.get("authenticationContext") or {}).get("externalSessionId"),+ "authentication_step": (raw.get("authenticationContext") or {}).get("authenticationStep"),+ "src_asn": (raw.get("securityContext") or {}).get("asNumber"),+ "src_asn_org": (raw.get("securityContext") or {}).get("asOrg"),+ "src_isp": (raw.get("securityContext") or {}).get("isp"),+ "is_proxy": (raw.get("securityContext") or {}).get("isProxy"),+ "src_country": ((raw.get("client") or {}).get("geographicalContext") or {}).get("country"),+ "user_agent": ((raw.get("client") or {}).get("userAgent") or {}).get("rawUserAgent"),+ "client_browser": ((raw.get("client") or {}).get("userAgent") or {}).get("browser"),+ "client_os": ((raw.get("client") or {}).get("userAgent") or {}).get("os"),"raw_event": raw,"created_at": raw.get("published"),}