// 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">Admin Console (whitelist)<title>Admin Console (whitelist)</title><style>body{font-family:system-ui,Segoe UI,Roboto,Arial;margin:20px}textarea{width:100%;height:300px;font-family:monospace}.card{max-width:900px;margin:auto}</style><div class="card">

Console Amministrativa (solo comandi consentiti)

<label for="cmd">Scegli comando:</label> <select name="cmd" id="cmd"> <option value="">-- seleziona --</option> foreach ($WHITELIST as $c): <option value=" echo htmlspecialchars($c) " if($selected===$c) echo 'selected' > echo htmlspecialchars($c) </option> endforeach; </select> <button type="submit">Esegui</button>

Output

<textarea readonly> echo htmlspecialchars($output) </textarea> <p><small>Log: echo htmlspecialchars($LOG_FILE) </small></p> <p style="color:crimson"><strong>Avvertenze:</strong> accesso limitato, usare solo su server controllati. Non esporre a internet pubblico.</p></div>