Skip to content

Windows command injection filter bypass

Filter / WAF bypassWindows charactersWindows

A Windows command injection bypass rebuilds a filtered command using cmd.exe's own parsing quirks. The caret `^` escapes the next character and is removed at runtime, inserted quotes are stripped, and environment-variable substring expansion (`%PROGRAMFILES:~10,1%`) can synthesise characters — so `who^ami`, `wh""oami` and substring tricks all run `whoami` while defeating keyword filters.

How it works

cmd.exe treats `^` as an escape character: it is consumed during parsing and the following character is taken literally. So `who^ami` is parsed as `whoami`. Inserted empty quotes (`wh""oami`) are likewise removed, mirroring the Linux quote trick.

Environment-variable substring expansion is a uniquely Windows primitive: `%VAR:~start,len%` extracts characters from an existing variable's value, letting an attacker build characters they can't type directly (for example pulling a `\` or a letter out of `%PROGRAMFILES%`). Combined with `set`/`for` loops, cmd.exe can assemble arbitrary strings from pieces.

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.

  • who^ami

    WindowsThe caret is consumed by cmd.exe parsing; the command runs as whoami.

  • wh""oami

    WindowsEmpty quote pair is stripped at runtime, defeating a 'whoami' keyword filter.

  • %PROGRAMFILES:~10,1%

    WindowsSubstring expansion extracts one character from %PROGRAMFILES%; used to synthesise characters a filter blocks.

  • s^et x=who^ami & call %x%

    WindowsBuilds the command in a variable using carets, then calls it indirectly to evade inline matching.

When it works

  • Input is interpreted by cmd.exe, which honours caret escapes, quote removal and %VAR:~% expansion.
  • The filter matches literal command strings rather than the resolved command.

When it fails

  • The process is launched directly (no cmd.exe), so carets and %VAR% stay literal.
  • An allowlist rejects the input regardless of how it's spelled.

Detection

Normalise cmd.exe escapes before matching and flag caret density, empty quotes and %VAR:~% substring expansions.

Signals to look for

  • Inputs with carets inside words (who^ami), empty quote pairs, or %VAR:~start,len% patterns.
  • Sysmon command lines where the resolved command differs from the raw string after caret/quote removal.
  • Use of set/call/for to assemble commands indirectly.

Where to look

Sysmon Event ID 1Security log 4688 with command lineWAF / application logs

Remediation

The correct fix: Call executables directly with an ArgumentList and UseShellExecute=false; cmd.exe's caret, quote and substring tricks never run because cmd.exe isn't invoked.

Principles

  • Keep cmd.exe out of the path — launch the .exe directly with separated arguments.
  • Don't blocklist command names; attackers rebuild them with carets, quotes and substrings.
  • Allowlist inputs that have a known shape.

Safe APIs by runtime

.NET / C#

ProcessStartInfo + ArgumentList, UseShellExecute=false; never cmd.exe /c.

var psi = new ProcessStartInfo("app.exe"){UseShellExecute=false}; psi.ArgumentList.Add(arg);
PowerShell

Use the call operator & with an argument array; avoid Invoke-Expression.

& 'app.exe' $arg

Not a fix: Filtering '^' or specific keywords. Quote insertion and %VAR:~% substring expansion reconstruct the same commands without a caret.

For the full secure-coding matrix across languages, see the command injection prevention guide.

References

Related techniques