/*
   Odin by Brandefense - Monograph 74 Bounded YARA Ruleset
   Detects invariant PHP structural skeletons across polymorphic XOR/hex-pack shells,
   eval-free AES-128-ECB CacheStream taglib implants, disable_functions harnesses,
   and CMS sys_info-to-taglib write-and-restore Python scripts.
*/

rule ODIN_R74_Polymorphic_XOR_HexPack_PHP_Implant {
    meta:
        author = "Odin by Brandefense"
        date = "2026-11-29"
        description = "Detects polymorphic PHP implants combining 16-byte zero-padded key normalization, pack('H*') hex blobs, and split stream_wrapper_register or temp .dat staging"
    strings:
        $php = { 3C 3F 70 68 70 }
        $pad = "str_pad($key, 16, chr(0))" ascii
        $hex = "pack(\"H*\", \"" ascii
        $ob  = "ob_get_clean()" ascii
        $sw  = "\"stream_wr\".\"apper_\"" ascii
        $dat = ".dat';" ascii
    condition:
        filesize < 64KB and $php at 0 and $pad and $hex and $ob and ($sw or $dat)
}

rule ODIN_R74_StreamWrapper_AES_Destruct_Taglib_Implant {
    meta:
        author = "Odin by Brandefense"
        date = "2026-11-29"
        description = "Detects eval-free PHP taglib implants using custom stream_open classes, AES-128-ECB openssl_decrypt, and stream_wrapper_register inside __destruct"
    strings:
        $php = { 3C 3F 70 68 70 }
        $so  = "function stream_open(" ascii
        $aes = "openssl_decrypt(pack(\"H*\"" ascii
        $ecb = "'AES-128-ECB'" ascii
        $swr = "stream_wrapper_register(" ascii
        $dst = "function __destruct()" ascii
    condition:
        filesize < 32KB and $php at 0 and $so and $aes and $ecb and $swr and $dst
}

rule ODIN_R74_PHP_DisableFunctions_and_ErrorLog_Harness {
    meta:
        author = "Odin by Brandefense"
        date = "2026-11-29"
        description = "Detects staged PHP capability and disable_functions escape scripts checking open_basedir, mb_send_mail, and error_log mode 3 file writes"
    strings:
        $php = { 3C 3F 70 68 70 }
        $df  = "disable_functions" ascii
        $obd = "open_basedir" ascii
        $mbs = "mb_send_mail" ascii
        $elg = "error_log" ascii
        $ini = "mail.force_extra_parameters" ascii
    condition:
        filesize < 32KB and $php at 0 and $df and $elg and $mbs and ($obd or $ini)
}

rule ODIN_R74_CMS_SysInfo_Taglib_Write_and_Restore_Script {
    meta:
        author = "Odin by Brandefense"
        date = "2026-11-29"
        description = "Detects Python CMS intrusion scripts pairing sys_info configuration injection with include/taglib/ library writes and WCM cookie or SOCKS5 session parameters"
    strings:
        $py1 = "#!/usr/bin/env python" ascii
        $py2 = "requests" ascii
        $sys = "sys_info" ascii
        $tag = "include/taglib/" ascii
        $lib = ".lib.php" ascii
        $wcm = "WcmUserID__ckMd5" ascii
        $s5  = "socks5h://127.0.0.1:" ascii
    condition:
        filesize < 64KB and ($py1 or $py2) and $sys and ($tag or $lib) and ($wcm or $s5)
}
