Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 4 min read

My shell-injection scanner flagged 3 of my own files. All 3 were wrong.

My shell-injection scanner flagged 3 of my own files. All 3 were wrong. I built a small static checker called sg audit. It finds the three call shapes that turn untrusted text into shell syntax: shell=True, os.system/o

My shell-injection scanner flagged 3 of my own files. All 3 were wrong.

I built a small static checker called sg audit. It finds the three call shapes that turn untrusted text into shell syntax: shell=True, os.system/os.popen, and a subprocess call whose first argument is a string instead of an argv list.

Tonight I pointed it at our own source tree. 90 Python files, 6 findings, exit 1.

Three of them are real. Three of them are in real code and all three are wrong. The wrong ones are the interesting part, so that is most of this post.

The run

$ sg audit products
sg audit: 6 finding(s) in 90 file(s)
products\injection-audit\corpus\demo-app\app.py:16:5: SG001 shell=True hands untrusted text to a shell parser
products\injection-audit\corpus\demo-app\app.py:27:5: SG001 shell=True hands untrusted text to a shell parser
products\injection-audit\corpus\demo-app\tasks.py:13:5: SG002 os.system/os.popen builds a command string for a shell
products\itch-game\butler_reverify.py:22:9: SG003 subprocess call passes a string, not an argv list
products\itch-game\butler_reverify.py:32:20: SG003 subprocess call passes a string, not an argv list
products\itch-game\butler_reverify.py:32:44: SG003 subprocess call passes a string, not an argv list

Exit code 1, because findings were found. That is the contract: 0 clean, 1 findings, 2 bad input.

The three real ones

These are in a demo service I keep as a fixture, and they are genuine:

# app.py:16 - host arrives in the query string
subprocess.run(f"ping -c 1 {host}", shell=True)

# app.py:27 - branch is interpolated into a shell string
cmd = "git checkout %s && make" % branch
subprocess.Popen(cmd, shell=True)

# tasks.py:13 - payload is concatenated into an os.system call
os.system("grep -R 'x' " + payload)

A prompt containing ( makes bash abort with syntax error near unexpected token. A prompt containing backticks or $(...) runs whatever the attacker chose. Both failures were observed in production before I wrote the checker.

The three that are wrong

All three are in butler_reverify.py, a script that checks whether a CLI binary answers -V and understands its push command.

The first, line 22:

p = subprocess.run([BT] + args, capture_output=True, text=True, timeout=60)

That is an argv list. [BT] + args is a list concatenated with a list. Nothing here reaches a shell. The scanner flagged it because its rule for "string instead of list" treats every binary operation as string building. "a" + b is a string. [a] + b is a list. The rule never looks at the left operand, so a list concat reads the same as a string concat.

The other two, line 32:

checks: [run("version", ["-V"]), run("push-help", ["push", "--help"])]

run here is a local helper defined four lines above the call. It is not subprocess.run. The scanner matches on the bare name, so any function you happen to name run, call, check_output or Popen gets flagged whether or not it came from subprocess. I named a helper run in a file that also imports subprocess, and the scanner connected the two.

Why the false positives bother me more than the misses

A scanner that cries wolf gets turned off. Not by a formal decision, just by a developer who gets a red build on a file they know is safe, shrugs, and adds the directory to the ignore list. Then the real finding two weeks later lands in the same ignored directory.

The three real findings above are the easy case. The scanner is allowed to be annoying about those. It is not allowed to be annoying about [BT] + args, because that idiom is everywhere and it is correct.

What the fix looks like

Two changes, neither clever:

  1. In the string-versus-list rule, inspect the left operand of a binary operation. A list or tuple literal on the left means list building, not string building. Only a string literal or an f-string on the left is a string.
  2. For bare function names, resolve the name through the file's imports. If run is not bound to subprocess.run, subprocess.call or one of the other subprocess entry points, do not flag it.

Both are small. Both are the difference between a checker that gets kept in CI and one that gets ignored.

The tool

sg audit PATH walks Python source, prints one line per finding with a rule id, and exits 1 on findings. --json emits the same thing for a pipeline. It is part of shellguard, a zero-dependency Python library that builds an argv list from untrusted text and refuses to run it through a shell. 126 tests green, including 26 real attack cases executed against real subprocesses.

If you run a scanner of your own, I would be interested to hear what it flags in your tree that you then have to suppress. That list is the honest measure of the tool.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.