[英]How can I determine what scripts are making use of fopen?
我的網站被一個腳本小子非常成功地攻擊了。 在自動的基礎上,在我的服務器上訪問了一個隱藏腳本,該腳本會導致修改我的所有 index.php 文件,並將 iframe 添加到它們的頂部(base 64 編碼)。
我無法讓我的托管服務提供商提供幫助,因為他們說他們在這件事上相當無助。
我懷疑如果我可以確定哪些腳本正在使用 php 的 fopen function,那么我將能夠發現主篡改腳本的位置並將其刪除。
有什么建議嗎?
保持被黑客入侵的服務器運行是不負責任的。 除了更改 index.php 文件之外,還可能安裝了真正的惡意工具 - 同時您的服務器可能是犯罪分子的開放代理。
除了 fopen 之外,還有十幾種方法可以從 PHP 中更改文件。 例子
exec('echo foo > /path/to/bar')
如果真的是fopen,那你就很幸運了。
使用find
查找所有 PHP 腳本,然后使用 grep 查找 fopen。 如果您沒有 shell 訪問權限,請下載包含腳本的整個目錄並在您的計算機上執行此操作。
find /base/dir/with/your/scripts -name '*.php' | xargs grep 'fopen'
我仍在使用 grep 和 ssh 訪問權限來發現和排除問題。 可能不是嵌入式腳本重寫我的文件,而是 php 代碼正在由站點某處的輸入表單執行。 我還不確定。 同時,我編寫了一個腳本來對抗 eval(base64_decode 注入我的 index.php 文件。這將檢查並刪除 index.php 文件中的 eval(base64_decode(''); 並將我的文件結構搜索到 5 目錄級別很深,這對我的站點來說已經足夠了。我將它設置為每 5 分鍾在 cronjob 上運行一次。它似乎根本不是服務器密集型的。
<?php
$level=5;
function get_sorted($path)
{
//$ignore = array('.',"'","error_log");
$dh = @opendir( $path );
while ($file = @readdir( $dh ))
{
if (!strstr($file,'.')&&!strstr($file,"'")&&!strstr($file,"error_log")&&!strstr($file,"README")&&!strstr($file,"cookietxt")&&$file!='.'&&$file!='..')
{
$directories[] = $file;
}
else
{
if ($file!='.'&&$file!='..')
{
$files[] = $file;
}
}
}
$array = array($directories,$files);
return $array;
}
function clean_files($files, $path)
{
//echo 1;exit;
if ($files)
{
foreach($files as $key=>$val)
{
//echo $val;
if ($val == 'index.php')
{
//echo 1; exit;
//fopen .htacess
$targetFile = "$path/$val";
echo "Checking: $targetFile <br>";
$handle1 = fopen($targetFile, 'r');
$data = @fread($handle1,filesize($targetFile));
fclose($handle1);
$string = preg_match('/eval.base64_decode(.*?)\;/', $data, $matches );
$string = $matches[0];
if ($string)
{
echo "MALWARE FOUND IN $targetFile ! ... rewriting!<br>";
$data = str_replace($string,'', $data);
$handle1 = fopen($targetFile, 'w');
fwrite($handle1, $data);
fclose($handle1);
//exit;
}
unset($string);
unset($data);
}
}
}
else
{
echo "<br>No files discovered in $path<br>";
}
}
//clean first level
$array = get_sorted('.');
$directories = $array[0];
$files = $array[1];
clean_files($files,'.');
//get second level & clean
foreach ($directories as $key=>$val)
{
$p_1 = "./$val";
$a_1 = get_sorted($val);
$d_1 = $a_1[0];
$f_1 = $a_1[1];
//echo $val;
//print_r($d_1);
//echo "<hr>";
clean_files($f_1, "{$p_1}");
//check and clean level 2
if ($d_1)
{
foreach ($d_1 as $k_1=>$v_1)
{
//echo $v_1;exit;
$p_2 = $p_1.'/'.$v_1;
$a_2 = get_sorted($p_2);
$d_2 = $a_2[0];
$f_2 = $a_2[1];
clean_files($f_2, $p_2);
if ($d_2)
{
//check and clean level 3
foreach ($d_2 as $k_2=>$v_2)
{
$p_3 = $p_2.'/'.$v_2;
//echo $p_3;
$a_3 = get_sorted($p_3);
$d_3 = $a_3[0];
$f_3 = $a_3[1];
//echo"<hr>$v_2";
//print_r($f_3);exit;
clean_files($f_3, $p_3);
//unset($
}
//check and clean level 4
if ($d_3)
{
foreach ($d_3 as $k_3=>$v_3)
{
$p_4 = $p_3.'/'.$v_3;
$a_4 = get_sorted($p_4);
$d_4 = $a_4[0];
$f_4 = $a_4[1];
clean_files($f_4, $p_4);
}
//check and clean level 5
if ($d_4&&$level==5)
{
foreach ($d_4 as $k_4=>$v_4)
{
$p_5 = $p_4.'/'.$v_4;
$a_5 = get_sorted($p_5);
$d_5 = $a_5[0];
$f_5 = $a_5[1];
clean_files($f_5, $p_5);
}
}
}
}
}
}
}
?>
如果您遇到類似問題並嘗試運行此腳本,請小心。 它將刪除所有 eval(base64_encode 腳本惡意或不在 index.php 文件中。您還可以編輯 preg_match 表達式以針對其他自然注入。您還可以使用此代碼來針對 index.php 以外的文件
Grep 可能是你的朋友。 開始猜測你的猜測,看看是否有任何不尋常的事情出現。
我還會仔細檢查所有我能找到的日志,看看我是否能確定攻擊的來源。 您至少可以在短期內阻止 IP 攻擊。 查看文件上的日期可能會告訴您上次訪問以找到該位置的時間。
另外,不排除 javascript 注入。 他們可以用它做一些非常卑鄙的事情,它看起來就像頁面底部的一堆隨機亂碼。
顯然,更改密碼以包含任何數據庫。 更新所有開源軟件,因為這通常是這些人進入的方式。您甚至可以嘗試將代碼所有權更改為不同的用戶。 下次他們嘗試幫助跟蹤有問題的代碼時,任何會彈出錯誤的信息。
而且,我會考慮尋找一個新的主機......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.