Advertisement
StephaneGourichon

Test php execute functions : do they call a shell ?

May 16th, 2013
1,012
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.27 KB | None | 0 0
  1.    
  2.  
  3.     <?
  4.      
  5.     function dump_all($vars, $msg) {
  6.             echo '<div style="border : 1px solid black ; padding : 1px 0.5em ; margin : 0.5em;">';
  7.             if (!empty($msg)) {
  8.                     echo "<h4 style='margin : 0.5em 0em'>$msg</h4>";
  9.             }
  10.             echo "<pre>";
  11.             print_r($vars);
  12.             echo "</pre>";
  13.             echo '</div>';
  14.     }
  15.      
  16.     function test_exec($commandtotest) {
  17.             $output = exec($commandtotest, $output, $rc);
  18.             dump_all(get_defined_vars(), "exec");
  19.             // => exec calls a shell
  20.     }
  21.      
  22.     function test_shell_exec($commandtotest) {
  23.             $output = shell_exec($commandtotest);
  24.             dump_all(get_defined_vars(), "shell_exec");
  25.             // => shell_exec calls a shell
  26.     }
  27.      
  28.     function test_backtick($commandtotest) {
  29.             $output = `$commandtotest`;
  30.             dump_all(get_defined_vars(), "backtick");
  31.             // => `` calls a shell
  32.     }
  33.      
  34.     function test_passthru($commandtotest) {
  35.             $output = passthru($commandtotest, $rc);
  36.             dump_all(get_defined_vars(), "passthru");
  37.             // => `` calls a shell
  38.     }
  39.      
  40.     function test_system($commandtotest) {
  41.             $output = system($commandtotest, $rc);
  42.             dump_all(get_defined_vars(), "system");
  43.             // => `` calls a shell
  44.     }
  45.      
  46.     function test_popen($commandtotest) {
  47.             $handle = popen($commandtotest, "r");
  48.         $read = fread($handle, 2096);
  49.             dump_all(get_defined_vars(), "popen");
  50.         pclose($handle);
  51.             // => `` calls a shell
  52.     }
  53.      
  54.      
  55.     // From http://www.php.net/manual/en/function.proc-open.php
  56.      
  57.     function test_proc_open($commandtotest) {
  58.             $descriptorspec = array(
  59.                     0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
  60.                     1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
  61.                     2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
  62.                     );
  63.      
  64.             $cwd = '/tmp';
  65.             $env = array('some_option' => 'aeiou');
  66.      
  67.             $process = proc_open($commandtotest, $descriptorspec, $pipes, $cwd, $env);
  68.      
  69.             if (is_resource($process)) {
  70.                     // $pipes now looks like this:
  71.                     // 0 => writeable handle connected to child stdin
  72.                     // 1 => readable handle connected to child stdout
  73.                     // Any error output will be appended to /tmp/error-output.txt
  74.      
  75.                     fwrite($pipes[0], '<?php print_r($_ENV); ?>');
  76.                     fclose($pipes[0]);
  77.      
  78.                     echo stream_get_contents($pipes[1]);
  79.                     fclose($pipes[1]);
  80.      
  81.                     // It is important that you close any pipes before calling
  82.                     // proc_close in order to avoid a deadlock
  83.                     $return_value = proc_close($process);
  84.      
  85.                     echo "command returned $return_value\n";
  86.             }
  87.             dump_all(get_defined_vars(), "proc_open");
  88.     }
  89.      
  90.     function launchchild($programexe,$programvars)
  91.     {
  92.             dump_all(get_defined_vars());
  93.             if (! function_exists('pcntl_fork')) { print('PCNTL functions not available on this PHP installation') ; return; }
  94.      
  95.             switch ($pid = pcntl_fork()) {
  96.             case -1:
  97.                     // @fail
  98.                     print "fail";
  99.                     throw new Exception("Could not fork.");        
  100.                     break;
  101.      
  102.             case 0:
  103.                     // @child: Include() misbehaving code here
  104.                     print "FORK: Child #{$x} \\n";
  105.          
  106.                     dump_all();
  107.      
  108.                     echo "child after fork:", getmypid(), PHP_EOL;
  109.                     pcntl_exec($programexe,$programvars);
  110.                     // generate_fatal_error(); // Undefined function
  111.                     break;
  112.      
  113.             default:
  114.                     // @parent
  115.                     print "FORK: $x Parent\\n";
  116.                     echo "parent after fork:", getmypid(), PHP_EOL;
  117.                     // pcntl_waitpid($pid, $status);
  118.                     break;
  119.             }
  120.      
  121.             print "Done! :^)\\n\\n";
  122.     }
  123.      
  124.     ////////////////////////////////////////////////////////////////////////
  125.      
  126.     $commandtotest='umask';
  127.      
  128.     /*
  129.       |   `-apache2,24369 -k start
  130.       |       `-sh,4142 -c /bin/sleep 10
  131.       |           `-sleep,4143 10
  132.     */
  133.     test_exec($commandtotest);
  134.     test_shell_exec($commandtotest);
  135.     test_backtick($commandtotest);
  136.     test_passthru($commandtotest);
  137.     test_system($commandtotest);
  138.     test_popen($commandtotest);
  139.      
  140.     test_proc_open($commandtotest);
  141.      
  142.     // Variant : use $commandtotest='/bin/sleep 10';
  143.      
  144.     /*
  145.     During the 10 seconds the request runs, do:
  146.     pstree -hpla | grep -i sleep -C 3
  147.     and see if a "sh" sits between apache and sleep.
  148.       |   |-apache2,19873 -k start
  149.       |   |   `-sh,4118 -c /bin/sleep 10
  150.       |   |       `-sleep,4119 10
  151.     test_proc_open($commandtotest);
  152.     */
  153.      
  154.      
  155.     launchchild("umask");
  156.     ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement