Audit OS command injection
Command injection happens when input reaches a shell interpreter with its metacharacters intact —
;, |, &&, backticks, $(), and newlines. The primary fix is to stop invoking a shell and pass
an argument vector to the kernel directly.
Procedure
Find shell-invoking sinks across languages:
rg -n "subprocess\.(run|call|Popen).*shell=True|os\.system\(|os\.popen\(|commands\.getoutput" . # Python
rg -n "exec\(|execSync|child_process|shelljs" . # Node
rg -n "Runtime\.getRuntime\(\)\.exec|ProcessBuilder" . # Java
rg -n "system\(|popen\(|`.*`" . # C/PHP/Ruby
Trace which part of the command string comes from input — often a filename, hostname, or a
user-chosen option, not the whole command.
Replace shell=True with an argument list; the process is then invoked without shell parsing:
# vulnerable
subprocess.run(f"convert {src} {dst}", shell=True)
# fixed — argv form, no shell
subprocess.run(["convert", src, dst], check=True)
In Node, use execFile/spawn with an args array rather than exec('cmd ' + input).
When a shell is genuinely required, pass the untrusted value as a positional argument
(sh -c 'mytool "$1"' _ "$input") so the shell never re-parses it, or quote with the shell's own
escaper (shlex.quote), never a hand-rolled regex.
Watch option injection: even with an argv form, input like -o /etc/cron.d/x becomes a flag.
Prefix user paths with -- and validate the value.
Restrict the child's environment and working directory to shrink the blast radius.
Prove the fix with a metacharacter payload and confirm the file it would create does not appear:
filename='x; touch /tmp/pwned'
# after the fix: /tmp/pwned must not exist
Pitfalls
shell=True with a constant string is fine; the danger is any input concatenated in — review
every format/f-string that feeds it.
- A denylist of
; and | misses newlines, $IFS, $(...), and %0a.
subprocess.run(..., shell=True) on Windows uses cmd.exe, whose escaping differs from POSIX.
- Escaping with
shlex.quote at the wrong layer (before a second interpolation) still exposes.
- Backtick interpolation in Ruby/PHP shell functions executes before the argument is passed.
- Time-of-check/time-of-use: validating a filename then passing the string lets a symlink race.
Verification
rg -n "shell=True|os\.system|execSync|child_process.*exec\(" src/ | rg -v none-expr
printf '%s' 'x; touch /tmp/pwned' | xargs -I{} sh -c 'true' ; test ! -e /tmp/pwned && echo "no injection"
Pass: the sink search returns only argv-form invocations (no shell=True with input) and the
metacharacter payload creates no side-effect file. Report the sinks converted, any remaining shell
use with its quoting strategy, and the payload result.
1---2name: audit-os-command-injection3description: Use when user input can reach a shell or an exec that passes a command string. Finds the sinks, removes the shell, and proves injection is closed with a metacharacter payload.4metadata:5 source: https://skillmd.com/skills/jiayaoqijia/audit-os-command-injection6---78# Audit OS command injection910Command injection happens when input reaches a shell interpreter with its metacharacters intact —11`;`, `|`, `&&`, backticks, `$()`, and newlines. The primary fix is to stop invoking a shell and pass12an argument vector to the kernel directly.1314## Procedure15161. Find shell-invoking sinks across languages:1718 rg -n "subprocess\.(run|call|Popen).*shell=True|os\.system\(|os\.popen\(|commands\.getoutput" . # Python19 rg -n "exec\(|execSync|child_process|shelljs" . # Node20 rg -n "Runtime\.getRuntime\(\)\.exec|ProcessBuilder" . # Java21 rg -n "system\(|popen\(|`.*`" . # C/PHP/Ruby22232. Trace which part of the command string comes from input — often a filename, hostname, or a24 user-chosen option, not the whole command.25263. Replace `shell=True` with an argument list; the process is then invoked without shell parsing:2728 # vulnerable29 subprocess.run(f"convert {src} {dst}", shell=True)30 # fixed — argv form, no shell31 subprocess.run(["convert", src, dst], check=True)3233 In Node, use `execFile`/`spawn` with an args array rather than `exec('cmd ' + input)`.34354. When a shell is genuinely required, pass the untrusted value as a positional argument36 (`sh -c 'mytool "$1"' _ "$input"`) so the shell never re-parses it, or quote with the shell's own37 escaper (`shlex.quote`), never a hand-rolled regex.38395. Watch option injection: even with an argv form, input like `-o /etc/cron.d/x` becomes a flag.40 Prefix user paths with `--` and validate the value.41426. Restrict the child's environment and working directory to shrink the blast radius.43447. Prove the fix with a metacharacter payload and confirm the file it would create does not appear:4546 filename='x; touch /tmp/pwned'47 # after the fix: /tmp/pwned must not exist4849## Pitfalls5051- `shell=True` with a *constant* string is fine; the danger is any input concatenated in — review52 every format/f-string that feeds it.53- A denylist of `;` and `|` misses newlines, `$IFS`, `$(...)`, and `%0a`.54- `subprocess.run(..., shell=True)` on Windows uses `cmd.exe`, whose escaping differs from POSIX.55- Escaping with `shlex.quote` at the wrong layer (before a second interpolation) still exposes.56- Backtick interpolation in Ruby/PHP shell functions executes before the argument is passed.57- Time-of-check/time-of-use: validating a filename then passing the string lets a symlink race.5859## Verification6061 rg -n "shell=True|os\.system|execSync|child_process.*exec\(" src/ | rg -v none-expr62 printf '%s' 'x; touch /tmp/pwned' | xargs -I{} sh -c 'true' ; test ! -e /tmp/pwned && echo "no injection"6364Pass: the sink search returns only argv-form invocations (no `shell=True` with input) and the65metacharacter payload creates no side-effect file. Report the sinks converted, any remaining shell66use with its quoting strategy, and the payload result.