<?php
/**
* 清理超长挂马代码的工具
*
* 特点:支持超长代码的清理
* 用法:
* 在本文件同目录下放置要清除的代码样本文件clear_code.txt (应只含清除代码样本)
* 命令行下执行: clear_code 指定目录
*/
if(!isset($argv[1]) or empty($argv[1])) die('not path!');
if(!is_dir($argv[1])) die($argv[1].' is not dir!');
if(!file_exists('clear_code.txt')) die('not find clear_code.txt');
$example_code = trim(file_get_contents('clear_code.txt'));
if(empty($example_code)){
die('clear_code.txt is empty!');
}
echo tool::clear_code($argv[1], $code);
class tool{
static public $regex = '';
static public $select_ext = array('.htm','.html');
static public $find_num = 0;
static public function clear_code($path, $example_code){
$start_code = str_replace('/','\/',quotemeta(substr($example_code, 0, 20)));
$end_code = str_replace('/','\/',quotemeta(substr($example_code, -20)));
self::$regex = '/'.$start_code.'.*'.$end_code.'/s';
return self::clear($path);
}
static public function clear($path){
$list = scandir($path);
foreach($list as $f){
if($f!='.' && $f!='..'){
$filepath = $path."\\".$f;
if(is_dir($filepath)){
self::clear($filepath);
}else{
$file_ext = strrchr($f,'.');
if(in_array($file_ext, self::$select_ext)){
$content = file_get_contents($filepath);
if(preg_match(self::$regex, $content)){
$new_content = preg_replace(self::$regex, '', $content);
file_put_contents($filepath, $new_content);
self::$find_num++;
}
}
}
}
}
return self::$find_num;
}
}//end class