PHP/SSH正则表达式脚本/命令从许多文件中删除相同的恶意软件代码

Tal*_*boy 3 php regex security wordpress

我有一种病毒感染了我客户服务器上的数千个文件.

幸运的是,我已经在这个人的服务器上处理了很多其他恶意软件,这个看起来很容易做简单的正则表达式(他把所有他的网站放在同一个帐户上:(但我正和他一起解决这个问题).

基本上,不像大多数恶意软件,我看到它在关闭之前注入了php?好的代码(使得很难确定什么是好代码/坏代码),这个当前的恶意软件总是添加一个新的<?php ... malware ... ?>.

所以基本上,说这里的代码很好:

<?php
require('./wp-blog-header.php'); 
?>
Run Code Online (Sandbox Code Playgroud)

而不是在require语句之后但在?>之前立即添加某种base64_decode eval(当页面恰好在条件/复杂语句中结束时可以使删除变得困难),这将始终添加以下代码,<?php ... ?>如此新:

<?php
require('./wp-blog-header.php'); 
?><?php ... malware ...?>
Run Code Online (Sandbox Code Playgroud)

我不想在这里放任何恶意代码,但这就是恶意代码始终启动的方式:

<?php @error_reporting(0); if (!isset($eva1fYlbakBcVSir)) {$eva1fYlbakBcVSir = "tons and tons of characters";$eva1tYlbakBcVSir = "\x6335\1443\3x6f\1534\x70\170\x65";$SNIPSNIPSNIPSNIP;} ?>
Run Code Online (Sandbox Code Playgroud)

我想搜索每个文件<?php @error_reporting(0); if (!isset,如果它是页面上的最后一个PHP语句,则删除该文件中的所有内容

Law*_*one 12

以下是使用纯php清理整个项目的方法.

对于任何损害,我不承担任何责任,包括但不限于因使用提供的代码而产生的直接,间接,特殊或后果性损害,无论是否基于在保修,合同,侵权或其他方面; 伤害是否由人或财产或其他方面造成; 以及该代码的使用是否会导致损失,或者是否产生损失.,p

<?php 
//Enter it as it is and escape any single quotes
$find='<?php @error_reporting(0); if (!isset($eva1fYlbakBcVSir)) {$eva1fYlbakBcVSir =\'\';?>';

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);
                        $pos = strpos($filesource, $find);
                        if ($pos === false) {
                            continue;
                        } else {
                        //The cleaning bit
                        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 />";
                        $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;
}
?>
Run Code Online (Sandbox Code Playgroud)

祝好运.

更新(使用正则表达式):

<?php 
error_reporting(E_ALL);
$find='<\?php @error_reporting\(0\); if \(!isset\((.*?)\?>';

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;
}
?>
Run Code Online (Sandbox Code Playgroud)