/*
   Odin by Brandefense - Report #07 YARA PAM Backdoor, Systemd Loop & Sliver Workbench Rules
   Classification: TLP:CLEAR
*/

rule ODIN_Linux_PAM_Credential_Harvester_Backdoor {
    meta:
        author = "Odin by Brandefense"
        description = "Detects Linux PAM pam_exec credential-harvesting shell scripts reading stdin tokens and logging PAM environment variables to hidden /var/log/.* sinks"
        classification = "TLP:CLEAR"
        mitre_attack = "T1556.003"
    strings:
        $s1 = "$PAM_USER" ascii
        $s2 = "$PAM_RHOST" ascii
        $s3 = "/var/log/." ascii
        $s4 = "read " ascii
    condition:
        filesize < 10KB and 3 of them
}

rule ODIN_Masquerading_Systemd_Cache_Helper_Loop {
    meta:
        author = "Odin by Brandefense"
        description = "Detects masquerading systemd unit files executing hidden /usr/local/bin/.* binaries in 60s loops"
        classification = "TLP:CLEAR"
        mitre_attack = "T1543.002"
    strings:
        $u1 = "Description=Systemd Resolver Helper" ascii
        $u2 = "/usr/local/bin/.cache-helper" ascii
        $u3 = "while true; do" ascii
        $u4 = "sleep 60; done" ascii
        $u5 = "Restart=always" ascii
    condition:
        filesize < 10KB and 3 of them
}

rule ODIN_PowerShell_Chunked_HTTP_GET_Exfiltrator {
    meta:
        author = "Odin by Brandefense"
        description = "Detects PowerShell x.ps1 utility slicing Base64 host recon into 2,000-character (~1,500-byte) HTTP GET query chunks"
        classification = "TLP:CLEAR"
        mitre_attack = "T1048.003"
    strings:
        $p1 = "[Convert]::ToBase64String($bytes)" ascii nocase
        $p2 = "$kl = 2000" ascii
        $p3 = "$w.Proxy = $null" ascii nocase
        $p4 = "'?s=' + $k + '&n=' + $n + '&d=' + $chunk" ascii
        $p5 = "DownloadString($u)" ascii nocase
    condition:
        filesize < 50KB and 3 of them
}

rule ODIN_Exposed_Sliver_Audit_Stream_And_DB_Workbench {
    meta:
        author = "Odin by Brandefense"
        description = "Detects exposed Sliver teamserver gRPC audit.json streams, server/tunnel console logs, or SQLite DB helper scripts"
        classification = "TLP:CLEAR"
        mitre_attack = "T1071.001"
    strings:
        $g1 = "/rpcpb.SliverRPC/GetSessions" ascii
        $g2 = "/rpcpb.SliverRPC/GetBeacons" ascii
        $g3 = "\\\"remote_ip\\\":\\\"bufconn\\\"" ascii
        $g4 = "/rpcpb.SliverRPC/GetVersion" ascii
        $t1 = "tunnels.go:45: Creating single instance of tunnels." ascii
        $t2 = "Starting tunnel data loop ..." ascii
        $t3 = "Waiting for TunnelData ..." ascii
        $s1 = "sliver/server/configs/database.go" ascii
        $s2 = "sliver/server/db/sql-go.go" ascii
        $s3 = "Connecting to SQLite database" ascii
        $s4 = "initializing traffic encoder map..." ascii
    condition:
        filesize < 5MB and (
            2 of ($g*) or
            2 of ($t*) or
            2 of ($s*)
        )
}
