Skip to content

alexgreensh/repo-forensics / skills/repo-forensics

repo-forensics

Flagged

Security forensics for git repos, AI skills, and MCP servers. Audits dependencies, detects prompt injection, credential theft, runtime dynamism, manifest drift, known CVEs, CISA KEV (actively exploited) vulns, and 2026 attack patterns. Not for fixing vulnerabilities or pentesting.

FileKindSize
.gitignoreother193 B
SKILL.mdskill md43.4 KB
checksums.jsonconfig10.8 KB
checksums.json.sigother64 B
data/README.mdmarkdown1.3 KB
data/compromised_versions.jsonconfig188.3 KB
data/rule_ids.csvother35.3 KB
data/rulepacks/.gitkeepother221 B
data/rulepacks/dead_anchors.jsonconfig9.0 KB
data/rulepacks/mcp_security.jsonconfig31.2 KB
data/rulepacks/runtime_dynamism.jsonconfig46.7 KB
data/rulepacks/sast.jsonconfig107.6 KB
data/rulepacks/secrets.jsonconfig30.4 KB
data/rulepacks/shared.jsonconfig4.4 KB
data/rulepacks/skill_threats.jsonconfig95.4 KB
data/runtime_floors.jsonconfig2.3 KB
data/yara/cryptominers.yarother1.8 KB
data/yara/hacktools.yarother2.2 KB
data/yara/malware.yarother2.2 KB
data/yara/manifest.jsonconfig8.0 KB
data/yara/webshells.yarother2.5 KB
references/cursor-hook-contract.mdmarkdown5.6 KB
references/kernel_privesc_sandbox_pack.mdmarkdown5.0 KB
references/mcp-attack-patterns.mdmarkdown8.7 KB
references/research_sources.mdmarkdown8.0 KB
references/threat_patterns.mdmarkdown20.3 KB
scripts/_context_gate.pyscript21.7 KB
scripts/_ed25519.pyscript5.1 KB
scripts/_pyc_unmarshal.pyscript17.1 KB
scripts/_shared_patterns.pyscript4.8 KB
scripts/adjudication.pyscript16.4 KB
scripts/adjudication_bridge.pyscript1.0 KB
scripts/aggregate_json.pyscript69.2 KB
scripts/auto_scan.pyexecutablescript152.1 KB
scripts/corpus_sync.pyscript10.0 KB
scripts/cursor_pre_scan.pyscript22.5 KB
scripts/dast_sandbox.sbother1.6 KB
scripts/dead_anchors_extract.pyscript14.8 KB
scripts/dead_anchors_probe.pyscript27.1 KB
scripts/forensics_core.pyscript176.5 KB
scripts/gen_rule_ids.pyscript8.6 KB
scripts/hook_adapter.pyscript19.4 KB
scripts/ioc_manager.pyscript49.6 KB
scripts/parse_pnpm_lock.pyscript7.9 KB
scripts/pre_scan.pyscript143.5 KB
scripts/pypi_pins.pyscript8.1 KB
scripts/refresh_controller.pyexecutablescript48.4 KB
scripts/refresh_threat_dbs.pyexecutablescript19.4 KB
scripts/rule_loader.pyscript44.8 KB
scripts/rulepack_feed.pyscript26.4 KB
scripts/run_forensics.shexecutablescript20.3 KB
scripts/sarif_export.pyscript18.6 KB
scripts/scan_agent_skills.pyscript33.2 KB
scripts/scan_archive.pyscript29.1 KB
scripts/scan_ast.pyscript44.9 KB
scripts/scan_binary.pyscript10.9 KB
scripts/scan_bytecode.pyscript28.2 KB
scripts/scan_dast.pyscript28.8 KB
scripts/scan_dataflow.pyscript15.4 KB
scripts/scan_dead_anchors.pyscript15.9 KB
scripts/scan_decode.pyscript37.5 KB
scripts/scan_dependencies.pyscript124.5 KB
scripts/scan_devcontainer.pyscript8.5 KB
scripts/scan_entropy.pyscript15.6 KB
scripts/scan_entrypoint.pyscript34.3 KB
scripts/scan_git_config.pyscript96.3 KB
scripts/scan_git_forensics.pyscript24.1 KB
scripts/scan_history.pyscript14.5 KB
scripts/scan_infra.pyscript54.3 KB
scripts/scan_integrity.pyscript41.0 KB
scripts/scan_lifecycle.pyscript65.1 KB
scripts/scan_manifest_drift.pyscript47.0 KB
scripts/scan_mcp_security.pyscript42.3 KB
scripts/scan_oversize.pyscript11.7 KB
scripts/scan_post_incident.pyscript36.9 KB
scripts/scan_provenance.pyscript19.4 KB
scripts/scan_runtime_dynamism.pyscript23.5 KB
scripts/scan_runtime_floors.pyscript33.5 KB
scripts/scan_sast.pyscript59.1 KB
scripts/scan_secrets.pyscript11.9 KB
scripts/scan_skill_threats.pyscript102.4 KB
scripts/scan_splitstream.pyscript29.5 KB
scripts/scan_yara.pyscript19.7 KB
scripts/session_scan.pyscript54.1 KB
scripts/suppressions.pyscript15.4 KB
scripts/validate_manifests.pyscript13.7 KB
scripts/verify_install.pyscript26.2 KB
scripts/vuln_feed.pyscript31.1 KB
tests/conftest.pyscript13.4 KB
tests/corpus/DEAD_ANCHORS_CORPUS.mdmarkdown2.3 KB
tests/corpus/budgets.jsonconfig5.6 KB
tests/cursor_helpers.pyscript12.6 KB
tests/fixtures/legacy_main_blocks.jsonconfig10.3 KB
tests/fixtures/powershell_verdict_corpus.jsonconfig114.9 KB
tests/golden/parity_mcp_security.jsonconfig17.5 KB
tests/golden/parity_runtime_dynamism.jsonconfig31.7 KB
tests/golden/parity_sast.jsonconfig59.8 KB
tests/golden/parity_secrets.jsonconfig18.8 KB
tests/golden/parity_skill_threats.jsonconfig48.8 KB
tests/parity_corpus.pyscript6.3 KB
tests/powershell_hook.pyscript8.2 KB
tests/report_builders.pyscript5.0 KB
tests/schemas/sarif-schema-2.1.0.jsonconfig109.1 KB
tests/shell_compat.pyscript2.2 KB
tests/test_adjudication_bridge.pyscript832 B
tests/test_adjudication_containment.pyscript1.4 KB
tests/test_adjudication_output.pyscript20.0 KB
tests/test_adjudication_suppressions.pyscript18.7 KB
tests/test_advisory_adjudication.pyscript1.3 KB
tests/test_agent_relay_rules.pyscript7.8 KB
tests/test_aggregate_json.pyscript10.0 KB
tests/test_auto_scan_patterns.pyscript10.5 KB
tests/test_benign_corpus.pyscript16.4 KB
tests/test_benign_new_scanners.pyscript6.1 KB
tests/test_build_output_skip.pyscript10.6 KB
tests/test_capability_ledger.pyscript18.8 KB
tests/test_codex_install.pyscript10.0 KB
tests/test_codex_marketplace.pyscript3.3 KB
tests/test_comment_false_positives.pyscript2.2 KB
tests/test_composer_dependencies.pyscript4.0 KB
tests/test_compromised_versions.pyscript51.2 KB
tests/test_context_gate.pyscript32.4 KB
tests/test_coverage_status.pyscript7.6 KB
tests/test_css_compounding_shrink.pyscript20.8 KB
tests/test_cursor_adapter.pyscript7.8 KB
tests/test_cursor_benign.pyscript3.4 KB
tests/test_cursor_degrade.pyscript12.7 KB
tests/test_cursor_install.pyscript17.1 KB
tests/test_cursor_latency.pyscript7.2 KB
tests/test_cursor_parity.pyscript5.8 KB
tests/test_cursor_redteam_fn.pyscript4.9 KB
tests/test_cursor_tamper_modify.pyscript14.2 KB
tests/test_cursor_wiring.pyscript32.3 KB
tests/test_d1_yara_correlation.pyscript9.2 KB
tests/test_dead_anchors_extract.pyscript7.6 KB
tests/test_dead_anchors_probe.pyscript19.7 KB
tests/test_evidence_engine_parity.pyscript8.1 KB
tests/test_evidence_model.pyscript11.4 KB
tests/test_forensics_core.pyscript79.2 KB
tests/test_fp_hardening_r3.pyscript9.7 KB
tests/test_fp_hardening_r4.pyscript18.8 KB
tests/test_fp_regression_pairs.pyscript62.5 KB
tests/test_fp_structural.pyscript21.2 KB
tests/test_freshness.pyscript6.2 KB
tests/test_frontmatter_validity.pyscript7.0 KB
tests/test_gpg_rce_hotfix.pyscript4.8 KB
tests/test_hook_inert_data.pyscript6.1 KB
tests/test_hook_scripts.pyscript14.0 KB
tests/test_inspection_cpu_budget.pyscript11.6 KB
tests/test_ioc_manager.pyscript55.4 KB
tests/test_kernel_privesc_rules.pyscript27.8 KB
tests/test_legit_precision_regressions.pyscript6.8 KB
tests/test_lethal_trifecta.pyscript20.7 KB
tests/test_manifest_signature.pyscript1.5 KB
tests/test_manifest_validity.pyscript2.9 KB
tests/test_monotonic_trust_e2e.pyscript4.4 KB
tests/test_non_breaking_contract.pyscript22.7 KB
tests/test_openclaw_install.pyscript6.6 KB
tests/test_pack_parity.pyscript24.8 KB
tests/test_parse_pnpm_lock.pyscript10.7 KB
tests/test_plugin_checkout_provenance.pyscript11.1 KB
tests/test_powershell_hook_wire.pyscript10.6 KB
tests/test_powershell_verdict_corpus.pyscript6.2 KB
tests/test_pr44_hardening.pyscript6.5 KB
tests/test_pre_scan.pyscript19.6 KB
tests/test_pre_scan_composer.pyscript7.4 KB
tests/test_precondition_fields.pyscript4.6 KB
tests/test_provenance_offline.pyscript2.5 KB
tests/test_published_rulepack_feed_current.pyscript1.4 KB
tests/test_py38_compat.pyscript3.3 KB
tests/test_pypi_pinned_versions.pyscript19.3 KB
tests/test_pyproject_string_forms.pyscript2.2 KB
tests/test_redteam_fn_regression.pyscript32.5 KB
tests/test_reflective_fetch_execute.pyscript23.5 KB
tests/test_reflective_review_fixes.pyscript17.8 KB
tests/test_refresh_controller.pyscript19.9 KB
tests/test_refresh_rulepacks.pyscript12.4 KB
tests/test_review_gate_regressions.pyscript9.3 KB
tests/test_rule_loader.pyscript23.2 KB
tests/test_rulepack_feed.pyscript38.1 KB
tests/test_run_forensics_json.pyscript1012 B
tests/test_run_scanner_fail_loud.pyscript6.5 KB
tests/test_runtime_floors.pyscript19.8 KB
tests/test_sandbox_probing_rules.pyscript9.6 KB
tests/test_sarif_export.pyscript19.9 KB
tests/test_scan_agent_skills.pyscript31.3 KB
tests/test_scan_archive.pyscript11.6 KB
tests/test_scan_ast.pyscript12.8 KB
tests/test_scan_binary.pyscript15.3 KB
tests/test_scan_bytecode.pyscript21.5 KB
tests/test_scan_dast.pyscript32.1 KB
tests/test_scan_dataflow.pyscript7.1 KB
tests/test_scan_dead_anchors.pyscript11.5 KB
tests/test_scan_decode.pyscript28.0 KB
tests/test_scan_dependencies.pyscript37.8 KB
tests/test_scan_devcontainer.pyscript8.5 KB
tests/test_scan_entropy.pyscript11.2 KB
tests/test_scan_entrypoint.pyscript19.5 KB
tests/test_scan_git_config.pyscript58.5 KB
tests/test_scan_git_config_review_fixes.pyscript33.3 KB

