|
| 1 | +"""Acceptance tests for issue #208: what the attacker controls in a process's command. |
| 2 | +
|
| 3 | +A string that a shell interprets, a string naming the program, or the program itself is |
| 4 | +a command injection, ``high``. An element of an argument list run without a shell |
| 5 | +cannot inject a command, only an option: ``medium`` by default when the executable is |
| 6 | +fixed, ``high`` when the option's dangerous behaviour is established by model data |
| 7 | +(``git push --receive-pack=<cmd>``, ``ssh -o ProxyCommand=...``, ``sh -c``). A constant |
| 8 | +prefix is judged by what the attacker still controls: ``f"v{x}"`` is no option at all, |
| 9 | +``f"--output={x}"`` is the value of a fixed option, ``f"--{x}"`` leaves the option open. |
| 10 | +A value after an option is that option's value only when the model establishes that the |
| 11 | +option consumes it, and a free element when the model establishes that the options before |
| 12 | +it take no value (``rm -rf``); otherwise the finding stays, for review. Severity is set per case: |
| 13 | +the ``hotspot`` verdict lowers the confidence, never the severity. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +from coretrace_python import engine |
| 23 | +from coretrace_python.frontend import build_hir |
| 24 | +from coretrace_python.ir.lowering import lower_module |
| 25 | +from coretrace_python.ir.model import BuildList, BuildTuple |
| 26 | +from coretrace_python.source import SourceManager |
| 27 | + |
| 28 | +PLUGINS = Path(__file__).resolve().parent.parent / "src" / "coretrace_python" / "bundled" |
| 29 | + |
| 30 | +HIGH = ("high", "vulnerability", "high") |
| 31 | +MEDIUM = ("medium", "vulnerability", "high") |
| 32 | +HIGH_TO_REVIEW = ("high", "hotspot", "medium") |
| 33 | +MEDIUM_TO_REVIEW = ("medium", "hotspot", "medium") |
| 34 | + |
| 35 | + |
| 36 | +def judged(call: str) -> list[tuple[str, str, str]]: |
| 37 | + """Severity, verdict and confidence of each command-injection finding on ``call``, |
| 38 | + made with ``x`` read from the command line and ``extra`` of unknown value.""" |
| 39 | + |
| 40 | + text = f"import os\nimport subprocess\nimport sys\n\n\ndef run(extra):\n x = sys.argv[1]\n {call}\n" |
| 41 | + findings = engine.check(SourceManager().add_source("app/run.py", text), [PLUGINS]) |
| 42 | + return [ |
| 43 | + (f.severity.value, f.metadata["verdict"], f.confidence.value) |
| 44 | + for f in findings |
| 45 | + if f.rule_id == "command-injection" |
| 46 | + ] |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize( |
| 50 | + ("call", "expected"), |
| 51 | + [ |
| 52 | + # 1, 2, 3, 8: the attacker reaches a shell or chooses the program. |
| 53 | + ('subprocess.run(f"git tag {x}", shell=True)', HIGH), |
| 54 | + ('os.system("ls " + x)', HIGH), |
| 55 | + ("subprocess.run(x)", HIGH), |
| 56 | + ('subprocess.run([x, "-l"])', HIGH), |
| 57 | + ('subprocess.run(["ls"], executable=x)', HIGH), |
| 58 | + ("subprocess.run(x, shell=extra)", HIGH_TO_REVIEW), |
| 59 | + # 4: a free element is an option the attacker chooses. |
| 60 | + ('subprocess.run(["ls", x])', MEDIUM), |
| 61 | + ('subprocess.run(["git", "status", x])', MEDIUM), |
| 62 | + ('subprocess.run(["git", "push", "origin", x])', HIGH), |
| 63 | + ('subprocess.run(["/usr/bin/git", "push", "origin", x])', HIGH), |
| 64 | + # 5, 5b, 5c: a constant prefix. |
| 65 | + ('subprocess.run(["git", "tag", f"v{x}"])', None), |
| 66 | + ('subprocess.run(["git", "commit", "-m", f"release {x}"])', None), |
| 67 | + ('subprocess.run(["curl", f"--output={x}"])', MEDIUM), |
| 68 | + ('subprocess.run(["git", "fetch", f"--upload-pack={x}"])', HIGH), |
| 69 | + ('subprocess.run(["ssh", f"-o{x}", "host"])', HIGH), |
| 70 | + ('subprocess.run(["ls", f"--{x}"])', MEDIUM), |
| 71 | + # 6: after the end of options, for a program known to honour it. |
| 72 | + ('subprocess.run(["git", "log", "--", x])', None), |
| 73 | + ('subprocess.run(["ls", "--", x])', MEDIUM), |
| 74 | + # 7: after an option that may take a value. |
| 75 | + ('subprocess.run(["ssh", "-o", x, "host"])', HIGH), |
| 76 | + ('subprocess.run(["sh", "-c", x])', HIGH), |
| 77 | + ('subprocess.run(["find", ".", "-exec", x, ";"])', HIGH), |
| 78 | + ('subprocess.run(["xcrun", "notarytool", "--keychain", x])', MEDIUM_TO_REVIEW), |
| 79 | + ('subprocess.run(["git", "push", "--force", x])', HIGH_TO_REVIEW), |
| 80 | + # Short options grouped in one element, read as getopt does. |
| 81 | + ('subprocess.run(["rm", "-rf", x])', MEDIUM), |
| 82 | + ('subprocess.run(["rm", "-r", "-f", x])', MEDIUM), |
| 83 | + ('subprocess.run(["rm", "-rz", x])', MEDIUM_TO_REVIEW), |
| 84 | + ('subprocess.run(["ssh", "-vo", x, "host"])', HIGH), |
| 85 | + ('subprocess.run(["ssh", "-ov", x, "host"])', HIGH), |
| 86 | + # Unpacked parts keep their place in the command. |
| 87 | + ('subprocess.run(["xcrun", "notarytool", *(["--keychain", x])])', MEDIUM_TO_REVIEW), |
| 88 | + ("subprocess.run([*extra, x])", HIGH), |
| 89 | + ('subprocess.run(["ls", *extra, x])', MEDIUM), |
| 90 | + ('subprocess.run(["ls", *x.split()])', MEDIUM), |
| 91 | + ], |
| 92 | +) |
| 93 | +def test_the_result_follows_what_the_attacker_controls( |
| 94 | + call: str, expected: tuple[str, str, str] | None |
| 95 | +) -> None: |
| 96 | + assert judged(call) == ([] if expected is None else [expected]) |
| 97 | + |
| 98 | + |
| 99 | +def test_an_unpacked_element_keeps_its_place_in_a_list_or_tuple_display() -> None: |
| 100 | + module = lower_module( |
| 101 | + build_hir( |
| 102 | + SourceManager().add_source("m.py", "def f(a, b):\n use([1, *a, 2, *b], (*a, 1))\n") |
| 103 | + ) |
| 104 | + ) |
| 105 | + instructions = [i for block in module.functions[0].blocks for i in block.instructions] |
| 106 | + (built_list,) = [i for i in instructions if isinstance(i, BuildList)] |
| 107 | + (built_tuple,) = [i for i in instructions if isinstance(i, BuildTuple)] |
| 108 | + |
| 109 | + assert built_list.unpacked_at == (1, 3) |
| 110 | + assert built_tuple.unpacked_at == (0,) |
0 commit comments