|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass, field |
| 4 | +from enum import Enum |
| 5 | +from typing import Any |
| 6 | + |
| 7 | + |
| 8 | +class Severity(str, Enum): |
| 9 | + LOW = "low" |
| 10 | + MEDIUM = "medium" |
| 11 | + HIGH = "high" |
| 12 | + CRITICAL = "critical" |
| 13 | + |
| 14 | + |
| 15 | +class Tactic(str, Enum): |
| 16 | + """MITRE ATT&CK Enterprise tactics, keyed by their official ID.""" |
| 17 | + |
| 18 | + INITIAL_ACCESS = "TA0001" |
| 19 | + EXECUTION = "TA0002" |
| 20 | + PERSISTENCE = "TA0003" |
| 21 | + PRIVILEGE_ESCALATION = "TA0004" |
| 22 | + DEFENSE_EVASION = "TA0005" |
| 23 | + CREDENTIAL_ACCESS = "TA0006" |
| 24 | + DISCOVERY = "TA0007" |
| 25 | + LATERAL_MOVEMENT = "TA0008" |
| 26 | + COLLECTION = "TA0009" |
| 27 | + EXFILTRATION = "TA0010" |
| 28 | + COMMAND_AND_CONTROL = "TA0011" |
| 29 | + IMPACT = "TA0040" |
| 30 | + |
| 31 | + |
| 32 | +@dataclass |
| 33 | +class DetectionRule: |
| 34 | + """The vendor-neutral internal representation of a detection rule. |
| 35 | +
|
| 36 | + Everything downstream (Kibana JSON, Sigma YAML, the deterministic linter, |
| 37 | + the AI critic) works off this one schema, so adding a new export target |
| 38 | + or a new check never requires touching the rule authoring format. |
| 39 | + """ |
| 40 | + |
| 41 | + name: str |
| 42 | + platform: str |
| 43 | + query: str |
| 44 | + index_patterns: list[str] |
| 45 | + description: str |
| 46 | + severity: Severity |
| 47 | + risk_score: int |
| 48 | + tactics: list[Tactic] = field(default_factory=list) |
| 49 | + techniques: list[str] = field(default_factory=list) |
| 50 | + tags: list[str] = field(default_factory=list) |
| 51 | + false_positives: list[str] = field(default_factory=list) |
| 52 | + author: str = "" |
| 53 | + interval: str = "30m" |
| 54 | + lookback: str = "45m" |
| 55 | + enabled: bool = False |
| 56 | + rule_id: str | None = None |
| 57 | + |
| 58 | + def __post_init__(self) -> None: |
| 59 | + if not 0 < self.risk_score <= 100: |
| 60 | + raise ValueError(f"risk_score must be in (0, 100], got {self.risk_score}") |
| 61 | + if not self.name.strip(): |
| 62 | + raise ValueError("name must not be empty") |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def from_dict(cls, data: dict[str, Any]) -> DetectionRule: |
| 66 | + try: |
| 67 | + severity = Severity(data["severity"]) |
| 68 | + except ValueError as exc: |
| 69 | + allowed = ", ".join(s.value for s in Severity) |
| 70 | + raise ValueError(f"severity must be one of: {allowed}") from exc |
| 71 | + |
| 72 | + tactics = [] |
| 73 | + for raw in data.get("tactics", []): |
| 74 | + try: |
| 75 | + tactics.append(Tactic[str(raw).upper()]) |
| 76 | + except KeyError as exc: |
| 77 | + allowed = ", ".join(t.name for t in Tactic) |
| 78 | + raise ValueError(f"Unknown tactic {raw!r}, expected one of: {allowed}") from exc |
| 79 | + |
| 80 | + return cls( |
| 81 | + name=data["name"], |
| 82 | + platform=data["platform"], |
| 83 | + query=data["query"], |
| 84 | + index_patterns=list(data.get("index_patterns", [])), |
| 85 | + description=data.get("description", ""), |
| 86 | + severity=severity, |
| 87 | + risk_score=int(data["risk_score"]), |
| 88 | + tactics=tactics, |
| 89 | + techniques=list(data.get("techniques", [])), |
| 90 | + tags=list(data.get("tags", [])), |
| 91 | + false_positives=list(data.get("false_positives", [])), |
| 92 | + author=data.get("author", ""), |
| 93 | + interval=data.get("interval", "30m"), |
| 94 | + lookback=data.get("lookback", "45m"), |
| 95 | + enabled=bool(data.get("enabled", False)), |
| 96 | + rule_id=data.get("rule_id"), |
| 97 | + ) |
0 commit comments