Skip to content

armgit / AiSOC

Public Imported from GitHub
0

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.

fix(detections): fold case in the string match operators (#1281)

Beenu Aroraeee213b76328
diff --git a/scripts/generate_detections.py b/scripts/generate_detections.py
index 88e4b1e04..010ae4dfc 100644
--- a/scripts/generate_detections.py
+++ b/scripts/generate_detections.py
@@ -274,33 +274,44 @@ def _check(field: str, op: str, expected: Any, event: dict[str, Any]) -> bool:
return True
return False
+ # Prefix/suffix/substring operators fold case. The namespaces they are
+ # used on — Windows paths and process names, DNS labels, registry keys —
+ # are themselves case-insensitive, so a case-sensitive comparison is a
+ # rename away from being evaded: `PROCDUMP.EXE` ran the same binary that
+ # `procdump.exe` did and must not read as a different one. `contains_any`,
+ # `contains_all`, `has_any`, `match` and `pattern_match` already folded,
+ # so these were also the odd half of a split the rule author could not see.
+ # Equality and set membership (`eq`, `in`, `not_in`) stay exact: they carry
+ # enum and identifier values where case is meaning, not spelling.
if op == "endswith":
- return isinstance(actual, str) and actual.endswith(str(expected))
+ return isinstance(actual, str) and actual.lower().endswith(str(expected).lower())
if op == "endswith_any":
if not isinstance(expected, list) or not isinstance(actual, str):
return False
- return any(actual.endswith(str(s)) for s in expected)
+ haystack = actual.lower()
+ return any(haystack.endswith(str(s).lower()) for s in expected)
if op == "startswith":
- return isinstance(actual, str) and actual.startswith(str(expected))
+ return isinstance(actual, str) and actual.lower().startswith(str(expected).lower())
if op == "startswith_any":
if not isinstance(expected, list) or not isinstance(actual, str):
return False
- return any(actual.startswith(str(s)) for s in expected)
+ haystack = actual.lower()
+ return any(haystack.startswith(str(s).lower()) for s in expected)
if op == "not_startswith":
if not isinstance(actual, str):
return False
- return not actual.startswith(str(expected))
+ return not actual.lower().startswith(str(expected).lower())
if op == "contains":
# Single substring contains. List actual ⇒ membership; string ⇒ substring.
if isinstance(actual, list):
- return expected in actual
+ return _to_lc_str(expected) in {_to_lc_str(x) for x in actual}
if isinstance(actual, str):
- return str(expected) in actual
+ return str(expected).lower() in actual.lower()
return False
if op == "has_any":
@@ -334,7 +345,8 @@ def _check(field: str, op: str, expected: Any, event: dict[str, Any]) -> bool:
# otherwise-broad endpoint rules (e.g. browser-credential-grabber).
if not isinstance(expected, list) or not isinstance(actual, str):
return False
- return not any(actual.endswith(str(s)) for s in expected)
+ haystack = actual.lower()
+ return not any(haystack.endswith(str(s).lower()) for s in expected)
# Three shipped rules used `not_startswith_any` and it had never been an
# operator, so `path_not_startswith_any` was read as a field name. The
@@ -347,12 +359,13 @@ def _check(field: str, op: str, expected: Any, event: dict[str, Any]) -> bool:
if op == "not_startswith_any":
if not isinstance(expected, list) or not isinstance(actual, str):
return False
- return not any(actual.startswith(str(s)) for s in expected)
+ haystack = actual.lower()
+ return not any(haystack.startswith(str(s).lower()) for s in expected)
if op == "not_endswith":
if not isinstance(actual, str):
return False
- return not actual.endswith(str(expected))
+ return not actual.lower().endswith(str(expected).lower())
if op == "not_contains_any":
if not isinstance(expected, list):
diff --git a/services/fusion/app/services/detection_matcher.py b/services/fusion/app/services/detection_matcher.py
index 52b155269..3b051cf20 100644
--- a/services/fusion/app/services/detection_matcher.py
+++ b/services/fusion/app/services/detection_matcher.py
@@ -146,32 +146,43 @@ def _check(field: str, op: str, expected: Any, event: dict[str, Any]) -> bool:
return True
return False
+ # Prefix/suffix/substring operators fold case. The namespaces they are
+ # used on — Windows paths and process names, DNS labels, registry keys —
+ # are themselves case-insensitive, so a case-sensitive comparison is a
+ # rename away from being evaded: `PROCDUMP.EXE` ran the same binary that
+ # `procdump.exe` did and must not read as a different one. `contains_any`,
+ # `contains_all`, `has_any`, `match` and `pattern_match` already folded,
+ # so these were also the odd half of a split the rule author could not see.
+ # Equality and set membership (`eq`, `in`, `not_in`) stay exact: they carry
+ # enum and identifier values where case is meaning, not spelling.
if op == "endswith":
- return isinstance(actual, str) and actual.endswith(str(expected))
+ return isinstance(actual, str) and actual.lower().endswith(str(expected).lower())
if op == "endswith_any":
if not isinstance(expected, list) or not isinstance(actual, str):
return False
- return any(actual.endswith(str(s)) for s in expected)
+ haystack = actual.lower()
+ return any(haystack.endswith(str(s).lower()) for s in expected)
if op == "startswith":
- return isinstance(actual, str) and actual.startswith(str(expected))
+ return isinstance(actual, str) and actual.lower().startswith(str(expected).lower())
if op == "startswith_any":
if not isinstance(expected, list) or not isinstance(actual, str):
return False
- return any(actual.startswith(str(s)) for s in expected)
+ haystack = actual.lower()
+ return any(haystack.startswith(str(s).lower()) for s in expected)
if op == "not_startswith":
if not isinstance(actual, str):
return False
- return not actual.startswith(str(expected))
+ return not actual.lower().startswith(str(expected).lower())
if op == "contains":
if isinstance(actual, list):
- return expected in actual
+ return _to_lc_str(expected) in {_to_lc_str(x) for x in actual}
if isinstance(actual, str):
- return str(expected) in actual
+ return str(expected).lower() in actual.lower()
return False
if op == "has_any":
@@ -199,7 +210,8 @@ def _check(field: str, op: str, expected: Any, event: dict[str, Any]) -> bool:
if op == "not_endswith_any":
if not isinstance(expected, list) or not isinstance(actual, str):
return False
- return not any(actual.endswith(str(s)) for s in expected)
+ haystack = actual.lower()
+ return not any(haystack.endswith(str(s).lower()) for s in expected)
# Three shipped rules used `not_startswith_any` and it had never been an
# operator, so `path_not_startswith_any` was read as a field name. The
@@ -213,12 +225,13 @@ def _check(field: str, op: str, expected: Any, event: dict[str, Any]) -> bool:
if op == "not_startswith_any":
if not isinstance(expected, list) or not isinstance(actual, str):
return False
- return not any(actual.startswith(str(s)) for s in expected)
+ haystack = actual.lower()
+ return not any(haystack.startswith(str(s).lower()) for s in expected)
if op == "not_endswith":
if not isinstance(actual, str):
return False
- return not actual.endswith(str(expected))
+ return not actual.lower().endswith(str(expected).lower())
if op == "not_contains_any":
if not isinstance(expected, list):
diff --git a/services/fusion/tests/test_detection_matcher_case_folding.py b/services/fusion/tests/test_detection_matcher_case_folding.py
new file mode 100644
index 000000000..f2b4b432b
--- /dev/null
+++ b/services/fusion/tests/test_detection_matcher_case_folding.py
@@ -0,0 +1,101 @@
+"""Case folding in the string operators (issue #1240).
+
+Windows paths and process names, DNS labels and registry keys are all
+case-insensitive namespaces. A rule written against one of them with a
+case-sensitive operator is evaded by renaming the case, which costs an
+attacker nothing: `PROCDUMP64.EXE -ma lsass.exe` ran the same binary
+`procdump64.exe` did.
+
+Half the string operators already folded (`contains_any`, `contains_all`,
+`has_any`, `match`, `pattern_match`) and half did not, so this was also a
+split a rule author had no way to see from the condition they wrote. These
+tests pin both halves of the resulting invariant: the prefix/suffix/substring
+operators fold, and `eq` / `in` / `not_in` do not.
+"""
+
+from __future__ import annotations
+
+import json
+from pathlib import Path
+
+import pytest
+from app.services.derived_fields import enrich, requested_derived_fields
+from app.services.detection_matcher import matches
+
+_REPO = Path(__file__).resolve().parents[3]
+
+# Every operator whose comparison is a prefix, suffix or substring of a
+# string. `_split_op` takes the longest matching suffix, so a bare field
+# name plus the suffix is the condition key a rule author writes.
+_FOLDING_CASES: list[tuple[str, object, str, str, bool]] = [
+ # (op suffix, expected value, matching actual, case-flipped actual, fires?)
+ ("_endswith", "procdump.exe", "c:\\tools\\procdump.exe", "C:\\TOOLS\\PROCDUMP.EXE", True),
+ ("_endswith_any", ["procdump.exe", "mimikatz.exe"], "x\\procdump.exe", "X\\PROCDUMP.EXE", True),
+ ("_startswith", "c:\\users\\", "c:\\users\\bob", "C:\\Users\\Bob", True),
+ ("_startswith_any", ["c:\\users\\"], "c:\\users\\bob", "C:\\Users\\Bob", True),
+ ("_contains", "lsass", "dump lsass now", "DUMP LSASS NOW", True),
+ # Negations are the same comparison with the verdict inverted: an
+ # exclusion must also survive a case rename, or the carve-out it
+ # encodes stops applying to the very name it was written for.
+ ("_not_endswith", "procdump.exe", "c:\\tools\\procdump.exe", "C:\\TOOLS\\PROCDUMP.EXE", False),
+ ("_not_endswith_any", ["procdump.exe"], "x\\procdump.exe", "X\\PROCDUMP.EXE", False),
+ ("_not_startswith", "c:\\windows\\", "c:\\windows\\x", "C:\\Windows\\X", False),
+ ("_not_startswith_any", ["c:\\windows\\"], "c:\\windows\\x", "C:\\Windows\\X", False),
+]
+
+
[email protected](("suffix", "expected", "actual", "flipped", "fires"), _FOLDING_CASES)
+def test_string_operators_fold_case(suffix: str, expected: object, actual: str, flipped: str, *, fires: bool) -> None:
+ clause = {f"process_name{suffix}": expected}
+ assert matches(clause, {"process_name": actual}) is fires
+ assert matches(clause, {"process_name": flipped}) is fires, (
+ f"{suffix} did not fold case: flipping the case of {actual!r} changed the verdict"
+ )
+
+
+def test_contains_folds_case_for_list_values() -> None:
+ clause = {"tags_contains": "Admin"}
+ assert matches(clause, {"tags": ["admin", "prod"]})
+ assert matches(clause, {"tags": ["ADMIN"]})
+
+
+ ("clause", "event"),
+ [
+ ({"event_name": "CreateAccessKey"}, {"event_name": "createaccesskey"}),
+ ({"status_in": ["Success"]}, {"status": "success"}),
+ ],
+)
+def test_equality_and_membership_stay_case_sensitive(clause: dict, event: dict) -> None:
+ """`eq` / `in` carry enum and identifier values, where case is meaning.
+
+ Folding them would make `CreateAccessKey` and `createaccesskey` the same
+ API call name, which is a different claim from two spellings of one file.
+ """
+ assert not matches(clause, event)
+
+
+def test_procdump_rule_fires_on_an_uppercase_rename() -> None:
+ """The reported symptom, replayed through the shipped rule.
+
+ `win-procdump-lsass` is curated critical / T1003.001. Reading it from the
+ compiled ruleset rather than restating its condition keeps this test
+ pointed at what the engine loads, so a later edit to the rule cannot
+ leave the test passing against a condition nobody runs.
+ """
+ ruleset = _REPO / "services" / "fusion" / "app" / "data" / "detection_ruleset.json"
+ all_rules = json.loads(ruleset.read_text())["rules"]
+ match_when = next(r["match_when"] for r in all_rules if r["slug"] == "win-procdump-lsass")
+ wanted = requested_derived_fields(all_rules)
+
+ for name in ("procdump.exe", "PROCDUMP.EXE", "ProcDump.exe"):
+ event = enrich(
+ {
+ "event_id": 1,
+ "log_source": "windows_sysmon",
+ "process_name": name,
+ "process_command_line": f"{name} -accepteula -ma lsass.exe out.dmp",
+ },
+ wanted,
+ )
+ assert matches(match_when, event), f"rule missed a rename to {name!r}"