scripts/scan_agent_skills.py

View on GitHub
#!/usr/bin/env python3
"""
scan_agent_skills.py - Agent Skill Security Scanner
Scans AI agent skills and plugins across ecosystems: Claude Code, OpenClaw,
Codex, Cursor, and generic MCP servers.
Detects: frontmatter abuse, tools.json poisoning, agent config injection,
.clawhubignore bypass, and ClawHavoc delivery patterns.

Sources: Koi Security (ClawHavoc), OWASP MCP Top 10 (2026), Snyk ToxicSkills.
Created by Alex Greenshpun
"""
import json
import os
import re
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import forensics_core as core
import rule_loader
from _shared_patterns import (
    AUTO_EXEC_PATHS_RE, GIT_UPDATABLE, REF_FILE_EXTS_RE,
    REF_VERBS_RE, SEED_FILES, WRITE_VERBS_RE,
)

SCANNER_NAME = "agent_skills"
_F = core.Finding  # shorthand

# --- Prompt-injection rules: shared with scan_skill_threats (U5 adoption) ---
# Historically agent_skills carried its own 9-pattern PROMPT_INJECTION_RE while
# scan_skill_threats had 13 (incl. LITL). The two diverged. We now source the
# prompt-injection regexes from the skill_threats rule pack so both scanners
# share one authoritative set. This is a DELIBERATE coverage gain: agent_skills
# gains the ~6 patterns it lacked (3 LITL + confirmation-bypass + concealment +
# "new system prompt"); agent_skills' 2 historical uniques
# ("new (system )?instructions" -> ST-PI-019, "pretend you are" -> ST-PI-020)
# were added to the shared pack so BOTH scanners keep them. The consumption
# sites below treat PROMPT_INJECTION_RE as a list of compiled regexes (search +
# break), exactly as before, so only the source of the list changed.
_SKILL_THREATS_PACK = rule_loader.load_pack("skill_threats")
PROMPT_INJECTION_PACK_ERROR = _SKILL_THREATS_PACK is None
PROMPT_INJECTION_RE = [
    rule.regex for rule in (_SKILL_THREATS_PACK.all_rules
                            if _SKILL_THREATS_PACK is not None else [])
    if rule.category == "prompt-injection"
]
CRED_ACCESS_RE = [
    re.compile(r'(?i)(os\.environ|process\.env)'), re.compile(r'(?i)\bAPI_KEY\b'),
    re.compile(r'(?i)\b(token|password|secret|credential)\s*[=:]'),
    re.compile(r'(?i)~/\.ssh\b'), re.compile(r'(?i)\bkeychain\b'), re.compile(r'(?i)\b\.env\b'),
]
TOOL_INJECTION_KW = [
    (re.compile(r'(?i)\bIMPORTANT\b'), "IMPORTANT directive in tool metadata", "critical"),
    (re.compile(r'(?i)ignore\s+previous\b'), "Instruction override in tool metadata", "critical"),
    (re.compile(r'(?i)you\s+must\b'), "Coercive directive in tool metadata", "high"),
    (re.compile(r'(?i)do\s+not\s+tell\b'), "Concealment directive in tool metadata", "high"),
    (re.compile(r'(?i)\bsystem:\b'), "System role injection in tool metadata", "critical"),
    (re.compile(r'(?i)\bassistant:\b'), "Assistant role injection in tool metadata", "critical"),
]
CRED_FIELD_RE = re.compile(r'(?i)(api_key|token|password|secret|credential|auth)')
BROAD_TRIGGERS = {'help', 'search', 'code', 'write', 'run', 'build', 'fix', 'test', 'chat', 'ask'}
IGNORE_EXEC_RE = [re.compile(p) for p in [
    r'^\*\.py$', r'^\*\.js$', r'^\*\.sh$', r'^\*\.ts$', r'^hooks/?$', r'^scripts/?$', r'^\.env$']]
