首页 > 解决方案 > 如何在 PHP 代码中存在一些字符串数据

问题描述

我有下面的代码,它列出了目录和所有子目录中的文件,并且工作正常。

现在我想要实现的是只显示包含以下字符串函数 eval、system、shell_exec 的 PHP 文件。

我想我必须创建一个像

$check=array("unescape", "system(","shell_exec(");

下面是列出所有 PHP 文件的代码

function c_check($path){

    if(file_exists($path) && is_dir($path)){
        $files = glob($path ."/*");
        if(count($files) > 0){
            // Loop through retuned array
            foreach($files as $file){
                if(is_file("$file")){

                    // Display only filename
                    echo basename($file) . "<br>";
                } else if(is_dir("$file")){
                    c_check("$file");
                }
            }
        } else{
            echo " directory file not found";
        }
    } else {
        echo " directory does not exist.";
    }
}

// Call the function
c_check("../content_folder");

标签: php

解决方案


您可以执行类似于以下代码的操作,加载每个文件,然后检查它是否包含任何这些字符串。

请记住,这不会很快,我会考虑使用 grep 之类的东西并解析它的输出。

function c_check($path)
{
    $checks = ["unescape", "system(", "shell_exec("];

    if (file_exists($path) && is_dir($path)) {
        $files = glob($path . "/*");
        if (count($files) > 0) {
            // Loop through returned array
            foreach ($files as $file) {
                if (is_file("$file")) {
                    $fileContents = file_get_contents($file);

                    foreach ($checks as $illegalString) {
                        if (strpos($fileContents, $illegalString) !== false) {
                            echo basename($file) . "<br>";
                        }
                    }
                } else {
                    if (is_dir("$file")) {
                        c_check("$file");
                    }
                }
            }
        } else {
            echo " directory file not found";
        }
    } else {
        echo " directory does not exist.";
    }
}

推荐阅读