/*
   Odin by Brandefense - Report #05 YARA Enterprise ERP, VCDA/VCD & Workflow RCE Rules
   Classification: TLP:CLEAR
*/

rule ODIN_Epicor_BpDirective_XAML_RCE_Injector {
    meta:
        author = "Odin by Brandefense"
        description = "Detects SQL injection scripts and migration telemetry targeting Epicor Kinetic Ice.BpDirective / Ice.BpMethod"
        classification = "TLP:CLEAR"
    strings:
        $tbl1 = "Ice.BpDirective" ascii wide nocase
        $tbl2 = "Ice.BpMethod" ascii wide nocase
        $xaml1 = "<DirectiveDefinition2" ascii wide
        $xaml2 = "CustomCodeAction" ascii wide nocase
        $m1 = "Ice.UserFile.Update" ascii wide nocase
        $m2 = "Ice.AlternateKey.Update" ascii wide nocase
        $err1 = "Invalid column name 'DisplayOrder'" ascii wide
        $err2 = "RC_UNKNOWN" ascii wide
        $src = "'DB'" ascii wide
        $rcx1 = "rcx_directive" ascii wide nocase
        $rcx2 = "RCX-BPM-RCE" ascii wide
        $rcx3 = "RCX-211-OK" ascii wide
    condition:
        filesize < 250KB and (
            (($tbl1 or $tbl2) and ($xaml1 or $xaml2 or $m1 or $m2) and ($src or $rcx1 or $rcx2))
            or ($err1 and $err2)
            or $rcx3
        )
}

rule ODIN_MSSQL_Agent_CmdExec_And_ADSI_Toolkit {
    meta:
        author = "Odin by Brandefense"
        description = "Detects MSSQL Agent sp_add_jobstep CmdExec/xp_cmdshell persistence scripts and ADSDSOObject AD recon queries"
        falsepositives = "Authorized DBA Active Directory ADSI linked-server scripts querying sAMAccountName"
        classification = "TLP:CLEAR"
    strings:
        $j1 = "sp_add_jobstep" ascii wide nocase
        $j2 = "CmdExec" ascii wide nocase
        $x1 = "xp_cmdshell" ascii wide nocase
        $p1 = "C:\\ProgramData\\" ascii wide nocase
        $p2 = "start_svc.ps1" ascii wide nocase
        $p3 = "rcx_" ascii wide nocase
        $ad1 = "sp_addlinkedserver" ascii wide nocase
        $ad2 = "ADSDSOObject" ascii wide nocase
        $ad3 = "OPENQUERY(ADSI" ascii wide nocase
        $ad4 = "sAMAccountName" ascii wide nocase
    condition:
        filesize < 150KB and (
            ($j1 and ($j2 or $x1) and ($p1 or $p2 or $p3))
            or ($x1 and $p3)
            or ($ad1 and $ad2 and ($p3 or ($ad3 and $ad4)))
        )
}

rule ODIN_VMware_VCDA_SSRF_And_VCD_Secret_Decryptor {
    meta:
        author = "Odin by Brandefense"
        description = "Detects VMware VCDA 4.7.4 pre-auth SSRF / Config-Secret scripts and standalone VCD 10.6.0 master-key decryption tools"
        classification = "TLP:CLEAR"
    strings:
        $v1 = "X-VCAV-Auth" ascii
        $v2 = "/config/root-password" ascii
        $v3 = "Config-Secret" ascii
        $v4 = "SourceIpAuthorizer.class" ascii
        $v5 = "/api-client-auth" ascii
        $v6 = "docs/..;/" ascii
        $v7 = ":8046/config" ascii
        $v8 = "/is-configured" ascii
        $c1 = "AES.MODE_ECB" ascii
        $c1b = "modes.ECB" ascii
        $c2 = "key_unwrap" ascii nocase
        $c2b = "aes_key_unwrap" ascii nocase
        $c3 = "PBKDF2" ascii nocase
    condition:
        filesize < 250KB and (
            2 of ($v1, $v2, $v3, $v4, $v5, $v6, $v7, $v8)
            or (($c1 or $c1b) and ($c2 or $c2b or $c3))
        )
}

rule ODIN_Activiti_BPMN_Groovy_And_n8n_Workflow_RCE {
    meta:
        author = "Odin by Brandefense"
        description = "Detects Apache Activiti BPMN 2.0 Groovy/EL/Modeler RCE payloads (plaintext and Base64) and n8n post-exploitation / pwnkit toolkits"
        classification = "TLP:CLEAR"
    strings:
        $a1 = "groovy" ascii nocase
        $a1_b64 = "groovy" ascii base64
        $a2 = "new ProcessBuilder(" ascii
        $a3_plain = "getScriptingEngines().evaluate(" ascii
        $a3_b64 = "getScriptingEngines().evaluate(" ascii base64
        $a4_b64 = "StartNoneEvent" ascii base64
        $a5_plain = "System.getProperty(\"user.name\")" ascii
        $a5_esc = "System.getProperty(\\\"user.name\\\")" ascii
        $a5_b64 = "System.getProperty(" ascii base64
        $a6_b64 = "InetAddress.getLocalHost(" ascii base64
        $a7 = "openStream(" ascii
        $a8 = "InetAddress.getLocalHost(" ascii
        $n1a = "credentials_entity" ascii
        $n1b = "/rest/workflows" ascii
        $n1c = "n8n-auth" ascii
        $n1d = "child_process" ascii
        $n2b = "COPY (SELECT" ascii nocase
        $n2c = "<?php" ascii nocase
        $n2d = "eval($_POST[" ascii nocase
        $n3a = "GCONV_PATH=." ascii
        $n3b = "/usr/bin/pkexec" ascii
    condition:
        filesize < 250KB and (
            ($a1 and ($a2 or (($a5_plain or $a5_esc) and ($a7 or $a8))))
            or ($a4_b64 and ($a1_b64 or $a5_b64 or $a6_b64))
            or $a3_plain or $a3_b64
            or (($n1a or $n1b or $n1c) and $n1d)
            or ($n1b and $n1c)
            or ($n2b and ($n2c or $n2d))
            or ($n3a and $n3b)
        )
}