IGNORE_WILDCARDS = {'*', '**/*', '**'}
CLAWHAVOC = [
    (re.compile(r'(?i)(OpenClawDriver|ClawDriver)'), "critical", "Fake prerequisite driver reference (ClawHavoc campaign)"),
    (re.compile(r'(?i)install\.app-distribution\.net'), "critical", "Known AMOS delivery domain (ClawHavoc campaign)"),
    (re.compile(r'(?i)base64\s+(-D|--decode)\s*\|\s*(bash|sh)'), "critical", "Base64 decode piped to shell execution"),
    (re.compile(r'(?i)pass(word)?:\s*openclaw'), "high", "Password-protected archive with OpenClaw password"),
]


def _read(path):
    try:
        with open(path, 'r', encoding='utf-8', errors='ignore') as f:
            return f.read()
    except (OSError, UnicodeDecodeError):
        return None


def is_agent_skill(repo_path):
    """Return True if repo looks like an AI agent skill, plugin, or extension.
    Covers: Claude Code, OpenClaw/NanoClaw/ClawHub, Codex, Cursor, generic MCP.
    """
    # OpenClaw/ClawHub markers
    for name in ('.clawhubignore', '.clawdhubignore', 'SOUL.md',
                 'USER.md', 'IDENTITY.md', 'HEARTBEAT.md', 'BOOT.md', 'BOOTSTRAP.md',
                 'openclaw.plugin.json'):
        if os.path.isfile(os.path.join(repo_path, name)):
            return True
    # Claude Code skill/plugin markers
    if os.path.isdir(os.path.join(repo_path, '.claude')):
        return True
    if os.path.isdir(os.path.join(repo_path, '.claude-plugin')):
        return True
    # Codex markers
    if os.path.isfile(os.path.join(repo_path, 'codex.json')):
        return True
    if os.path.isdir(os.path.join(repo_path, '.codex')):
        return True
    # Cursor extension markers
    if os.path.isdir(os.path.join(repo_path, '.cursor')):
        return True
    # AGENTS.md (used by multiple agent frameworks)
    if os.path.isfile(os.path.join(repo_path, 'AGENTS.md')):
        return True
    # SKILL.md (any agent skill with frontmatter or without)
    if os.path.isfile(os.path.join(repo_path, 'SKILL.md')):
        return True
    # package.json with openclaw namespace
    pkg_json = os.path.join(repo_path, 'package.json')
    if os.path.isfile(pkg_json):
        content = _read(pkg_json)
        if content:
            try:
                data = json.loads(content)
                if 'openclaw' in data and 'extensions' in data.get('openclaw', {}):
                    return True
            except (json.JSONDecodeError, TypeError):
                pass
    # tools.json with MCP-style tool definitions (any MCP server)
    tools_json = os.path.join(repo_path, 'tools.json')
    if os.path.isfile(tools_json):
        content = _read(tools_json)
        if content:
            try:
                data = json.loads(content)
                tools = data if isinstance(data, list) else data.get('tools', [])
                if tools and isinstance(tools[0], dict) and ('inputSchema' in tools[0] or 'description' in tools[0]):
                    return True
            except (json.JSONDecodeError, IndexError, TypeError):
                pass
    # MCP config files (.mcp.json, mcp.json)
    for name in ('.mcp.json', 'mcp.json'):
        if os.path.isfile(os.path.join(repo_path, name)):
            return True
    return False


