php - 如何在 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");
解决方案
您可以执行类似于以下代码的操作,加载每个文件,然后检查它是否包含任何这些字符串。
请记住,这不会很快,我会考虑使用 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.";
}
}
推荐阅读
- javascript - for 循环中的函数。只有最后一个元素受到影响
- c# - ApplicationInsightDataClient GetCustomEventWithHttpMessageAsync 未加载自定义维度
- android - 当我尝试同时使用 grpc 和 cordova 时,程序类型已经存在 com.squareup.okhttp.address
- java - 从 Kotlin 将 null 传递给不可为 null 的 Java 方法
- python - 无论顺序如何,都在 python 中返回一对唯一的列
- amazon-web-services - CloudFront 缓存包含授权数据的页面并将其提供给未经授权的用户是否存在任何危险?
- installation - 如何对包中的签名二进制文件进行身份验证?
- sql - 如何减去“Sysdate”的日期范围
- python - 需要制作一个数据框来记录每个文件夹中文件类型的数量
- excel - Excel VBA - [Microsoft][ODBC 文本驱动程序] 记录太大