/*
   Odin by Brandefense - Monograph 48 / 52 Bounded YARA Self-Audit Suite
   Scope: Static posture scanning of web document roots, upload directories, and container artifacts for exposed PEM RSA/EC signing keys, compiled Symfony LightSAML credential caches, binary PKCS#12 (.p12/.pfx) keystores, and Laravel/Craft debug logs leaking bearer auth context.
*/

rule ODIN_Exposed_PEM_RSA_Or_EC_Private_Signing_Key {
    meta:
        author = "Odin by Brandefense"
        monograph = "48 / 52"
        description = "Detects PEM-encoded RSA, PKCS#8, or EC asymmetric private signing keys inside web-accessible trees"
        mitre_attack = "T1552.004"
        date = "2026-11-21"
    strings:
        $hdr1 = "-----BEGIN RSA PRIVATE KEY-----" ascii
        $hdr2 = "-----BEGIN PRIVATE KEY-----" ascii
        $hdr3 = "-----BEGIN EC PRIVATE KEY-----" ascii
        $hdr4 = "-----BEGIN ENCRYPTED PRIVATE KEY-----" ascii
        $ftr1 = "-----END RSA PRIVATE KEY-----" ascii
        $ftr2 = "-----END PRIVATE KEY-----" ascii
        $ftr3 = "-----END EC PRIVATE KEY-----" ascii
        $ftr4 = "-----END ENCRYPTED PRIVATE KEY-----" ascii
        $asn1_b64_a = "MII" ascii
        $asn1_b64_b = "MHcCAQEE" ascii
        $asn1_b64_c = "MIGkAgEB" ascii
    condition:
        filesize < 64KB and (1 of ($hdr*)) and (1 of ($ftr*)) and (1 of ($asn1_b64_*))
}

rule ODIN_Compiled_Symfony_LightSAML_Credential_Store_Cache {
    meta:
        author = "Odin by Brandefense"
        monograph = "48 / 52"
        description = "Detects compiled Symfony DependencyInjection container LightSAML credential store or resolver PHP service classes"
        mitre_attack = "T1606.002"
        date = "2026-11-21"
    strings:
        $cls1 = "LightSaml\\Credential\\" ascii
        $cls2 = "X509Credential" ascii
        $cls3 = "KeyHelper::createPrivateKey" ascii
        $di1 = "Symfony\\Component\\DependencyInjection" ascii
        $di2 = "Container" ascii
    condition:
        filesize < 256KB and (2 of ($cls*)) and $di1 and $di2
}

rule ODIN_PKCS12_Merchant_Or_TLS_Keystore_Bundle {
    meta:
        author = "Odin by Brandefense"
        monograph = "48 / 52"
        description = "Detects binary RFC 7292 PKCS#12 (.p12 and .pfx) merchant mTLS or TLS keystore bundles"
        mitre_attack = "T1552.004"
        date = "2026-11-21"
    strings:
        $p12_v3 = { 30 82 ?? ?? 02 01 03 30 82 }
        $oid_pkcs7_data = { 06 09 2A 86 48 86 F7 0D 01 07 01 }
        $oid_pkcs7_enc = { 06 09 2A 86 48 86 F7 0D 01 07 06 }
        $oid_pkcs12_bag = { 06 0B 2A 86 48 86 F7 0D 01 0C 0A 01 }
    condition:
        filesize < 128KB and $p12_v3 at 0 and $oid_pkcs7_data and ($oid_pkcs7_enc or $oid_pkcs12_bag)
}

rule ODIN_Exposed_Laravel_Or_Craft_Debug_Log_With_Auth_Context {
    meta:
        author = "Odin by Brandefense"
        monograph = "48 / 52"
        description = "Detects exposed Laravel or Craft/Yii2 framework debug log files containing stack traces alongside bearer token or OAuth signing context"
        mitre_attack = "T1552.001"
        date = "2026-11-21"
    strings:
        $fw_laravel1 = ".ERROR:" ascii
        $fw_laravel2 = "[stacktrace]" ascii
        $fw_laravel3 = "Illuminate\\Foundation\\" ascii
        $fw_craft1 = "yii\\web\\" ascii
        $fw_craft2 = "craft\\web\\Application" ascii
        $auth1 = "Authorization" ascii
        $auth2 = "Bearer eyJ" ascii
        $auth3 = "League\\OAuth2\\Server" ascii
    condition:
        filesize < 50MB and ((2 of ($fw_laravel*)) or (1 of ($fw_craft*))) and (1 of ($auth*))
}
