Ravindra BagaleCourses & study guides

29. OWASP Top 10 Web Vulnerabilities

29.4 A03 Injection: Command Injection

Command injection tevha hoto jevha app user cha input thet operating-system command madhe vaparto.

// vulnerable: pinging a host the user typed
$ip = $_GET['ip'];
system("ping -c 1 " . $ip);

If the attacker sends 8.8.8.8; cat /etc/passwd, the server runs both commands. Fixes: avoid calling the shell; if you must, use safe APIs and strict allow-lists, and escape with escapeshellarg().

$ip = $_GET['ip'];
if (filter_var($ip, FILTER_VALIDATE_IP)) {        // allow only a valid IP
    system("ping -c 1 " . escapeshellarg($ip));
}

Ravindra Bagale's Tip

Shakya asel tar OS command टाळाच – bahutek kaamansathi PHP/Python madhe library aahe (file, network). Command vaparne garjeche asel tar allow-list (fakt valid IP/naav) ani escapeshellarg(). User input thet system() madhe – kadhihi nahi.

Lab

In DVWA (Command Injection, low), append ; whoami to the IP field and see it run. On high, see it blocked. Write a safe version of the ping feature using FILTER_VALIDATE_IP and escapeshellarg().