// admin_shell.php// Attenzione: usare SOLO su server di test o in rete interna protetta// --- Configurazione ---$USERNAME = 'admin';$PASSWORD_HASH = password_hash('admin', PASSWORD_DEFAULT); // genera hash e sostituisci$LOG_FILE = __DIR__ . '/admin_shell.log';// Lista di comandi consentiti (esatti). Non aggiungere comandi con input libero.$WHITELIST = [ 'whoami', 'uptime', 'df -h', 'free -m', 'ls -la /var/www', // esempio: percorso specifico];// --- Basic HTTP Auth ---if (!isset($_SERVER['PHP_AUTH_USER'])) { header('WWW-Authenticate: Basic realm="Admin Shell"'); header('HTTP/1.0 401 Unauthorized'); echo 'Autenticazione richiesta'; exit;}if ($_SERVER['PHP_AUTH_USER'] !== $USERNAME || !password_verify($_SERVER['PHP_AUTH_PW'], $PASSWORD_HASH)) { header('HTTP/1.0 403 Forbidden'); echo 'Credenziali non valide'; exit;}// --- Gestione form ---$selected = $_POST['cmd'] ?? null;$output = '';if ($selected) { // Verifica whitelist if (!in_array($selected, $WHITELIST, true)) { $output = "Comando non consentito."; } else { // Esegui comando in modo limitato // Non usare input utente per costruire il comando $safe_cmd = escapeshellcmd($selected); $output = shell_exec($safe_cmd . ' 2>&1'); // cattura stderr // Log $entry = sprintf("[%s] user=%s cmd=%s\n", date('c'), $_SERVER['PHP_AUTH_USER'], $selected); file_put_contents($LOG_FILE, $entry, FILE_APPEND | LOCK_EX); }}<!doctype html><html lang="it">
<meta charset="utf-8">