Попытка выполнить скрипт Python тайм-аут (cmsmap) с php - не удалось получить содержимое - PullRequest
0 голосов
/ 03 ноября 2019

Я пытаюсь запустить cmsmap на centos с php и с помощью команды timeout linux - мне не удается получить выходные данные.

Привет

Я пытаюсь запустить cmsmap на своих веб-сайтах вежедневно. Это выполняется с помощью PHP-скрипта, и результаты автоматически отправляются мне по почте. Проблема в том, что я хочу отменить задание после 80 секунд на страницу, поэтому я попытался использовать две разные вещи: - команда timeout: timeout 80 python3 cmsmap.py - скрипт завершения процесса php, который я нашел здесь: https://blog.dubbelboer.com/2012/08/24/execute-with-timeout.html

Я сделал то же самое с sqlmap, и он работает отлично. Странная вещь: при запуске cmsmap.py, если я использую версию тайм-аута, будут сделаны следующие выходные данные (даже если все выходные данные должны быть перенаправлены в сценарии php):

[i] Found (# 1):/home/merlin/exploitdb/files_shellcodes.csv

[i] Чтобы удалить это сообщение, отредактируйте "/home/merlin/exploitdb/.searchsploit_rc" для "files_shellcodes.csv" (package_array: exploitdb)

(и таких предупреждений гораздо больше)

Может быть, в этом проблема, и именно поэтому я не получаю вывод, полученный из cmsmap.py?

Спасибо за помощь!

Мерлин


<?php
    function exec_timeout($cmd, $timeout) {
        /*------------------------------------------------------------------- USING TIMEOUT ----------------------------------------------------------------*/

      /*$cmd = "timeout ".$timeout." ".$cmd;

        var_dump($cmd);

        exec($cmd, $output, $return_var);

        return implode("\n", $output);*/

        /*------------------------------------------------------------------- https://blog.dubbelboer.com/2012/08/24/execute-with-timeout.html ----------------------------------------------------------------*/

      // File descriptors passed to the process.
      $descriptors = array(
        0 => array('pipe', 'r'),  // stdin
        1 => array('pipe', 'w'),  // stdout
        2 => array('pipe', 'w')   // stderr
      );

      // Start the process.
      $process = proc_open('exec ' . $cmd, $descriptors, $pipes);

      if (!is_resource($process)) {
        throw new \Exception('Could not execute process');
      }

      // Set the stdout stream to non-blocking.
      stream_set_blocking($pipes[1], 0);

      // Set the stderr stream to non-blocking.
      stream_set_blocking($pipes[2], 0);

      // Turn the timeout into microseconds.
      $timeout = $timeout * 1000000;

      // Output buffer.
      $buffer = '';

      // While we have time to wait.
      while ($timeout > 0) {
        $start = microtime(true);

        // Wait until we have output or the timer expired.
        $read  = array($pipes[1]);
        $other = array();
        stream_select($read, $other, $other, 0, $timeout);

        // Get the status of the process.
        // Do this before we read from the stream,
        // this way we can't lose the last bit of output if the process dies between these functions.
        $status = proc_get_status($process);

        // Read the contents from the buffer.
        // This function will always return immediately as the stream is non-blocking.
        $buffer .= stream_get_contents($pipes[1]);

        if (!$status['running']) {
          // Break from this loop if the process exited before the timeout.
          break;
        }

        // Subtract the number of microseconds that we waited.
        $timeout -= (microtime(true) - $start) * 1000000;
      }

      // Check if there were any errors.
      // No Errors!!!
      /*$errors = stream_get_contents($pipes[2]);

      if (!empty($errors)) {
        throw new \Exception($errors);
      }*/

      // Kill the process in case the timeout expired and it's still running.
      // If the process already exited this won't do anything.
      proc_terminate($process, 9);

      // Close all streams.
      fclose($pipes[0]);
      fclose($pipes[1]);
      fclose($pipes[2]);

      proc_close($process);
      return $buffer;
    }
?>

[i] Найдено (# 1): /home/merlin/exploitdb/files_shellcodes.csv [i] Чтобы удалить это сообщение, пожалуйста,отредактируйте "/home/merlin/exploitdb/.searchsploit_rc" для "files_shellcodes.csv" (package_array: exploitdb)

...