PHP - `shell_exec` не работает с NMap (Windows Server)

Я пытался понять, почему я не могу заставить NMap дать мне какой-либо вывод или даже работать в этом отношении через PHP.

Вещи, которые я пробовал до сих пор:

// this doesn't return anything because it's wrong
$output = passthru('nmap -V');
echo $output;

// this returns a negated integer value
passthru('nmap -V', $output);
echo $output;

// this doesn't return anything either
$stream = popen('C:\nmap -V', 'r');
while (!feof($stream))
{
    $buffer = fread($stream, 1024);
    echo $buffer;
}
pclose($stream);

// this doesn't do anything as well
$output = system('C:\nmap -V');
echo $output;

// this does nothing also...
ob_start(); // start output buffering
fpassthru('C:\nmap -V'); // flush COMPLETE output of nmap
$output = ob_get_contents(); // capture output buffer contents
ob_end_clean(); // shutdown output buffers
echo $output; // echo it

.

// okay, how about we try a 'proc_open()'?
// nope, this doesn't work either. I just get a value of "command returned -1073741515"
$descriptorspec = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child will read from
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to
    2 => array("file", "errors/errors.txt", "a") // stderr is a file to write to
 );
 
 $cwd = 'errors';
 $env = array('some_option' => 'aeiou');
 
 $process = proc_open('C:/nmap -V', $descriptorspec, $pipes, $cwd, $env);
 
 if (is_resource($process))
 {
     // $pipes now looks like this:
     // 0 => writeable handle connected to child stdin
     // 1 => readable handle connected to child stdout
     // Any error output will be appended to /errors/errors.txt
 
     fwrite($pipes[0], '<?php print_r($_ENV); ?>');
     fclose($pipes[0]);
 
     echo stream_get_contents($pipes[1]);
     fclose($pipes[1]);
 
     // It is important that you close any pipes before calling
     // proc_close in order to avoid a deadlock
     $return_value = proc_close($process);
 
     echo "command returned $return_value\n";
 }

И многие другие, но я не получаю абсолютно НИЧЕГО от $output. Я тоже много искал в Google, но до сих пор не могу понять. Многие примеры также, кажется, для Linux, что не помогает.

Спасибо.


person t0rxe    schedule 29.04.2021    source источник
comment
Я бы рекомендовал перечитать документацию для passthru(), так как эта функция в любом случае не возвращает никаких данных.   -  person esqew    schedule 29.04.2021
comment
Итак, мы можем вычеркнуть passthru.   -  person t0rxe    schedule 29.04.2021
comment
Отвечает ли это на ваш вопрос? Как использовать Nmap в PHP exec   -  person esqew    schedule 29.04.2021
comment
shell_exec() также не обязательно ничего возвращает.   -  person Alex Howansky    schedule 29.04.2021
comment
@esqew, нет. Это тоже не работает, и это было то, что я уже нашел в своем поиске Google.   -  person t0rxe    schedule 29.04.2021
comment
Я не могу говорить конкретно с nmap, но я предпочитаю proc_open потому что вы можете указать каналы для stdin, stdout и stderr (и я сейчас работаю над проектом, используя его).   -  person Chris Haas    schedule 30.04.2021
comment
Тоже не работает. Я добавил свой код в пример.   -  person t0rxe    schedule 30.04.2021


Ответы (1)


Хорошо, я получаю вывод, используя этот код. Я продолжу кодирование и закончу остальную часть программы. Спасибо 'Chris Haas' за предложение по использованию proc_open.

ПРИМЕЧАНИЕ. Каталог, содержащий файл «errors.txt», должен иметь права на запись «IIS_IUSRS». Если вы сомневаетесь, проверьте журнал ошибок PHP.

 $descriptorSpec = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child will read from
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to
    2 => array("file", "errors/errors.txt", "a") // stderr is a file to write to
 );

 $env = array('bypass_shell' => true);
 $process = proc_open("NMAP.EXE -V", $descriptorSpec, $pipes, "C:\\Program Files (x86)\\NMap", $env);

 if (is_resource($process))
 {
     // '$pipes' now looks like this:
     // 0 => writeable handle connected to child stdin
     // 1 => readable handle connected to child stdout
     fwrite($pipes[0], '<?php print_r($_ENV); ?>');
     fclose($pipes[0]);
 
     echo stream_get_contents($pipes[1]);
     fclose($pipes[1]);
 
     // it is important that you close any pipes before calling
     // proc_close in order to avoid a deadlock
     $return_value = proc_close($process);
 
     echo "<br /><br />Command Returned: $return_value\n";
 }

Nmap версии 7.91 ( https://nmap.org ) Платформа: i686-pc-windows-windows Скомпилировано с: nmap- liblua-5.3.5 openssl-1.1.1h nmap-libssh2-1.9.0 nmap-libz-1.2.11 nmap-libpcre-7.6 Npcap-1.00 nmap-libdnet-1.12 ipv6 Скомпилировано без: Доступных движков nsock: выбор опроса iocp

Команда возвращена: 0

person t0rxe    schedule 30.04.2021