【问题标题】:Find all php files which first or second line have more than N characters查找第一行或第二行超过 N 个字符的所有 php 文件
【发布时间】:2014-03-06 19:08:26
【问题描述】:

我将如何继续编写一个程序来查找我的所有第一行或第二行包含 N 个以上字符的 php 文件?我想显示文件名和行内容

我可以用这个逐个文件:

    <html><head><title>Find String</title></head><body>
    <?php
    find_files('.');
    function find_files($seed) {
        if(! is_dir($seed)) return false;
        $files = array();
        $dirs = array($seed);
        while(NULL !== ($dir = array_pop($dirs)))
        {
            if($dh = opendir($dir))
            {
                while( false !== ($file = readdir($dh)))
                {
                    if($file == '.' || $file == '..') continue;
                    $path = $dir . '/' . $file;
                    if(is_dir($path)) { $dirs[] = $path; }
                    else { if(preg_match('/^.*\.(php[\d]?)$/i', $path)) { check_files($path); }}
                }
                closedir($dh);
            }
        }
    }

    function check_files($this_file){

        if(!($content = file_get_contents($this_file)))
        { echo("<p>Could not check $this_file You should check the contents manually!</p>\n"); }
        else
        {
            // What to do here??
        }
        unset($content);
    }
    ?>
</body></html>

【问题讨论】:

  • 这是现实世界的要求吗? :-)
  • @Dagon 你是什么意思?我已经在我的 php 文件中注入了代码,通常内容是在第一行或第二行中注入的......
  • @HommerSmith 你不知道还有什么被注入或改变,也不知道在哪里。唯一明智的做法是删除所有内容并从干净的备份中恢复。之后,修补您的软件以堵住攻击的漏洞。
  • 如果这不是解决问题的方法,请从备份中恢复。你不能确定你会使用这种方法获得所有注入的代码。
  • 我知道我需要从备份中恢复。我责怪服务器上说“没有备份”的人。谢谢你们的建议。

标签: php regex grep find


【解决方案1】:

我会与 cmets 一起从备份中恢复,并识别和消除漏洞。但要回答这个问题:

foreach(glob('path/*.php') as $file) {
    $lines = file($file);

    if(strlen($lines[0]) > $N) {
        echo "$file : line 1 : {$line[0]}\n";
    }
    elseif(strlen($lines[1]) > $N) {
        echo "$file : line 2 : {$line[1]}\n";
    }
}

昨天有一个类似的问题:Find php files with strings that have more than 50 characters 有同样的问题。

【讨论】:

  • 如何以更性感的方式执行此操作的 linux 脚本?我还不知道,但我很确定“查找”可以做类似的事情。
  • 是的,请查看 OP 其他问题的链接。
猜你喜欢
  • 1970-01-01
  • 2018-03-22
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多