移除& lt; ?PHP *** ?>包含特定文本的行


Remove <?php *** ?> line that contains a specific text

几天前我发现我被一个恶意脚本入侵,该脚本已经传播到所有index.php文件,config.phpwp -config.php.

我试图得到一个代码并修改它来查找包含此脚本的文件夹中的文件并自动删除它。

遗憾的是我没有成功。

代码是这样开始的:

<?php  $J24fj = 'bH1Vrb2sswS7fUn8HyCx.... AND MUCH MORE ?>

这个代码总是在变化,但是里面有一个常量:这个文本:ZXZhbChiYXNlNjRfZGVjb2RlKCJaW

我找到了这个代码,我怎么能改变它来搜索常数和删除整个行<?php ******* ?>

<?php 
$find='ZXZhbChiYXNlNjRfZGVjb2RlKCJaW';
echo findString('./',$find);
function findString($path,$find){
    $return='';
    ob_start();
    if ($handle = opendir($path)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                if(is_dir($path.'/'.$file)){
                    $sub=findString($path.'/'.$file,$find);
                    if(isset($sub)){
                        echo $sub.PHP_EOL;
                    }
                }else{
                    $ext=substr(strtolower($file),-3);
                    if($ext=='php'){
                        $filesource=file_get_contents($path.'/'.$file);
                        //The cleaning bit
                        echo "The string '".htmlentities($find)."' was found in the file '$path/$file and has been removed from the source file.<br />";
                        $clean_source = preg_replace('#'.$find.'#','',$filesource);
                        // $clean_source = str_replace($find,'',$filesource);
                        file_put_contents($path.'/'.$file,$clean_source);
                    }else{
                        continue;
                    }
                }
            }
        }
        closedir($handle);
    }
    $return = ob_get_contents();
    ob_end_clean();
    return $return;
}
?>

Thanks in advance

安东尼奥

比如

preg_replace ('/(' & lt; ' ?php + ?) (ZXZhbChiYXNlNjRfZGVjb2RlKCJaWZXZhbChiYXNlNjRfZGVjb2RlKCJaW) (. + ?'?>)/' '',$ filesource);

如果PHP的开始和结束标签是自包含的,也就是说,不包含任何你希望保留的代码,

应该可以完成这个任务。

请注意,preg_replace不需要将文件的内容复制到一个变量中,然后将新字符串复制到$cleansource变量中。

此外,在输出一个表示代码已被替换的字符串之前,您应该小心地进行更改!