def scan_frontmatter(repo_path):
    """Cat 1: Parse SKILL.md frontmatter, validate name/author/triggers/description."""
    findings = []
    content = _read(os.path.join(repo_path, 'SKILL.md'))
    if not content:
        return findings
    fm_match = re.match(r'^---\s*\n(.*?)\n---', content, re.DOTALL)
    if not fm_match:
        return findings
    fm_text = fm_match.group(1)
    fields = {}
    for line in fm_text.split('\n'):
        m = re.match(r'^(\w[\w-]*):\s*(.*)', line)
        if m:
            fields[m.group(1).lower()] = m.group(2).strip()
    if not fields.get('name'):
        findings.append(_F(SCANNER_NAME, "high", "Missing skill name in frontmatter",
            "SKILL.md frontmatter has no 'name' field.", "SKILL.md", 1, fm_text[:120], "frontmatter"))
    if not fields.get('author'):
        # Note: 'author' is not an official OpenClaw frontmatter field (identity comes from
        # ClawHub account), but unattributed skills in the wild are a supply-chain signal.
        findings.append(_F(SCANNER_NAME, "medium", "Missing skill author in frontmatter (unattributed skill)",
            "No author field in frontmatter. In OpenClaw, author comes from ClawHub account, but standalone skills should declare authorship.",
            "SKILL.md", 1, fm_text[:120], "frontmatter"))
    if 'triggers' in fields:
        raw = fields['triggers']
        for w in re.findall(r'[\w]+', raw.lower()):
            if w in BROAD_TRIGGERS:
                findings.append(_F(SCANNER_NAME, "medium", f"Overly broad trigger keyword: '{w}'",
                    f"Trigger '{w}' matches too many intents, risking skill hijacking.",
                    "SKILL.md", 1, raw[:120], "frontmatter"))
    desc = fields.get('description', '')
    for pat in PROMPT_INJECTION_RE:
        if pat.search(desc):
            findings.append(_F(SCANNER_NAME, "high", "Prompt injection in skill description",
                "Frontmatter description contains prompt injection.", "SKILL.md", 1, desc[:120], "frontmatter"))
            break
    return findings


def scan_tools_json(repo_path):
    """Cat 2: Check tools.json for schema poisoning and credential fields."""
    findings = []
    path = os.path.join(repo_path, 'tools.json')
    raw = _read(path)
    if not raw:
        return findings
    try:
        data = json.loads(raw)
    except json.JSONDecodeError:
        findings.append(_F(SCANNER_NAME, "medium", "Unparseable tools.json",
            "tools.json could not be parsed as JSON. Manual review required.",
            "tools.json", 0, raw[:80], "tool-poisoning"))
        return findings
    tools = data if isinstance(data, list) else data.get('tools', [data]) if isinstance(data, dict) else []
    raw_lines = raw.split('\n')
    for idx, tool in enumerate(tools):
        if not isinstance(tool, dict):
            continue
        tname = tool.get('name', f'#{idx}')
        for fld in ('description', 'name', 'title', 'summary'):
            val = tool.get(fld, '')
            if not isinstance(val, str):
                continue
            for pat, title, sev in TOOL_INJECTION_KW:
                if pat.search(val):
                    ln = next((i+1 for i, line in enumerate(raw_lines) if val[:40] in line), 0)
                    findings.append(_F(SCANNER_NAME, sev, title,
                        f"Tool '{tname}' field '{fld}' contains injection pattern.",
                        "tools.json", ln, val[:120], "tool-poisoning"))
        schema = tool.get('inputSchema', {})
        if isinstance(schema, dict):
            for prop in (schema.get('properties', {}) or {}):
                if CRED_FIELD_RE.search(prop):
                    findings.append(_F(SCANNER_NAME, "high", "Tool requests credential input",
                        f"Tool '{tname}' has credential-type input '{prop}'.",
                        "tools.json", 0, f"inputSchema.properties.{prop}", "tool-poisoning"))
    return findings


