Guest User

para forobeta

a guest
Nov 6th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. <?php
  2. /*
  3. * Sample class usage
  4. */
  5. $scanner = new SimpleFixScanner();
  6. $scanner->scan();
  7.  
  8. /**
  9. * Simple trojan scanner to fix some tedious trojan, that
  10. * corrupt some files on the server.
  11. *
  12. * You can modify this code as you need, to add a new trojan fix
  13. * simply add a method that give in input a filepath and return
  14. * the appropriate exit status (see FixExitStatus class for details), and add the
  15. * trojan name and the method name to the fixList[] array for the callback.
  16. * See fix336988() for an example.
  17. *
  18. * Currently supported trojan:
  19. * - 336988 (Thanks to fatsouls32 - http://www.freestuff.gr/forums/viewtopic.php?t=64419 for 336988 regex fix)
  20. *
  21. * @author Franco D'Agostino [email protected]
  22. *
  23. */
  24. class SimpleFixScanner {
  25. var $fileTypeToScan = array('php','html','htm','tpl',);
  26. var $fixList = array(
  27. //'Scanner Regex Check'=>'devCheckRegex', //Use to check wich files are scannd
  28. 'Trojan 336988' => 'fix336988',
  29. );
  30. var $startTime;
  31. var $memoryLimit = "200M";
  32. var $docRoot;
  33. var $filesToScan;
  34. var $filesScannedCount = 0;
  35. var $filesFixed = array();
  36.  
  37.  
  38. /**
  39. * Wrapper for the scan process
  40. * @see $this->doScan()
  41. */
  42. function scan(){
  43. echo "<h3>Simple Fix Scanner</h3>";
  44. echo "<hr />";
  45. echo "<p>Prepare the scanner... ";
  46. $this->prepareScanner();
  47. echo "<i>done</i>";
  48. echo "<br><small>(Directory: " . $this->docRoot . ")</small></p>";
  49.  
  50. // Do the scann process
  51. echo "<p>Do scan... ";
  52. $this->doScan();
  53. echo "<i>done</i></p>";
  54.  
  55. // Echo scan results
  56. $fileFixedCount = count($this->filesFixed);
  57. if ( $fileFixedCount > 0 ){
  58. echo "<h4>Matches:</h4>";
  59. echo "<p>Fixed " . $fileFixedCount . " of " . $this->filesScannedCount . " files scanned</p>";
  60. echo "<ul>";
  61. foreach($this->filesFixed as $item) {
  62. $exitStatus = FixExitStatus::translateExitStatus($item['exitStatus']);
  63. echo sprintf("<li>{$exitStatus} - <strong>{$item['fix']}</strong> was found in file {$item['file']}</li>"); ;
  64. }
  65. echo "</ul>";
  66. } else {
  67. echo "<h4>No match found.</h4>";
  68. echo "<p>{$this->filesScannedCount} file scanned.</p>";
  69. }
  70.  
  71.  
  72.  
  73. $endtime = microtime(true);
  74. $totaltime = ($endtime - $this->startTime);
  75. echo "<p><small>Time elpased: ".$totaltime." seconds</small></p>";
  76. }
  77.  
  78.  
  79. /**
  80. * Prepare the scanner
  81. */
  82. function prepareScanner(){
  83. ini_set('memory_limit', $this->memoryLimit);
  84. $this->startTime = microtime(true);
  85. if (!$this->docRoot)
  86. $this->docRoot = $_SERVER['DOCUMENT_ROOT'];
  87. $this->filesToScan = $this->getFilesToScan($this->docRoot);
  88. }
  89.  
  90. /**
  91. * Execute the scan process
  92. * @param unknown $param
  93. */
  94. function doScan() {
  95. foreach ($this->filesToScan as $search) {
  96. $this->filesScannedCount++;
  97. foreach ($this->fixList as $name => $method){
  98. $chekFile = call_user_func( array($this, $method), $search[0] );
  99. if ( $chekFile != FixExitStatus::FILE_OK )
  100. $this->filesFixed[] = array('fix' => $name, 'file' => $search[0], 'exitStatus' => $chekFile);
  101. }
  102. }
  103. }
  104.  
  105. /**
  106. * Helper to get the list of the files to scan
  107. */
  108. function getFilesToScan($rootDir){
  109. $directoryIterator = new RecursiveDirectoryIterator($rootDir);
  110. $iterator = new RecursiveIteratorIterator($directoryIterator);
  111. $regex ='/^.+\.(' .implode("|", $this->fileTypeToScan ) . ')$/i';
  112. $files = new RegexIterator($iterator, $regex, RecursiveRegexIterator::GET_MATCH);
  113. return $files;
  114. }
  115.  
  116. /**
  117. * Return true, just for check if the regex works.
  118. * @param unknown $path
  119. */
  120. function devCheckRegex($path) {
  121. if(is_file($path))
  122. return true;
  123. else
  124. return false;
  125. }
  126.  
  127.  
  128. /**
  129. * Check and fix file for:
  130. * 336988 Trojan
  131. * @param unknown $path
  132. * @return true if trojan foud and fixed; otherwise false;
  133. */
  134. function fix336988( $path ) {
  135. $fileFixed = false;
  136. $regexPaterns = array(
  137. "/#336988#(.*?)#\/336988#/ism", // php
  138. "/\<!--336988-->(.*?)\<!--\/336988-->/ism", // html
  139. '#(/\*336988\*/).*?(/\*/336988\*/)#ism', //js
  140. );
  141. $data = file_get_contents($path);
  142.  
  143. foreach ($regexPaterns as $regex) {
  144. if (preg_match($regex,$data)){
  145. // If foud, replace malicious code with empty string
  146. $data = preg_replace($regex,"",$data);
  147. $fileFixed = FixExitStatus::FILE_FIXED;
  148. }
  149. }
  150. if ($fileFixed != FixExitStatus::FILE_OK)
  151. file_put_contents( $path, $data);
  152.  
  153. return $fileFixed;
  154. }
  155. }
  156.  
  157.  
  158. final class FixExitStatus {
  159. private function __constructor() {}
  160. // fix exit status
  161. const FILE_OK = 0;
  162. const FILE_FIXED = 1;
  163. const CANT_FIX = 2;
  164.  
  165. public static function translateExitStatus($status) {
  166. switch ($status) {
  167. case FixExitStatus::FILE_OK:
  168. return "File is safe";
  169. break;
  170. case FixExitStatus::FILE_FIXED:
  171. return "File fixed";
  172. break;
  173. case FixExitStatus::CANT_FIX:
  174. return "Can't fix file";
  175. break;
  176. }
  177.  
  178.  
  179.  
  180. }
  181. }
  182.  
  183. ?>
Advertisement
Add Comment
Please, Sign In to add comment