/*
   Odin by Brandefense - Monograph #10 YARA Suite for Exposed AI Agent Session & Credential Artifacts
   Classification: TLP:CLEAR
   Scope: 4 deterministic structural rules identifying exposed Claude Code, OpenClaw, OpenAI Codex CLI,
          Cursor IDE, and Hermes Agent credential stores and subagent/trajectory JSONL files while
          avoiding false positives on bundled OSS skill documentation and standard web configs.
*/

rule ODIN_Claude_Credentials_And_Subagent_Compaction
{
    meta:
        author = "Odin by Brandefense"
        classification = "TLP:CLEAR"
        date = "2026-11-18"
        description = "Detects exposed Claude Code .credentials.json OAuth stores and acompact/aside_question subagent JSONL transcripts"
        mitre_attack = "T1552.001"
    strings:
        $oauth1 = "\"claudeAiOauth\"" ascii
        $oauth2 = "\"accessToken\"" ascii
        $oauth3 = "\"refreshToken\"" ascii
        $sub_parent = "\"parentUuid\"" ascii
        $sub_side = "\"isSidechain\":true" ascii
        $sub_acompact = "acompact-" ascii
        $sub_aside = "aside_question-" ascii
    condition:
        filesize < 20MB and (
            ($oauth1 and $oauth2 and $oauth3) or
            ($sub_parent and $sub_side and ($sub_acompact or $sub_aside))
        )
}

rule ODIN_OpenClaw_Trajectory_AuthProfiles_And_DeviceKey
{
    meta:
        author = "Odin by Brandefense"
        classification = "TLP:CLEAR"
        date = "2026-11-18"
        description = "Detects exposed OpenClaw auth-profiles.json, device.json private identity keys, and .trajectory.jsonl logs"
        mitre_attack = "T1552.001"
    strings:
        $ap1 = "\"version\":" ascii
        $ap2 = "\"profiles\":" ascii
        $ap3 = "\"provider\":" ascii
        $ap4 = "\"apiKey\"" ascii
        $ap5 = "\"access\"" ascii
        $ap6 = "\"token\"" ascii
        $ap7 = "\"key\"" ascii
        $tr1 = "\"trajectoryVersion\":\"openclaw." ascii
        $tr2 = "\"toolCall\"" ascii
        $dv_id = "\"deviceId\"" ascii
        $dv_ts = "\"createdAtMs\"" ascii
        $dv_pub = "\"publicKey\"" ascii
        $dv_prv = "\"privateKey\"" ascii
        $dv_prvpem = "\"privateKeyPem\"" ascii
        $dv_pem = "-----BEGIN PRIVATE KEY-----" ascii
    condition:
        filesize < 20MB and (
            ($ap1 and $ap2 and $ap3 and ($ap4 or $ap5 or $ap6 or $ap7)) or
            ($tr1 and $tr2) or
            ($dv_id and $dv_ts and (($dv_pub and $dv_prv) or ($dv_prvpem and $dv_pem)))
        )
}

rule ODIN_Codex_Rollout_Auth_And_ShellSnapshot
{
    meta:
        author = "Odin by Brandefense"
        classification = "TLP:CLEAR"
        date = "2026-11-18"
        description = "Detects exposed OpenAI Codex CLI rollout-*.jsonl session logs and auth.json token stores"
        mitre_attack = "T1552.001"
    strings:
        $ro1 = "\"session_meta\"" ascii
        $ro2 = "\"originator\":\"codex_cli" ascii
        $ro3 = "\"response_item\"" ascii
        $au1 = "\"OPENAI_API_KEY\"" ascii
        $au2 = "\"tokens\"" ascii
        $au3 = "\"access_token\"" ascii
        $au4 = "\"refresh_token\"" ascii
        $au5 = "\"id_token\"" ascii
        $au6 = /"OPENAI_API_KEY"[ \t]*:[ \t]*"sk-/ ascii
    condition:
        filesize < 20MB and (
            ($ro1 and $ro2 and $ro3) or
            ($au6) or
            ($au2 and $au3 and $au4) or
            ($au1 and $au2 and ($au3 or $au4 or $au5))
        )
}

rule ODIN_Cursor_MCP_Transcript_And_Hermes_SessionDump
{
    meta:
        author = "Odin by Brandefense"
        classification = "TLP:CLEAR"
        date = "2026-11-18"
        description = "Detects exposed Cursor mcp.json with embedded secrets, Cursor agent-transcripts JSONL, and Hermes request_dump or auth.json credential stores (version/providers/credential_pool layout)"
        mitre_attack = "T1552.001"
    strings:
        $mcp1 = "\"mcpServers\"" ascii
        $mcp2 = "\"env\"" ascii
        $mcp_sec1 = "DATABASE_URL" ascii
        $mcp_sec2 = "API_KEY" ascii
        $mcp_sec3 = "GITHUB_PERSONAL_ACCESS_TOKEN" ascii
        $cur1 = "\"cursorAgentTranscript\":true" ascii
        $cur2 = "\"composerId\"" ascii
        $her_d1 = "\"hermes_agent_version\"" ascii
        $her_d2 = "\"request_dump_id\"" ascii
        $her_r1 = "\"session_id\"" ascii
        $her_r2 = "\"reason\"" ascii
        $her_r3 = "\"request\"" ascii
        $her_r4 = "\"messages\"" ascii
        $her_p1 = "\"version\"" ascii
        $her_p2 = "\"providers\"" ascii
        $her_p3 = "\"active_provider\"" ascii
        $her_p4 = "\"credential_pool\"" ascii
        $her_p5 = "\"access_token\"" ascii
        $her_p6 = "\"refresh_token\"" ascii
        $her_p7 = "\"agent_key\"" ascii
        $her_p8 = "\"api_key\"" ascii
    condition:
        filesize < 20MB and (
            ($mcp1 and $mcp2 and ($mcp_sec1 or $mcp_sec2 or $mcp_sec3)) or
            ($cur1 and $cur2) or
            ($her_d1 and $her_d2) or
            ($her_r1 and $her_r2 and $her_r3 and $her_r4) or
            ($her_p1 and $her_p2 and ($her_p3 or $her_p4) and ($her_p5 or $her_p6 or $her_p7 or $her_p8))
        )
}