def scan_agent_configs(repo_path):
    """Cat 3: Scan SOUL.md, AGENTS.md, CLAUDE.md, memory/*.md for injection + credential access."""
    findings = []
    targets = ['SOUL.md', 'AGENTS.md', 'CLAUDE.md']
    mem_dir = os.path.join(repo_path, 'memory')
    try:
        if os.path.isdir(mem_dir):
            targets += [os.path.join('memory', f) for f in os.listdir(mem_dir) if f.endswith('.md')]
    except OSError:
        findings.append(_F(SCANNER_NAME, "medium", "Memory directory unreadable",
            "Could not list memory/ directory - memory files were not scanned for injection.",
            "memory/", 0, "", "agent-access-error"))
    for rel in targets:
        content = _read(os.path.join(repo_path, rel))
        if not content:
            continue
        for i, line in enumerate(content.split('\n')):
            line = core.clip_line(line)
            for pat in PROMPT_INJECTION_RE:
                if pat.search(line):
                    findings.append(_F(SCANNER_NAME, "critical", "Safety override in agent config",
                        f"Prompt injection in {rel}.", rel, i+1, line.strip()[:120], "agent-injection"))
                    break
            for pat in CRED_ACCESS_RE:
                if pat.search(line):
                    findings.append(_F(SCANNER_NAME, "high", "Credential access in agent config",
                        f"Agent config {rel} references credentials.", rel, i+1, line.strip()[:120], "agent-injection"))
                    break
    return findings


def scan_clawhubignore(repo_path):
    """Cat 4: Check .clawhubignore (or legacy .clawdhubignore) for patterns that hide executable code."""
    findings = []
    # Support both current and legacy spelling
    content = _read(os.path.join(repo_path, '.clawhubignore'))
    if not content:
        content = _read(os.path.join(repo_path, '.clawdhubignore'))
    if not content:
        return findings
    for i, raw in enumerate(content.split('\n')):
        line = raw.strip()
        if not line or line.startswith('#'):
            continue
        if line in IGNORE_WILDCARDS:
            findings.append(_F(SCANNER_NAME, "critical", "Wildcard ignore suppresses all ClawHub scanning",
                f"Pattern '{line}' hides ALL files from ClawHub review.",
                ".clawhubignore", i+1, line, "clawhubignore-bypass"))
        else:
            for pat in IGNORE_EXEC_RE:
                if pat.search(line):
                    findings.append(_F(SCANNER_NAME, "high",
                        "Ignore pattern hides executable code from ClawHub scanner",
                        f"Pattern '{line}' hides reviewable files.",
                        ".clawhubignore", i+1, line, "clawhubignore-bypass"))
                    break
    return findings


def scan_clawhavoc(repo_path):
    """Cat 5: Scan OpenClaw-specific files for ClawHavoc delivery patterns.
    Only scans SKILL.md, tools.json, SOUL.md, AGENTS.md to avoid duplicating
    scan_skill_threats.py which already covers the full repo for these IOCs.
    """
    findings = []
    openclaw_files = ['SKILL.md', 'tools.json', 'SOUL.md', 'AGENTS.md']
    for name in openclaw_files:
        file_path = os.path.join(repo_path, name)
        content = _read(file_path)
        if not content:
            continue
        for i, line in enumerate(content.split('\n')):
            line = core.clip_line(line)
            for pat, sev, title in CLAWHAVOC:
                if pat.search(line):
                    findings.append(_F(SCANNER_NAME, sev, title,
                        f"ClawHavoc delivery indicator in {name}",
                        name, i+1, line.strip()[:120], "clawhavoc-delivery"))
    return findings


def scan_plugin_manifest(repo_path):
    """Cat 6: Check openclaw.plugin.json for suspicious patterns."""
    findings = []
    content = _read(os.path.join(repo_path, 'openclaw.plugin.json'))
    if not content:
        return findings
    try:
        data = json.loads(content)
    except json.JSONDecodeError:
        findings.append(_F(SCANNER_NAME, "medium", "Unparseable openclaw.plugin.json",
            "Plugin manifest could not be parsed.", "openclaw.plugin.json", 0, content[:80], "plugin-manifest"))
        return findings
    # Check for missing required fields
    if not data.get('id'):
        findings.append(_F(SCANNER_NAME, "high", "Missing plugin id in manifest",
            "openclaw.plugin.json missing required 'id' field.", "openclaw.plugin.json", 0, "", "plugin-manifest"))
    if not data.get('configSchema'):
        findings.append(_F(SCANNER_NAME, "medium", "Missing configSchema in manifest",
            "openclaw.plugin.json missing required 'configSchema' field.", "openclaw.plugin.json", 0, "", "plugin-manifest"))
    # Check description/name for injection
    for fld in ('name', 'description'):
        val = data.get(fld, '')
        if isinstance(val, str):
            for pat in PROMPT_INJECTION_RE:
                if pat.search(val):
                    findings.append(_F(SCANNER_NAME, "critical", f"Prompt injection in plugin manifest {fld}",
                        f"Plugin manifest '{fld}' contains injection pattern.", "openclaw.plugin.json", 0, val[:120], "plugin-manifest"))
                    break
    return findings


# ============================================================
# Cat 7: Workspace Config Write Requests (high)
# Skills that instruct agents to write to auto-executed config files.
# Source: Terra Security OpenClaw vulnerability research (May 2026)
# ============================================================
_AUTO_EXEC_FILES = AUTO_EXEC_PATHS_RE
_WRITE_VERBS = WRITE_VERBS_RE

CONFIG_WRITE_PATTERNS = [
    (re.compile(r'(?i)\b' + _WRITE_VERBS + r'\b[^\n]{0,60}\b(?:to|in|into)\s+' + _AUTO_EXEC_FILES), "Config write request: modify auto-executed file (Terra Security OpenClaw, May 2026)"),
    (re.compile(r'(?i)\b' + _WRITE_VERBS + r'\b[^\n]{0,40}' + _AUTO_EXEC_FILES), "Config write request: target auto-executed file (Terra Security OpenClaw, May 2026)"),
    (re.compile(r'(?i)\b(?:create|install|register|set\s+up)\s+(?:a\s+)?(?:PreToolUse|PostToolUse|SessionStart|hook)\b'), "Config write request: hook installation directive (Terra Security OpenClaw, May 2026)"),
]


