Linux command injection separators and chaining
Linux command injection separators are the shell metacharacters that end one command and begin another. When user input is concatenated into a string that reaches /bin/sh, a separator lets an attacker append a second command of their choosing to the one the application meant to run.
How it works
A Bourne-style shell treats several characters as command terminators or list operators. The semicolon runs the next command unconditionally. `&&` runs the next command only if the previous one succeeded (exit status 0); `||` runs it only on failure. A single `&` backgrounds the first command and runs the next immediately. A literal newline also terminates a command.
If the application builds something like `sh -c "host $INPUT"` and the attacker controls `$INPUT`, supplying `; id` turns the single intended command into two: the original, then `id`. Because the shell parses the assembled string, the injected separator is interpreted as syntax rather than data.
Representative payloads
Illustrative fragments for authorized testing. The injected marker commands (id, whoami) are harmless proofs of execution — swap in your engagement’s agreed markers.
; idLinux / UnixUnconditionally terminates the intended command and runs `id`. The simplest proof that two commands executed.
&& idLinux / UnixRuns `id` only if the first command exits 0. Useful when the leading command is expected to succeed.
| idLinux / UnixPipes the first command's output into `id`. Runs even when the first command fails, and avoids `;`/`&&` if those are filtered.
Note: See the dedicated pipe-injection entry for output-capturing variants.
%0a idLinux / UnixA URL-encoded newline (LF). Delivered through an HTTP parameter, it terminates the line and runs `id` on the next.
x; id #Linux / UnixCloses a preceding token with a benign value, injects `id`, then `#` comments out the rest of the original command so trailing text doesn't cause a syntax error.
When it works
- User input is concatenated into a command string that is executed by a shell (system(), exec/execSync with a string, shell=True, backticks, sh -c …).
- The input lands outside quotes, or the attacker can break out of the surrounding quotes first.
- No allowlist constrains the value to a safe shape before it reaches the shell.
When it fails
- The program is launched with an argv array and no shell, so separators are passed as literal argument text.
- The value is single-quoted and the quote character itself is stripped or escaped, leaving no way to break out.
- A strict allowlist (e.g. digits only) rejects the separator before execution.
Detection
Look for shell metacharacters in values that feed a process launch, and for a single request producing two distinct command executions.
Signals to look for
- Request parameters containing ; && || & | or encoded newlines (%0a, %0d) in fields that drive a command.
- Process-creation telemetry showing a web/app user spawning sh -c with a second command after the expected one.
- Unexpected child processes (id, whoami, cat, curl) parented to the application runtime.
Where to look
Remediation
The correct fix: Execute the target program with an argv array through a shell-free API so separators can only ever be literal argument data.
Principles
- Do not build a command string and hand it to a shell. Pass the program and each argument as separate elements.
- Prefer a native library (e.g. a DNS resolver) over spawning a command at all.
- If a value must match a known shape, validate it against an allowlist as defense in depth.
Safe APIs by runtime
Use execFile/spawn with an args array; never exec() or spawn with { shell: true }.
execFile("host", ["--", userInput]);Use subprocess.run([...]) with the default shell=False.
subprocess.run(["host", "--", user_input], check=True)Use exec.Command(name, args...); never sh -c with a built string.
exec.Command("host", "--", userInput).Run()Not a fix: Blocklisting ';', '&&' and '|' with a regex. Attackers reach the same result with newlines, pipes, $() substitution or encoding, and the shell is still in the loop.
For the full secure-coding matrix across languages, see the command injection prevention guide.
Variants
- Newline (%0a / %0d%0a) injection when the sink splits on lines.
- Backgrounding with a trailing & to detach a long-running injected process.
Frequently asked questions
- Which Linux separator should I test first?
- Try a semicolon and a newline first; if those are filtered, move to pipes and $() command substitution, which often slip past naive blocklists.
- Does the injected command need the first one to succeed?
- Only with &&. A semicolon, a pipe or a newline will run your command regardless of the first command's exit status.