PHP实现清除wordpress里恶意代码(4)

<?php /** * 文件名:delUnwantedCode.php * 功能:删除FTP里恶意代码 * 使用说明: * 请将文件上传到需要清除恶意代码的目录,然后通过CLI或浏览器访问即可,原有被感染的文件会自动备份 */ $path = dirname(__FILE__); #定义需要处理的目录 $bak_path = $path.DIRECTORY_SEPARATOR.basename(__FILE__,'.php'); #定义源文件备份目录,程序过滤恶意代码前,先按原有的路径备份文档到此目录 $fileType = array('php'); #定义需要处理的文件类型(后缀名),小写 $search = array('@<\?php\s*if\(\!isset\(\$GLOBALS\["\\\x61\\\156\\\x75\\\156\\\x61"\]\)\).*\$bssaiikhvn=\$jtgibaqypx-1;\s*\?>@si'); #定义需要过滤的恶意代码规则 $search_count = array( 'all_file'=>array(), #所有文件 'search_file0'=>array(), #没有恶意代码文件 'search_file1'=>array() #含有恶意代码文件 ); $filelist = listDir($path,$fileType,false); #读取目录里符合条件文件列表 if(!empty($filelist)){ foreach ($filelist as $file){ $file = (isset($file['name'])?$file['name']:$file); $search_count['all_file'][] = $file; $fileContent = file_get_contents($file); $compile_fileContent = preg_replace($search, '', $fileContent); if(strlen($fileContent) != strlen($compile_fileContent) && str_replace($bak_path, '', $file)==$file){ #过滤后文件长度不一致,则表示含有恶意代码(备份文件所在目录不过滤) $search_count['search_file1'][] = $file; ############备份原有文件 开始############### $bakFile = str_replace($path, $bak_path, $file); @make_dir(dirname($bakFile)); @file_put_contents($bakFile, $fileContent); ############备份原有文件 结束############### #重新写入过滤后的内容到原有的PHP文件 @file_put_contents($file, $compile_fileContent); }else{ $search_count['search_file0'][] = $file; } } } #print_r($search_count);die; echo sprintf('从%s里共搜索到%s个符合条件的文件,其中%s个存在恶意代码,已处理结束',$path,count($search_count['all_file']), count($search_count['search_file1']));die; ######################## ## 辅助函数 ######################## /** * 检查目标文件夹是否存在,如果不存在则自动创建该目录 * * @access public * @param string folder 目录路径。不能使用相对于网站根目录的URL * * @return bool */ function make_dir($folder){ $reval = false; if (!file_exists($folder)){ #如果目录不存在则尝试创建该目录 @umask(0); #将目录路径拆分成数组 preg_match_all('/([^\/]*)\/?/i', $folder, $atmp); #如果第一个字符为/则当作物理路径处理 $base = ($atmp[0][0] == 'https://www.jb51.net/') ? 'https://www.jb51.net/' : ''; #遍历包含路径信息的数组 foreach ($atmp[1] AS $val){ if ('' != $val){ $base .= $val; if ('..' == $val || '.' == $val){ #如果目录为.或者..则直接补/继续下一个循环 $base .= 'https://www.jb51.net/'; continue; } }else{ continue; } $base .= 'https://www.jb51.net/'; if (!file_exists($base)){ #尝试创建目录,如果创建失败则继续循环 if (@mkdir(rtrim($base, 'https://www.jb51.net/'), 0777)){ @chmod($base, 0777); $reval = true; } } } }else{ #路径已经存在。返回该路径是不是一个目录 $reval = is_dir($folder); } clearstatcache(); return $reval; } ########获取目录下所有文件,包括子目录 开始################ function listDir($path,$fileType=array(),$fileInfo=true){ $path = str_replace(array('https://www.jb51.net/','\\'), DIRECTORY_SEPARATOR, $path); if(!file_exists($path)||!is_dir($path)){ return ''; } if(substr($path, -1,1)==DIRECTORY_SEPARATOR){ $path = substr($path, 0,-1); } $dirList=array(); $dir=opendir($path); while($file=readdir($dir)){ #若有定义$fileType,并且文件类型不在$fileType范围内或文件是一个目录,则跳过 if($file!=='.'&&$file!=='..'){ $file = $path.DIRECTORY_SEPARATOR.$file; if(is_dir($file)){ if(empty($fileType)){ $dirList[] = ($fileInfo==true?array('name'=>$file,'isDir'=>intval(is_dir($file))):$file); } $dirList = array_merge($dirList,listDir($file,$fileType)); }elseif(!empty($fileType) && (in_array(pathinfo($file, PATHINFO_EXTENSION), $fileType))){ $dirList[] = ($fileInfo==true?array('name'=>$file,'isDir'=>intval(is_dir($file)),'md5_file'=>md5_file($file),'filesize'=>filesize($file),'filemtime'=>filemtime($file)):$file); } }; }; closedir($dir); return $dirList; } ########获取目录下所有文件,包括子目录 结束################

删除FTP里恶意代码(支持任意数量的文件处理)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2037e9ed15a4c9b25dac5862f5a1be17.html