def scan_config_write_requests(repo_path):
    """Cat 7: Detect skills that instruct agents to write to auto-executed config files."""
    findings = []
    for fpath, rel in core.walk_repo(repo_path, skip_lockfiles=False, skip_binary=True):
        ext = os.path.splitext(fpath)[1].lower()
        if ext not in ('.md', '.txt', '.yml', '.yaml', '.toml', '.cfg', '.ini', '.json'):
            continue
        content = _read(fpath)
        if not content:
            continue
        for i, line in enumerate(content.split('\n')):
            line = core.clip_line(line)
            for pat, title in CONFIG_WRITE_PATTERNS:
                if pat.search(line):
                    if re.search(r'(?i)\b(?:users?\s+can|you\s+(?:can|may)|documentation|how\s+to)\b', line):
                        continue
                    findings.append(_F(SCANNER_NAME, "high", title,
                        "Skill instructs agent to write to auto-executed config file",
                        rel, i + 1, line.strip()[:120], "config-write-request"))
                    break
    return findings


# ============================================================
# Cat 8: Trusted File Reference Chains (medium/high)
# Transitive reference chains that create trust-laundering pipelines.
# Source: Terra Security OpenClaw vulnerability research (May 2026)
# ============================================================
_SEED_FILES = SEED_FILES
_GIT_UPDATABLE = GIT_UPDATABLE
_REF_PATTERN = re.compile(r'(?i)\b' + REF_VERBS_RE + r'\s+(\S+\.' + REF_FILE_EXTS_RE + r')\b')
_MAX_CHAIN_DEPTH = 5


def scan_reference_chains(repo_path):
    """Cat 8: Detect transitive file reference chains from seed config files."""
    findings = []
    graph = {}
    for seed in _SEED_FILES:
        seed_path = os.path.join(repo_path, seed)
        content = _read(seed_path)
        if not content:
            continue
        refs = _REF_PATTERN.findall(content)
        if refs:
            graph[seed] = [r for r in refs if r.lower() != seed.lower()]

    for ref_file in list(graph.values()):
        for rf in ref_file:
            rf_path = os.path.join(repo_path, rf)
            content = _read(rf_path)
            if not content:
                continue
            refs = _REF_PATTERN.findall(content)
            if refs:
                graph[rf] = [r for r in refs if r.lower() != rf.lower()]

    for seed in _SEED_FILES:
        if seed not in graph:
            continue
        visited_edges = set()
        _find_chains(seed, graph, [], findings, repo_path, visited_edges)

    return findings


def _find_chains(current, graph, path, findings, repo_path, visited_edges):
    if len(path) >= _MAX_CHAIN_DEPTH:
        return
    for ref in graph.get(current, []):
        if ref in path:
            continue
        edge = (current, ref)
        if edge in visited_edges:
            continue
        visited_edges.add(edge)
        new_path = path + [current]
        chain_depth = len(new_path)
        if chain_depth >= 2:
            terminates_at_updatable = ref.lower() in _GIT_UPDATABLE
            severity = "high" if chain_depth >= 3 or terminates_at_updatable else "medium"
            chain_str = " -> ".join(new_path + [ref])
            findings.append(_F(SCANNER_NAME, severity,
                f"Trusted file reference chain (depth {chain_depth})",
                f"Chain: {chain_str}. Transitive references create a trust-laundering pipeline (Terra Security OpenClaw, May 2026).",
                new_path[0], 0, chain_str[:120], "reference-chain"))
        if ref in graph:
            _find_chains(ref, graph, new_path, findings, repo_path, visited_edges)


# ============================================================
# Cat 9: Memory/RAG Poisoning Indicators (high/critical)
# Content designed to persist in agent memory or RAG corpus as backdoors.
# Source: DeepMind AI Agent Traps "Cognitive State Poisoning" (March 2026)
# ============================================================
MEMORY_POISONING_PATTERNS = [
    (re.compile(r'(?i)(?:memory_store|memory_write|remember|store_memory|add_memory|save_to_memory)\s*\([^)]*(?:ignore|override|system|admin|always|never|must)'), "Memory write with injection keywords (DeepMind Agent Traps, March 2026)"),
    (re.compile(r'(?i)(?:add|write|store|save|persist|remember)\s+(?:to|in|into)\s+(?:memory|knowledge.?base|rag|vector.?store|embedding|index|corpus).*(?:ignore|override|system|always|never|must|from\s+now\s+on)'), "Memory/RAG poisoning: injection payload in memory write (DeepMind Agent Traps, March 2026)"),
    (re.compile(r'(?i)(?:when\s+(?:retrieved|queried|searched|asked|recalled))\s*[,:]\s*(?:always|never|must|ignore|override|instead)'), "RAG trigger: conditional instruction on retrieval (DeepMind Agent Traps, March 2026)"),
    (re.compile(r'(?i)(?:if\s+(?:this|an)\s+(?:document|chunk|passage|entry)\s+is\s+(?:found|retrieved|returned))\s*[,:]\s*(?:always|ignore|execute|run|override)'), "RAG trigger: conditional execution on document retrieval (DeepMind Agent Traps, March 2026)"),
]

PROVENANCE_STRIP_PATTERNS = [
    (re.compile(r'(?i)(?:do\s+not|never|don\'t)\s+(?:include|add|store|record|log)\s+(?:the\s+)?(?:source|origin|provenance|attribution|timestamp|author)'), "Provenance stripping: suppressing attribution (DeepMind Agent Traps, March 2026)"),
    (re.compile(r'(?i)(?:strip|remove|clear|delete)\s+(?:the\s+)?(?:source|origin|provenance|metadata|attribution)[\w\s]{0,30}(?:from|before|when)'), "Provenance stripping: removing metadata (DeepMind Agent Traps, March 2026)"),
]


_MEMORY_SCAN_EXTS = {'.md', '.txt', '.py', '.js', '.ts', '.json', '.yml', '.yaml'}


