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

<?php /** * 文件名:delAllUnwantedCode.php * 功能:删除FTP里恶意代码(支持任意数量的文件处理) * 使用说明: * 请将文件上传到需要清除恶意代码的目录,然后通过CLI或浏览器访问即可,原有被感染的文件会自动备份 */ set_time_limit(0);ignore_user_abort(true); $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'); #定义需要过滤的恶意代码规则 $file_count = array( 'all_file'=>0, #所有文件 'filter_file'=>0 #含有恶意代码文件 ); replaceUnwantedCode($path); #执行过滤 #print_r($search_count);die; echo sprintf('从%s里共搜索到%s个符合条件的文件,其中%s个存在恶意代码已清理,原始文件保存在%s',$path, ($file_count['all_file']), ($file_count['filter_file']), $bak_path);die; function replaceUnwantedCode($path){ global $bak_path,$fileType,$search,$file_count; $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); } $dir=opendir($path); while($file=readdir($dir)){ #若有定义$fileType,并且文件类型不在$fileType范围内或文件是一个目录,则跳过 if($file!=='.'&&$file!=='..'){ $file = $path.DIRECTORY_SEPARATOR.$file; if(is_dir($file)){ replaceUnwantedCode($file); }elseif(!empty($fileType) && (in_array(pathinfo($file, PATHINFO_EXTENSION), $fileType))){ ################################ @$file_count['all_file']++; $fileContent = file_get_contents($file); #文件原始代码 $compile_fileContent = preg_replace($search, '', $fileContent); #过滤后的内容 if(strlen($fileContent) != strlen($compile_fileContent) && str_replace($bak_path, '', $file)==$file){ #过滤后文件长度不一致,则表示含有恶意代码(备份文件所在目录不过滤) $file_count['filter_file']++; ############备份原有文件 开始############### $bakFile = str_replace($path, $bak_path, $file); @make_dir(dirname($bakFile)); @file_put_contents($bakFile, $fileContent); ############备份原有文件 结束############### #重新写入过滤后的内容到原有的PHP文件 @file_put_contents($file, $compile_fileContent); } ################################ unset($fileContent,$compile_fileContent); } }; }; closedir($dir); return true; } ######################## ## 辅助函数 ######################## /** * 检查目标文件夹是否存在,如果不存在则自动创建该目录 * * @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; }

您可能感兴趣的文章:

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

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