Windows command injection separators in cmd.exe
Windows command injection separators are the cmd.exe operators that split one command line into several. When an application concatenates user input into a command passed to cmd.exe /c, these operators let an attacker append their own command after the intended one.
How it works
cmd.exe uses `&` to run the next command unconditionally, `&&` to run it only on success, and `||` to run it only on failure. The pipe `|` sends output from one command to the next. Unlike Linux, the semicolon is an argument delimiter in many Windows programs, not a command separator for cmd.exe.
When input reaches `cmd.exe /c "nslookup " + host`, an attacker who sets `host` to `& whoami` produces two commands. The caret `^` is cmd.exe's escape character and is often combined with separators to evade filters (covered in the Windows filter-bypass entry).
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.
& whoamiWindowsUnconditionally runs `whoami` after the intended command in cmd.exe.
&& whoamiWindowsRuns `whoami` only if the preceding command succeeded (errorlevel 0).
| whoamiWindowsPipes the first command's output into `whoami`; runs regardless of the first command's result.
%0a whoamiWindowsA URL-encoded newline delivered through an HTTP parameter can terminate the line in some sinks and start a new command.
When it works
- Input is concatenated into a string executed through cmd.exe (System.Diagnostics with a built string, WScript.Shell.Run, PHP/Python on Windows calling the shell).
- The attacker's value is placed outside quotes, or they can inject a closing quote first.
When it fails
- The process is created with CreateProcess/ProcessStartInfo using an ArgumentList and UseShellExecute=false, so cmd.exe never parses the value.
- The value is validated against an allowlist before use.
Detection
Watch for cmd.exe spawned by a service or web process with chained operators, and for an unexpected second command after the intended one.
Signals to look for
- Request parameters containing & | && || or ^ in fields that drive a command.
- Sysmon Event ID 1 (process creation) showing cmd.exe or a utility (whoami, net, certutil) parented to w3wp.exe, php-cgi, java, node, etc.
- Command lines where the expected utility is followed by a separator and a second binary.
Where to look
Remediation
The correct fix: Launch the executable directly with an argument list and UseShellExecute=false; do not route user input through cmd.exe /c.
Principles
- Avoid cmd.exe entirely — call the target .exe directly with separated arguments.
- Use ArgumentList so the runtime quotes each value as one argv entry.
- Validate values that have a known shape (hostnames, IDs) with an allowlist.
Safe APIs by runtime
ProcessStartInfo with ArgumentList and UseShellExecute=false; never cmd.exe /c with a concatenated string.
var psi = new ProcessStartInfo("nslookup"){UseShellExecute=false}; psi.ArgumentList.Add(host);Use the call operator with an argument array; avoid Invoke-Expression on user input.
& 'nslookup.exe' $hostsubprocess.run([...]) with shell=False (the default).
subprocess.run(["nslookup", host])Not a fix: Stripping '&' and '|' from input. The caret escape, quoting tricks and environment-variable substring expansion let attackers rebuild the same operators.
For the full secure-coding matrix across languages, see the command injection prevention guide.
Frequently asked questions
- Does the semicolon work on Windows like on Linux?
- Not for cmd.exe. The semicolon is treated as an argument separator by many Windows programs, so use &, && or | to chain commands in cmd.exe.