def scan_memory_poisoning(repo_path):
    """Cat 9: Detect memory/RAG poisoning indicators."""
    findings = []
    for fpath, rel in core.walk_repo(repo_path, skip_binary=True):
        ext = os.path.splitext(fpath)[1].lower()
        if ext not in _MEMORY_SCAN_EXTS:
            continue
        content = _read(fpath)
        if not content:
            continue
        in_code_fence = False
        for i, line in enumerate(content.split('\n')):
            stripped = line.strip()
            if stripped.startswith('```'):
                in_code_fence = not in_code_fence
                continue
            if in_code_fence and ext in ('.md', '.txt'):
                continue
            line = core.clip_line(line)
            for pat, title in MEMORY_POISONING_PATTERNS:
                if pat.search(line):
                    findings.append(_F(SCANNER_NAME, "high", title,
                        "Content designed to persist as backdoor in agent memory or RAG corpus (DeepMind Agent Traps, March 2026)",
                        rel, i + 1, line.strip()[:120], "memory-poisoning"))
                    break
            for pat, title in PROVENANCE_STRIP_PATTERNS:
                if pat.search(line):
                    findings.append(_F(SCANNER_NAME, "high", title,
                        "Provenance stripping enables untraceable memory poisoning (DeepMind Agent Traps, March 2026)",
                        rel, i + 1, line.strip()[:120], "provenance-stripping"))
                    break
    return findings


# ============================================================
# Cat 10: Memory + Network Co-occurrence (high)
# Detects skills that both access user memory/data AND make
# outbound network requests — the exfiltration primitive from
# The Memory Heist (Ayush Paul, July 2026).
#
# The attack: an agent with memory access + web_fetch can be
# tricked by fetched content into encoding user PII (stored in
# memory) into URL paths that the attacker logs server-side.
# The memory store itself is secure; the vulnerability is the
# COMBINATION of memory access + any outbound channel.
# ============================================================

# Memory/data access signals — patterns indicating the skill reads
# from user memory, knowledge bases, conversation history, or PII stores.
_MEMORY_ACCESS_SIGNALS = [
    re.compile(r'(?i)\b(?:total.?recall|recall|memory|knowledge.?base|rag|vector.?store)\b'),
    re.compile(r'(?i)\b(?:conversation.?search|memory.?search|recall.?search)\b'),
    re.compile(r'(?i)\b(?:read.?memory|fetch.?memory|get.?memory|load.?memory|query.?memory)\b'),
    re.compile(r'(?i)\b(?:user.?profile|user.?data|user.?context|personal.?info|PII)\b'),
    re.compile(r'(?i)\b(?:sqlite|\.db\b|database|indexeddb|leveldb)\b'),
    re.compile(r'(?i)\b(?:mcp__recall|mcp__memory|mcp__knowledge)\b'),
]

# Outbound network signals — patterns indicating the skill makes
# outbound HTTP requests, web fetches, or delegates to external services.
_NETWORK_ACCESS_SIGNALS = [
    re.compile(r'(?i)\b(?:web_fetch|web_search|fetch|requests?\.get|requests?\.post)\b'),
    re.compile(r'(?i)\b(?:http\.get|http\.post|http\.request|axios|got\(|node.?fetch)\b'),
    re.compile(r'(?i)\b(?:curl|wget|urllib|urlopen)\b'),
    re.compile(r'(?i)\bhttps?://[^\s\'"]{10,}'),
    re.compile(r'(?i)\b(?:outsource|delegate|openrouter|api\.openai|api\.anthropic)\b'),
    re.compile(r'(?i)\b(?:mcp__.*fetch|mcp__.*search|mcp__.*web)\b'),
]

# In code and config a bare URL or the word "fetch" is metadata (an author
# link, `fetch-depth: 0`); only a call-shaped signal counts as network access.
_CODE_NETWORK_SIGNALS = [
    re.compile(r'\b(?:fetch|got|axios(?:\.\w+)?|requests\.\w+|httpx\.\w+|aiohttp\.\w+'
               r'|urlopen|https?\.(?:get|request)|http\.client\.\w+)\s*\('),
    re.compile(r'\b(?:urllib\.request|urllib3|node-fetch|socket\.(?:connect|create_connection))\b'),
    re.compile(r'(?:^|[\s;|&(`"\'])(?:curl|wget)\s+\S'),
    re.compile(r'\b(?:web_fetch|web_search|WebFetch|WebSearch)\b'),
    re.compile(r'\bmcp__\w*(?:fetch|search|web)\w*'),
    re.compile(r'(?i)\b(?:api\.openai\.com|api\.anthropic\.com|openrouter\.ai)\b'),
]

_COOCCURRENCE_SCAN_EXTS = {'.md', '.txt', '.py', '.js', '.ts', '.json', '.yml', '.yaml', '.sh', '.bash', '.toml'}
# Agent instructions (SKILL.md, CLAUDE.md, markdown under skills/ or commands/)
# are read with every signal: there the words are the payload. Everywhere else
# the memory side must name a real memory store (a memory file, a memory tool,
# an explicit memory API) rather than the plain word "memory", and in code the
# evidence must come from code, not from a comment.
_PROSE_EXTS = {'.md', '.txt'}
# Markdown under these folders is agent instructions (skills, slash commands,
# subagents, prompt libraries), read with the broad signals like SKILL.md.
_INSTRUCTION_DIRS = {'skills', 'commands', 'agents', 'prompts', 'references', '.claude', 'rules'}
_STRONG_MEMORY_SIGNALS = [
    re.compile(r'(?i)(?:^|[\s/"\'`(~])(?:MEMORY|CLAUDE|AGENTS|GEMINI)\.md\b'),
    re.compile(r'(?i)[~/]\.(?:claude|codex|gemini|cursor)/(?:projects|memory|memories)\b'),
    re.compile(r'(?i)\b(?:mcp__recall|mcp__memory|mcp__knowledge)\w*'),
    re.compile(r'(?i)\b(?:read|fetch|get|load|query|search|dump|export)_?memor(?:y|ies)\b'),
    re.compile(r'(?i)\b(?:memory|conversation|recall)_?search\b'),
    re.compile(r'(?i)\btotal[_-]?recall\b'),
]
_HASH_COMMENT_CODE = {'.py', '.sh', '.bash', '.yml', '.yaml', '.toml'}
_SLASH_COMMENT_CODE = {'.js', '.ts'}


