/*
   Odin by Brandefense - Report #04 YARA Workbench & AI Gateway Exploitation Rules
   Classification: TLP:CLEAR
   Note: Rules 1-3 are validated against retrieved workbench source/config artifacts;
         Rule 4 targets offensive LiteLLM SQLi and credential-extraction scripts while
         excluding upstream LiteLLM package source, Prisma schemas, and FinOps spend queries.
*/

rule ODIN_CyberStrikeAI_Workbench_Config_And_Rollback {
    meta:
        author = "Odin by Brandefense"
        description = "Detects CyberStrikeAI live configuration (upstream example templates may also match; review hits), autonomous runner/updater scripts, or SQLite RBAC admin credential rollback utilities"
        classification = "TLP:CLEAR"
    strings:
        $cfg1 = "tls_auto_self_sign: true" ascii
        $cfg2 = "auth_failure_cooldown_seconds:" ascii
        $cfg3 = "external_mcp_max_concurrent_per_server:" ascii
        $run1 = "# CyberStrikeAI one-click deploy and start script" ascii
        $run2 = "pypi.tuna.tsinghua.edu.cn/simple" ascii
        $upg1 = "DEFAULT_GITHUB_REPO=\"AIPentest/CyberStrikeAI\"" ascii
        $upg2 = "BINARY_NAME=\"cyberstrike-ai\"" ascii
        $rb1 = "/CyberStrikeAI/data/conversations.db" ascii
        $rb2 = "UPDATE rbac_users SET password_hash=?, updated_at=?" ascii
    condition:
        filesize < 256KB and (
            all of ($cfg1, $cfg2, $cfg3)
            or all of ($run*)
            or all of ($upg*)
            or all of ($rb*)
        )
}

rule ODIN_ARTEX_Finding_Notifier_And_Reset {
    meta:
        author = "Odin by Brandefense"
        description = "Detects ARTEX Go Telegram/Webhook finding notification modules, ARTEX OpenCode Go session headers, and PostgreSQL pgcrypto admin reset scripts"
        classification = "TLP:CLEAR"
    strings:
        $rst1 = "ARTEX_PG_DSN" ascii
        $rst2 = "auth.password_hash" ascii
        $rst3 = "BCRYPT_COST=10" ascii
        $tg1 = "KindTelegram" ascii
        $tg2 = "telegramTextLimit = 4096" ascii
        $tg3 = "DefaultRatePerMin() int { return 20 }" ascii
        $wh1 = "KindWebhook" ascii
        $wh2 = "webhookDefaultTemplate" ascii
        $wh3 = "\"finding_id\": {{$it.FindingID}}" ascii
        $oc1 = "github.com/Autumn-27/norma" ascii
        $oc2 = "quotaAwareTransport" ascii
        $oc3 = "sessionHeaderKey" ascii
    condition:
        filesize < 256KB and (
            (2 of ($rst*))
            or (2 of ($tg*))
            or (2 of ($wh*))
            or ($oc1 and $oc2 and $oc3)
        )
}

rule ODIN_Rogue_OpenAI_BaseURL_SSTI_Server {
    meta:
        author = "Odin by Brandefense"
        description = "Detects rogue OpenAI-compatible HTTP servers explicitly serving the ssti-1 model identifier alongside BaseHTTPRequestHandler or chat.completion spoofing"
        classification = "TLP:CLEAR"
    strings:
        $m1 = "\"object\":\"chat.completion\"" ascii
        $m2 = "\"id\":\"ssti-1\"" ascii
        $m3 = "BaseHTTPRequestHandler" ascii
        $m4 = "Access-Control-Allow-Origin" ascii
    condition:
        filesize < 256KB and $m2 and 2 of ($m1, $m3, $m4)
}

rule ODIN_LiteLLM_Database_Key_Harvester {
    meta:
        author = "Odin by Brandefense"
        description = "Detects offensive scripts extracting API keys via LiteLLM SQLi or ProxyModelTable credential dumps while excluding upstream LiteLLM source and FinOps spend queries"
        classification = "TLP:CLEAR"
    strings:
        $t1 = "LiteLLM_SpendLogs" ascii nocase
        $t2 = "LiteLLM_ProxyModelTable" ascii nocase
        $t3 = "LiteLLM_VerificationTokenView" ascii nocase
        $sqli1 = "UNION SELECT" ascii nocase
        $sqli2 = "pg_sleep" ascii nocase
        $sqli3 = "Authorization header SQLi" ascii nocase
        $sqli4 = "SELECT " ascii nocase
        $scan1 = "fast_litellm_scan" ascii
        $harv1 = "sk-ant-" ascii
        $harv2 = "litellm_params" ascii nocase
        $db1 = "psycopg2" ascii
        $db2 = "asyncpg" ascii
    condition:
        filesize < 256KB and (
            $scan1
            or (any of ($t*) and $harv1)
            or (any of ($t*) and ($sqli1 or $sqli2 or $sqli3))
            or (($t2 or $t3) and $sqli4 and any of ($db*) and any of ($harv*))
        )
}
