如何确定哪些脚本正在使用fopen?

时间:2011-06-14 19:25:00

标签: php security fopen

我的网站遭到脚本小伙的攻击非常成功。在自动化的基础上,我的服务器上访问一个隐藏的脚本,导致修改我的所有index.php文件,并在它们的顶部添加一个iframe(base 64编码)。

我无法让我的托管服务提供商提供帮助,因为他们说他们在这件事上相当无助。

我怀疑如果我可以确定哪些脚本正在使用php的fopen函数,那么我将能够发现主篡改脚本的位置并将其删除。

有什么建议吗?

4 个答案:

答案 0 :(得分:3)

后续步骤

  1. 关闭所有内容
  2. 重新设置您的服务器
  3. 一点一点地分析你的代码,找到漏洞。
  4. 让黑客服务器保持运行是不负责任的。除了更改index.php文件之外,可能还安装了恶意工具 - 在此期间,您的服务器可能是犯罪分子的开放代理。

    的fopen

    除了fopen之外,还有十几种方法可以在PHP中更改文件。实例

    • file_put_contents
    • move_uploaded_file
    • exec('echo foo > /path/to/bar')
    • 与system,passthru,shell_exec,proc_open
    • 相同

    如果真的很开心,那你很幸运。

答案 1 :(得分:2)

使用find查找所有PHP脚本,然后使用grep for fopen。如果您没有shell访问权限,请使用脚本下载整个目录并在您的计算机上执行。

find /base/dir/with/your/scripts -name '*.php' | xargs grep 'fopen'

答案 2 :(得分:2)

我仍然在使用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以外的文件

答案 3 :(得分:1)

Grep可能就是你的朋友。在猜测时开始掠过,看看是否有任何异常现象出现。

我还会仔细检查所有可以查看攻击来源的日志。您可能能够至少在短期内阻止IP攻击。查看文件上的日期可能会告诉您上次访问的内容以查找位置。

另外,不排除javascript注入。他们可以用它做一些非常卑鄙的事情,它看起来就像在页面底部的一堆随机乱码。

显然,更改密码,包括任何数据库。更新所有开源软件,因为这些人经常会这样做。您甚至可以尝试将代码所有权更改为其他用户。下次他们尝试帮助跟踪违规代码时,弹出错误的任何内容。

而且,我想考虑找一个新的主人......

相关问题