def _code_only(line, ext, state):
    '''Return the part of a code line outside comments. `state` carries an
    open /* */ block across lines for JS/TS. Quote-aware and linear.'''
    out = []
    quote = ''
    i, n = 0, len(line)
    while i < n:
        c = line[i]
        if state.get('block'):
            end = line.find('*/', i)
            if end < 0:
                return ''.join(out)
            state['block'] = False
            i = end + 2
            continue
        if quote:
            out.append(c)
            if c == '\\':
                if i + 1 < n:
                    out.append(line[i + 1])
                i += 2
                continue
            if c == quote:
                quote = ''
            i += 1
            continue
        if c in '"\'`':
            quote = c
            out.append(c)
            i += 1
            continue
        if ext in _HASH_COMMENT_CODE and c == '#':
            break
        if ext in _SLASH_COMMENT_CODE and c == '/' and line.startswith('//', i):
            break
        if ext in _SLASH_COMMENT_CODE and c == '/' and line.startswith('/*', i):
            state['block'] = True
            i += 2
            continue
        out.append(c)
        i += 1
    return ''.join(out)


def scan_memory_exfil_cooccurrence(repo_path):
    """Cat 10: Detect skills that combine memory access with outbound network.

    Scans all files in the skill repo for both memory-access signals and
    network-access signals. If both are present, emits a HIGH finding
    describing the co-occurrence as an exfiltration risk.

    This is a WARN, not a BLOCK — many legitimate skills combine memory
    with network (e.g., research tools that recall context then fetch
    web pages). The finding prompts human review of what data flows
    from memory to the network channel.

    Source: The Memory Heist (Ayush Paul, July 2026)
    """
    findings = []
    memory_hits = []   # (rel_path, line, signal_text)
    network_hits = []  # (rel_path, line, signal_text)

    for fpath, rel in core.walk_repo(repo_path, skip_binary=True):
        ext = os.path.splitext(fpath)[1].lower()
        if ext not in _COOCCURRENCE_SCAN_EXTS:
            continue
        content = _read(fpath)
        if not content:
            continue
        prose = ext in _PROSE_EXTS
        instructions = (os.path.basename(rel).upper() in core._AGENT_INSTRUCTION_BASENAMES
                        or (prose and _INSTRUCTION_DIRS & set(rel.replace('\\', '/').lower().split('/')[:-1])))
        memory_signals = _MEMORY_ACCESS_SIGNALS if instructions else _STRONG_MEMORY_SIGNALS
        comment_state = {}
        for i, line in enumerate(content.split('\n')):
            line = core.clip_line(line)
            if not prose and not instructions:
                line = _code_only(line, ext, comment_state)
                if not line.strip():
                    continue
            for pat in memory_signals:
                m = pat.search(line)
                if m:
                    memory_hits.append((rel, i + 1, m.group(0)))
                    break
            if prose and not instructions:
                continue            # a link in a README or notes file is not a network call
            for pat in (_NETWORK_ACCESS_SIGNALS if instructions else _CODE_NETWORK_SIGNALS):
                m = pat.search(line)
                if m:
                    network_hits.append((rel, i + 1, m.group(0)))
                    break

    if memory_hits and network_hits:
        # Directory walk order differs by filesystem; report the same sample
        # file on every machine.
        memory_hits.sort()
        network_hits.sort()
        # Deduplicate by file
        memory_files = sorted(set(h[0] for h in memory_hits))
        network_files = sorted(set(h[0] for h in network_hits))
        memory_sample = memory_hits[0]
        network_sample = network_hits[0]
        findings.append(_F(
            SCANNER_NAME, "high",
            "Memory + Network co-occurrence: exfiltration risk (Memory Heist, July 2026)",
            (f"This skill accesses user memory/data in {len(memory_files)} file(s) "
             f"and makes outbound network requests in {len(network_files)} file(s). "
             f"An attacker who controls fetched web content can trick the agent into "
             f"encoding memory-derived PII into URL paths (The Memory Heist, Ayush Paul, "
             f"July 2026). Review what data flows from memory to the network channel. "
             f"Memory signal: '{memory_sample[2]}' in {memory_sample[0]}:{memory_sample[1]}. "
             f"Network signal: '{network_sample[2]}' in {network_sample[0]}:{network_sample[1]}."),
            memory_sample[0], memory_sample[1],
            f"memory={memory_sample[2]} network={network_sample[2]}",
            "memory-exfil-cooccurrence"
        ))

    return findings


def main(args):
    """Run all agent skill checks. Returns list[Finding].
    Args can be a namespace with .repo_path or a string path (for testing).
    """
    repo_path = args if isinstance(args, str) else args.repo_path
    output_format = "text" if isinstance(args, str) else getattr(args, "format", "text")
    if not is_agent_skill(repo_path):
        core.emit_status(output_format, "[+] Not an agent skill. Skipping.")
        return []
    core.emit_status(output_format, f"[*] Scanning agent skill in {repo_path}...")
    findings = []
    findings.extend(scan_frontmatter(repo_path))
    findings.extend(scan_tools_json(repo_path))
    findings.extend(scan_agent_configs(repo_path))
    findings.extend(scan_clawhubignore(repo_path))
    findings.extend(scan_clawhavoc(repo_path))
    findings.extend(scan_plugin_manifest(repo_path))
    findings.extend(scan_config_write_requests(repo_path))
    findings.extend(scan_reference_chains(repo_path))
    findings.extend(scan_memory_poisoning(repo_path))
    findings.extend(scan_memory_exfil_cooccurrence(repo_path))
    return findings


if __name__ == "__main__":
    args = core.parse_common_args(sys.argv, "Agent Skill Scanner")
    findings = main(args)
    core.output_findings(findings, args.format, SCANNER_NAME)
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 194kUpdated Claude CodeCodexVerifiedInstall