SHARE
TWEET

Remove malicious code recursively

sarmadhassan Dec 18th, 2013 75 Never
  1. <?php
  2. //Enter it as it is and escape any single quotes
  3. $find= ‘enter bad code here ‘;
  4.  
  5. echo findString(./,$find);
  6.  
  7. function findString($path,$find){
  8.         $return=‘’;
  9.         ob_start();
  10.         if ($handle = opendir($path)) {
  11.                 while (false !== ($file = readdir($handle))) {
  12.                         if ($file !=.&& $file !=..) {
  13.                                 if(is_dir($path./.$file)){
  14.                                         $sub=findString($path./.$file,$find);
  15.                                         if(isset($sub)){
  16.                                                 echo $sub.PHP_EOL;
  17.                                         }
  18.                                 }else{
  19.                                         $ext=substr(strtolower($file),-3);
  20.                                         if($ext==‘php’){
  21.                                                 $filesource=file_get_contents($path./.$file);
  22.                                                 $pos = strpos($filesource, $find);
  23.                                                 if ($pos === false) {
  24.                                                         continue;
  25.                                                 } else {
  26.                                                 //The cleaning bit
  27.                                                 echo “The string ‘”.htmlentities($find).”’ was found in the file$path/$file and exists at position $pos and has been removed from the source file.<br />;
  28.                                                 $clean_source = str_replace($find,’’,$filesource);
  29.                                                 file_put_contents($path./.$file,$clean_source);
  30.                                                 }
  31.                                         }else{
  32.                                                 continue;
  33.                                         }
  34.                                 }
  35.                         }
  36.                 }
  37.                 closedir($handle);
  38.         }
  39.         $return = ob_get_contents();
  40.         ob_end_clean();
  41.         return $return;
  42. }
  43. ?>
RAW Paste Data
Top