Pipe command injection with the | operator
Pipe command injection uses the shell pipe `|` to connect the intended command to an attacker-supplied one. The pipe runs the second command regardless of whether the first succeeds, which makes it a dependable fallback when semicolons and `&&` are filtered on both Linux and Windows.
How it works
A pipe takes the standard output of the command on its left and feeds it as standard input to the command on the right, running both. For injection purposes the key property is that the right-hand command executes even if the left-hand one errors out — unlike `&&`, which requires success.
If an application runs `grep $INPUT file` and the attacker sets `$INPUT` to `x | id`, the shell runs `grep x file` and pipes its output into `id`. On Windows cmd.exe the same `|` works. Pipes are frequently missed by blocklists that focus on `;` and `&&`.
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 / UnixRuns `id` with the first command's output as input; executes even if the first command fails.
| whoamiWindowscmd.exe pipe variant to run `whoami` after the intended command.
|idLinux / UnixNo space after the pipe — still valid, and slips past filters that only match '| ' with a trailing space.
| sleep 5Linux / UnixTiming probe through a pipe when the response body shows no command output.
When it works
- User input is concatenated into a shell command and `;`/`&&` are filtered but `|` is not.
- The attacker's command does not depend on the first command's exit status.
When it fails
- No shell is used (argv array execution treats `|` as a literal character).
- The pipe character is rejected by an allowlist.
Detection
Flag pipe characters in command-driving inputs and correlate with a two-stage process execution from one request.
Signals to look for
- Parameters containing | (with or without surrounding spaces) where a plain value is expected.
- Process telemetry showing the expected command immediately followed by a piped second binary.
- WAF hits for pipe metacharacters that correlate with anomalous child processes.
Where to look
Remediation
The correct fix: Use a shell-free argv execution API; the pipe then has no special meaning and is passed as literal text.
Principles
- Launch the program directly with separate arguments — no sh -c, no cmd /c.
- Where you genuinely need to pipe data, wire the processes together in code (connect stdin/stdout) instead of asking a shell.
- Validate simple values with an allowlist as defense in depth.
Safe APIs by runtime
spawn with an args array; connect child.stdout to another child's stdin in code if you need a pipeline.
spawn("grep", [pattern, file]);subprocess.run([...]) and wire Popen stdout→stdin in Python for real pipelines.
subprocess.run(["grep", pattern, file])Not a fix: Blocking only ';' and '&&'. The pipe reaches the same outcome, as do newlines and command substitution.
For the full secure-coding matrix across languages, see the command injection prevention guide.