threat_patterns.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """Shared threat-pattern library (prompt injection / promptware / exfiltration) for
  2. ``agent/prompt_builder.py``, ``tools/memory_tool.py`` and ``agent/tool_dispatch_helpers.py``.
  3. Each pattern is ``(regex, pattern_id, scope)``; scope is cumulative: ``"all"`` everywhere,
  4. ``"context"`` adds promptware / C2 / role hijack for context files, memory and tool results
  5. (warn-level: that content is not user-authored), ``"strict"`` adds aggressive checks only for
  6. user-mediated writes (memory, skill installs) where a block is resolvable. New patterns must
  7. anchor on C2 vocabulary or unambiguous attack behavior, NOT bossy English ("you must" is common
  8. in legitimate AGENTS.md); filler between tokens is the bounded ``_FILLER``."""
  9. from __future__ import annotations
  10. import re
  11. import unicodedata
  12. from typing import List, Optional, Tuple
  13. # Hard cap on scanned text: scanners are advisory, so bound worst-case runtime.
  14. MAX_SCAN_CHARS = 65_536
  15. # Bounded filler between key attack words (unbounded ``(?:\w+\s+)*`` backtracks badly).
  16. _FILLER = r"(?:\w+\s+){0,8}"
  17. # Env var reference ending in a secret-ish suffix (see exfil comment below).
  18. _SECRET_VAR = r"\$\{?\w*(?:KEY|TOKEN|SECRET|PASSWORD|CREDENTIAL)S?\b"
  19. # Verb prefix for "modify agent config" patterns.
  20. _MODIFY = r"(update|modify|edit|write|change|append|add\s+to)\s+[^\n]{0,2048}"
  21. # (regex, pattern_id, scope); scope ∈ {"all", "context", "strict"}
  22. _PATTERNS: list[tuple[str, str, str]] = [
  23. # ── Classic prompt injection (applies everywhere) ────────────────
  24. (rf'ignore\s+{_FILLER}(previous|all|above|prior)\s+{_FILLER}instructions', "prompt_injection", "all"),
  25. (r'system\s+prompt\s+override', "sys_prompt_override", "all"),
  26. (rf'disregard\s+{_FILLER}(your|all|any)\s+{_FILLER}(instructions|rules|guidelines)', "disregard_rules", "all"),
  27. (rf'act\s+as\s+(if|though)\s+{_FILLER}you\s+{_FILLER}(have\s+no|don\'t\s+have)\s+{_FILLER}(restrictions|limits|rules)', "bypass_restrictions", "all"),
  28. (r'<!--[^>]{0,512}(?:ignore|override|system|secret|hidden)[^>]{0,512}-->', "html_comment_injection", "all"),
  29. (r'<\s*div\s+style\s*=\s*["\'][^>]{0,2048}display\s*:\s*none', "hidden_div", "all"),
  30. (
  31. r"translate\s+[^\n]{0,512}\s+into\s+\w+(?:[\s-]+\w+){0,2}\s+and\s+(execute|run|eval)\b",
  32. "translate_execute",
  33. "all",
  34. ),
  35. (rf'do\s+not\s+{_FILLER}tell\s+{_FILLER}the\s+user', "deception_hide", "all"),
  36. # ── Role-play / identity hijack (scraped web content, poisoned context files) ──
  37. (rf'you\s+are\s+{_FILLER}now\s+(?:a|an|the)\s+', "role_hijack", "context"),
  38. (rf'pretend\s+{_FILLER}(you\s+are|to\s+be)\s+', "role_pretend", "context"),
  39. (rf'output\s+{_FILLER}(system|initial)\s+prompt', "leak_system_prompt", "context"),
  40. (rf'(respond|answer|reply)\s+without\s+{_FILLER}(restrictions|limitations|filters|safety)', "remove_filters", "context"),
  41. (rf'you\s+have\s+been\s+{_FILLER}(updated|upgraded|patched)\s+to', "fake_update", "context"),
  42. # Brainworm tell: identity override via spec. Verb pair anchored so "name your variables" is safe.
  43. (r'\bname\s+yourself\s+\w+', "identity_override", "context"),
  44. # ── C2 / Brainworm-style promptware (context scope) ──────────────
  45. # Anchored on C2 vocabulary. "register as a node" appears in legitimate distributed-systems
  46. # docs, so this is WARN not block: a researcher reading the Brainworm post keeps their session.
  47. (r'register\s+(as\s+)?a?\s*node', "c2_node_registration", "context"),
  48. (r'(heartbeat|beacon|check[\s\-]?in)\s+(to|with)\s+', "c2_heartbeat", "context"),
  49. (r'pull\s+(down\s+)?(?:new\s+)?task(?:ing|s)?\b', "c2_task_pull", "context"),
  50. (r'connect\s+to\s+the\s+network\b', "c2_network_connect", "context"),
  51. # C2-specific verbs avoid the broader "you must X" false positive.
  52. (r'you\s+must\s+(?:\w+\s+){0,3}(register|connect|report|beacon)\b', "forced_action", "context"),
  53. # Anti-forensic instructions: near-zero false positive in legitimate content.
  54. (r'only\s+use\s+one[\s\-]?liners?\b', "anti_forensic_oneliner", "context"),
  55. (rf'never\s+{_FILLER}(?:create|write)\s+{_FILLER}(?:script|file)\s+{_FILLER}disk', "anti_forensic_disk", "context"),
  56. # Unsetting agent-runtime env vars is pure attack behavior (Brainworm sub-session bypass).
  57. (r'unset\s+\w*(?:CLAUDE|CODEX|HERMES|AGENT|OPENAI|ANTHROPIC)\w*', "env_var_unset_agent", "context"),
  58. # ── Known C2 / red-team framework names (warn-only) ─────────────
  59. # Every token must be a distinctive offensive-security brand: a common English word here
  60. # (e.g. "praxis", also a legitimate agent name) false-positives whole AGENTS.md / SOUL.md files.
  61. (r'\b(?:cobalt\s*strike|sliver|havoc|mythic|metasploit|brainworm)\b', "known_c2_framework", "context"),
  62. (r'\bc2\s+(?:server|channel|infrastructure|beacon)\b', "c2_explicit", "context"),
  63. (r'\bcommand\s+and\s+control\b', "c2_explicit_long", "context"),
  64. # ── Exfiltration via curl/wget/cat with secrets (applies everywhere) ──
  65. # The var name ends with \b so benign names containing KEY/TOKEN as substrings
  66. # ($TRILLIUM_ETAPI_URL) pass. API is deliberately absent: mid-name API is ubiquitous in
  67. # benign vars, and every real secret it caught ($OPENAI_API_KEY) already ends in KEY/TOKEN.
  68. (rf'curl\s+[^\n]{{0,2048}}{_SECRET_VAR}', "exfil_curl", "all"),
  69. (rf'wget\s+[^\n]{{0,2048}}{_SECRET_VAR}', "exfil_wget", "all"),
  70. (r'cat\s+[^\n]{0,2048}(\.env|credentials|\.netrc|\.pgpass|\.npmrc|\.pypirc)', "read_secrets", "all"),
  71. (r'(send|post|upload|transmit)\s+[^\n]{0,2048}\s+(to|at)\s+https?://', "send_to_url", "strict"),
  72. (rf'(include|output|print|share)\s+{_FILLER}(conversation|chat\s+history|previous\s+messages|full\s+context|entire\s+context)', "context_exfil", "strict"),
  73. # ── Persistence / SSH backdoor (strict scope — memory + skills) ──
  74. (r'authorized_keys', "ssh_backdoor", "strict"),
  75. # Write-verb gated like the *_config_mod rules: a bare path match blocked ordinary docs
  76. # ("check $HOME/.ssh is chmod 700"). ``>>?`` covers a leading redirect with no verb word;
  77. # ``open(`` covers the scripted-write shape; chmod/chown/sed/truncate/rm/touch/curl/wget/git
  78. # mutate the directory without an obvious copy verb.
  79. (r'(?:\b(?:echo|cat|cp|mv|dd|tee|install|printf|rsync|scp|ln|append|add|write'
  80. r'|sed|chmod|chown|truncate|rm|touch|curl|wget|git)\b|\bopen\s*\(|>>?)'
  81. r'[^\n]{0,512}(?:\$HOME/\.ssh|~/\.ssh)', "ssh_access", "strict"),
  82. (r'\$HOME/\.hermes/\.env|\~/\.hermes/\.env', "hermes_env", "strict"),
  83. (rf'{_MODIFY}(?:AGENTS\.md|CLAUDE\.md|\.cursorrules|\.clinerules)', "agent_config_mod", "strict"),
  84. (rf'{_MODIFY}\.hermes/(config\.yaml|SOUL\.md)', "hermes_config_mod", "strict"),
  85. # ── Hardcoded secrets ────────────────────────────────────────────
  86. # The lookahead skips a value that is itself an environment-variable NAME
  87. # (SHOUTY_SNAKE, ≥2 underscore-separated segments): ENV_PASSWORD =
  88. # "MYPLUGIN_APP_PASSWORD" says where the credential lives, it does not embed
  89. # one (#116221). Scoped case-sensitive on purpose — the pattern compiles with
  90. # IGNORECASE and a lowercase snake value is the password-passphrase shape
  91. # ("correct_horse_battery_staple"); requiring an underscore segment keeps
  92. # underscore-free all-caps credentials (AWS AKIA…, base32) matched.
  93. (r'(?:api[_-]?key|token|secret|password)\s*[=:]\s*["\']'
  94. r'(?!(?-i:[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)+)["\'])'
  95. r'[A-Za-z0-9+/=_-]{20,}', "hardcoded_secret", "strict"),
  96. ]
  97. # Invisible / bidirectional unicode used in injection attacks (aligned with skills_guard.py
  98. # INVISIBLE_CHARS): zero-width space/non-joiner/joiner, word joiner, invisible times/separator/
  99. # plus, BOM, LTR/RTL embedding + pop + overrides, LTR/RTL/first-strong isolates + pop.
  100. INVISIBLE_CHARS = frozenset(
  101. "\u200b\u200c\u200d\u2060\u2062\u2063\u2064\ufeff"
  102. "\u202a\u202b\u202c\u202d\u202e\u2066\u2067\u2068\u2069")
  103. # Compiled per scope at import; inclusion is cumulative (all ⊂ context ⊂ strict).
  104. _SCOPE_SETS = {"all": ("all", "context", "strict"), "context": ("context", "strict"), "strict": ("strict",)}
  105. def _compile() -> dict[str, list[tuple[re.Pattern, str]]]:
  106. compiled: dict[str, list[tuple[re.Pattern, str]]] = {"all": [], "context": [], "strict": []}
  107. for pattern, pid, scope in _PATTERNS:
  108. if scope not in _SCOPE_SETS:
  109. raise ValueError(f"threat_patterns: unknown scope {scope!r} for pattern {pid!r}")
  110. for s in _SCOPE_SETS[scope]:
  111. compiled[s].append((re.compile(pattern, re.IGNORECASE), pid))
  112. return compiled
  113. _COMPILED = _compile()
  114. def scan_for_threats(content: str, scope: str = "context") -> list[str]:
  115. """Matched pattern IDs in ``content`` for ``scope``; invisible codepoints are
  116. reported as ``"invisible_unicode_U+XXXX"``. Raises ValueError on an unknown scope."""
  117. if not content:
  118. return []
  119. if (patterns := _COMPILED.get(scope)) is None:
  120. raise ValueError(f"scan_for_threats: unknown scope {scope!r}")
  121. content = content[:MAX_SCAN_CHARS]
  122. # Invisible unicode is checked on the RAW content: NFKC below can strip these codepoints.
  123. findings: list[str] = [f"invisible_unicode_U+{ord(ch):04X}" for ch in set(content) & INVISIBLE_CHARS]
  124. # NFKC folds full-width / compatibility variants (cat → cat) against homograph bypass.
  125. # It does NOT fold cross-script confusables (Cyrillic ``а``) — that needs a TR#39 database.
  126. normalised = unicodedata.normalize("NFKC", content)
  127. findings.extend(pid for compiled, pid in patterns if compiled.search(normalised))
  128. return findings
  129. def first_threat_message(content: str, scope: str = "strict") -> Optional[str]:
  130. """User-facing error for the first threat found, or None (block-on-first-hit paths)."""
  131. findings = scan_for_threats(content, scope=scope)
  132. if not findings:
  133. return None
  134. pid = findings[0]
  135. if pid.startswith("invisible_unicode_"):
  136. codepoint = pid.replace("invisible_unicode_", "")
  137. return f"Blocked: content contains invisible unicode character {codepoint} (possible injection)."
  138. return (f"Blocked: content matches threat pattern '{pid}'. "
  139. f"Content is injected into the system prompt and must not contain "
  140. f"injection or exfiltration payloads.")
  141. __all__ = ["INVISIBLE_CHARS", "MAX_SCAN_CHARS", "first_threat_message", "scan_for_threats"]