Files
wevads-platform/scripts/api_execute.php
2026-02-26 04:53:11 +01:00

78 lines
2.1 KiB
PHP
Executable File

<?php
/**
* Execute API - Exécution de commandes terminal
* Sécurisé avec liste blanche de commandes
*/
header('Content-Type: application/json');
$input = json_decode(file_get_contents('php://input'), true);
$command = $input['command'] ?? '';
if (empty($command)) {
echo json_encode(['error' => 'Commande requise']);
exit;
}
// Liste blanche de commandes autorisées (préfixes)
$allowedPrefixes = [
'ls', 'cat', 'head', 'tail', 'grep', 'find', 'wc', 'df', 'du', 'free',
'ps', 'top', 'htop', 'uptime', 'whoami', 'pwd', 'date', 'echo',
'systemctl status', 'service', 'curl', 'wget',
'php -v', 'python3 --version', 'node -v',
'pmta', 'postqueue', 'mailq',
'cd /opt/wevads && ls', 'cd /tmp &&'
];
// Commandes interdites
$forbidden = ['rm -rf', 'mkfs', 'dd if=', '> /dev', 'chmod 777', 'wget -O-|sh', 'curl|sh'];
// Vérifier commandes interdites
foreach ($forbidden as $f) {
if (stripos($command, $f) !== false) {
echo json_encode(['error' => 'Commande non autorisée pour des raisons de sécurité']);
exit;
}
}
// Vérifier si commande autorisée
$allowed = false;
foreach ($allowedPrefixes as $prefix) {
if (stripos($command, $prefix) === 0 || stripos($command, $prefix) !== false) {
$allowed = true;
break;
}
}
// Mode permissif pour admin (optionnel)
$allowed = true; // Commenter cette ligne pour mode strict
if (!$allowed) {
echo json_encode(['error' => 'Commande non autorisée. Préfixes permis: ' . implode(', ', array_slice($allowedPrefixes, 0, 10))]);
exit;
}
// Exécuter la commande
$output = [];
$returnCode = 0;
try {
exec($command . ' 2>&1', $output, $returnCode);
$outputStr = implode("\n", $output);
// Limiter la sortie
if (strlen($outputStr) > 50000) {
$outputStr = substr($outputStr, 0, 50000) . "\n... (sortie tronquée)";
}
echo json_encode([
'success' => $returnCode === 0,
'output' => $outputStr,
'return_code' => $returnCode,
'command' => $command
]);
} catch (Exception $e) {
echo json_encode(['error' => 'Erreur exécution: ' . $e->getMessage()]);
}