PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/DecodingServer/index.php

https://github.com/bwall/PHP-RFI-Payload-Decoder
PHP | 299 lines | 282 code | 5 blank | 12 comment | 16 complexity | 23512af2df29d20723e7a9a1e6a64050 MD5 | raw file
  1. <?php
  2. /*
  3. * Generates a name for a bot dump
  4. * Set $dumpFolder to a folder that is not web viewable, its suggested to use the full path
  5. *
  6. * returns false if it already exists, else returns the filename
  7. */
  8. function GetFileName($uri, $tail = "", $canFail = true)
  9. {
  10. $dumpFolder = "temp/";
  11. if(file_exists($dumpFolder.md5($uri).$tail))
  12. {
  13. if($canFail)
  14. return false;
  15. }
  16. return $dumpFolder.md5($uri).$tail;
  17. }
  18. function GetUrl($uri)
  19. {
  20. return "read.php?u=".md5($uri);
  21. }
  22. function RemoveComments($str)
  23. {
  24. $done = false;
  25. while($done === false)
  26. {
  27. if(preg_match('/\/\*.*\*\//m', $str, $matches))
  28. {
  29. $str = str_replace($matches[0], "", $str);
  30. }
  31. //else if(preg_match('/\/\/.*\n/m', $str, $matches))
  32. //{
  33. //Causing issues in some base64
  34. //$str = str_replace($matches[0], "", $str);
  35. //}
  36. else
  37. {
  38. $done = true;
  39. }
  40. }
  41. return $str;
  42. }
  43. function ExpandLines($str)
  44. {
  45. return $str;
  46. //return str_replace(";", ";\n", $str);
  47. }
  48. function ClearEmptyEvals(&$str)
  49. {
  50. $done = false;
  51. while($done === false)
  52. {
  53. if(preg_match('/eval\(["\'][[:space:]]*["\']\);/m', $str, $matches))
  54. {
  55. $str = str_replace($matches[0], "", $str);
  56. }
  57. else
  58. $done = true;
  59. }
  60. }
  61. function Decode($funcArray, &$str, &$aliases, &$steps)
  62. {
  63. $count = count($funcArray);
  64. $funcs = "";
  65. $tail = "";
  66. $toEval = "";
  67. $endEval = "";
  68. for($i = 0; $i < $count; $i++)
  69. {
  70. $funcs .= $funcArray[$i]."[[:space:]]*\([[:space:]]*";
  71. $tail .= '[[:space:]]*\)';
  72. $toEval .= $funcArray[$i]."(";
  73. $endEval .= ")";
  74. }
  75. $endEval .= ";";
  76. if(preg_match('/'.$funcs.'(?<data>"[^"]+")'.$tail.'/m', $str, $matches))
  77. {
  78. $str = str_replace($matches[0], "'".ExpandLines(eval("return ".$toEval.$matches["data"].$endEval))."'", $str);
  79. $steps .= $toEval.$matches["data"].$endEval."\n";
  80. return true;
  81. }
  82. else if(preg_match('/'.$funcs.'(?<data>\'[^\']+\')'.$tail.'/m', $str, $matches))
  83. {
  84. $str = str_replace($matches[0], "'".ExpandLines(eval("return ".$toEval.$matches["data"].$endEval))."'", $str);
  85. $steps .= $toEval.$matches["data"].$endEval."\n";
  86. return true;
  87. }
  88. else if(preg_match('/'.$funcs.'(?<data>\'[^\']+\')/m', $str, $matches))
  89. {
  90. $str = str_replace($matches[0], "'".ExpandLines(eval("return ".$toEval.$matches["data"].$endEval))."'", $str);
  91. $steps .= $toEval.$matches["data"].$endEval."\n";
  92. return true;
  93. }
  94. else if(preg_match('/'.$funcs.'(?<data>"[^"]+")/m', $str, $matches))
  95. {
  96. $str = str_replace($matches[0], "'".ExpandLines(eval("return ".$toEval.$matches["data"].$endEval))."'", $str);
  97. $steps .= $toEval.$matches["data"].$endEval."\n";
  98. return true;
  99. }
  100. else if(preg_match('/'.$funcs.'(?<data>"[^"]+)/m', $str, $matches))
  101. {
  102. $str = str_replace($matches[0], "'".ExpandLines(eval("return ".$toEval.$matches["data"].'"'.$endEval))."'", $str);
  103. $steps .= $toEval.$matches["data"].$endEval."\n";
  104. return true;
  105. }
  106. else if(preg_match('/'.$funcs.'(?<data>\'[^\']+)/m', $str, $matches))
  107. {
  108. $str = str_replace($matches[0], "'".ExpandLines(eval("return ".$toEval.$matches["data"]."'".$endEval))."'", $str);
  109. $steps .= $toEval.$matches["data"].$endEval."\n";
  110. return true;
  111. }
  112. else
  113. {
  114. return false;
  115. }
  116. }
  117. function RemoteFileSize($url)
  118. {
  119. $sch = parse_url($url, PHP_URL_SCHEME);
  120. if (($sch != "http") && ($sch != "https") && ($sch != "ftp") && ($sch != "ftps")) {
  121. return false;
  122. }
  123. if (($sch == "http") || ($sch == "https")) {
  124. $headers = get_headers($url, 1);
  125. if ($headers === false || (!array_key_exists("Content-Length", $headers))) { return false; }
  126. return $headers["Content-Length"];
  127. }
  128. if (($sch == "ftp") || ($sch == "ftps")) {
  129. $server = parse_url($url, PHP_URL_HOST);
  130. $port = parse_url($url, PHP_URL_PORT);
  131. $path = parse_url($url, PHP_URL_PATH);
  132. $user = parse_url($url, PHP_URL_USER);
  133. $pass = parse_url($url, PHP_URL_PASS);
  134. if ((!$server) || (!$path)) { return false; }
  135. if (!$port) { $port = 21; }
  136. if (!$user) { $user = "anonymous"; }
  137. if (!$pass) { $pass = "phpos@"; }
  138. switch ($sch) {
  139. case "ftp":
  140. $ftpid = ftp_connect($server, $port);
  141. break;
  142. case "ftps":
  143. $ftpid = ftp_ssl_connect($server, $port);
  144. break;
  145. }
  146. if (!$ftpid) { return false; }
  147. $login = ftp_login($ftpid, $user, $pass);
  148. if (!$login) { return false; }
  149. $ftpsize = ftp_size($ftpid, $path);
  150. ftp_close($ftpid);
  151. if ($ftpsize == -1) { return false; }
  152. return $ftpsize;
  153. }
  154. }
  155. function AutoDecode(&$str, &$steps)
  156. {
  157. $str = RemoveComments($str);
  158. $str = ExpandLines($str);
  159. $done = FALSE;
  160. $aliases = array();
  161. $variables = array();
  162. while($done === FALSE)
  163. {
  164. if(Decode(array("gzinflate", "str_rot13", "base64_decode"), $str, $aliases, $steps) ||
  165. Decode(array("gzuncompress", "str_rot13", "base64_decode"), $str, $aliases, $steps) ||
  166. Decode(array("gzinflate", "str_rot13"), $str, $aliases, $steps) ||
  167. Decode(array("gzuncompress", "str_rot13"), $str, $aliases, $steps) ||
  168. Decode(array("gzinflate", "base64_decode"), $str, $aliases, $steps) ||
  169. Decode(array("gzuncompress", "base64_decode"), $str, $aliases, $steps) ||
  170. Decode(array("base64_decode"), $str, $aliases, $steps) ||
  171. Decode(array("gzinflate", "str_rot13"), $str, $aliases, $steps) ||
  172. Decode(array("gzinflate", "base64_decode", "str_rot13"), $str, $aliases, $steps) ||
  173. Decode(array("base64_decode", "str_rot13"), $str, $aliases, $steps))
  174. {
  175. }
  176. else
  177. {
  178. $done = true;
  179. if(preg_match_all('/(\$[[:alnum:]_]+)[[:space:]]*=[[:space:]]*("[^"]+");/s', $str, $matches) != 0)
  180. {
  181. $count = count($matches[0]);
  182. for($i = 0; $i < $count; $i++)
  183. {
  184. $name = $matches[1][$i];
  185. if(in_array($name, $variables) === true)
  186. {
  187. continue;
  188. }
  189. $value = $matches[2][$i];
  190. if($str !== preg_replace('/('.preg_quote($name).')([^<>[:alnum:]_ \=])/m', "$value$2", $str) && strstr($value, $name) === false)
  191. {
  192. $done = false;
  193. array_push($variables, $name);
  194. $str = preg_replace('/('.preg_quote($name).')([^<>[:alnum:]_ \=])/m', "$value$2", $str);
  195. $steps .= "Replacing $name with $value\n";
  196. }
  197. }
  198. }
  199. if(preg_match_all('/(\$[[:alnum:]_]+)[[:space:]]*=[[:space:]]*(\'[^\']+\');/s', $str, $matches) != 0)
  200. {
  201. $count = count($matches[0]);
  202. for($i = 0; $i < $count; $i++)
  203. {
  204. $name = $matches[1][$i];
  205. if(in_array($name, $variables) === true)
  206. {
  207. continue;
  208. }
  209. $value = $matches[2][$i];
  210. if($str !== preg_replace('/('.preg_quote($name).')([^<>[:alnum:]_ \=])/m', "$value$2", $str) && strstr($value, $name) === false)
  211. {
  212. $done = false;
  213. array_push($variables, $name);
  214. $str = preg_replace('/('.preg_quote($name).')([^<>[:alnum:]_ \=])/m', "$value$2", $str);
  215. $steps .= "Replacing $name with $value\n";
  216. }
  217. }
  218. }
  219. }
  220. ClearEmptyEvals($str);
  221. }
  222. }
  223. $str = "";
  224. $steps = "";
  225. $meta = "";
  226. if(isset($_POST['input']) && !empty($_POST['input']) && !(isset($_POST['url']) && !empty($_POST['url'])))
  227. {
  228. $str = $_POST['input'];
  229. $raw = $str;
  230. AutoDecode($str, $steps);
  231. $file = GetFileName($str, ".DecodedOnWeb");
  232. if($file !== false)
  233. {
  234. $toFile = "Timestamp: ".strftime('%c')."\n";
  235. $toFile .= "Submitter: ".exec("htdeny ".$_SERVER['REMOTE_ADDR'])."\n";
  236. $toFile .= "Was decoded from text on server.\n\n";
  237. $toFile .= "Shell -> ".base64_encode($str)."\n\n";
  238. $toFile .= "Raw -> ".base64_encode($raw)."\n\n";
  239. file_put_contents($file, $toFile);
  240. }
  241. }
  242. else if(isset($_POST['url']) && !empty($_POST['url']))
  243. {
  244. $file = GetFileName($_POST['url'], ".DecodedByUrl");
  245. if($file !== false)
  246. {
  247. $str = file_get_contents($_POST['url'], false, null, 0, 1024 * 1024 * 16);
  248. $raw = $str;
  249. if($str !== false)
  250. {
  251. AutoDecode($str, $steps);
  252. $toFile = "Timestamp: ".strftime('%c')."\n";
  253. $toFile .= "Submitter: ".$_SERVER['REMOTE_ADDR']."\n";
  254. $toFile .= "URL: ".$_POST['url']."\n";
  255. $toFile .= "Was decoded from url on server.\n\n";
  256. $toFile .= "Shell -> ".base64_encode($str)."\n\n";
  257. $toFile .= "Raw -> ".base64_encode($raw)."\n\n";
  258. file_put_contents($file, $toFile);
  259. $meta = "<META HTTP-EQUIV=REFRESH CONTENT=\"1; URL=".GetUrl($_POST['url'])."\">";
  260. }
  261. }
  262. else
  263. {
  264. $meta = "<META HTTP-EQUIV=REFRESH CONTENT=\"1; URL=".GetUrl($_POST['url'])."\">";
  265. }
  266. }
  267. print "<!DOCTYPE html>
  268. <html>
  269. $meta
  270. <body>
  271. <form action=\"\" method=\"post\">
  272. <table width=\"100%\" height=\"100%\" border=\"1\"><tr><td colspan=\"2\">
  273. <h1>PHP Decoder</h1><a href=\"https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=KCHYQRCBZEWML\">Donate</a><br/> <a href='https://github.com/bwall/PHP-RFI-Payload-Decoder'>Source</a></td></tr><tr valign=\"top\">
  274. <td style=\"width:100px;text-align:top;\"><b>Tools</b><br />
  275. <br />URL: <input type=\"text\" name=\"url\" />
  276. <br /><input type=\"submit\" value=\"Decode\" />
  277. <br />
  278. <p><a href=\"https://www.firebwall.com/decoding/read.php\">Decoded Bots</a></p><br />
  279. </td>
  280. <td style=\"height:100%;text-align:top;\">
  281. <table width=\"99%\" border=\"0\">
  282. <tr style=\"width:90%;text-align:top;\">
  283. <td style=\"width:100%;text-align:top;\">
  284. <p>PHP to Decode</p>
  285. <textarea name=\"input\" style=\"width:100%;height:400px;\">
  286. ".htmlentities($str)."</textarea></td></tr>
  287. </table></tr></td><tr>
  288. <td colspan=\"2\" style=\"text-align:center;\">
  289. Copyright &copy; fireBwall 2012. All rights reserved</td></tr></table></form></body>
  290. </html>
  291. ";
  292. ?>