PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/DecodingServer/submit.php

https://github.com/bwall/PHP-RFI-Payload-Decoder
PHP | 212 lines | 196 code | 4 blank | 12 comment | 16 complexity | 23c0b2553d6f16d994badf6e73856a59 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 RemoveComments($str)
  19. {
  20. $done = false;
  21. while($done === false)
  22. {
  23. if(preg_match('/\/\*.*\*\//m', $str, $matches))
  24. {
  25. $str = str_replace($matches[0], "", $str);
  26. }
  27. //else if(preg_match('/\/\/.*\n/m', $str, $matches))
  28. //{
  29. //Causing issues in some base64
  30. //$str = str_replace($matches[0], "", $str);
  31. //}
  32. else
  33. {
  34. $done = true;
  35. }
  36. }
  37. return $str;
  38. }
  39. function ExpandLines($str)
  40. {
  41. return $str;
  42. //return str_replace(";", ";\n", $str);
  43. }
  44. function ClearEmptyEvals(&$str)
  45. {
  46. $done = false;
  47. while($done === false)
  48. {
  49. if(preg_match('/eval\(["\'][[:space:]]*["\']\);/m', $str, $matches))
  50. {
  51. $str = str_replace($matches[0], "", $str);
  52. }
  53. else
  54. $done = true;
  55. }
  56. }
  57. function Decode($funcArray, &$str, &$aliases, &$steps)
  58. {
  59. $count = count($funcArray);
  60. $funcs = "";
  61. $tail = "";
  62. $toEval = "";
  63. $endEval = "";
  64. for($i = 0; $i < $count; $i++)
  65. {
  66. $funcs .= $funcArray[$i]."[[:space:]]*\([[:space:]]*";
  67. $tail .= '[[:space:]]*\)';
  68. $toEval .= $funcArray[$i]."(";
  69. $endEval .= ")";
  70. }
  71. $endEval .= ";";
  72. if(preg_match('/'.$funcs.'(?<data>"[^"]+")'.$tail.'/m', $str, $matches))
  73. {
  74. $str = str_replace($matches[0], "'".ExpandLines(eval("return ".$toEval.$matches["data"].$endEval))."'", $str);
  75. $steps .= $toEval.$matches["data"].$endEval."\n";
  76. return true;
  77. }
  78. else if(preg_match('/'.$funcs.'(?<data>\'[^\']+\')'.$tail.'/m', $str, $matches))
  79. {
  80. $str = str_replace($matches[0], "'".ExpandLines(eval("return ".$toEval.$matches["data"].$endEval))."'", $str);
  81. $steps .= $toEval.$matches["data"].$endEval."\n";
  82. return true;
  83. }
  84. else if(preg_match('/'.$funcs.'(?<data>\'[^\']+\')/m', $str, $matches))
  85. {
  86. $str = str_replace($matches[0], "'".ExpandLines(eval("return ".$toEval.$matches["data"].$endEval))."'", $str);
  87. $steps .= $toEval.$matches["data"].$endEval."\n";
  88. return true;
  89. }
  90. else if(preg_match('/'.$funcs.'(?<data>"[^"]+")/m', $str, $matches))
  91. {
  92. $str = str_replace($matches[0], "'".ExpandLines(eval("return ".$toEval.$matches["data"].$endEval))."'", $str);
  93. $steps .= $toEval.$matches["data"].$endEval."\n";
  94. return true;
  95. }
  96. else if(preg_match('/'.$funcs.'(?<data>"[^"]+)/m', $str, $matches))
  97. {
  98. $str = str_replace($matches[0], "'".ExpandLines(eval("return ".$toEval.$matches["data"].'"'.$endEval))."'", $str);
  99. $steps .= $toEval.$matches["data"].$endEval."\n";
  100. return true;
  101. }
  102. else if(preg_match('/'.$funcs.'(?<data>\'[^\']+)/m', $str, $matches))
  103. {
  104. $str = str_replace($matches[0], "'".ExpandLines(eval("return ".$toEval.$matches["data"]."'".$endEval))."'", $str);
  105. $steps .= $toEval.$matches["data"].$endEval."\n";
  106. return true;
  107. }
  108. else
  109. {
  110. return false;
  111. }
  112. }
  113. function AutoDecode(&$str, &$steps)
  114. {
  115. $str = RemoveComments($str);
  116. $str = ExpandLines($str);
  117. $done = FALSE;
  118. $aliases = array();
  119. $variables = array();
  120. while($done === FALSE)
  121. {
  122. if(Decode(array("gzinflate", "str_rot13", "base64_decode"), $str, $aliases, $steps) ||
  123. Decode(array("gzuncompress", "str_rot13", "base64_decode"), $str, $aliases, $steps) ||
  124. Decode(array("gzinflate", "str_rot13"), $str, $aliases, $steps) ||
  125. Decode(array("gzuncompress", "str_rot13"), $str, $aliases, $steps) ||
  126. Decode(array("gzinflate", "base64_decode"), $str, $aliases, $steps) ||
  127. Decode(array("gzuncompress", "base64_decode"), $str, $aliases, $steps) ||
  128. Decode(array("base64_decode"), $str, $aliases, $steps) ||
  129. Decode(array("gzinflate", "str_rot13"), $str, $aliases, $steps) ||
  130. Decode(array("gzinflate", "base64_decode", "str_rot13"), $str, $aliases, $steps) ||
  131. Decode(array("base64_decode", "str_rot13"), $str, $aliases, $steps))
  132. {
  133. }
  134. else
  135. {
  136. $done = true;
  137. if(preg_match_all('/(\$[[:alnum:]_]+)[[:space:]]*=[[:space:]]*("[^"]+");/s', $str, $matches) != 0)
  138. {
  139. $count = count($matches[0]);
  140. for($i = 0; $i < $count; $i++)
  141. {
  142. $name = $matches[1][$i];
  143. if(in_array($name, $variables) === true)
  144. {
  145. continue;
  146. }
  147. $value = $matches[2][$i];
  148. if($str !== preg_replace('/('.preg_quote($name).')([^<>[:alnum:]_ \=])/m', "$value$2", $str) && strstr($value, $name) === false)
  149. {
  150. $done = false;
  151. array_push($variables, $name);
  152. $str = preg_replace('/('.preg_quote($name).')([^<>[:alnum:]_ \=])/m', "$value$2", $str);
  153. $steps .= "Replacing $name with $value\n";
  154. }
  155. }
  156. }
  157. if(preg_match_all('/(\$[[:alnum:]_]+)[[:space:]]*=[[:space:]]*(\'[^\']+\');/s', $str, $matches) != 0)
  158. {
  159. $count = count($matches[0]);
  160. for($i = 0; $i < $count; $i++)
  161. {
  162. $name = $matches[1][$i];
  163. if(in_array($name, $variables) === true)
  164. {
  165. continue;
  166. }
  167. $value = $matches[2][$i];
  168. if($str !== preg_replace('/('.preg_quote($name).')([^<>[:alnum:]_ \=])/m', "$value$2", $str) && strstr($value, $name) === false)
  169. {
  170. $done = false;
  171. array_push($variables, $name);
  172. $str = preg_replace('/('.preg_quote($name).')([^<>[:alnum:]_ \=])/m', "$value$2", $str);
  173. $steps .= "Replacing $name with $value\n";
  174. }
  175. }
  176. }
  177. }
  178. ClearEmptyEvals($str);
  179. }
  180. }
  181. $str = "";
  182. $steps = "";
  183. $meta = "";
  184. if(isset($_REQUEST['u']) && !empty($_REQUEST['u']))
  185. {
  186. $url = base64_decode(urldecode($_REQUEST['u']));
  187. $file = GetFileName($url, ".DecodedByUrl");
  188. if($file !== false)
  189. {
  190. $str = file_get_contents($url, false, null, 0, 1024 * 1024 * 16);
  191. $raw = $str;
  192. if($str !== false)
  193. {
  194. AutoDecode($str, $steps);
  195. $toFile = "Timestamp: ".strftime('%c')."\n";
  196. $toFile .= "Submitter: ".$_SERVER['REMOTE_ADDR']."\n";
  197. $toFile .= "URL: ".$url."\n";
  198. $toFile .= "Was decoded from url on server.\n\n";
  199. $toFile .= "Shell -> ".base64_encode($str)."\n\n";
  200. $toFile .= "Raw -> ".base64_encode($raw)."\n\n";
  201. file_put_contents($file, $toFile);
  202. }
  203. }
  204. }
  205. ?>