/*
   Odin by Brandefense - Monograph 73 YARA Suite
   4 filesize-bounded structural rules covering Polkit/Dirty COW escalation wrappers,
   UID-0 toor and OpenSSH AllowUsers persistence scripts, Rust bore TCP tunnel wrappers,
   and post-run Linux log cleanup scripts (zero victim or vendor identifiers).
*/

rule ODIN_R73_Polkit_or_DirtyCow_Wrapper_Script
{
    meta:
        author = "Odin by Brandefense"
        monograph = "73"
        description = "Detects staged Linux shell wrappers referencing Polkit pkexec CVE-2021-4034 or Dirty COW CVE-2016-5195 markers"
        date = "2026-11-27"
    strings:
        $sh1 = "#!/bin/sh" ascii
        $sh2 = "#!/bin/bash" ascii
        $p1 = "pkexec" ascii
        $p2 = "GCONV_PATH" ascii
        $p3 = "pwnkit" ascii nocase
        $c1 = "/proc/self/mem" ascii
        $c2 = "cow_restart" ascii nocase
    condition:
        filesize < 32KB and ($sh1 or $sh2) and (2 of ($p*) or ($c1 and $c2))
}

rule ODIN_R73_UID0_Toor_and_SSHD_AllowUsers_Script
{
    meta:
        author = "Odin by Brandefense"
        monograph = "73"
        description = "Detects scripts staging a secondary toor account or changing OpenSSH sshd_config AllowUsers"
        date = "2026-11-27"
    strings:
        $u1 = "toor" ascii
        $u2 = "/etc/passwd" ascii
        $u3 = "useradd" ascii
        $s1 = "sshd_config" ascii
        $s2 = "AllowUsers" ascii
        $s3 = "authorized_keys" ascii
    condition:
        filesize < 16KB and (($u1 and ($u2 or $u3)) or ($s1 and $s2))
}

rule ODIN_R73_Rust_Bore_TCP_Tunnel_Wrapper
{
    meta:
        author = "Odin by Brandefense"
        monograph = "73"
        description = "Detects shell wrappers launching the Rust bore TCP tunnel binary or unpacking bore.tgz"
        date = "2026-11-27"
    strings:
        $b1 = "bore local" ascii
        $b2 = "bore.tgz" ascii
        $t1 = "--to " ascii
        $t2 = "--port" ascii
        $t3 = "setsid" ascii
        $t4 = "nohup" ascii
    condition:
        filesize < 16KB and ($b1 or $b2) and $t1 and 1 of ($t2, $t3, $t4)
}

rule ODIN_R73_PostRun_Log_and_History_Cleanup_Script
{
    meta:
        author = "Odin by Brandefense"
        monograph = "73"
        description = "Detects post-run Linux shell scripts targeting authentication logs, shell history variables, and temporary staging paths"
        date = "2026-11-27"
    strings:
        $sh1 = "#!/bin/" ascii
        $l1 = "/var/log/auth.log" ascii
        $l2 = "/var/log/secure" ascii
        $l3 = ".bash_history" ascii
        $l4 = "/var/log/wtmp" ascii
        $c1 = "HISTFILE" ascii
        $c2 = "/tmp/" ascii
    condition:
        filesize < 16KB and $sh1 and 2 of ($l*) and $c1 and $c2
}
