From 386d0fc65d2ab49d591464015335854dfce2166e Mon Sep 17 00:00:00 2001 From: adrien Date: Tue, 15 Oct 2024 16:24:07 +0200 Subject: [PATCH] use sudo on need --- README.md | 6 +----- config.php | 4 ++-- scan.php | 13 +++++++++++-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 054cb06..589efc4 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,7 @@ Scanne le réseau avec `nmap` et affiche les résultats dans une page web. Certaines options nécessitent l'accès root. Pour donner les droits à lanScan sous Linux, installer `sudo` au besoin, puis créer le fichier `/etc/sudoers.d/lanScan` avec le contenu -(en remplaçant `www-data` par le compte du service web) : +(en remplaçant `www-data` par l'utilisateur du service web) : ``` www-data ALL = NOPASSWD: /usr/bin/nmap ```` -et modifier le fichier `config.php` avec : -```php -$sudo = true; -``` \ No newline at end of file diff --git a/config.php b/config.php index db6d2ec..64593da 100644 --- a/config.php +++ b/config.php @@ -16,8 +16,8 @@ $HOSTSCAN_OPTIONS = [ 'stylesheet' => "$BASEDIR/hostScan.xsl" ]; +$refreshPeriod = 60; + $SCANSDIR = 'scans'; $DATADIR = '/usr/share/nmap'; -$sudo = true; -$refreshPeriod = 60; diff --git a/scan.php b/scan.php index 15a146f..305982a 100755 --- a/scan.php +++ b/scan.php @@ -28,8 +28,17 @@ foreach ($inputs as $arg => $value) { $tempPath = tempnam(sys_get_temp_dir(), 'scan_').".xml"; -exec(($sudo ? "sudo " : "") . "nmap$args -oX '$tempPath' $targets 2>&1", $stderr, $code); -if ($code) { +$command = "nmap$args -oX '$tempPath' $targets 2>&1"; + +exec($command, $stderr, $retcode); + +if ($retcode && strpos(implode($stderr), " root ") !== false) { + // Retry with sudo + $recode = 0; + exec("sudo $command", $stderr, $retcode); +} + +if ($retcode) { http_response_code(500); die(implode("
\n", $stderr)); }