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.

48.6 KB
Raw Blame
"""
Detection Pack v1 - Part 3 / Identity (120 new specs)
=====================================================

Native identity-and-access detection rules covering Okta, Auth0, Duo, Ping,
generic SAML/OIDC IdPs, AD/Kerberos, password-attack telemetry, and SaaS-side
identity events. Extends ``detection_specs.IDENTITY`` from 35 to ~155 native
identity detections.

These rules complement the cloud IAM detections in part3-cloud — anything that
is *exclusively* about an IdP (Okta admin events, AD audit log, generic
sign-in telemetry) lives here; cloud-provider IAM (AWS IAM, Entra audit,
GCP IAM) lives in cloud.
"""

from __future__ import annotations

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

# ---------------------------------------------------------------------------
# Authentication failures / brute force / credential stuffing (25)
# ---------------------------------------------------------------------------
AUTH_RULES: list[dict] = [
    S(
        slug="ident-password-spray-many-targets",
        name="Password Spray: Single IP, Many Targets, Few Tries Each",
        severity="high",
        mitre=["t1110.003"],
        product="identity",
        service="auth",
        when={"event_type": "auth_failure", "distinct_users_per_minute_gt": 30, "tries_per_user_lte": 5},
        fp=["VPN concentrator with shared egress"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-password-spray-distributed",
        name="Distributed Password Spray From Many IPs",
        severity="high",
        mitre=["t1110.003"],
        product="identity",
        service="auth",
        when={"event_type": "auth_failure", "distinct_ips_per_user_per_hour_gt": 30},
        fp=["Mobile-app users with rotating cellular IPs"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-credstuffing-known-breached-list",
        name="Credential Stuffing Pattern Against Login Endpoint",
        severity="high",
        mitre=["t1110.004"],
        product="identity",
        service="auth",
        when={"event_type": "auth_failure", "user_agent_in_known_botnet_list": True, "tries_per_minute_gt": 50},
        fp=["Load test (must be ticketed)"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-bruteforce-success-after-failures",
        name="Successful Login After 50+ Recent Failures",
        severity="high",
        mitre=["t1110.001"],
        product="identity",
        service="auth",
        when={"event_type": "auth_success", "user_failures_last_15m_gt": 50},
        fp=["Forgot-password flow", "Helpdesk reset"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-impossible-travel-15m",
        name="Impossible Travel: Two Successful Logins <15m, >1000km",
        severity="high",
        mitre=["t1078"],
        product="identity",
        service="auth",
        when={"event_type": "auth_success", "geo_distance_km_gt": 1000, "minutes_between_lt": 15},
        fp=["Corporate VPN (egress IP changes)", "Mobile-data roaming"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-tor-exit-login-success",
        name="Successful Login From Tor Exit Node",
        severity="high",
        mitre=["t1090.003"],
        product="identity",
        service="auth",
        when={"event_type": "auth_success", "src_ip_tor_exit": True},
        fp=["Privacy-conscious researcher (must be ticketed)"],
        playbook="tpl-initial-access",
    ),
    S(
        slug="ident-anon-vpn-login-priv-user",
        name="Privileged User Login From Anonymising VPN",
        severity="high",
        mitre=["t1090.003"],
        product="identity",
        service="auth",
        when={"event_type": "auth_success", "src_ip_anonymous_vpn": True, "user_priv": True},
        fp=["Approved BYOD-VPN policy"],
        playbook="tpl-initial-access",
    ),
    S(
        slug="ident-mfa-fatigue-burst",
        name="MFA Fatigue Burst Against Single User",
        severity="high",
        mitre=["t1621"],
        product="identity",
        service="auth",
        when={"event_type": "mfa_challenge", "user_challenges_per_minute_gt": 5},
        fp=["Genuine push-loop after device swap"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-mfa-deny-then-success",
        name="User Denied Push Then Approved Within 60s",
        severity="medium",
        mitre=["t1621"],
        product="identity",
        service="auth",
        when={"event_type": "mfa_state_change", "previous_state": "deny", "current_state": "approve", "seconds_between_lt": 60},
        fp=["Misclick on first prompt"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-account-locked-many-times",
        name="Account Locked Out 3+ Times In 1 Hour",
        severity="medium",
        mitre=["t1110"],
        product="identity",
        service="auth",
        when={"event_type": "account_locked", "lockouts_per_hour_gte": 3},
        fp=["Stuck mobile credential", FP_TUNING],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-disabled-user-auth-attempt",
        name="Authentication Attempt Against Disabled User",
        severity="high",
        mitre=["t1078"],
        product="identity",
        service="auth",
        when={"event_type": "auth_failure", "result": "user_disabled"},
        fp=["Lingering session token", "Failed deprovision"],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-deleted-user-auth-success",
        name="Authentication Success For Recently Deleted User",
        severity="critical",
        mitre=["t1078"],
        product="identity",
        service="auth",
        when={"event_type": "auth_success", "user_state": "deleted"},
        fp=[],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-mfa-bypassed-via-emergency-code",
        name="MFA Bypass Via Emergency / Backup Code",
        severity="high",
        mitre=["t1556.006"],
        product="identity",
        service="auth",
        when={"event_type": "auth_success", "mfa_factor": "backup_code"},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-fido2-key-removed-priv",
        name="FIDO2 Hardware Key Removed From Privileged User",
        severity="high",
        mitre=["t1556.006"],
        product="identity",
        service="mfa",
        when={"event_type": "factor_remove", "factor_type": "fido2", "user_priv": True},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-fido2-key-added-then-removed-old",
        name="FIDO2 Key Added And Old Key Removed In Same Session",
        severity="high",
        mitre=["t1556.006"],
        product="identity",
        service="mfa",
        when={"event_type": "factor_swap", "old_factor_age_days_gt": 30, "session_seconds_lt": 600},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-totp-secret-revealed",
        name="TOTP Secret QR-Code Re-Issued Without Old Factor Removal",
        severity="medium",
        mitre=["t1556"],
        product="identity",
        service="mfa",
        when={"event_type": "factor_create", "factor_type": "totp", "old_factor_removed": False},
        fp=["Helpdesk-driven recovery"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-static-password-policy-loosened",
        name="Password Policy Min-Length / Complexity Loosened",
        severity="high",
        mitre=["t1556"],
        product="identity",
        service="policy",
        when={"event_type": "policy_update", "min_length_decreased": True},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-password-policy-history-reduced",
        name="Password History Length Reduced",
        severity="medium",
        mitre=["t1556"],
        product="identity",
        service="policy",
        when={"event_type": "policy_update", "history_length_decreased": True},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-session-timeout-extended-priv",
        name="Session Timeout Extended For Privileged Group",
        severity="medium",
        mitre=["t1556"],
        product="identity",
        service="policy",
        when={"event_type": "policy_update", "session_timeout_minutes_increased": True, "scope_priv": True},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-suspicious-user-agent-login",
        name="Login With Headless / Bot User Agent",
        severity="medium",
        mitre=["t1078"],
        product="identity",
        service="auth",
        when={"event_type": "auth_success", "user_agent_contains_any": ["python-requests", "curl/", "headless", "phantomjs", "selenium"]},
        fp=["Internal API client (must be ticketed)"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-login-from-known-asn-ddos",
        name="Login From ASN Recently Seen In Credential-Stuffing Activity",
        severity="medium",
        mitre=["t1110.004"],
        product="identity",
        service="auth",
        when={"event_type": "auth_success", "src_asn_recent_credstuffing": True},
        fp=["Mobile carrier shared NAT"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-new-country-priv-user",
        name="Privileged User Login From New Country",
        severity="medium",
        mitre=["t1078"],
        product="identity",
        service="auth",
        when={"event_type": "auth_success", "user_priv": True, "country_first_seen_for_user": True},
        fp=["Authorised travel"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-rapid-language-change",
        name="User Login Locale Switched Rapidly",
        severity="low",
        mitre=["t1078"],
        product="identity",
        service="auth",
        when={"event_type": "auth_success", "locale_changes_in_session_gt": 2},
        fp=["Shared/kiosk machine"],
        playbook="tpl-discovery",
    ),
    S(
        slug="ident-empty-useragent-login",
        name="Login With Empty User-Agent",
        severity="medium",
        mitre=["t1078"],
        product="identity",
        service="auth",
        when={"event_type": "auth_success", "user_agent": ""},
        fp=["Custom internal client"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-totp-skewed-clock",
        name="TOTP Verification Repeatedly Skews Clock",
        severity="low",
        mitre=["t1110"],
        product="identity",
        service="mfa",
        when={"event_type": "mfa_challenge", "totp_clock_skew_seconds_gt": 60, "events_per_minute_gt": 5},
        fp=["Phone clock drift"],
        playbook="tpl-credential-access",
    ),
]


# ---------------------------------------------------------------------------
# Privileged-account events / role assignment / persistence (25)
# ---------------------------------------------------------------------------
PRIV_RULES: list[dict] = [
    S(
        slug="ident-priv-role-grant-out-of-hours",
        name="Privileged Role Granted Outside Business Hours",
        severity="high",
        mitre=["t1078"],
        product="identity",
        service="audit",
        when={"event_type": "role_grant", "role_priv": True, "is_business_hours": False},
        fp=[FP_BREAK_GLASS, "On-call rotation"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="ident-self-grant-priv-role",
        name="User Granted Privileged Role To Themselves",
        severity="critical",
        mitre=["t1098"],
        product="identity",
        service="audit",
        when={"event_type": "role_grant", "role_priv": True, "actor_eq_target": True},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="ident-priv-role-grant-to-svc-account",
        name="Privileged Role Granted To Service Account",
        severity="high",
        mitre=["t1078.004"],
        product="identity",
        service="audit",
        when={"event_type": "role_grant", "role_priv": True, "target_user_type": "service_account"},
        fp=["IaC pipeline"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="ident-pim-activation-self-approval",
        name="JIT/PIM Privileged Activation Self-Approved",
        severity="high",
        mitre=["t1078"],
        product="identity",
        service="pim",
        when={"event_type": "pim_activation", "approver_eq_actor": True},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="ident-pim-activation-anomalous-time",
        name="JIT/PIM Activation At Unusual Hour For User",
        severity="medium",
        mitre=["t1078"],
        product="identity",
        service="pim",
        when={"event_type": "pim_activation", "hour_unusual_for_user": True},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="ident-priv-role-burst-granted",
        name="Privileged Role Grant Burst Across Many Users",
        severity="high",
        mitre=["t1098"],
        product="identity",
        service="audit",
        when={"event_type": "role_grant", "role_priv": True, "events_per_minute_gt": 10},
        fp=["Bulk reorg (must be ticketed)"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="ident-svc-account-interactive-login",
        name="Service Account Used For Interactive Login",
        severity="high",
        mitre=["t1078.004"],
        product="identity",
        service="auth",
        when={"event_type": "auth_success", "user_type": "service_account", "is_interactive": True},
        fp=["Approved automation runner using human UI"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-svc-account-mfa-skipped",
        name="Service Account Login Without MFA In Region That Requires It",
        severity="medium",
        mitre=["t1078.004"],
        product="identity",
        service="auth",
        when={"event_type": "auth_success", "user_type": "service_account", "mfa_used": False, "policy_requires_mfa": True},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-onboard-priv-roles-immediate",
        name="New User Granted Privileged Role <24h After Creation",
        severity="high",
        mitre=["t1098"],
        product="identity",
        service="audit",
        when={"event_type": "role_grant", "role_priv": True, "user_age_minutes_lt": 1440},
        fp=[FP_ONBOARD],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="ident-app-impersonation-priv",
        name="OAuth App Acting As Privileged User",
        severity="high",
        mitre=["t1528"],
        product="identity",
        service="oauth",
        when={"event_type": "oauth_action", "act_as_user_priv": True, "scope_contains_any": ["full_access", "admin", "*.write"]},
        fp=["Approved IaaS automation"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-priv-role-removed-bulk",
        name="Privileged Role Removed From Many Users In Burst",
        severity="medium",
        mitre=["t1531"],
        product="identity",
        service="audit",
        when={"event_type": "role_revoke", "role_priv": True, "events_per_minute_gt": 10},
        fp=["Approved cleanup project"],
        playbook="tpl-impact",
    ),
    S(
        slug="ident-orphan-svc-acct-many-keys",
        name="Service Account With 5+ Active API Keys",
        severity="medium",
        mitre=["t1098.001"],
        product="identity",
        service="audit",
        when={"event_type": "key_inventory", "active_keys_per_account_gt": 5},
        fp=[FP_AUTOMATION],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-svc-account-key-from-non-pipeline",
        name="Service Account Key Used From Non-Pipeline IP",
        severity="high",
        mitre=["t1078.004"],
        product="identity",
        service="auth",
        when={"event_type": "auth_success", "user_type": "service_account", "src_in_pipeline_cidr": False},
        fp=["Approved migration"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-domain-admin-added",
        name="Domain Admin Group Membership Added (AD)",
        severity="critical",
        mitre=["t1098.007"],
        product="active-directory",
        service="audit",
        when={"event_id": 4728, "group_name_in": ["Domain Admins", "Enterprise Admins", "Schema Admins"]},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="ident-ad-acl-sensitive-changed",
        name="ACL On Sensitive AD Object Modified",
        severity="high",
        mitre=["t1484.002"],
        product="active-directory",
        service="audit",
        when={"event_id": 4670, "object_class_in": ["domainDNS", "groupPolicyContainer"]},
        fp=["Approved AD reorg"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="ident-ad-dcsync-rights-granted",
        name="DCSync Rights Granted Outside Domain Controllers",
        severity="critical",
        mitre=["t1003.006"],
        product="active-directory",
        service="audit",
        when={"event_id": 5136, "rights_added_contains_any": ["DS-Replication-Get-Changes", "DS-Replication-Get-Changes-All"]},
        fp=["Approved Azure AD Connect / sync engine"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-ad-gpo-modified-priv",
        name="Privileged GPO Modified",
        severity="high",
        mitre=["t1484.001"],
        product="active-directory",
        service="audit",
        when={"event_id": 5136, "gpo_link_priv": True},
        fp=["Approved GPO refactor"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-ad-trust-modified",
        name="AD Domain Trust Modified",
        severity="critical",
        mitre=["t1484.002"],
        product="active-directory",
        service="audit",
        when={"event_id_in": [4716, 4865, 4867]},
        fp=["Approved forest merger"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="ident-ad-userauth-anonymous-named-pipe",
        name="AD Anonymous Logon Allowed Via Null Session",
        severity="high",
        mitre=["t1078.001"],
        product="active-directory",
        service="auth",
        when={"event_id": 4624, "logon_type": 3, "user_name": "ANONYMOUS LOGON"},
        fp=["Legacy app (must be ticketed)"],
        playbook="tpl-initial-access",
    ),
    S(
        slug="ident-ad-pwd-not-required",
        name="AD Account Set With PASSWD_NOTREQD",
        severity="high",
        mitre=["t1098"],
        product="active-directory",
        service="audit",
        when={"event_id": 4738, "useraccountcontrol_flag_passwd_notreqd": True},
        fp=[FP_TUNING],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-ad-pwd-never-expires-priv",
        name="Privileged AD Account Set With DONT_EXPIRE_PASSWORD",
        severity="medium",
        mitre=["t1098"],
        product="active-directory",
        service="audit",
        when={"event_id": 4738, "useraccountcontrol_flag_dont_expire": True, "user_priv": True},
        fp=["Service account by design"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-ad-machine-account-quota-abuse",
        name="Machine Account Quota Exhausted By Single User",
        severity="high",
        mitre=["t1136.001"],
        product="active-directory",
        service="audit",
        when={"event_id": 4741, "machines_created_per_user_per_day_gt": 5},
        fp=["Approved imaging pipeline"],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-ad-protected-users-removed",
        name="User Removed From Protected Users Group",
        severity="high",
        mitre=["t1556"],
        product="active-directory",
        service="audit",
        when={"event_id": 4729, "group_name": "Protected Users"},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-ad-sidhistory-modified",
        name="AD sIDHistory Attribute Modified",
        severity="high",
        mitre=["t1078.002"],
        product="active-directory",
        service="audit",
        when={"event_id": 4738, "attribute_modified": "sIDHistory"},
        fp=["Approved domain migration"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="ident-ad-userlogon-disabled-account",
        name="Disabled AD Account Authenticated Successfully",
        severity="high",
        mitre=["t1078.002"],
        product="active-directory",
        service="auth",
        when={"event_id": 4624, "account_disabled": True},
        fp=["Stale ticket cache"],
        playbook="tpl-initial-access",
    ),
]


# ---------------------------------------------------------------------------
# Kerberos / NTLM / token-theft (15)
# ---------------------------------------------------------------------------
KERB_RULES: list[dict] = [
    S(
        slug="ident-kerberoasting-tgs-rc4",
        name="Kerberoasting: TGS Request With RC4_HMAC",
        severity="high",
        mitre=["t1558.003"],
        product="active-directory",
        service="kerberos",
        when={"event_id": 4769, "ticket_encryption_type": "0x17"},
        fp=["Legacy app (must be ticketed)"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-asreproast-no-preauth",
        name="AS-REP Roasting: TGT Without Pre-Auth",
        severity="high",
        mitre=["t1558.004"],
        product="active-directory",
        service="kerberos",
        when={"event_id": 4768, "preauth_required": False},
        fp=["Approved scanner / vuln check"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-golden-ticket-anomalous-tgt-lifetime",
        name="Golden Ticket Indicator: TGT Lifetime > 10h",
        severity="critical",
        mitre=["t1558.001"],
        product="active-directory",
        service="kerberos",
        when={"event_id": 4768, "ticket_lifetime_hours_gt": 10},
        fp=["Approved long-life ticket policy"],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-silver-ticket-anomalous-spn",
        name="Silver Ticket Indicator: TGS Without Matching TGT",
        severity="high",
        mitre=["t1558.002"],
        product="active-directory",
        service="kerberos",
        when={"event_id": 4769, "tgt_seen_for_user_in_window": False},
        fp=["Logging gap"],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-skeleton-key-encryption-downgrade",
        name="Skeleton Key Indicator: Forced Downgrade To DES/RC4",
        severity="critical",
        mitre=["t1556.001"],
        product="active-directory",
        service="kerberos",
        when={"event_id": 4624, "logon_type": 3, "encryption_type_in": ["DES", "RC4"], "domain_controller": True},
        fp=["Legacy app integration"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-kerberos-failure-spike-from-host",
        name="Kerberos Pre-Auth Failure Spike From Host",
        severity="medium",
        mitre=["t1110"],
        product="active-directory",
        service="kerberos",
        when={"event_id": 4771, "failures_per_host_per_minute_gt": 50},
        fp=["Misconfigured app"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-ntlm-relay-indicator",
        name="NTLM Authentication With Mismatched WorkstationName",
        severity="high",
        mitre=["t1557.001"],
        product="active-directory",
        service="ntlm",
        when={"event_id": 8004, "auth_type": "NTLMv2", "workstation_neq_source_address": True},
        fp=[FP_PENTEST],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-ntlm-priv-account-from-untrusted",
        name="Privileged Account NTLM Logon From Untrusted Host",
        severity="high",
        mitre=["t1078.002"],
        product="active-directory",
        service="ntlm",
        when={"event_id": 4624, "auth_type": "NTLMv2", "user_priv": True, "src_host_trusted": False},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-pass-the-hash-anomalous-logon",
        name="Pass-The-Hash Indicator: NTLMv2 With NewCredentials",
        severity="high",
        mitre=["t1550.002"],
        product="active-directory",
        service="auth",
        when={"event_id": 4624, "logon_type": 9, "auth_type": "NTLMv2"},
        fp=["RunAs /netonly use"],
        playbook="tpl-lateral-movement",
    ),
    S(
        slug="ident-pass-the-ticket-anomalous-tgt-source",
        name="Pass-The-Ticket Indicator: TGT Source IP Mismatch",
        severity="high",
        mitre=["t1550.003"],
        product="active-directory",
        service="kerberos",
        when={"event_id": 4624, "logon_type": 3, "ticket_source_ip_neq_session_ip": True},
        fp=["NAT / proxy environment"],
        playbook="tpl-lateral-movement",
    ),
    S(
        slug="ident-token-theft-cookie-replay",
        name="Session Cookie Replayed From New Geo Within 1h",
        severity="critical",
        mitre=["t1539"],
        product="identity",
        service="session",
        when={"event_type": "session_continuation", "geo_changed": True, "minutes_between_lt": 60},
        fp=["Mobile-tethering carrier IP change"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-oauth-refresh-token-replay",
        name="OAuth Refresh Token Replayed From Different IP",
        severity="high",
        mitre=["t1550"],
        product="identity",
        service="oauth",
        when={"event_type": "token_refresh", "src_ip_changed": True, "geo_distance_km_gt": 500},
        fp=["Roaming user"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-oauth-token-theft-impossible-time",
        name="OAuth Access Token Used Before Issue +1s",
        severity="high",
        mitre=["t1550"],
        product="identity",
        service="oauth",
        when={"event_type": "token_use", "seconds_after_issue_lt": 1, "ip_changed": True},
        fp=["Clock skew"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-saml-assertion-replay",
        name="SAML Assertion Replayed Within Window",
        severity="high",
        mitre=["t1550"],
        product="identity",
        service="saml",
        when={"event_type": "saml_consume", "assertion_id_seen_recently": True},
        fp=["IdP retry due to relay-state confusion"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-saml-issuer-mismatch",
        name="SAML Assertion Issuer Mismatched",
        severity="critical",
        mitre=["t1556"],
        product="identity",
        service="saml",
        when={"event_type": "saml_consume", "issuer_in_trusted_list": False},
        fp=["IdP migration window"],
        playbook="tpl-defense-evasion",
    ),
]


# ---------------------------------------------------------------------------
# IdP admin events (Okta, Auth0, Duo, Ping, Keycloak) (35)
# ---------------------------------------------------------------------------
IDP_RULES: list[dict] = [
    # Okta (12)
    S(
        slug="ident-okta-admin-app-assigned-many-users",
        name="Okta Admin App Assigned To Many Users In Burst",
        severity="medium",
        mitre=["t1098"],
        product="okta",
        service="audit",
        when={"event_type": "application.user_membership.add", "events_per_minute_gt": 25},
        fp=["Bulk app rollout"],
        playbook="tpl-discovery",
    ),
    S(
        slug="ident-okta-policy-everyone-bypass-mfa",
        name="Okta Policy Allows 'Everyone' Without MFA",
        severity="high",
        mitre=["t1556"],
        product="okta",
        service="audit",
        when={"event_type_in": ["policy.lifecycle.activate", "policy.rule.update"], "factor_required": False, "applies_to_everyone": True},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-okta-network-zone-modified-trusted",
        name="Okta Trusted Network Zone Modified",
        severity="medium",
        mitre=["t1556"],
        product="okta",
        service="audit",
        when={"event_type": "zone.update", "zone_type": "IP", "trusted": True},
        fp=["Office relocation"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-okta-superadmin-added",
        name="Okta Super Administrator Role Granted",
        severity="critical",
        mitre=["t1098"],
        product="okta",
        service="audit",
        when={"event_type": "user.account.privilege.grant", "role": "SUPER_ADMIN"},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="ident-okta-suspicious-login-bot",
        name="Okta Login Marked Suspicious-Activity Behaviour",
        severity="medium",
        mitre=["t1078"],
        product="okta",
        service="auth",
        when={"event_type": "user.session.start", "behaviors_contains_any": ["new_device", "new_geolocation", "velocity"]},
        fp=["Mobile user travel"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-okta-app-secret-rotated-priv",
        name="Okta App Sign-On Secret Rotated By Non-Admin",
        severity="high",
        mitre=["t1098.001"],
        product="okta",
        service="audit",
        when={"event_type": "application.lifecycle.update", "actor_role_priv": False, "secret_rotated": True},
        fp=[FP_TUNING],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-okta-mfa-factor-reset-priv",
        name="Okta MFA Factor Reset For Privileged User",
        severity="high",
        mitre=["t1556.006"],
        product="okta",
        service="audit",
        when={"event_type": "user.mfa.factor.reset_all", "target_user_priv": True},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-okta-policy-deactivated",
        name="Okta Sign-On Policy Deactivated",
        severity="high",
        mitre=["t1562.001"],
        product="okta",
        service="audit",
        when={"event_type": "policy.lifecycle.deactivate"},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-okta-app-assigned-foreign-domain",
        name="Okta App Assigned With Federated External Domain",
        severity="high",
        mitre=["t1199"],
        product="okta",
        service="audit",
        when={"event_type": "application.federated.user.assign", "external_domain": True},
        fp=["Approved partner integration"],
        playbook="tpl-initial-access",
    ),
    S(
        slug="ident-okta-api-token-created-priv",
        name="Okta API Token Created With Admin Role",
        severity="high",
        mitre=["t1098.001"],
        product="okta",
        service="audit",
        when={"event_type": "system.api_token.create", "scope_contains": "admin"},
        fp=["Approved automation"],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-okta-radius-app-added",
        name="Okta RADIUS Application Added",
        severity="medium",
        mitre=["t1133"],
        product="okta",
        service="audit",
        when={"event_type": "application.lifecycle.create", "app_type": "radius"},
        fp=["Approved network gear"],
        playbook="tpl-initial-access",
    ),
    S(
        slug="ident-okta-mass-deactivate-users",
        name="Okta Mass User Deactivation",
        severity="high",
        mitre=["t1531"],
        product="okta",
        service="audit",
        when={"event_type": "user.lifecycle.deactivate", "events_per_minute_gt": 25},
        fp=["Bulk offboarding"],
        playbook="tpl-impact",
    ),
    # Auth0 (5)
    S(
        slug="ident-auth0-rule-added",
        name="Auth0 Rule Added (Custom Code In Login Pipeline)",
        severity="high",
        mitre=["t1556"],
        product="auth0",
        service="audit",
        when={"event_type": "Successful API Operation", "operation": "rules.create"},
        fp=["Approved tenant config"],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-auth0-action-deployed",
        name="Auth0 Action Deployed",
        severity="high",
        mitre=["t1556"],
        product="auth0",
        service="audit",
        when={"event_type": "Successful API Operation", "operation": "actions.deploy"},
        fp=["Approved tenant config"],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-auth0-management-api-key-created",
        name="Auth0 Management API Key Created",
        severity="high",
        mitre=["t1098.001"],
        product="auth0",
        service="audit",
        when={"event_type": "Successful API Operation", "operation": "client_grants.create", "scope_contains": "manage"},
        fp=["Approved automation"],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-auth0-connection-deleted",
        name="Auth0 Connection Deleted",
        severity="medium",
        mitre=["t1485"],
        product="auth0",
        service="audit",
        when={"event_type": "Successful API Operation", "operation": "connections.delete"},
        fp=["Tenant cleanup"],
        playbook="tpl-impact",
    ),
    S(
        slug="ident-auth0-anomaly-detection-disabled",
        name="Auth0 Anomaly Detection Disabled",
        severity="high",
        mitre=["t1562.001"],
        product="auth0",
        service="audit",
        when={"event_type": "Successful API Operation", "operation": "anomaly.disable"},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    # Duo (5)
    S(
        slug="ident-duo-bypass-code-generated",
        name="Duo Bypass Code Generated",
        severity="high",
        mitre=["t1556.006"],
        product="duo",
        service="admin",
        when={"event_type": "user_create_bypass_code"},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-duo-policy-changed-mfa-removed",
        name="Duo Policy Changed: MFA Requirement Removed",
        severity="high",
        mitre=["t1556"],
        product="duo",
        service="admin",
        when={"event_type": "policy_update", "auth_required": False},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-duo-admin-added-from-self",
        name="Duo Admin Created Via Self-Service Activation",
        severity="critical",
        mitre=["t1098"],
        product="duo",
        service="admin",
        when={"event_type": "admin_create", "actor_eq_target": True},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="ident-duo-fraud-reported",
        name="Duo Push Marked As Fraud By User",
        severity="medium",
        mitre=["t1621"],
        product="duo",
        service="auth",
        when={"event_type": "auth", "result": "fraud"},
        fp=["Misclick / training"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-duo-application-bypass-policy",
        name="Duo Application Set To Bypass Policy",
        severity="high",
        mitre=["t1556"],
        product="duo",
        service="admin",
        when={"event_type": "integration_update", "policy_assigned": "bypass"},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    # Ping (5)
    S(
        slug="ident-ping-policy-mfa-removed",
        name="PingOne MFA Policy Removed",
        severity="high",
        mitre=["t1556"],
        product="ping",
        service="audit",
        when={"event_type": "MFA_POLICY_DELETED"},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-ping-admin-role-burst",
        name="PingOne Admin Role Burst Granted",
        severity="high",
        mitre=["t1098"],
        product="ping",
        service="audit",
        when={"event_type": "ADMIN_ROLE_GRANT", "events_per_minute_gt": 5},
        fp=["Bulk admin onboarding"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="ident-ping-pingid-device-removed",
        name="PingID Device Removed From Privileged User",
        severity="high",
        mitre=["t1556.006"],
        product="ping",
        service="audit",
        when={"event_type": "DEVICE_DELETED", "target_user_priv": True},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-ping-policy-bypass-everyone",
        name="PingOne Policy Bypass Applied To 'Everyone'",
        severity="high",
        mitre=["t1556"],
        product="ping",
        service="audit",
        when={"event_type": "POLICY_UPDATE", "applies_to_everyone": True, "factor_required": False},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-ping-trust-store-modified",
        name="Ping Trust Store Certificate Added",
        severity="medium",
        mitre=["t1553.004"],
        product="ping",
        service="audit",
        when={"event_type": "TRUSTED_CERT_ADDED"},
        fp=["Approved IdP federation"],
        playbook="tpl-persistence",
    ),
    # Keycloak / Generic OIDC (8)
    S(
        slug="ident-keycloak-master-realm-user-created",
        name="Keycloak Master Realm User Created",
        severity="high",
        mitre=["t1136"],
        product="keycloak",
        service="audit",
        when={"event_type": "USER_CREATE", "realm": "master"},
        fp=["Bootstrap"],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-keycloak-admin-realm-role-grant",
        name="Keycloak realm-admin Role Granted",
        severity="high",
        mitre=["t1098"],
        product="keycloak",
        service="audit",
        when={"event_type": "ROLE_ASSIGN", "role": "realm-admin"},
        fp=["Bootstrap"],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="ident-keycloak-client-secret-regen",
        name="Keycloak Client Secret Regenerated",
        severity="medium",
        mitre=["t1098.001"],
        product="keycloak",
        service="audit",
        when={"event_type": "CLIENT_UPDATE", "secret_changed": True},
        fp=[FP_AUTOMATION],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-keycloak-impersonation-used",
        name="Keycloak User Impersonation Used",
        severity="high",
        mitre=["t1078"],
        product="keycloak",
        service="audit",
        when={"event_type": "IMPERSONATE"},
        fp=[FP_FORENSIC],
        playbook="tpl-privilege-escalation",
    ),
    S(
        slug="ident-oidc-issuer-trust-changed",
        name="Generic OIDC Issuer Trust Modified",
        severity="high",
        mitre=["t1556"],
        product="identity",
        service="oidc",
        when={"event_type": "issuer.update", "discovery_url_changed": True},
        fp=["Approved IdP migration"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-oidc-many-clients-from-single-actor",
        name="OIDC Client Burst Created By Single Actor",
        severity="medium",
        mitre=["t1136"],
        product="identity",
        service="oidc",
        when={"event_type": "client.create", "events_per_minute_per_actor_gt": 5},
        fp=[FP_AUTOMATION],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-oidc-secret-leaked-pattern",
        name="OIDC Client Secret Logged In Plaintext",
        severity="medium",
        mitre=["t1552.004"],
        product="identity",
        service="oidc",
        when={"event_type": "log_emit", "field_contains_oidc_secret": True},
        fp=[],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-oidc-redirect-uri-wildcard",
        name="OIDC Client Configured With Wildcard Redirect URI",
        severity="high",
        mitre=["t1539"],
        product="identity",
        service="oidc",
        when={"event_type": "client.update", "redirect_uri_contains": "*"},
        fp=["Approved local-dev client"],
        playbook="tpl-credential-access",
    ),
]


# ---------------------------------------------------------------------------
# SaaS-side identity / collaboration (20)
# ---------------------------------------------------------------------------
SAAS_RULES: list[dict] = [
    S(
        slug="ident-google-workspace-mfa-disabled-priv",
        name="Google Workspace 2SV Disabled For Admin",
        severity="critical",
        mitre=["t1556.006"],
        product="google-workspace",
        service="admin",
        when={"event_name": "ENFORCE_STRONG_AUTHENTICATION", "value": False, "user_priv": True},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-google-workspace-app-pw-issued",
        name="Google Workspace App Password Issued To Priv User",
        severity="high",
        mitre=["t1556.006"],
        product="google-workspace",
        service="admin",
        when={"event_name": "GENERATE_APP_PASSWORD", "user_priv": True},
        fp=["Legacy MAPI client (must be ticketed)"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-google-workspace-3p-app-data-grant",
        name="Google Workspace 3rd-Party App Granted Drive Read",
        severity="high",
        mitre=["t1528"],
        product="google-workspace",
        service="oauth_token",
        when={"event_name": "AUTHORIZE", "scope_contains": "https://www.googleapis.com/auth/drive"},
        fp=["Approved SaaS"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-google-workspace-suspicious-login-flag",
        name="Google Workspace Login Flagged 'Suspicious'",
        severity="medium",
        mitre=["t1078"],
        product="google-workspace",
        service="login",
        when={"event_name": "login_failure", "type": "suspicious_login"},
        fp=["Travel"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-m365-mailbox-rule-forward-external",
        name="M365 Inbox Rule Forwards To External Address",
        severity="high",
        mitre=["t1114.003"],
        product="m365",
        service="exchange",
        when={"operation": "New-InboxRule", "forward_external": True},
        fp=["Approved alias"],
        playbook="tpl-collection",
    ),
    S(
        slug="ident-m365-mailbox-permission-add-foreign",
        name="M365 Mailbox Permission Granted To External User",
        severity="high",
        mitre=["t1098"],
        product="m365",
        service="exchange",
        when={"operation": "Add-MailboxPermission", "trustee_external": True},
        fp=["Approved partner integration"],
        playbook="tpl-collection",
    ),
    S(
        slug="ident-m365-app-consent-graph-mail",
        name="M365 App Granted Mail.Read / Mail.ReadWrite",
        severity="high",
        mitre=["t1528"],
        product="m365",
        service="azure-ad",
        when={"operation": "Consent to application", "permissions_contains_any": ["Mail.Read", "Mail.ReadWrite"]},
        fp=["Approved SaaS integration"],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-m365-sharing-policy-anyone",
        name="M365 SharePoint Sharing Policy 'Anyone' Enabled",
        severity="high",
        mitre=["t1530"],
        product="m365",
        service="sharepoint",
        when={"operation": "TenantSettingsModified", "sharing_capability": "ExternalUserAndGuestSharing"},
        fp=[FP_TUNING],
        playbook="tpl-data-exposure",
    ),
    S(
        slug="ident-m365-tap-removed-priv",
        name="M365 Temporary Access Pass Generated For Priv",
        severity="high",
        mitre=["t1556.006"],
        product="m365",
        service="azure-ad",
        when={"operation": "Add temporary access pass", "target_user_priv": True},
        fp=[FP_BREAK_GLASS],
        playbook="tpl-credential-access",
    ),
    S(
        slug="ident-slack-workspace-2fa-disabled",
        name="Slack Workspace 2FA Disabled",
        severity="high",
        mitre=["t1556"],
        product="slack",
        service="admin",
        when={"action_in": ["workspace_two_factor_required_disabled"]},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-slack-app-installed-bot-token",
        name="Slack App Installed With Bot Scope chat:write.public",
        severity="medium",
        mitre=["t1525"],
        product="slack",
        service="audit",
        when={"action": "app_installed", "scopes_contains": "chat:write.public"},
        fp=["Approved bot"],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-slack-channel-shared-external",
        name="Slack Channel Shared Externally",
        severity="high",
        mitre=["t1567"],
        product="slack",
        service="audit",
        when={"action_in": ["external_shared_channel_invite_accepted", "shared_channel_external_org_added"]},
        fp=["Approved partner channel"],
        playbook="tpl-exfiltration",
    ),
    S(
        slug="ident-github-org-2fa-disabled",
        name="GitHub Org 2FA Requirement Disabled",
        severity="critical",
        mitre=["t1556"],
        product="github",
        service="audit",
        when={"action": "org.disable_two_factor_requirement"},
        fp=["Org transition (must be ticketed)"],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-github-pat-classic-created-with-broad-scopes",
        name="GitHub Classic PAT Created With repo+workflow+admin:org",
        severity="high",
        mitre=["t1098.001"],
        product="github",
        service="audit",
        when={"action": "personal_access_token.create", "scopes_contains_all": ["repo", "workflow", "admin:org"]},
        fp=["CI bootstrap (must be ticketed)"],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-github-org-collaborator-removed-bulk",
        name="GitHub Org Bulk Member Removed",
        severity="medium",
        mitre=["t1531"],
        product="github",
        service="audit",
        when={"action": "org.remove_member", "events_per_minute_gt": 20},
        fp=["Approved bulk offboarding"],
        playbook="tpl-impact",
    ),
    S(
        slug="ident-github-deploy-key-added-org-wide",
        name="GitHub Deploy Key Added Across Many Repos",
        severity="medium",
        mitre=["t1098.001"],
        product="github",
        service="audit",
        # GitHub has no deploy_key.* audit category; a deploy-key add is
        # `public_key.create` with the repo slug. The windowed translation
        # counts distinct `repo` values per actor, and an entry with no repo
        # (a user SSH-key add) is skipped by the distinct counter rather
        # than counted (issue #1545).
        when={"action": "public_key.create", "distinct_repos_per_actor_per_hour_gt": 10},
        fp=[FP_AUTOMATION],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-zoom-passcode-disabled-meeting-policy",
        name="Zoom Account Policy Disables Meeting Passcode",
        severity="medium",
        mitre=["t1556"],
        product="zoom",
        service="admin",
        when={"action_in": ["update_account_settings", "update_user_settings"], "passcode_required": False},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-atlassian-org-required-2fa-off",
        name="Atlassian Org 2FA Requirement Removed",
        severity="high",
        mitre=["t1556"],
        product="atlassian",
        service="audit",
        when={"action_in": ["org_security_policy_modified"], "two_step_required": False},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
    S(
        slug="ident-atlassian-api-token-created",
        name="Atlassian Org API Token Created",
        severity="medium",
        mitre=["t1098.001"],
        product="atlassian",
        service="audit",
        when={"action": "api_token_created"},
        fp=[FP_AUTOMATION],
        playbook="tpl-persistence",
    ),
    S(
        slug="ident-jamf-mdm-policy-mfa-removed",
        name="MDM Policy Removed Requiring Device Compliance",
        severity="high",
        mitre=["t1556"],
        product="mdm",
        service="audit",
        when={"action": "compliance_policy_disabled"},
        fp=[FP_TUNING],
        playbook="tpl-defense-evasion",
    ),
]


# ---------------------------------------------------------------------------
# Combined IDENTITY extras
# ---------------------------------------------------------------------------
IDENTITY_EXTRA: list[dict] = [
    *AUTH_RULES,
    *PRIV_RULES,
    *KERB_RULES,
    *IDP_RULES,
    *SAAS_RULES,
]


if __name__ == "__main__":
    print(f"Auth/MFA:    {len(AUTH_RULES)}")
    print(f"Priv events: {len(PRIV_RULES)}")
    print(f"Kerberos:    {len(KERB_RULES)}")
    print(f"IdP admin:   {len(IDP_RULES)}")
    print(f"SaaS:        {len(SAAS_RULES)}")
    print(f"TOTAL:       {len(IDENTITY_EXTRA)}")