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.

109 KB
Raw Blame
"""
Detection Pack v1 - Part 3 / Endpoint (240 new specs)
=====================================================

Native endpoint detection rules covering Windows, Linux, macOS, container
hosts and mobile/EDR posture. These extend the 40 stable endpoint rules in
``detection_specs.ENDPOINT`` to a total of 280 native endpoint detections.

Authoring rules are documented in ``detection_specs_part3_helpers.py``; the
short version is "use the ``S()`` builder, supply a flat ``match_when``, and
the helper synthesises both fixtures."
"""

from __future__ import annotations

from detection_specs_part3_helpers import (  # type: ignore[import-not-found]
    FP_AUTOMATION,
    FP_BACKUP,
    FP_BREAK_GLASS,
    FP_FORENSIC,
    FP_PATCH,
    FP_PENTEST,
    FP_TUNING,
    S,
)

# ---------------------------------------------------------------------------
# Windows (90 rules)
# ---------------------------------------------------------------------------
WINDOWS_RULES: list[dict] = [
    # ---- Persistence: services, tasks, registry (20) ----
    S(
        slug="win-service-create-suspicious-binpath",
        name="Windows Service Created With Suspicious BinaryPath",
        severity="high",
        mitre=["t1543.003"],
        product="windows",
        service="services",
        when={"event_id": 7045, "service_image_path_contains_any": ["\\users\\public\\", "\\appdata\\local\\temp\\", "\\programdata\\"]},
        fp=[FP_PATCH, FP_AUTOMATION],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-service-modify-image-path",
        name="Windows Service Image Path Modified",
        severity="high",
        mitre=["t1543.003", "t1574"],
        product="windows",
        service="services",
        when={"event_id": 4697, "service_image_path_changed": True},
        fp=[FP_PATCH, "Approved binary upgrade"],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-service-svchost-loaded-suspicious-dll",
        name="Windows svchost.exe Loaded DLL From User Path",
        severity="high",
        mitre=["t1574.002"],
        product="windows",
        service="sysmon",
        when={"image_loaded_path_contains_any": ["\\users\\", "\\temp\\", "\\appdata\\"], "process_name": "svchost.exe"},
        fp=["Sanctioned 3rd-party tooling installer"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="win-task-create-from-untrusted-process",
        name="Scheduled Task Created From Office or Browser",
        severity="high",
        mitre=["t1053.005"],
        product="windows",
        service="task-scheduler",
        when={"event_id": 4698, "creator_process_endswith_any": ["winword.exe", "excel.exe", "outlook.exe", "chrome.exe", "msedge.exe"]},
        fp=[FP_AUTOMATION],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-task-runs-encoded-powershell",
        name="Scheduled Task Runs Encoded PowerShell",
        severity="high",
        mitre=["t1053.005", "t1027"],
        product="windows",
        service="task-scheduler",
        when={"event_id": 4698, "task_command_contains": "-encodedcommand"},
        fp=["Vendor agent that uses encoded payloads (allow-listed)"],
        playbook="tpl-execution",
    ),
    S(
        slug="win-runkey-write-binary-temp",
        name="Registry Run Key Points To Temp Path",
        severity="high",
        mitre=["t1547.001"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 13,
            "target_object_contains": "\\currentversion\\run",
            "value_data_contains_any": ["\\temp\\", "\\appdata\\local\\temp\\"],
        },
        fp=["Installer pre-warm (verify hash)"],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-runonceex-write",
        name="Registry RunOnceEx Key Modified",
        severity="medium",
        mitre=["t1547.001"],
        product="windows",
        service="sysmon",
        when={"event_id": 13, "target_object_contains": "\\runonceex"},
        fp=[FP_PATCH],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-image-file-execution-options-debugger",
        name="Image File Execution Options Debugger Set",
        severity="critical",
        mitre=["t1546.012"],
        product="windows",
        service="sysmon",
        when={"event_id": 13, "target_object_contains": "image file execution options", "value_name": "Debugger"},
        fp=["Authorised debugger setup (development host)"],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-appinit-dll-write",
        name="AppInit_DLLs Registry Value Modified",
        severity="high",
        mitre=["t1546.010"],
        product="windows",
        service="sysmon",
        when={"event_id": 13, "target_object_endswith": "\\appinit_dlls"},
        fp=["Endpoint protection installer"],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-winlogon-userinit-modified",
        name="Winlogon Userinit Registry Value Modified",
        severity="critical",
        mitre=["t1547.004"],
        product="windows",
        service="sysmon",
        when={"event_id": 13, "target_object_endswith_any": ["\\userinit", "\\shell"]},
        fp=["Imaging baseline (verify deployment ticket)"],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-screensaver-registry-binary",
        name="ScreenSaver Registry Path Set To Custom Binary",
        severity="medium",
        mitre=["t1546.002"],
        product="windows",
        service="sysmon",
        when={"event_id": 13, "target_object_endswith": "\\scrnsave.exe"},
        fp=["Corporate-branded screensaver via GPO"],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-bootexecute-modified",
        name="HKLM BootExecute Registry Value Modified",
        severity="critical",
        mitre=["t1547"],
        product="windows",
        service="sysmon",
        when={"event_id": 13, "target_object_endswith": "\\session manager\\bootexecute"},
        fp=[FP_PATCH],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-startup-folder-write",
        name="Startup Folder Written By Non-Installer Process",
        severity="medium",
        mitre=["t1547.001"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 11,
            "target_filename_contains": "\\start menu\\programs\\startup\\",
            "creator_process_not_endswith_any": ["msiexec.exe", "setup.exe"],
        },
        fp=["Self-published in-house tooling (allow-listed signer)"],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-logon-script-registry",
        name="Logon Script Registry Modified",
        severity="high",
        mitre=["t1037.001"],
        product="windows",
        service="sysmon",
        when={"event_id": 13, "target_object_endswith": "\\environment\\userinitmprlogonscript"},
        fp=[FP_AUTOMATION],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-com-hijack-classes-root",
        name="COM Hijack Via HKCR\\CLSID\\InprocServer32 Modification",
        severity="high",
        mitre=["t1546.015"],
        product="windows",
        service="sysmon",
        when={"event_id": 13, "target_object_contains": "\\classes\\clsid\\", "target_object_endswith": "\\inprocserver32\\(default)"},
        fp=["MSI install of legitimate COM component"],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-wmi-eventfilter-create",
        name="Permanent WMI EventFilter Created",
        severity="high",
        mitre=["t1546.003"],
        product="windows",
        service="sysmon",
        when={"event_id": 19},
        fp=["Endpoint mgmt agent uses WMI persistence (allow-listed)"],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-wmi-eventconsumer-create",
        name="Permanent WMI EventConsumer Created",
        severity="high",
        mitre=["t1546.003"],
        product="windows",
        service="sysmon",
        when={"event_id": 20},
        fp=["Endpoint mgmt agent uses WMI persistence (allow-listed)"],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-wmi-binding-create",
        name="WMI Filter-To-Consumer Binding Created",
        severity="high",
        mitre=["t1546.003"],
        product="windows",
        service="sysmon",
        when={"event_id": 21},
        fp=["Endpoint mgmt agent uses WMI persistence (allow-listed)"],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-shim-database-installed",
        name="Application Compatibility Shim Database Installed",
        severity="high",
        mitre=["t1546.011"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "sdbinst.exe", "process_command_line_contains_any": [".sdb", "/install"]},
        fp=["Microsoft compatibility tooling rolled by IT"],
        playbook="tpl-persistence",
    ),
    S(
        slug="win-print-monitor-installed",
        name="Print Monitor DLL Installed",
        severity="high",
        mitre=["t1547.010"],
        product="windows",
        service="sysmon",
        when={"event_id": 13, "target_object_contains": "\\control\\print\\monitors\\"},
        fp=["Vendor printer driver install"],
        playbook="tpl-persistence",
    ),
    # ---- Defense evasion (15) ----
    S(
        slug="win-defender-disabled-via-registry",
        name="Microsoft Defender Disabled Via Registry",
        severity="high",
        mitre=["t1562.001"],
        product="windows",
        service="sysmon",
        when={"event_id": 13, "target_object_contains": "\\windows defender\\", "value_name": "DisableAntiSpyware", "value_data": "1"},
        fp=["Conflicting AV product install"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="win-defender-realtime-monitoring-off",
        name="Defender Real-Time Monitoring Disabled",
        severity="high",
        mitre=["t1562.001"],
        product="windows",
        service="defender",
        when={"event_id": 5001},
        fp=["Conflicting AV product install"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="win-defender-exclusion-added",
        name="Microsoft Defender Path Exclusion Added",
        severity="medium",
        mitre=["t1562.001"],
        product="windows",
        service="defender",
        when={"event_name": "AddPreference", "exclusion_path_contains_any": ["\\users\\", "\\temp\\", "\\appdata\\"]},
        fp=["Approved third-party tool exclusion"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="win-amsi-provider-loaded",
        name="AMSI Provider DLL Loaded From User Path",
        severity="high",
        mitre=["t1562.001"],
        product="windows",
        service="sysmon",
        when={"event_id": 13, "target_object_endswith": "\\amsi\\providers", "value_data_contains_any": ["\\users\\", "\\temp\\"]},
        fp=[FP_PENTEST],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="win-etw-tampering-via-registry",
        name="ETW Disabled Via Registry",
        severity="high",
        mitre=["t1562.006"],
        product="windows",
        service="sysmon",
        when={"event_id": 13, "target_object_endswith": "\\autologger\\eventlog-application", "value_name": "Start", "value_data": "0"},
        fp=[FP_PATCH],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="win-eventlog-service-stopped",
        name="Windows Event Log Service Stopped",
        severity="high",
        mitre=["t1562.001"],
        product="windows",
        service="services",
        when={"event_id": 7036, "service_name": "eventlog", "service_state": "stopped"},
        fp=[FP_PATCH],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="win-application-log-cleared",
        name="Windows Application Event Log Cleared",
        severity="high",
        mitre=["t1070.001"],
        product="windows",
        service="security",
        when={"event_id": 1102, "log_name": "Application"},
        fp=["Approved log archival job"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="win-system-log-cleared",
        name="Windows System Event Log Cleared",
        severity="high",
        mitre=["t1070.001"],
        product="windows",
        service="security",
        when={"event_id": 104, "log_name": "System"},
        fp=["Approved log archival job"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="win-sysmon-config-changed",
        name="Sysmon Configuration Changed",
        severity="high",
        mitre=["t1562.001"],
        product="windows",
        service="sysmon",
        when={"event_id": 16},
        fp=[FP_TUNING, FP_AUTOMATION],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="win-uac-bypass-fodhelper",
        name="UAC Bypass Via fodhelper.exe",
        severity="high",
        mitre=["t1548.002"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "fodhelper.exe", "parent_process_endswith_any": ["powershell.exe", "cmd.exe"]},
        fp=["Microsoft Optional Features install workflow"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="win-uac-bypass-eventvwr",
        name="UAC Bypass Via eventvwr.exe",
        severity="high",
        mitre=["t1548.002"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 1,
            "process_name_endswith": "eventvwr.exe",
            "child_process_endswith_any": ["cmd.exe", "powershell.exe", "wscript.exe"],
        },
        fp=["Admin support session"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="win-uac-bypass-sdclt",
        name="UAC Bypass Via sdclt.exe",
        severity="high",
        mitre=["t1548.002"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "sdclt.exe", "child_process_endswith_any": ["cmd.exe", "powershell.exe"]},
        fp=["Backup utility usage"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="win-always-install-elevated",
        name="AlwaysInstallElevated Registry Enabled",
        severity="high",
        mitre=["t1548.002"],
        product="windows",
        service="sysmon",
        when={"event_id": 13, "target_object_endswith": "\\installer\\alwaysinstallelevated", "value_data": "1"},
        fp=[FP_AUTOMATION],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="win-bcdedit-recovery-disabled",
        name="bcdedit Used To Disable Recovery",
        severity="high",
        mitre=["t1490"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 1,
            "process_name_endswith": "bcdedit.exe",
            "process_command_line_contains_any": ["recoveryenabled no", "bootstatuspolicy ignoreallfailures"],
        },
        fp=[FP_FORENSIC],
        playbook="tpl-impact",
    ),
    S(
        slug="win-vssadmin-list-shadows-then-delete",
        name="vssadmin Used To Enumerate And Delete Shadow Copies",
        severity="critical",
        mitre=["t1490"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "vssadmin.exe", "process_command_line_contains_all": ["delete", "shadows"]},
        fp=[FP_BACKUP],
        playbook="tpl-impact",
    ),
    # ---- Execution / scripting (15) ----
    S(
        slug="win-powershell-iex-from-web",
        name="PowerShell Invoke-Expression On Web Download",
        severity="high",
        mitre=["t1059.001"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "powershell.exe", "process_command_line_contains_all": ["iex", "downloadstring"]},
        fp=[FP_AUTOMATION, "Allow-listed deployment script"],
        playbook="tpl-execution",
    ),
    S(
        slug="win-powershell-bypass-flag",
        name="PowerShell Run With ExecutionPolicy Bypass",
        severity="medium",
        mitre=["t1059.001"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 1,
            "process_name_endswith": "powershell.exe",
            "process_command_line_contains_any": ["-executionpolicy bypass", "-ep bypass"],
        },
        fp=[FP_AUTOMATION, "Sanctioned dev environment"],
        playbook="tpl-execution",
    ),
    S(
        slug="win-powershell-amsi-bypass-string",
        name="PowerShell Command Line Contains AMSI Bypass String",
        severity="high",
        mitre=["t1562.001", "t1059.001"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_command_line_contains_any": ["amsiUtils", "amsiInitFailed", "amsiContext"]},
        fp=[FP_PENTEST],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="win-mshta-running-script",
        name="mshta.exe Executed Inline Script",
        severity="high",
        mitre=["t1218.005"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 1,
            "process_name_endswith": "mshta.exe",
            "process_command_line_contains_any": ["javascript:", "vbscript:", "http"],
        },
        fp=["Vendor installer that abuses mshta (allow-listed)"],
        playbook="tpl-execution",
    ),
    S(
        slug="win-rundll32-from-temp",
        name="rundll32.exe Loaded DLL From Temp Path",
        severity="high",
        mitre=["t1218.011"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 1,
            "process_name_endswith": "rundll32.exe",
            "process_command_line_contains_any": ["\\temp\\", "\\appdata\\local\\temp"],
        },
        fp=[FP_PATCH],
        playbook="tpl-execution",
    ),
    S(
        slug="win-rundll32-no-args",
        name="rundll32.exe Started Without Arguments",
        severity="medium",
        mitre=["t1218.011"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "rundll32.exe", "process_command_line_endswith": "rundll32.exe"},
        fp=["Cobalt Strike known false positive in dev VMs"],
        playbook="tpl-execution",
    ),
    S(
        slug="win-regsvr32-scrobj",
        name="regsvr32.exe Used With scrobj.dll (Squiblydoo)",
        severity="high",
        mitre=["t1218.010"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "regsvr32.exe", "process_command_line_contains_all": ["scrobj.dll", "/i:"]},
        fp=[FP_PENTEST],
        playbook="tpl-execution",
    ),
    S(
        slug="win-wmic-process-call-create-remote",
        name="wmic process call create Used For Remote Execution",
        severity="high",
        mitre=["t1047"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "wmic.exe", "process_command_line_contains_all": ["/node:", "process call create"]},
        fp=["IT remote support tooling (allow-listed)"],
        playbook="tpl-lateral-movement",
    ),
    S(
        slug="win-cscript-from-temp",
        name="cscript.exe Executing Script From Temp",
        severity="medium",
        mitre=["t1059.005"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "cscript.exe", "process_command_line_contains_any": ["\\temp\\", "\\downloads\\"]},
        fp=[FP_AUTOMATION],
        playbook="tpl-execution",
    ),
    S(
        slug="win-bitsadmin-download",
        name="bitsadmin.exe Downloading File",
        severity="medium",
        mitre=["t1197"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "bitsadmin.exe", "process_command_line_contains_any": ["/transfer", "http"]},
        fp=["Microsoft auto-update workflow"],
        playbook="tpl-command-and-control",
    ),
    S(
        slug="win-certutil-decode",
        name="certutil.exe Used To Decode Encoded File",
        severity="high",
        mitre=["t1140"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 1,
            "process_name_endswith": "certutil.exe",
            "process_command_line_contains_any": ["-decode", "-decodehex", "-urlcache -split -f"],
        },
        fp=[FP_PATCH],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="win-certutil-download-url",
        name="certutil.exe Used To Download Remote File",
        severity="high",
        mitre=["t1105"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "certutil.exe", "process_command_line_contains": "-urlcache"},
        fp=[FP_PATCH],
        playbook="tpl-command-and-control",
    ),
    S(
        slug="win-cmstp-load-untrusted-inf",
        name="cmstp.exe Loaded INF From Suspicious Path",
        severity="high",
        mitre=["t1218.003"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "cmstp.exe", "process_command_line_contains_any": ["\\appdata\\", "\\temp\\"]},
        fp=["Microsoft Configuration Manager rollouts"],
        playbook="tpl-execution",
    ),
    S(
        slug="win-installutil-from-temp",
        name="InstallUtil.exe Run From Temp Path",
        severity="high",
        mitre=["t1218.004"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "installutil.exe", "process_command_line_contains_any": ["\\temp\\", "\\appdata\\"]},
        fp=["Approved .NET deploy"],
        playbook="tpl-execution",
    ),
    S(
        slug="win-msbuild-running-inline-task",
        name="MSBuild Running Inline Task XML From User Path",
        severity="high",
        mitre=["t1127.001"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 1,
            "process_name_endswith": "msbuild.exe",
            "process_command_line_contains_any": ["\\users\\public\\", "\\appdata\\"],
        },
        fp=["Developer build pipeline (allow-listed)"],
        playbook="tpl-execution",
    ),
    # ---- Credential access (12) ----
    S(
        slug="win-procdump-lsass",
        name="ProcDump Used Against lsass.exe",
        severity="critical",
        mitre=["t1003.001"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 1,
            "process_name_endswith_any": ["procdump.exe", "procdump64.exe", "procdump64a.exe"],
            "process_command_line_contains_any": ["lsass", "-ma lsass"],
        },
        fp=[FP_FORENSIC, "Microsoft support memory capture"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="win-comsvcs-minidump",
        name="comsvcs.dll MiniDump Of lsass",
        severity="critical",
        mitre=["t1003.001"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "rundll32.exe", "process_command_line_contains_all": ["comsvcs.dll", "minidump"]},
        fp=[FP_FORENSIC],
        playbook="tpl-credential-access",
    ),
    S(
        slug="win-mimikatz-strings-on-cli",
        name="Mimikatz Marker String In Process Command Line",
        severity="critical",
        mitre=["t1003"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_command_line_contains_any": ["sekurlsa::", "lsadump::", "kerberos::", "crypto::"]},
        fp=[FP_PENTEST],
        playbook="tpl-credential-access",
    ),
    S(
        slug="win-ntds-extract-ntdsutil",
        name="ntdsutil.exe Used To Extract NTDS",
        severity="critical",
        mitre=["t1003.003"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 1,
            "process_name_endswith": "ntdsutil.exe",
            "process_command_line_contains_any": ["ifm", "create full", "activate instance ntds"],
        },
        fp=["AD recovery procedure"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="win-vss-shadow-then-copy-ntds",
        name="VSS Shadow Created Then NTDS.dit Copied",
        severity="critical",
        mitre=["t1003.003"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 11,
            "target_filename_endswith": "ntds.dit",
            "creator_process_endswith_any": ["copy.exe", "robocopy.exe", "powershell.exe"],
        },
        fp=[FP_BACKUP],
        playbook="tpl-credential-access",
    ),
    S(
        slug="win-secretsdump-impacket",
        name="Impacket secretsdump.py Pattern Detected",
        severity="critical",
        mitre=["t1003"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_command_line_contains_any": ["secretsdump", "impacket", "sam.save", "system.save"]},
        fp=[FP_PENTEST],
        playbook="tpl-credential-access",
    ),
    S(
        slug="win-reg-save-sam",
        name="reg save Used Against SAM/SECURITY/SYSTEM",
        severity="critical",
        mitre=["t1003.002"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "reg.exe", "process_command_line_contains_all": ["save", "hklm\\sam"]},
        fp=[FP_FORENSIC],
        playbook="tpl-credential-access",
    ),
    S(
        slug="win-credential-manager-vault-dump",
        name="vaultcmd.exe Listed All Stored Credentials",
        severity="high",
        mitre=["t1555.004"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "vaultcmd.exe", "process_command_line_contains": "/listcreds"},
        fp=[FP_FORENSIC],
        playbook="tpl-credential-access",
    ),
    S(
        slug="win-dpapi-masterkey-access",
        name="DPAPI Master Key File Read From Roaming Dir",
        severity="high",
        mitre=["t1555.003"],
        product="windows",
        service="sysmon",
        when={"event_id": 11, "target_filename_contains": "\\protect\\", "creator_process_not_endswith_any": ["lsass.exe", "explorer.exe"]},
        fp=["Migration / profile fix-up tool"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="win-browser-credential-file-read",
        name="Chromium Login Data SQLite File Read",
        severity="high",
        mitre=["t1555.003"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 11,
            "target_filename_endswith": "\\login data",
            "creator_process_not_endswith_any": ["chrome.exe", "msedge.exe", "brave.exe"],
        },
        fp=["Browser sync / migration tool"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="win-dcsync-via-replication",
        name="DCSync Replication Right Used By Non-DC Account",
        severity="critical",
        mitre=["t1003.006"],
        product="windows",
        service="security",
        when={"event_id": 4662, "object_properties_contains": "1131f6aa-9c07-11d1-f79f-00c04fc2dcd2", "account_is_dc": False},
        fp=["AD migration tool with replication granted"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="win-kerberos-asrep-roast",
        name="Kerberos AS-REP Roasting Pattern",
        severity="high",
        mitre=["t1558.004"],
        product="windows",
        service="security",
        when={"event_id": 4768, "preauth_required": False, "ticket_encryption_type": "0x17"},
        fp=["Legacy app with preauth disabled (deprecated, must be tracked)"],
        playbook="tpl-credential-access",
    ),
    # ---- Discovery / lateral (12) ----
    S(
        slug="win-net-localgroup-administrators",
        name="net localgroup administrators Enumerated",
        severity="medium",
        mitre=["t1087.001"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_command_line_contains_any": ["net localgroup administrators", "net.exe localgroup administrators"]},
        fp=["IT helpdesk routine check"],
        playbook="tpl-discovery",
    ),
    S(
        slug="win-net-group-domain-admins",
        name='net group "Domain Admins" Enumerated',
        severity="high",
        mitre=["t1087.002"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_command_line_contains_all": ["net", "group", "domain admins"]},
        fp=["IT helpdesk routine check"],
        playbook="tpl-discovery",
    ),
    S(
        slug="win-nltest-domain-trusts",
        name="nltest /domain_trusts Enumerated",
        severity="medium",
        mitre=["t1482"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "nltest.exe", "process_command_line_contains": "/domain_trusts"},
        fp=["AD trust diagnostics"],
        playbook="tpl-discovery",
    ),
    S(
        slug="win-adfind-enumeration",
        name="AdFind.exe Enumeration Detected",
        severity="high",
        mitre=["t1087.002"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "adfind.exe"},
        fp=["IT diagnostic with security ticket"],
        playbook="tpl-discovery",
    ),
    S(
        slug="win-bloodhound-sharphound",
        name="SharpHound Collection Run Detected",
        severity="high",
        mitre=["t1087.002"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_command_line_contains_any": ["sharphound", "invoke-bloodhound", "bloodhound.py"]},
        fp=[FP_PENTEST],
        playbook="tpl-discovery",
    ),
    S(
        slug="win-arp-cache-enum",
        name="arp -a Enumeration By Workstation",
        severity="low",
        mitre=["t1018"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "arp.exe", "process_command_line_contains": "-a"},
        fp=["Network diagnostics"],
        playbook="tpl-discovery",
    ),
    S(
        slug="win-route-print",
        name="route print Run From Workstation",
        severity="low",
        mitre=["t1018"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "route.exe", "process_command_line_contains": "print"},
        fp=["Network diagnostics"],
        playbook="tpl-discovery",
    ),
    S(
        slug="win-net-share-enum",
        name="net view Used To Enumerate File Shares",
        severity="low",
        mitre=["t1135"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_command_line_contains_any": ["net view \\\\", "net.exe share"]},
        fp=["IT routine inventory"],
        playbook="tpl-discovery",
    ),
    S(
        slug="win-psexec-execution-host",
        name="PsExec.exe Service Created On Host",
        severity="high",
        mitre=["t1021.002"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "psexec.exe", "process_command_line_contains": "\\\\"},
        fp=["IT remote support tooling (allow-listed)"],
        playbook="tpl-lateral-movement",
    ),
    S(
        slug="win-psexesvc-service-create",
        name="PSEXESVC Service Created (Remote PsExec)",
        severity="high",
        mitre=["t1021.002"],
        product="windows",
        service="services",
        when={"event_id": 7045, "service_name_endswith": "psexesvc", "service_image_path_contains": "psexec"},
        fp=["IT remote support tooling (allow-listed)"],
        playbook="tpl-lateral-movement",
    ),
    S(
        slug="win-paexec-service-create",
        name="PAExec Service Created",
        severity="high",
        mitre=["t1021.002"],
        product="windows",
        service="services",
        when={"event_id": 7045, "service_name_contains": "paexec"},
        fp=["IT remote support tooling (allow-listed)"],
        playbook="tpl-lateral-movement",
    ),
    S(
        slug="win-winrm-shell-from-workstation",
        name="WinRM Shell From Workstation To DC",
        severity="high",
        mitre=["t1021.006"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_command_line_contains": "enter-pssession", "destination_role": "domain-controller"},
        fp=["Authorised AD admin from PAW"],
        playbook="tpl-lateral-movement",
    ),
    # ---- Process injection / impact (10) ----
    S(
        slug="win-process-hollowing-marker",
        name="Process Hollowing Marker (CreateProcess Suspended + Unmapped)",
        severity="high",
        mitre=["t1055.012"],
        product="windows",
        service="sysmon",
        when={"event_id": 8, "create_remote_thread_target_process_endswith_any": ["explorer.exe", "svchost.exe", "lsass.exe"]},
        fp=["EDR injection for protection (allow-listed)"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="win-create-remote-thread-lsass",
        name="CreateRemoteThread Targeting lsass.exe",
        severity="critical",
        mitre=["t1055.001"],
        product="windows",
        service="sysmon",
        when={"event_id": 8, "target_image_endswith": "lsass.exe"},
        fp=["EDR-allowed process injection"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="win-suspicious-thread-into-explorer",
        name="Suspicious CreateRemoteThread Into explorer.exe",
        severity="high",
        mitre=["t1055"],
        product="windows",
        service="sysmon",
        when={"event_id": 8, "target_image_endswith": "explorer.exe", "source_image_path_contains_any": ["\\temp\\", "\\appdata\\"]},
        fp=["EDR-allowed process injection"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="win-cipher-w-secure-delete",
        name="cipher.exe /w Used (Secure Delete)",
        severity="medium",
        mitre=["t1485"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "cipher.exe", "process_command_line_contains": "/w"},
        fp=["Decommission / disposal procedure"],
        playbook="tpl-impact",
    ),
    S(
        slug="win-format-com-quick",
        name="format Command Used Against Drive",
        severity="critical",
        mitre=["t1485"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "format.com", "process_command_line_contains_any": ["/q", "/p:0"]},
        fp=["Asset disposal workflow"],
        playbook="tpl-impact",
    ),
    S(
        slug="win-icacls-deny-readonly",
        name="icacls Used To Deny Access On System Path",
        severity="high",
        mitre=["t1222.001"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "icacls.exe", "process_command_line_contains_all": ["/deny", "everyone"]},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-impact",
    ),
    S(
        slug="win-takeown-system-path",
        name="takeown.exe Used Against System Path",
        severity="medium",
        mitre=["t1222.001"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "takeown.exe", "process_command_line_contains_any": ["c:\\windows\\", "system32"]},
        fp=[FP_PATCH],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="win-rundll32-shell32-controlrunDLL",
        name="rundll32.exe shell32 Control_RunDLL Used Suspiciously",
        severity="medium",
        mitre=["t1218.011"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 1,
            "process_name_endswith": "rundll32.exe",
            "process_command_line_contains_any": ["shell32.dll,control_rundll", "shell32.dll, control_rundll"],
        },
        fp=["Control panel applet legitimately invoked"],
        playbook="tpl-execution",
    ),
    S(
        slug="win-ransomware-ext-rename-burst",
        name="Burst Of File Extension Renames To Common Ransom Markers",
        severity="critical",
        mitre=["t1486"],
        product="windows",
        service="sysmon",
        when={"event_id": 11, "target_filename_endswith_any": [".locked", ".enc", ".crypt", ".lockbit", ".conti", ".royal"]},
        fp=[FP_PENTEST],
        playbook="tpl-impact",
    ),
    S(
        slug="win-shadow-copy-mass-delete",
        name="Mass Shadow Copy Deletion Via WMI",
        severity="critical",
        mitre=["t1490"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_name_endswith": "wmic.exe", "process_command_line_contains_all": ["shadowcopy", "delete"]},
        fp=[FP_BACKUP],
        playbook="tpl-impact",
    ),
    # ---- Office & living-off-the-land (6) ----
    S(
        slug="win-office-spawn-mshta",
        name="Office Application Spawned mshta.exe",
        severity="high",
        mitre=["t1218.005"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 1,
            "parent_process_endswith_any": ["winword.exe", "excel.exe", "powerpnt.exe"],
            "process_name_endswith": "mshta.exe",
        },
        fp=["Macro-enabled enterprise template (allow-listed)"],
        playbook="tpl-execution",
    ),
    S(
        slug="win-office-spawn-rundll32",
        name="Office Application Spawned rundll32.exe",
        severity="high",
        mitre=["t1218.011"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 1,
            "parent_process_endswith_any": ["winword.exe", "excel.exe", "powerpnt.exe"],
            "process_name_endswith": "rundll32.exe",
        },
        fp=["Sanctioned add-in"],
        playbook="tpl-execution",
    ),
    S(
        slug="win-onenote-spawn-cmd",
        name="OneNote Spawned cmd.exe",
        severity="high",
        mitre=["t1204.002"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 1,
            "parent_process_endswith": "onenote.exe",
            "process_name_endswith_any": ["cmd.exe", "powershell.exe", "wscript.exe"],
        },
        fp=["Sanctioned macro-enabled OneNote (rare)"],
        playbook="tpl-execution",
    ),
    S(
        slug="win-pdfreader-spawn-shell",
        name="PDF Reader Spawned Shell",
        severity="high",
        mitre=["t1204.002"],
        product="windows",
        service="sysmon",
        when={
            "event_id": 1,
            "parent_process_endswith_any": ["acrord32.exe", "acrobat.exe", "foxitreader.exe"],
            "process_name_endswith_any": ["cmd.exe", "powershell.exe"],
        },
        fp=["PDF embedding signed plug-in"],
        playbook="tpl-execution",
    ),
    S(
        slug="win-iso-mount-from-temp",
        name="ISO File Mounted From User Temp",
        severity="medium",
        mitre=["t1204.002"],
        product="windows",
        service="sysmon",
        when={"event_id": 11, "target_filename_endswith": ".iso", "target_filename_contains_any": ["\\temp\\", "\\downloads\\"]},
        fp=["Software install workflow"],
        playbook="tpl-execution",
    ),
    S(
        slug="win-lnk-file-from-removable",
        name="LNK File Executed From Removable Media",
        severity="medium",
        mitre=["t1204.002"],
        product="windows",
        service="sysmon",
        when={"event_id": 1, "process_command_line_contains_any": ["e:\\", "f:\\"], "parent_image_endswith": "explorer.exe"},
        fp=["Approved kiosk workflow"],
        playbook="tpl-execution",
    ),
]


# ---------------------------------------------------------------------------
# Linux / Unix (60 rules)
# ---------------------------------------------------------------------------
LINUX_RULES: list[dict] = [
    # ---- Persistence (15) ----
    S(
        slug="linux-systemd-service-create-untrusted",
        name="Systemd Unit File Written By Non-Package Process",
        severity="high",
        mitre=["t1543.002"],
        product="linux",
        service="auditd",
        when={
            "syscall": "openat",
            "path_contains": "/etc/systemd/system/",
            "exe_not_endswith_any": ["/dpkg", "/rpm", "/yum", "/apt-get", "/zypper"],
        },
        fp=[FP_AUTOMATION, "Configuration management agent"],
        playbook="tpl-persistence",
    ),
    S(
        slug="linux-systemd-timer-create",
        name="Systemd Timer Unit Created",
        severity="medium",
        mitre=["t1053.006"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path_contains": "/etc/systemd/system/", "path_endswith": ".timer"},
        fp=[FP_AUTOMATION],
        playbook="tpl-persistence",
    ),
    S(
        slug="linux-cron-d-write",
        name="File Written Into /etc/cron.d Outside Package Mgr",
        severity="high",
        mitre=["t1053.003"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path_contains": "/etc/cron.d/", "exe_not_endswith_any": ["/dpkg", "/rpm", "/apt-get", "/yum", "/dnf"]},
        fp=[FP_AUTOMATION],
        playbook="tpl-persistence",
    ),
    S(
        slug="linux-anacron-job-add",
        name="Anacron Job File Added",
        severity="medium",
        mitre=["t1053.003"],
        product="linux",
        service="auditd",
        when={
            "syscall": "openat",
            "path_contains_any": ["/etc/anacrontab", "/etc/cron.daily/", "/etc/cron.weekly/"],
            "exe_not_endswith_any": ["/dpkg", "/rpm"],
        },
        fp=[FP_AUTOMATION],
        playbook="tpl-persistence",
    ),
    S(
        slug="linux-at-job-create",
        name="at Job Created",
        severity="medium",
        mitre=["t1053.001"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith": "/at"},
        fp=["IT scheduled task workflow"],
        playbook="tpl-persistence",
    ),
    S(
        slug="linux-rclocal-modified",
        name="/etc/rc.local Modified",
        severity="high",
        mitre=["t1037.004"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path": "/etc/rc.local"},
        fp=["Legacy boot workflow refresh"],
        playbook="tpl-persistence",
    ),
    S(
        slug="linux-initd-script-add",
        name="/etc/init.d/ Script Added By Non-Pkg Process",
        severity="high",
        mitre=["t1037.004"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path_contains": "/etc/init.d/", "exe_not_endswith_any": ["/dpkg", "/rpm"]},
        fp=[FP_AUTOMATION],
        playbook="tpl-persistence",
    ),
    S(
        slug="linux-bashrc-modified-by-other-user",
        name="~/.bashrc Modified By Other User",
        severity="medium",
        mitre=["t1546.004"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path_endswith": "/.bashrc", "actor_uid_neq_owner_uid": True},
        fp=[FP_AUTOMATION, "Configuration mgmt managed dotfiles"],
        playbook="tpl-persistence",
    ),
    S(
        slug="linux-zshrc-modified-by-other-user",
        name="~/.zshrc Modified By Other User",
        severity="medium",
        mitre=["t1546.004"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path_endswith": "/.zshrc", "actor_uid_neq_owner_uid": True},
        fp=[FP_AUTOMATION],
        playbook="tpl-persistence",
    ),
    S(
        slug="linux-pam-d-modified",
        name="/etc/pam.d/ Configuration Modified",
        severity="high",
        mitre=["t1556.003"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path_contains": "/etc/pam.d/", "exe_not_endswith_any": ["/dpkg", "/rpm", "/yum"]},
        fp=[FP_AUTOMATION],
        playbook="tpl-credential-access",
    ),
    S(
        slug="linux-nss-module-installed",
        name="NSS Module Installed In /usr/lib",
        severity="high",
        mitre=["t1556"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path_startswith": "/usr/lib", "path_contains": "libnss_", "exe_not_endswith_any": ["/dpkg", "/rpm"]},
        fp=[FP_AUTOMATION],
        playbook="tpl-credential-access",
    ),
    S(
        slug="linux-sudoers-d-add",
        name="New File Added Under /etc/sudoers.d/",
        severity="high",
        mitre=["t1098"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path_contains": "/etc/sudoers.d/", "exe_not_endswith_any": ["/visudo", "/dpkg", "/rpm"]},
        fp=[FP_AUTOMATION],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="linux-nopasswd-sudo-line",
        name="Sudoers File Granted NOPASSWD Privilege",
        severity="high",
        mitre=["t1548.003"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path_contains_any": ["/etc/sudoers", "/etc/sudoers.d/"], "file_content_contains": "NOPASSWD"},
        fp=[FP_AUTOMATION],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="linux-authorized-keys-bulk-append",
        name="Multiple Authorized Keys Appended In Short Window",
        severity="high",
        mitre=["t1098.004"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path_endswith": "/.ssh/authorized_keys", "append_count_5min_gt": 3},
        fp=["Bulk team onboarding via Ansible"],
        playbook="tpl-persistence",
    ),
    S(
        slug="linux-ssh-config-permitrootlogin-yes",
        name="sshd_config PermitRootLogin Set To Yes",
        severity="high",
        mitre=["t1556.004"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path": "/etc/ssh/sshd_config", "value_contains": "PermitRootLogin yes"},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-credential-access",
    ),
    # ---- Defense evasion / log tampering (10) ----
    S(
        slug="linux-auditd-stopped",
        name="auditd Service Stopped",
        severity="high",
        mitre=["t1562.012"],
        product="linux",
        service="syslog",
        when={"unit": "auditd", "active_state": "inactive"},
        fp=[FP_PATCH],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="linux-auditctl-disable",
        name="auditctl Used To Disable Audit Rules",
        severity="high",
        mitre=["t1562.012"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith": "/auditctl", "argv_contains_any": ["-D", "-e 0"]},
        fp=[FP_TUNING, FP_FORENSIC],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="linux-syslog-config-removed",
        name="rsyslog Configuration File Removed",
        severity="high",
        mitre=["t1562.001"],
        product="linux",
        service="auditd",
        when={"syscall": "unlinkat", "path_contains_any": ["/etc/rsyslog.d/", "/etc/rsyslog.conf"]},
        fp=[FP_PATCH],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="linux-utmp-tampering",
        name="/var/log/wtmp Or /var/log/btmp Modified Suspiciously",
        severity="high",
        mitre=["t1070.002"],
        product="linux",
        service="auditd",
        when={
            "syscall": "openat",
            "path_contains_any": ["/var/log/wtmp", "/var/log/btmp"],
            "exe_not_endswith_any": ["/login", "/sshd", "/init"],
        },
        fp=[FP_FORENSIC],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="linux-history-cleared",
        name="Bash History File Truncated Or Removed",
        severity="medium",
        mitre=["t1070.003"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path_endswith": "/.bash_history", "flags_contains": "O_TRUNC"},
        fp=[FP_FORENSIC, "User cleared their own history (educate)"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="linux-history-disabled-export",
        name="HISTFILE Set To /dev/null",
        severity="medium",
        mitre=["t1070.003"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "argv_contains_any": ["histfile=/dev/null", "set +o history"]},
        fp=[FP_FORENSIC],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="linux-selinux-permissive",
        name="SELinux Set To Permissive",
        severity="high",
        mitre=["t1562.001"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith": "/setenforce", "argv_contains_any": ["0", "permissive"]},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="linux-apparmor-disabled",
        name="AppArmor Profile Disabled",
        severity="high",
        mitre=["t1562.001"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith_any": ["/aa-disable", "/apparmor_parser"], "argv_contains": "-R"},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="linux-iptables-flush",
        name="iptables -F Used To Flush Rules",
        severity="high",
        mitre=["t1562.004"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith": "/iptables", "argv_contains_any": ["-F", "--flush"]},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="linux-ufw-disable",
        name="ufw Firewall Disabled",
        severity="medium",
        mitre=["t1562.004"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith": "/ufw", "argv_contains": "disable"},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    # ---- Discovery / privilege (8) ----
    S(
        slug="linux-shadow-read",
        name="/etc/shadow Read By Non-Root Process",
        severity="high",
        mitre=["t1003.008"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path": "/etc/shadow", "actor_uid_neq": 0},
        fp=["Approved security scanner running with sudo"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="linux-passwd-modified",
        name="/etc/passwd Modified By Non-Pkg Process",
        severity="high",
        mitre=["t1136.001"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path": "/etc/passwd", "exe_not_endswith_any": ["/useradd", "/usermod", "/groupadd", "/userdel"]},
        fp=[FP_AUTOMATION],
        playbook="tpl-persistence",
    ),
    S(
        slug="linux-suid-binary-create",
        name="SUID Bit Set On Newly Created Binary",
        severity="high",
        mitre=["t1548.001"],
        product="linux",
        service="auditd",
        when={"syscall": "fchmod", "mode_octal_contains": "4", "path_not_startswith_any": ["/usr/bin", "/usr/sbin", "/bin", "/sbin"]},
        fp=["Approved deploy of internal tool"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="linux-guid-binary-create",
        name="SGID Bit Set On Newly Created Binary",
        severity="medium",
        mitre=["t1548.001"],
        product="linux",
        service="auditd",
        when={"syscall": "fchmod", "mode_octal_contains": "2", "path_not_startswith_any": ["/usr/bin", "/usr/sbin", "/bin", "/sbin"]},
        fp=["Approved deploy"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="linux-cap-setuid-binary",
        name="setuid Capability Granted To New Binary",
        severity="high",
        mitre=["t1548.001"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith": "/setcap", "argv_contains": "cap_setuid"},
        fp=[FP_AUTOMATION],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="linux-mass-userlist-read",
        name="/etc/passwd Read By Many Processes In Short Window",
        severity="low",
        mitre=["t1087.001"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path": "/etc/passwd", "distinct_pid_5min_gt": 50},
        fp=["Container fleet startup (allow-listed)"],
        playbook="tpl-discovery",
    ),
    S(
        slug="linux-netcat-listen",
        name="netcat Listening Mode Detected",
        severity="medium",
        mitre=["t1059.004"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith_any": ["/nc", "/ncat", "/netcat"], "argv_contains_any": ["-l", "-lvp", "--listen"]},
        fp=["Lab / test exercise"],
        playbook="tpl-command-and-control",
    ),
    S(
        slug="linux-curl-piped-bash",
        name="curl/wget Piped Directly To Shell",
        severity="medium",
        mitre=["t1059.004"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "argv_contains_any": ["curl | bash", "curl | sh", "wget -O- | bash", "wget -qO- | sh"]},
        fp=["Vendor install script (allow-listed)"],
        playbook="tpl-execution",
    ),
    # ---- Container escape & kernel (10) ----
    S(
        slug="linux-docker-socket-read-non-root",
        name="Docker Socket Accessed By Non-Root User",
        severity="high",
        mitre=["t1611"],
        product="linux",
        service="auditd",
        when={"syscall": "connect", "path": "/var/run/docker.sock", "actor_uid_neq": 0},
        fp=["docker group user (deprecated, must be ticketed)"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="linux-docker-socket-mounted-into-container",
        name="Docker Socket Mounted Into Container",
        severity="critical",
        mitre=["t1611"],
        product="linux",
        service="docker",
        when={"event_type": "container_create", "host_path": "/var/run/docker.sock"},
        fp=["Authorised CI runner pattern"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="linux-host-pid-namespace",
        name="Container Started With --pid=host",
        severity="high",
        mitre=["t1611"],
        product="linux",
        service="docker",
        when={"event_type": "container_create", "pid_mode": "host"},
        fp=["Performance-monitoring agent (allow-listed)"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="linux-host-network-namespace",
        name="Container Started With --network=host",
        severity="medium",
        mitre=["t1611"],
        product="linux",
        service="docker",
        when={"event_type": "container_create", "network_mode": "host"},
        fp=["Approved CNI sidecar"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="linux-cap-sys-admin-add",
        name="Container Granted CAP_SYS_ADMIN",
        severity="high",
        mitre=["t1611"],
        product="linux",
        service="docker",
        when={"event_type": "container_create", "added_caps_has_any": ["CAP_SYS_ADMIN", "ALL"]},
        fp=["Documented container with documented exception"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="linux-mount-procfs-rw",
        name="/proc Re-Mounted With Read-Write",
        severity="high",
        mitre=["t1611"],
        product="linux",
        service="auditd",
        when={"syscall": "mount", "target_startswith": "/proc", "options_contains": "rw"},
        fp=["Container debugging procedure"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="linux-kernel-module-load-insmod",
        name="Kernel Module Loaded With insmod",
        severity="high",
        mitre=["t1547.006"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith": "/insmod"},
        fp=[FP_PATCH],
        playbook="tpl-persistence",
    ),
    S(
        slug="linux-ebpf-prog-load-non-root",
        name="eBPF Program Loaded By Non-Root",
        severity="high",
        mitre=["t1547"],
        product="linux",
        service="auditd",
        when={"syscall": "bpf", "cmd": "BPF_PROG_LOAD", "actor_uid_neq": 0},
        fp=["Sanctioned observability agent"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="linux-ptrace-pid1",
        name="ptrace Attached To PID 1",
        severity="high",
        mitre=["t1611"],
        product="linux",
        service="auditd",
        when={"syscall": "ptrace", "target_pid": 1},
        fp=[FP_FORENSIC],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="linux-memfd-create-then-execve",
        name="memfd_create Then Executed Anonymous Memory Region",
        severity="high",
        mitre=["t1620"],
        product="linux",
        service="auditd",
        when={"syscall": "execveat", "fd_path_startswith": "/memfd:"},
        fp=["Sanctioned packer / runtime that uses memfd"],
        playbook="tpl-defense-evasion",
    ),
    # ---- Exfil / staging / impact (10) ----
    S(
        slug="linux-tar-pipe-curl",
        name="tar Output Piped To curl Upload",
        severity="high",
        mitre=["t1041"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "argv_contains_all": ["tar", "curl"]},
        fp=["Sanctioned backup script (allow-listed)"],
        playbook="tpl-exfiltration",
    ),
    S(
        slug="linux-rclone-process",
        name="rclone Binary Run On Non-Backup Host",
        severity="medium",
        mitre=["t1041"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith": "/rclone"},
        fp=[FP_BACKUP],
        playbook="tpl-exfiltration",
    ),
    S(
        slug="linux-large-stage-tmp",
        name="Large Archive Created In /tmp",
        severity="medium",
        mitre=["t1074.001"],
        product="linux",
        service="auditd",
        when={
            "syscall": "openat",
            "path_startswith": "/tmp/",
            "path_endswith_any": [".tar", ".tar.gz", ".zip", ".7z"],
            "size_bytes_gt": 524288000,
        },
        fp=["Sanctioned local backup"],
        playbook="tpl-collection",
    ),
    S(
        slug="linux-base64-decode-then-execve",
        name="base64 Decoded Then Executed In Shell",
        severity="medium",
        mitre=["t1027"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "argv_contains_any": ["base64 -d | bash", "base64 -d | sh", "echo  | base64 -d | sh"]},
        fp=[FP_PENTEST],
        playbook="tpl-execution",
    ),
    S(
        slug="linux-cryptominer-process",
        name="Likely Cryptominer Process Detected",
        severity="critical",
        mitre=["t1496"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith_any": ["/xmrig", "/minerd", "/cpuminer"]},
        fp=[FP_PENTEST, "Authorized stress-test"],
        playbook="tpl-impact",
    ),
    S(
        slug="linux-shred-on-prod-data",
        name="shred Used Against Production Data Path",
        severity="high",
        mitre=["t1485"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith": "/shred", "argv_contains_any": ["/var/lib", "/data", "/srv"]},
        fp=["Decommission workflow"],
        playbook="tpl-impact",
    ),
    S(
        slug="linux-wipe-disk-dd",
        name="dd Writing /dev/zero To Block Device",
        severity="critical",
        mitre=["t1561.002"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith": "/dd", "argv_contains_all": ["if=/dev/zero", "of=/dev/"]},
        fp=["Authorised disk wipe"],
        playbook="tpl-impact",
    ),
    S(
        slug="linux-systemctl-mass-stop",
        name="Multiple Critical Services Stopped In Quick Burst",
        severity="high",
        mitre=["t1489"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith": "/systemctl", "argv_contains_any": ["stop", "disable"], "stops_5min_gt": 5},
        fp=[FP_PATCH],
        playbook="tpl-impact",
    ),
    S(
        slug="linux-mass-rename-suspicious-ext",
        name="Mass File Rename To Ransom Marker Extension",
        severity="critical",
        mitre=["t1486"],
        product="linux",
        service="auditd",
        when={"syscall": "renameat2", "new_name_endswith_any": [".locked", ".enc", ".ryuk", ".lockbit"], "rename_count_60s_gt": 50},
        fp=[FP_PENTEST],
        playbook="tpl-impact",
    ),
    S(
        slug="linux-fileless-via-procfd",
        name="Process Executed From /proc/self/fd Anonymous Inode",
        severity="high",
        mitre=["t1620"],
        product="linux",
        service="auditd",
        when={"syscall": "execveat", "fd_path_startswith": "/proc/self/fd/"},
        fp=["Sanctioned interpreter pattern"],
        playbook="tpl-defense-evasion",
    ),
    # ---- Ingress / loaders (7) ----
    S(
        slug="linux-wget-suspicious-tld",
        name="wget Pulling From Newly Registered TLD",
        severity="medium",
        mitre=["t1105"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith_any": ["/wget", "/curl"], "argv_contains_any": [".xyz", ".top", ".click", ".support"]},
        fp=[FP_PATCH],
        playbook="tpl-command-and-control",
    ),
    S(
        slug="linux-chmod-plus-x-tmp",
        name="chmod +x Applied To File In /tmp",
        severity="medium",
        mitre=["t1222.002"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith": "/chmod", "argv_contains_all": ["+x", "/tmp/"]},
        fp=["Test/CI ephemeral helpers"],
        playbook="tpl-execution",
    ),
    S(
        slug="linux-execve-from-dev-shm",
        name="execve From /dev/shm",
        severity="high",
        mitre=["t1059.004"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_startswith": "/dev/shm/"},
        fp=["Sanctioned cluster init"],
        playbook="tpl-execution",
    ),
    S(
        slug="linux-systemd-portable-attach",
        name="systemd-portable Image Attached",
        severity="medium",
        mitre=["t1543.002"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith": "/portablectl", "argv_contains": "attach"},
        fp=["Authorised platform engineering"],
        playbook="tpl-persistence",
    ),
    S(
        slug="linux-modprobe-suspicious",
        name="modprobe Loaded Module From /tmp",
        severity="high",
        mitre=["t1547.006"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "exe_endswith": "/modprobe", "argv_contains_any": ["/tmp/", "/dev/shm/"]},
        fp=["Diagnostic kernel debug"],
        playbook="tpl-persistence",
    ),
    S(
        slug="linux-ld-preload-set-tmp",
        name="LD_PRELOAD Pointing To /tmp",
        severity="high",
        mitre=["t1574.006"],
        product="linux",
        service="auditd",
        when={"syscall": "execve", "env_ld_preload_startswith": "/tmp/"},
        fp=[FP_AUTOMATION],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="linux-yum-add-untrusted-repo",
        name="yum/dnf Added Repo From Untrusted Domain",
        severity="medium",
        mitre=["t1195.002"],
        product="linux",
        service="auditd",
        when={"syscall": "openat", "path_startswith": "/etc/yum.repos.d/", "value_contains": "baseurl=http://"},
        fp=[FP_AUTOMATION, FP_PATCH],
        playbook="tpl-supply-chain",
    ),
]


# ---------------------------------------------------------------------------
# macOS (40 rules)
# ---------------------------------------------------------------------------
MACOS_RULES: list[dict] = [
    # ---- Persistence (12) ----
    S(
        slug="macos-launchdaemon-write-system",
        name="LaunchDaemon Plist Written To /Library/LaunchDaemons",
        severity="high",
        mitre=["t1543.001"],
        product="macos",
        service="endpointsecurity",
        when={
            "event_type": "ES_EVENT_TYPE_NOTIFY_WRITE",
            "path_contains": "/Library/LaunchDaemons/",
            "creator_signing_id_not_in": ["com.apple.installer", "com.apple.softwareupdated"],
        },
        fp=["Vendor pkg install (allow-listed)"],
        playbook="tpl-persistence",
    ),
    S(
        slug="macos-launchagent-write-user",
        name="User LaunchAgent Plist Created",
        severity="medium",
        mitre=["t1543.004"],
        product="macos",
        service="endpointsecurity",
        when={
            "event_type": "ES_EVENT_TYPE_NOTIFY_WRITE",
            "path_contains_any": ["/Users/", "/Library/LaunchAgents/"],
            "path_endswith": ".plist",
        },
        fp=["IDE / sync utility installer"],
        playbook="tpl-persistence",
    ),
    S(
        slug="macos-loginitem-add",
        name="Login Item Added Via osascript",
        severity="medium",
        mitre=["t1547.015"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/osascript", "argv_contains": "login items"},
        fp=["IT helpdesk onboarding script"],
        playbook="tpl-persistence",
    ),
    S(
        slug="macos-plistbuddy-from-non-system",
        name="PlistBuddy Used By Non-System Process",
        severity="medium",
        mitre=["t1547"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/PlistBuddy", "responsible_signing_id_not_startswith": "com.apple."},
        fp=["Approved homebrew formula"],
        playbook="tpl-persistence",
    ),
    S(
        slug="macos-emond-installed",
        name="Apple emond Rule Installed",
        severity="high",
        mitre=["t1546.014"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "ES_EVENT_TYPE_NOTIFY_WRITE", "path_contains": "/etc/emond.d/rules/"},
        fp=[FP_PATCH],
        playbook="tpl-persistence",
    ),
    S(
        slug="macos-periodic-script-modified",
        name="/private/etc/periodic Script Modified",
        severity="medium",
        mitre=["t1037"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "ES_EVENT_TYPE_NOTIFY_WRITE", "path_contains": "/private/etc/periodic/"},
        fp=[FP_PATCH],
        playbook="tpl-persistence",
    ),
    S(
        slug="macos-cron-tab-create",
        name="User crontab Created (Rare On macOS)",
        severity="medium",
        mitre=["t1053.003"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/crontab", "argv_contains": "-l"},
        fp=["Migration from Linux"],
        playbook="tpl-persistence",
    ),
    S(
        slug="macos-ssh-authorized-keys-append",
        name="~/.ssh/authorized_keys Modified",
        severity="medium",
        mitre=["t1098.004"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "ES_EVENT_TYPE_NOTIFY_WRITE", "path_endswith": "/.ssh/authorized_keys"},
        fp=[FP_AUTOMATION],
        playbook="tpl-persistence",
    ),
    S(
        slug="macos-mdm-enrollment-removed",
        name="MDM Enrollment Profile Removed",
        severity="high",
        mitre=["t1562.001"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/profiles", "argv_contains": "remove"},
        fp=["Approved decommission of asset"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="macos-config-profile-installed",
        name="Configuration Profile Installed",
        severity="medium",
        mitre=["t1078.003"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/profiles", "argv_contains": "install"},
        fp=["MDM rollout"],
        playbook="tpl-persistence",
    ),
    S(
        slug="macos-com-apple-loginitems-modified",
        name="com.apple.loginitems Modified",
        severity="medium",
        mitre=["t1547.015"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "ES_EVENT_TYPE_NOTIFY_WRITE", "path_endswith": "com.apple.backgroundtaskmanagement.plist"},
        fp=["Apple OS update"],
        playbook="tpl-persistence",
    ),
    S(
        slug="macos-app-bundle-shared",
        name="Custom .app Bundle Written To /Users/Shared",
        severity="medium",
        mitre=["t1574.002"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "ES_EVENT_TYPE_NOTIFY_WRITE", "path_startswith": "/Users/Shared/", "path_endswith": ".app/Contents/MacOS/main"},
        fp=["Multi-user enterprise app"],
        playbook="tpl-persistence",
    ),
    # ---- Defense evasion (10) ----
    S(
        slug="macos-spctl-master-disable",
        name="spctl --master-disable (Gatekeeper Off)",
        severity="high",
        mitre=["t1553.001"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/spctl", "argv_contains": "--master-disable"},
        fp=["Engineer running unsigned dev build (educate)"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="macos-csrutil-disable",
        name="csrutil disable (SIP Off)",
        severity="critical",
        mitre=["t1553"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/csrutil", "argv_contains": "disable"},
        fp=["Approved kernel-driver development workstation"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="macos-nvram-boot-args",
        name="nvram boot-args Modified",
        severity="critical",
        mitre=["t1542.001"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/nvram", "argv_contains": "boot-args="},
        fp=["Kernel driver development"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="macos-quarantine-attribute-removed",
        name="com.apple.quarantine xattr Stripped",
        severity="medium",
        mitre=["t1553.001"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/xattr", "argv_contains_all": ["-d", "com.apple.quarantine"]},
        fp=["Sanctioned dev workflow"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="macos-tcc-db-direct-write",
        name="TCC Database Modified Outside tccd",
        severity="high",
        mitre=["t1562.001"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "ES_EVENT_TYPE_NOTIFY_WRITE", "path_endswith": "/Library/Application Support/com.apple.TCC/TCC.db"},
        fp=["Apple OS update"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="macos-kextload-unsigned",
        name="kextload Of Unsigned Kernel Extension",
        severity="critical",
        mitre=["t1547.006"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/kextload", "kext_signed": False},
        fp=["Approved hardware vendor (allow-listed)"],
        playbook="tpl-persistence",
    ),
    S(
        slug="macos-xprotect-disabled",
        name="XProtect Updates Disabled",
        severity="high",
        mitre=["t1562.001"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/defaults", "argv_contains_all": ["com.apple.MRT", "DisableMRTShim"]},
        fp=[FP_PENTEST],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="macos-firewall-disabled",
        name="Application Firewall Disabled",
        severity="medium",
        mitre=["t1562.004"],
        product="macos",
        service="endpointsecurity",
        when={
            "event_type": "exec",
            "process_path_endswith": "/socketfilterfw",
            "argv_contains_any": ["--setglobalstate off", "--setblockall off"],
        },
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="macos-mas-receipt-tamper",
        name="App Store Receipt File Tampered",
        severity="medium",
        mitre=["t1562"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "ES_EVENT_TYPE_NOTIFY_WRITE", "path_endswith": "_MASReceipt/receipt"},
        fp=["MAS app reinstall"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="macos-codesign-force-bypass",
        name="codesign --force --deep Used By User",
        severity="medium",
        mitre=["t1553.002"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/codesign", "argv_contains_all": ["--force", "--deep"]},
        fp=["Engineer signing personal build"],
        playbook="tpl-defense-evasion",
    ),
    # ---- Credential / data access (8) ----
    S(
        slug="macos-security-export-keychain",
        name="security export Used Against Login Keychain",
        severity="high",
        mitre=["t1555.001"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/security", "argv_contains": "export"},
        fp=[FP_FORENSIC],
        playbook="tpl-credential-access",
    ),
    S(
        slug="macos-security-find-generic-password",
        name="security find-generic-password Used",
        severity="medium",
        mitre=["t1555.001"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/security", "argv_contains": "find-generic-password"},
        fp=["Sanctioned helper tool"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="macos-keychain-db-read-by-other-uid",
        name="login.keychain-db Read By Non-Owner UID",
        severity="high",
        mitre=["t1555.001"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "ES_EVENT_TYPE_NOTIFY_OPEN", "path_endswith": "/Keychains/login.keychain-db", "actor_uid_neq_owner_uid": True},
        fp=[FP_FORENSIC],
        playbook="tpl-credential-access",
    ),
    S(
        slug="macos-screencapture-binary",
        name="screencapture Binary Used",
        severity="low",
        mitre=["t1113"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/screencapture"},
        fp=["User-driven screenshot (very common)"],
        playbook="tpl-collection",
    ),
    S(
        slug="macos-osascript-keychain-access",
        name="osascript Used To Read Keychain",
        severity="high",
        mitre=["t1555.001"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/osascript", "argv_contains_any": ["security 2>&1", "find-generic-password"]},
        fp=[FP_AUTOMATION],
        playbook="tpl-credential-access",
    ),
    S(
        slug="macos-defaults-com-apple-loginwindow",
        name="defaults Wrote To com.apple.loginwindow",
        severity="medium",
        mitre=["t1547.005"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/defaults", "argv_contains_all": ["write", "com.apple.loginwindow"]},
        fp=["MDM rollout"],
        playbook="tpl-persistence",
    ),
    S(
        slug="macos-icloud-keychain-toggle",
        name="iCloud Keychain Sync Toggled Off",
        severity="medium",
        mitre=["t1562"],
        product="macos",
        service="endpointsecurity",
        when={
            "event_type": "exec",
            "process_path_endswith": "/defaults",
            "argv_contains_all": ["com.apple.security", "AccountSettings", "Keychain"],
        },
        fp=["Privacy compliance preference"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="macos-systemextension-load-non-apple",
        name="SystemExtension Loaded From Non-Apple Bundle",
        severity="high",
        mitre=["t1547.006"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/systemextensionsctl", "argv_contains": "install"},
        fp=["Approved security vendor agent"],
        playbook="tpl-persistence",
    ),
    # ---- Execution / loaders (10) ----
    S(
        slug="macos-osascript-do-shell-script",
        name="osascript do shell script with administrator privileges",
        severity="high",
        mitre=["t1059.002", "t1548.004"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/osascript", "argv_contains": "do shell script"},
        fp=[FP_AUTOMATION],
        playbook="tpl-execution",
    ),
    S(
        slug="macos-launchctl-load-from-tmp",
        name="launchctl Loading Plist From /tmp",
        severity="high",
        mitre=["t1543.001"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/launchctl", "argv_contains_any": ["/tmp/", "/private/tmp/"]},
        fp=["Test workflow on dev machine"],
        playbook="tpl-persistence",
    ),
    S(
        slug="macos-curl-pipe-shell",
        name="curl Output Piped To Shell",
        severity="medium",
        mitre=["t1105"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/sh", "argv_contains_any": ["curl | sh", "curl | bash"]},
        fp=["Sanctioned install scripts"],
        playbook="tpl-execution",
    ),
    S(
        slug="macos-dyld-insert-libraries",
        name="DYLD_INSERT_LIBRARIES Set To /tmp Or User Path",
        severity="high",
        mitre=["t1574.006"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "env_dyld_insert_libraries_startswith_any": ["/tmp/", "/Users/"]},
        fp=["Sanctioned dev workflow"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="macos-dock-modified",
        name="com.apple.dock Modified By Non-System Process",
        severity="low",
        mitre=["t1547.005"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "ES_EVENT_TYPE_NOTIFY_WRITE", "path_endswith": "/Preferences/com.apple.dock.plist"},
        fp=["MDM workflow"],
        playbook="tpl-persistence",
    ),
    S(
        slug="macos-systemsetup-remote-login",
        name="systemsetup Enabled Remote Login (SSH)",
        severity="medium",
        mitre=["t1021.004"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/systemsetup", "argv_contains_all": ["-setremotelogin", "on"]},
        fp=["IT remote support"],
        playbook="tpl-lateral-movement",
    ),
    S(
        slug="macos-hyperkit-launch-untrusted",
        name="hyperkit Launched From Non-Standard Path",
        severity="medium",
        mitre=["t1610"],
        product="macos",
        service="endpointsecurity",
        when={
            "event_type": "exec",
            "process_path_contains": "/hyperkit",
            "process_path_not_startswith_any": ["/Applications/", "/usr/local/bin/"],
        },
        fp=["Custom virtualisation tooling"],
        playbook="tpl-execution",
    ),
    S(
        slug="macos-binary-from-shared",
        name="Mach-O Binary Executed From /Users/Shared",
        severity="medium",
        mitre=["t1059.004"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_startswith": "/Users/Shared/"},
        fp=["Multi-user enterprise tool"],
        playbook="tpl-execution",
    ),
    S(
        slug="macos-tccutil-reset-all",
        name="tccutil reset All Subsystems",
        severity="medium",
        mitre=["t1562.001"],
        product="macos",
        service="endpointsecurity",
        when={"event_type": "exec", "process_path_endswith": "/tccutil", "argv_contains_all": ["reset", "All"]},
        fp=["Privacy reset workflow"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="macos-find-iphone-disabled",
        name="Find My Mac Disabled",
        severity="medium",
        mitre=["t1562"],
        product="macos",
        service="endpointsecurity",
        when={
            "event_type": "exec",
            "process_path_endswith": "/defaults",
            "argv_contains_all": ["com.apple.icloud", "FindMyMacEnabled", "0"],
        },
        fp=["Asset offboarding"],
        playbook="tpl-defense-evasion",
    ),
]


# ---------------------------------------------------------------------------
# Container / Kubernetes host (25 rules)
# ---------------------------------------------------------------------------
CONTAINER_RULES: list[dict] = [
    S(
        slug="container-docker-tcp-listening",
        name="Docker Daemon Listening On TCP Socket",
        severity="critical",
        mitre=["t1610"],
        product="docker",
        service="dockerd",
        when={"event_type": "daemon_listening", "address_contains": "tcp://0.0.0.0"},
        fp=["Approved CI runner pattern (must require mTLS)"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="container-image-from-public-registry",
        name="Production Container Pulled From Public Registry",
        severity="medium",
        mitre=["t1525"],
        product="docker",
        service="dockerd",
        when={"event_type": "image_pull", "image_registry_in": ["docker.io", "ghcr.io"], "environment": "prod"},
        fp=[FP_AUTOMATION],
        playbook="tpl-supply-chain",
    ),
    S(
        slug="container-image-tag-latest-prod",
        name="Container Pulled With :latest Tag In Production",
        severity="medium",
        mitre=["t1525"],
        product="docker",
        service="dockerd",
        when={"event_type": "image_pull", "image_tag": "latest", "environment": "prod"},
        fp=[FP_AUTOMATION],
        playbook="tpl-supply-chain",
    ),
    S(
        slug="container-running-as-root-prod",
        name="Container Started As UID 0 In Production",
        severity="medium",
        mitre=["t1611"],
        product="docker",
        service="dockerd",
        when={"event_type": "container_start", "user_uid": 0, "environment": "prod"},
        fp=["Approved root-required workload (allow-listed)"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="container-hostpath-mount-root",
        name="Container Mounted Host Root Filesystem",
        severity="critical",
        mitre=["t1611"],
        product="docker",
        service="dockerd",
        when={"event_type": "container_create", "host_mount_path": "/"},
        fp=["Sanctioned node-agent (allow-listed)"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="container-mount-etc",
        name="Container Mounted /etc From Host",
        severity="high",
        mitre=["t1611"],
        product="docker",
        service="dockerd",
        when={"event_type": "container_create", "host_mount_path_contains": "/etc"},
        fp=["Configuration injection pattern"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="container-readonly-fs-disabled",
        name="Container Created With ReadOnlyRootFilesystem False",
        severity="low",
        mitre=["t1611"],
        product="docker",
        service="dockerd",
        when={"event_type": "container_create", "readonly_root_fs": False, "environment": "prod"},
        fp=["Workload requires writable rootfs (allow-listed)"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="container-build-on-host",
        name="docker build Run On Production Host",
        severity="medium",
        mitre=["t1612"],
        product="docker",
        service="dockerd",
        when={"event_type": "image_build", "host_role": "production"},
        fp=["Documented bake host"],
        playbook="tpl-execution",
    ),
    S(
        slug="container-dind-detected",
        name="Docker-In-Docker Pattern Detected",
        severity="medium",
        mitre=["t1611"],
        product="docker",
        service="dockerd",
        when={"event_type": "container_create", "image_name_contains": "docker:dind"},
        fp=["Sanctioned CI runner"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="k8s-pod-hostnetwork-true",
        name="Pod Created With hostNetwork: true",
        severity="high",
        mitre=["t1611"],
        product="kubernetes",
        service="audit",
        when={"event_type": "create_pod", "host_network": True},
        fp=["Sanctioned CNI plugin"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="k8s-pod-hostpid-true",
        name="Pod Created With hostPID: true",
        severity="high",
        mitre=["t1611"],
        product="kubernetes",
        service="audit",
        when={"event_type": "create_pod", "host_pid": True},
        fp=["Sanctioned monitoring agent"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="k8s-pod-hostipc-true",
        name="Pod Created With hostIPC: true",
        severity="medium",
        mitre=["t1611"],
        product="kubernetes",
        service="audit",
        when={"event_type": "create_pod", "host_ipc": True},
        fp=["Specialised workload"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="k8s-pod-privileged",
        name="Pod Created With privileged Security Context",
        severity="critical",
        mitre=["t1611"],
        product="kubernetes",
        service="audit",
        when={"event_type": "create_pod", "container_privileged": True},
        fp=["Documented system DaemonSet"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="k8s-pod-allow-priv-esc",
        name="Pod Allows Privilege Escalation",
        severity="medium",
        mitre=["t1611"],
        product="kubernetes",
        service="audit",
        when={"event_type": "create_pod", "allow_privilege_escalation": True, "environment": "prod"},
        fp=["Workload that needs setuid binaries (rare)"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="k8s-rolebinding-cluster-admin",
        name="RoleBinding Created Bound To cluster-admin ClusterRole",
        severity="critical",
        mitre=["t1098.003"],
        product="kubernetes",
        service="audit",
        when={"event_type": "create_rolebinding", "role_ref_name": "cluster-admin"},
        fp=["Initial cluster bootstrap"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="k8s-clusterrolebinding-system-masters",
        name="ClusterRoleBinding To system:masters",
        severity="critical",
        mitre=["t1098.003"],
        product="kubernetes",
        service="audit",
        when={"event_type": "create_clusterrolebinding", "subject_group": "system:masters"},
        fp=["Initial cluster bootstrap"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="k8s-impersonate-verb",
        name="Kubernetes Verb 'impersonate' Used",
        severity="high",
        mitre=["t1078.004"],
        product="kubernetes",
        service="audit",
        when={"event_type": "audit", "verb": "impersonate"},
        fp=["Operator framework using impersonation"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="k8s-mutating-webhook-changed",
        name="MutatingWebhookConfiguration Modified",
        severity="high",
        mitre=["t1098"],
        product="kubernetes",
        service="audit",
        when={"event_type_in": ["create_mutatingwebhookconfiguration", "update_mutatingwebhookconfiguration"]},
        fp=["Approved admission controller upgrade"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="k8s-validating-webhook-changed",
        name="ValidatingWebhookConfiguration Modified",
        severity="high",
        mitre=["t1098"],
        product="kubernetes",
        service="audit",
        when={"event_type_in": ["create_validatingwebhookconfiguration", "update_validatingwebhookconfiguration"]},
        fp=["Approved admission controller upgrade"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="k8s-pod-security-disabled-namespace",
        name="Pod Security Admission Mode Disabled For Namespace",
        severity="high",
        mitre=["t1562.001"],
        product="kubernetes",
        service="audit",
        when={"event_type": "label_namespace", "label_key": "pod-security.kubernetes.io/enforce", "label_value": "privileged"},
        fp=["Sandbox namespace"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="k8s-anonymous-auth-enabled",
        name="kube-apiserver Started With --anonymous-auth=true",
        severity="critical",
        mitre=["t1078.004"],
        product="kubernetes",
        service="apiserver",
        when={"event_type": "apiserver_start", "flag_anonymous_auth": True},
        fp=["Lab cluster"],
        playbook="tpl-initial-access",
    ),
    S(
        slug="k8s-exec-into-kube-system",
        name="Exec Into Pod In kube-system Namespace",
        severity="high",
        mitre=["t1611"],
        product="kubernetes",
        service="audit",
        when={"event_type": "pod_exec", "namespace": "kube-system"},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="k8s-secret-mass-read",
        name="Many Secrets Read From Single Service Account",
        severity="high",
        mitre=["t1552.007"],
        product="kubernetes",
        service="audit",
        when={"event_type": "audit", "verb": "get", "resource": "secrets", "events_5min_gt": 30},
        fp=["Sanctioned controller (allow-listed)"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="k8s-networkpolicy-deleted-restricted-ns",
        name="NetworkPolicy Deleted In Restricted Namespace",
        severity="high",
        mitre=["t1562.004"],
        product="kubernetes",
        service="audit",
        when={"event_type": "delete_networkpolicy", "namespace_in": ["prod", "production", "default"]},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="k8s-pod-runs-cryptominer-image",
        name="Pod Created From Known Cryptominer Image Family",
        severity="critical",
        mitre=["t1496"],
        product="kubernetes",
        service="audit",
        when={"event_type": "create_pod", "image_contains_any": ["xmrig", "monero", "minerd"]},
        fp=[FP_PENTEST],
        playbook="tpl-impact",
    ),
]


# ---------------------------------------------------------------------------
# Mobile / EDR posture (15 rules)
# ---------------------------------------------------------------------------
MOBILE_RULES: list[dict] = [
    S(
        slug="mobile-ios-jailbreak-detected",
        name="iOS Jailbreak Detected By MDM",
        severity="critical",
        mitre=["t1404"],
        product="ios",
        service="mdm",
        when={"event_type": "compliance", "jailbroken": True},
        fp=["Decommissioned dev device"],
        playbook="tpl-impact",
    ),
    S(
        slug="mobile-android-root-detected",
        name="Android Root Detected By MDM",
        severity="critical",
        mitre=["t1404"],
        product="android",
        service="mdm",
        when={"event_type": "compliance", "rooted": True},
        fp=["Decommissioned dev device"],
        playbook="tpl-impact",
    ),
    S(
        slug="mobile-mdm-out-of-compliance",
        name="Device Marked Out Of MDM Compliance",
        severity="medium",
        mitre=["t1078"],
        product="mdm",
        service="compliance",
        when={"event_type": "compliance", "compliant": False},
        fp=["Pending OS patch window"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="mobile-app-sideloaded",
        name="App Sideloaded Outside Of Managed Store",
        severity="high",
        mitre=["t1659"],
        product="mobile",
        service="mdm",
        when={"event_type": "app_install", "source_in": ["unknown", "sideload", "adb"]},
        fp=["Internal beta tester"],
        playbook="tpl-execution",
    ),
    S(
        slug="mobile-mdm-removal-attempt",
        name="MDM Profile Removal Attempted",
        severity="critical",
        mitre=["t1562"],
        product="mdm",
        service="compliance",
        when={"event_type": "profile_remove_attempt"},
        fp=["Authorised offboarding"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="mobile-vpn-app-installed",
        name="VPN App Installed On Managed Device",
        severity="medium",
        mitre=["t1572"],
        product="mobile",
        service="mdm",
        when={"event_type": "app_install", "category": "vpn"},
        fp=["Approved corporate VPN"],
        playbook="tpl-command-and-control",
    ),
    S(
        slug="mobile-find-device-disabled",
        name="Find My Device Disabled",
        severity="high",
        mitre=["t1562"],
        product="mobile",
        service="mdm",
        when={"event_type": "compliance", "find_device_enabled": False},
        fp=["Privacy compliance setting (educate)"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="mobile-screen-lock-disabled",
        name="Screen Lock Disabled On Managed Device",
        severity="high",
        mitre=["t1078"],
        product="mobile",
        service="mdm",
        when={"event_type": "compliance", "screen_lock_enabled": False},
        fp=["Decommissioned device pending wipe"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="mobile-icloud-account-swap",
        name="iCloud Account Changed On Managed iOS Device",
        severity="high",
        mitre=["t1078"],
        product="ios",
        service="mdm",
        when={"event_type": "account_change", "service": "icloud"},
        fp=["Reassigned device"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="mobile-google-account-swap",
        name="Google Account Changed On Managed Android Device",
        severity="high",
        mitre=["t1078"],
        product="android",
        service="mdm",
        when={"event_type": "account_change", "service": "google"},
        fp=["Reassigned device"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="mobile-accessibility-permission-granted",
        name="Accessibility Permission Granted To Untrusted App",
        severity="high",
        mitre=["t1530"],
        product="android",
        service="mdm",
        when={"event_type": "permission_grant", "permission": "ACCESSIBILITY", "app_signed_by_known_publisher": False},
        fp=["Approved screen-reader app"],
        playbook="tpl-collection",
    ),
    S(
        slug="mobile-network-proxy-configured",
        name="Network Proxy Configured On Managed Device",
        severity="medium",
        mitre=["t1556"],
        product="mobile",
        service="mdm",
        when={"event_type": "config_change", "key": "http_proxy"},
        fp=["Corporate proxy rollout"],
        playbook="tpl-command-and-control",
    ),
    S(
        slug="mobile-developer-mode-enabled",
        name="Developer Mode Enabled On Managed Device",
        severity="medium",
        mitre=["t1078"],
        product="mobile",
        service="mdm",
        when={"event_type": "compliance", "developer_mode": True},
        fp=["Approved engineering device"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="mobile-mdm-cert-untrusted",
        name="Untrusted Root Certificate Installed On Device",
        severity="high",
        mitre=["t1553.004"],
        product="mobile",
        service="mdm",
        when={"event_type": "cert_install", "trust_level": "user_trusted"},
        fp=["Corporate proxy CA install"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="mobile-bluetooth-tethering-public",
        name="Personal Hotspot Or Bluetooth Tethering Enabled In Restricted Site",
        severity="low",
        mitre=["t1011"],
        product="mobile",
        service="mdm",
        when={"event_type": "compliance", "tethering_enabled": True, "geofence_in_restricted_zone": True},
        fp=["Field engineer in approved zone"],
        playbook="tpl-exfiltration",
    ),
]


# ---------------------------------------------------------------------------
# Host network / posture (10 rules)
# ---------------------------------------------------------------------------
HOST_NETWORK_RULES: list[dict] = [
    S(
        slug="host-firewall-disabled-managed",
        name="Host Firewall Disabled On Managed Endpoint",
        severity="high",
        mitre=["t1562.004"],
        product="endpoint",
        service="firewall",
        when={"event_type": "firewall_state_change", "state": "disabled"},
        fp=[FP_TUNING, FP_BREAK_GLASS],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="host-promiscuous-interface",
        name="Network Interface Set To Promiscuous Mode",
        severity="high",
        mitre=["t1040"],
        product="endpoint",
        service="agent",
        when={"event_type": "interface_state", "promiscuous": True},
        fp=["Sanctioned packet capture"],
        playbook="tpl-collection",
    ),
    S(
        slug="host-arp-poison-detected",
        name="ARP Cache Poisoning Indicators On Host",
        severity="high",
        mitre=["t1557.002"],
        product="endpoint",
        service="agent",
        when={"event_type": "arp_anomaly", "duplicate_macs_for_gateway": True},
        fp=["Failover NIC"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="host-llmnr-replies-spoofed",
        name="Suspicious LLMNR Reply Pattern (Responder-Like)",
        severity="high",
        mitre=["t1557.001"],
        product="endpoint",
        service="agent",
        when={"event_type": "llmnr", "self_response_for_external_query": True},
        fp=["Lab environment"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="host-wpad-probe",
        name="WPAD Probe Reaching External Resolver",
        severity="medium",
        mitre=["t1557.002"],
        product="endpoint",
        service="dns",
        when={"event_type": "dns_query", "query": "wpad.local", "external_resolver": True},
        fp=["Misconfigured DHCP"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="host-rogue-dhcp-host",
        name="Host Advertising As DHCP Server",
        severity="critical",
        mitre=["t1557"],
        product="endpoint",
        service="agent",
        when={"event_type": "dhcp_offer_outbound"},
        fp=["Approved DHCP failover"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="host-mdns-suspicious-tld",
        name="mDNS Query For Non-.local Suffix",
        severity="low",
        mitre=["t1018"],
        product="endpoint",
        service="dns",
        when={"event_type": "mdns_query", "query_endswith_any": [".com", ".net", ".io"]},
        fp=["Misbehaving consumer-grade IoT device"],
        playbook="tpl-discovery",
    ),
    S(
        slug="host-upnp-from-server",
        name="UPnP/SSDP Beacons From Server Subnet",
        severity="medium",
        mitre=["t1018"],
        product="endpoint",
        service="agent",
        when={"event_type": "ssdp_egress", "source_subnet_role": "server"},
        fp=["Approved IoT lab"],
        playbook="tpl-discovery",
    ),
    S(
        slug="host-rdp-mitm-cert-mismatch",
        name="RDP Client Saw Certificate Mismatch",
        severity="high",
        mitre=["t1557.002"],
        product="endpoint",
        service="rdp",
        when={"event_type": "rdp_cert_mismatch"},
        fp=["Newly imaged target machine"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="host-sccm-client-untrusted",
        name="SCCM Client Installed From Untrusted Source",
        severity="medium",
        mitre=["t1195.002"],
        product="endpoint",
        service="agent",
        when={"event_type": "agent_install", "agent": "sccm", "source_signed_by_known_publisher": False},
        fp=["Migration to new MECM tenant"],
        playbook="tpl-supply-chain",
    ),
]


# ---------------------------------------------------------------------------
# osquery-native detections (migrated from splunk-imports/_quarantine/)
# IDs: det-endpoint-281 … det-endpoint-296
# ---------------------------------------------------------------------------
OSQUERY_RULES: list[dict] = [
    # 281 ── AMoS Stealer VM Check
    S(
        slug="osquery-macos-amos-stealer-vm-check",
        name="AMoS Stealer Virtual Machine Detection via osquery",
        severity="high",
        mitre=["t1059.002"],
        product="osquery",
        service="processes",
        when={
            "event_type": "osquery_query_row",
            "cmdline_contains_all": ["osascript", "system_profiler"],
            "cmdline_contains_any": ["VMware", "QEMU", "VirtualBox"],
        },
        fp=["Admin/developer running virtual machine checks (allow-listed)"],
        playbook="tpl-malware-execution",
    ),
    # 282 ── macOS Data Chunking
    S(
        slug="osquery-macos-data-chunking",
        name="macOS Data Chunking via dd or split",
        severity="high",
        mitre=["t1030"],
        product="osquery",
        service="processes",
        when={
            "event_type": "osquery_query_row",
            "cmdline_contains_any": ["dd if=", "split -b"],
        },
        fp=["Legitimate backup or data-processing script (allow-listed)"],
        playbook="tpl-data-exfil",
    ),
    # 283 ── macOS Gatekeeper Bypass
    S(
        slug="osquery-macos-gatekeeper-bypass",
        name="macOS Gatekeeper Bypass Attempt",
        severity="high",
        mitre=["t1553.001"],
        product="osquery",
        service="processes",
        when={
            "event_type": "osquery_query_row",
            "cmdline_contains_any": [
                "com.apple.quarantine",
                "spctl --master-disable",
            ],
        },
        fp=["Developer removing quarantine attr for internal build (allow-listed)"],
        playbook="tpl-defense-evasion",
    ),
    # 284 ── Suspicious PlistBuddy LaunchAgent
    S(
        slug="osquery-macos-suspicious-plistbuddy",
        name="Suspicious PlistBuddy LaunchAgent Modification",
        severity="high",
        mitre=["t1543.001"],
        product="osquery",
        service="processes",
        when={
            "event_type": "osquery_query_row",
            "cmdline_contains_all": ["PlistBuddy"],
            "cmdline_contains_any": ["LaunchAgents", "RunAtLoad"],
        },
        fp=["Legitimate plist management by administrator (allow-listed)"],
        playbook="tpl-persistence",
    ),
    # 285 ── Baron Samedit CVE-2021-3156
    S(
        slug="osquery-linux-baron-samedit-cve-2021-3156",
        name="Baron Samedit CVE-2021-3156 Exploit Attempt",
        severity="medium",
        mitre=["t1068"],
        product="osquery",
        service="processes",
        when={
            "event_type": "osquery_query_row",
            "cmdline_startswith": "sudoedit -s ",
        },
        fp=["Security testing in authorised environment"],
        playbook="tpl-privilege-escalation",
    ),
    # 286 ── Linux osqueryd Service Stop (auditd)
    S(
        slug="osquery-linux-auditd-service-stop",
        name="Linux osqueryd Service Stop Detected via auditd",
        severity="high",
        mitre=["t1489"],
        product="linux",
        service="auditd",
        when={
            "event_type": "osquery_query_row",
            "cmdline_contains_any": [
                "service osqueryd stop",
                "systemctl stop osqueryd",
            ],
        },
        fp=["Admin stopping osqueryd for maintenance (allow-listed)"],
        playbook="tpl-defense-evasion",
    ),
    # 287 ── macOS Account Created
    S(
        slug="osquery-macos-account-created",
        name="macOS Local User Account Created",
        severity="medium",
        mitre=["t1136.001"],
        product="osquery",
        service="processes",
        when={
            "event_type": "osquery_query_row",
            "cmdline_contains_any": [
                "sysadminctl -addUser",
                "dscl . -create /Users/",
                "createhomedir -u ",
            ],
        },
        fp=["IT administrator provisioning new user"],
        playbook="tpl-account-anomaly",
    ),
    # 288 ── macOS Hidden File or Directory
    S(
        slug="osquery-macos-hidden-file-directory",
        name="macOS Hidden File or Directory Created",
        severity="medium",
        mitre=["t1564.001"],
        product="osquery",
        service="processes",
        when={
            "event_type": "osquery_query_row",
            "cmdline_contains_any": [
                "chflags hidden",
                "xattr -wx com.apple",
            ],
        },
        fp=["Developer setting extended attributes for application bundle"],
        playbook="tpl-defense-evasion",
    ),
    # 289 ── macOS kextload
    S(
        slug="osquery-macos-kextload",
        name="macOS Kernel Extension Loaded via kextload",
        severity="high",
        mitre=["t1543"],
        product="osquery",
        service="processes",
        when={
            "event_type": "osquery_query_row",
            "name": "kextload",
        },
        fp=["Authorised kernel extension for security or virtualisation tool"],
        playbook="tpl-persistence",
    ),
    # 290 ── macOS Keychain Dump
    S(
        slug="osquery-macos-keychain-dump",
        name="macOS Keychain Dumping Attempt",
        severity="high",
        mitre=["t1555.001"],
        product="osquery",
        service="processes",
        when={
            "event_type": "osquery_query_row",
            "cmdline_contains_any": [
                "dump-keychain -d",
                "find-certificate -p",
                "keychaindump",
                "Library/Keychains/",
            ],
        },
        fp=["Authorised credential management or backup script"],
        playbook="tpl-credential-theft",
    ),
    # 291 ── macOS System Log Removal
    S(
        slug="osquery-macos-log-removal",
        name="macOS System Log Removed or Manually Rotated",
        severity="medium",
        mitre=["t1070"],
        product="osquery",
        service="processes",
        when={
            "event_type": "osquery_query_row",
            "cmdline_contains_all": ["system.log"],
            "cmdline_contains_any": ["rm ", "truncate", "audit -n"],
        },
        fp=["Scheduled log rotation via cron or launchd"],
        playbook="tpl-defense-evasion",
    ),
    # 292 ── macOS LoginHook Persistence
    S(
        slug="osquery-macos-loginhook-persistence",
        name="macOS LoginHook Persistence via defaults write",
        severity="high",
        mitre=["t1037.002"],
        product="osquery",
        service="processes",
        when={
            "event_type": "osquery_query_row",
            "cmdline_contains_all": ["defaults", "write", "loginwindow", "LoginHook"],
        },
        fp=["Authorised enterprise login script (allow-listed)"],
        playbook="tpl-persistence",
    ),
    # 293 ── macOS LOLBin Execution
    S(
        slug="osquery-macos-lolbin-execution",
        name="macOS Suspicious Living-Off-The-Land Binary Execution",
        severity="low",
        mitre=["t1059.004"],
        product="osquery",
        service="processes",
        when={
            "event_type": "osquery_query_row",
            "cmdline_startswith_any": [
                "funzip ",
                "openssl s_client",
                "openssl base64",
                "python -c import",
            ],
        },
        fp=["Developer using openssl for TLS testing (allow-listed)"],
        playbook="tpl-execution",
    ),
    # 294 ── macOS Network Share Discovery
    S(
        slug="osquery-macos-network-share-discovery",
        name="macOS Network Share Discovery via showmount or smbutil",
        severity="low",
        mitre=["t1135"],
        product="osquery",
        service="processes",
        when={
            "event_type": "osquery_query_row",
            "name_in": ["showmount", "smbutil"],
        },
        fp=["Admin auditing network shares"],
        playbook="tpl-discovery",
    ),
    # 295 ── macOS plutil Plist Modification
    S(
        slug="osquery-macos-plutil-plist-modification",
        name="macOS Plist Modification via plutil",
        severity="medium",
        mitre=["t1647"],
        product="osquery",
        service="processes",
        when={
            "event_type": "osquery_query_row",
            "path": "/usr/bin/plutil",
        },
        fp=["Admin or developer editing plist files"],
        playbook="tpl-persistence",
    ),
    # 296 ── macOS Keyboard Event Tap
    S(
        slug="osquery-macos-keyboard-event-tap",
        name="Process Tapping macOS Keyboard Events",
        severity="high",
        mitre=["t1056.001"],
        product="osquery",
        service="es_process_events",
        when={
            "event_type": "osquery_query_row",
            "query_name_contains": "Keyboard_Event_Tap",
        },
        fp=["Authorised accessibility software or approved input manager"],
        playbook="tpl-collection",
    ),
]


# ---------------------------------------------------------------------------
# Aggregate
# ---------------------------------------------------------------------------
ENDPOINT_EXTRA: list[dict] = WINDOWS_RULES + LINUX_RULES + MACOS_RULES + CONTAINER_RULES + MOBILE_RULES + HOST_NETWORK_RULES + OSQUERY_RULES


__all__ = [
    "ENDPOINT_EXTRA",
    "WINDOWS_RULES",
    "LINUX_RULES",
    "MACOS_RULES",
    "CONTAINER_RULES",
    "MOBILE_RULES",
    "HOST_NETWORK_RULES",
    "OSQUERY_RULES",
]