Java代码审计–SSTI模板注入

admin 2026-06-26 06:46:35 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文详细解析Java中的SSTI模板注入漏洞,阐述SSTI与SpEL的本质区别及漏洞产生原因。通过FreeMarker实例演示危险写法与安全防护方案,强调避免用户输入直接作为模板内容解析、禁止动态拼接模板字符串等核心防护措施。文档包含完整的漏洞环境搭建代码和可操作的安全编码建议。 综合评分: 88 文章分类: 代码审计,漏洞分析,WEB安全,安全开发,安全工具


cover_image

Java 代码审计 – SSTI 模板注入

原创

GOWLSJ125 GOWLSJ125

走在网安路上的哥布林

2026年6月9日 11:17 福建

在小说阅读器读本章

去阅读

Java 代码审计·SSTI 模板注入

✨ 什么是 SSTI 模板注入

  SSTI 是服务器端模板注入(Server-SideTemplateInjection)的英文首字母编写。模板引擎支持使用静态模板文件,在运行时用 HTML 页面中的实际值替换变量/占位符,从而让 HTML 页面的设计变得更容易。当前广泛应用的模板引擎有 Smarty、Twig、Jinja2、FreeMarker、Velocity 等。若攻击者可以完全控制输入模板的指令,并且模板能够在服务器端被成功地进行解析,则会造成模板注入漏洞。   简单来说,SSTI 指的是攻击者可控的数据被服务端模板引擎当作“模板代码”解析执行,而不是普通文本输出

✨ SSTI 与 SpEL 的区别

  SSTI 是一类漏洞场景/漏洞类型;SpEL 是一种表达式语言/技术组件。SpEL 使用不当时,可以导致表达式注入,也可能出现在 SSTI 场景中。

📝 SSTI

  SSTI 是服务端模板注入,描述的是一种漏洞类型,用户输入被服务端模板引擎当作模板代码解析执行。常见模板引擎包括reeMarkerVelocityThymeleafPebbleMustacheJSP / ELGroovy TemplateSpring Expression Language(SpEL)OGNL / MVEL等表达式引擎。

  正常情况下,模板引擎用于渲染页面,例如:Hello, ${username}  服务端传入:model.put("username", "法外狂徒-张三");  渲染结果:Hello, 法外狂徒-张三  若如果攻击者能控制模板内容,例如:Hello, ${2 * 3}  服务端模板引擎可能会把它当成表达式执行,最终输出:Hello, 6

📝 SpEL

  SpEL 是 Spring 表达式语言,是 Spring 提供的一种表达式语言。用于:配置解析、Bean 属性访问、注解条件判断、Spring Security 权限表达式、Spring Cache 表达式、Spring Data 查询表达式、动态规则判断。

ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("2 * 3");
Object result = expression.getValue();

📝 二者的核心区别

| 对比项 | SSTI | SpEL | | — | — | — | | 本质 | 漏洞类型/攻击场景 | 表达式语言/技术组件 | | 范围 | 更宽 | 更具体 | | 典型位置 | 模板引擎渲染阶段 | Spring 表达式解析阶段 | | 常见引擎 | FreeMarker、Velocity、Thymeleaf 等 | Spring Expression Language | | 是否一定是漏洞 | 是安全问题描述 | 本身不是漏洞 | | 产生漏洞条件 | 用户输入被当作模板解析 | 用户输入被当作 SpEL 表达式解析 | | 关系 | 可包含 SpEL 注入场景 | 可能导致类似 SSTI 的服务端表达式执行 |

✨ SSTI 漏洞产生的根本原因

📝 将用户输入当作模板内容解析

  • 危险的写法:
String template = request.getParameter("template");
templateEngine.process(template, context);

攻击者直接控制了模板内容。

  • 正确做法应该是:
templateEngine.process("email/welcome", context);

模板名称或模板文件应由后端固定,而不是由用户随意传入。

📝 动态拼接模板字符串

String username = request.getParameter("username");
String template = "Hello " + username;
templateEngine.process(template, context);

如果传入的 username 是:${7*7},模板最终变成:

Hello ${7*7}

模板引擎会执行表达式。

📝 用户可控模板文件、邮件模板、CMS 页面模板

一些系统允许用户编辑:

  • 邮件模板
  • 短信模板
  • 报表模板
  • CMS 页面模板
  • 通知内容模板
  • 工作流表达式
  • 规则引擎表达式

如果这些模板支持表达式,而系统没有沙箱限制,就可能导致 SSTI。

尊敬的 ${user.name},您的订单号是 ${order.id}

如果普通用户也能修改模板,就可以尝试构造恶意表达式。

📝 模板引擎暴露了危险对象

例如模板上下文中暴露了:

model.put("request", request);
model.put("response", response);
model.put("session", session);
model.put("applicationContext", applicationContext);
model.put("classLoader", classLoader);

攻击者一旦可以执行模板表达式,就可能通过这些对象继续访问更敏感的 API。

📝 使用了危险表达式引擎

  例如SpELOGNLMVELJEXL等。如果代码中存在:

ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression(userInput);
Object value = exp.getValue();

会触发 SpEL 表达式注入。

💡 Freemarker SSTI 演示

  FreeMarker 是一个 Java 模板引擎,常用于生成:HTML 页面、邮件内容、配置文件、文本报表、代码模板。它的模板文件通常以.ftl结尾。   FreeMarker 本身不依赖 Servlet,可以用于 Web,也可以用于普通 Java 程序。

📝 基本语法

💻 基本变量输出

模板:Hello, ${name}数据:name = "Alice"输出:Hello, Alice

💻 条件判断

<#if&nbsp;user.vip>
&nbsp; &nbsp; VIP 用户
<#else>
&nbsp; &nbsp; 普通用户
</#if>

💻 循环

<#list&nbsp;users as user>
&nbsp; &nbsp; 用户名:${user.name}
</#list>

💻 默认值

${username!"匿名用户"},如果 username 为不存在,输出:匿名用户

💻 判断变量是否存在

<#if&nbsp;username??>
&nbsp; &nbsp; ${username}
<#else>
&nbsp; &nbsp; 用户名不存在
</#if>

💻 常见内建函数

${name?upper_case}
${name?lower_case}
${date?string("yyyy-MM-dd")}
${users?size}

比如:${"hello"?upper_case},输出:HELLO

📝 漏洞环境搭建

  • pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
&nbsp; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
&nbsp; xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
&nbsp; https://maven.apache.org/xsd/maven-4.0.0.xsd">
&nbsp; <modelVersion>4.0.0</modelVersion>

&nbsp; <groupId>com.study</groupId>
&nbsp; <artifactId>freemarker-safe-lab</artifactId>
&nbsp; <version>1.0-SNAPSHOT</version>
&nbsp; <name>freemarker-safe-lab</name>

&nbsp; <parent>
&nbsp; &nbsp; <groupId>org.springframework.boot</groupId>
&nbsp; &nbsp; <artifactId>spring-boot-starter-parent</artifactId>
&nbsp; &nbsp; <version>2.7.18</version>
&nbsp; &nbsp; <relativePath/>
&nbsp; </parent>

&nbsp; <properties>
&nbsp; &nbsp; <java.version>8</java.version>
&nbsp; </properties>

&nbsp; <dependencies>
&nbsp; &nbsp; <dependency>
&nbsp; &nbsp; &nbsp; <!-- Spring Boot Web 支持 -->
&nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>
&nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-web</artifactId>
&nbsp; &nbsp; </dependency>

&nbsp; &nbsp; <dependency>
&nbsp; &nbsp; &nbsp; <!-- Freemarker 模板引擎 -->
&nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>
&nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-freemarker</artifactId>
&nbsp; &nbsp; </dependency>
&nbsp; </dependencies>

&nbsp; <build>
&nbsp; &nbsp; <plugins>
&nbsp; &nbsp; &nbsp; <!-- Spring Boot 打包运行插件 -->
&nbsp; &nbsp; &nbsp; <plugin>
&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>
&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-maven-plugin</artifactId>
&nbsp; &nbsp; &nbsp; </plugin>
&nbsp; &nbsp; </plugins>
&nbsp; </build>
</project>
  • application.properties
# 服务器端口号
server.port=8080

# FreeMarker 模板文件的加载路径(classpath 下的 templates 目录)
spring.freemarker.template-loader-path=classpath:/templates/
# 模板文件的后缀名
spring.freemarker.suffix=.ftl
# 模板文件的字符编码
spring.freemarker.charset=UTF-8
# 是否启用模板缓存,开发环境建议关闭以便实时修改生效
spring.freemarker.cache=false
# 是否将 HttpServletRequest 中的属性暴露给模板
spring.freemarker.expose-request-attributes=false
# 是否将 HttpSession 中的属性暴露给模板
spring.freemarker.expose-session-attributes=false
# 是否暴露 Spring 宏助手(SpringMacroHelper),用于在模板中使用 Spring 的宏
spring.freemarker.expose-spring-macro-helpers=true

📝 安全渲染示例

  • SafeController.java
package com.study.demo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
&nbsp;* SafeController - 安全的 FreeMarker SSTI 示例控制器
&nbsp;* 演示如何安全地使用 FreeMarker 模板引擎,避免服务端模板注入(SSTI)攻击。
&nbsp;*/
@Controller
public class SafeController {
&nbsp; &nbsp; /**
&nbsp; &nbsp; &nbsp;* 演示页面 - /hello
&nbsp; &nbsp; &nbsp;* 将固定的 "FreeMarker" 字符串作为 name 属性传递给模板,不接收用户输入,因此是安全的。
&nbsp; &nbsp; &nbsp;*
&nbsp; &nbsp; &nbsp;* @param model Spring MVC 的 Model 对象,用于向模板传递数据
&nbsp; &nbsp; &nbsp;* @return 模板名称 "hello",对应 templates/hello.ftl
&nbsp; &nbsp; &nbsp;*/
&nbsp; &nbsp; @GetMapping("/hello")
&nbsp; &nbsp; public String hello(Model model) {
&nbsp; &nbsp; &nbsp; &nbsp; // 将固定的 name 值添加到模型中,模板通过 ${name} 获取
&nbsp; &nbsp; &nbsp; &nbsp; model.addAttribute("name", "FreeMarker");
&nbsp; &nbsp; &nbsp; &nbsp; return "hello";
&nbsp; &nbsp; }

&nbsp; &nbsp; /**
&nbsp; &nbsp; &nbsp;* 安全用户输入处理 - /safe
&nbsp; &nbsp; &nbsp;* 接收用户通过 URL 参数传入的 name,并使用安全的方式将其传递给 FreeMarker 模板
&nbsp; &nbsp; &nbsp;* 关键安全措施:直接将用户输入作为普通字符串变量传递,而非拼接到模板中,从而防止 SSTI
&nbsp; &nbsp; &nbsp;*/
&nbsp; &nbsp; @GetMapping("/safe")
&nbsp; &nbsp; public String safe(@RequestParam(defaultValue = "guest") String name, Model model) {
&nbsp; &nbsp; &nbsp; &nbsp; model.addAttribute("name", name);
&nbsp; &nbsp; &nbsp; &nbsp; return "safe";
&nbsp; &nbsp; }
}
  • hello.ftl
<!DOCTYPE html>
<html>
<head>
&nbsp; &nbsp; <meta charset="UTF-8">
&nbsp; &nbsp; <title>Hello</title>
</head>
<body>
<h1>Hello Page</h1>
<p>Hello, ${name}</p>
</body>
</html>
  • safe.ftl
<!DOCTYPE html>
<html>
<head>
&nbsp; &nbsp; <meta charset="UTF-8">
&nbsp; &nbsp; <title>Safe</title>
</head>
<body>
<h1>Safe Variable Rendering</h1>
<p>Your input:</p>
<p>${name}</p>
</body>
</html>

输入的内容会作为数据出现,不会被二次解析。

📝 风险渲染示例

  • ReviewController.java
package com.study.demo.controller;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

/**
&nbsp;* ReviewController - 评论模板渲染控制器(存在 SSTI 漏洞的示例)
&nbsp;* 用户输入的 content 被直接作为 FreeMarker 模板内容进行解析和渲染,
&nbsp;* 攻击者可以通过构造恶意模板表达式来执行任意代码。
&nbsp;*/
@Controller
public class ReviewController {
&nbsp; &nbsp; private final Configuration configuration;
&nbsp; &nbsp; public ReviewController(Configuration configuration) {
&nbsp; &nbsp; &nbsp; &nbsp; this.configuration = configuration;
&nbsp; &nbsp; }

&nbsp; &nbsp; @GetMapping("/review")
&nbsp; &nbsp; public String reviewPage() {
&nbsp; &nbsp; &nbsp; &nbsp; return "review";
&nbsp; &nbsp; }

&nbsp; &nbsp; @PostMapping("/review/render")
&nbsp; &nbsp; public String renderReview(@RequestParam String content, Model model) {
&nbsp; &nbsp; &nbsp; &nbsp; model.addAttribute("submittedContent", content);

&nbsp; &nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 漏洞点:将用户输入的 content 直接作为 FreeMarker 模板源码创建 Template 对象
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 这是 SSTI 漏洞的根源——用户输入被当作可执行的模板代码解析
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Template template = new Template("userTpl", new StringReader(content), configuration);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringWriter out = new StringWriter();

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String, Object> dataModel = new HashMap<String, Object>();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataModel.put("name", "张三");

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 执行模板渲染,将结果写入 StringWriter
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; template.process(dataModel, out);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.addAttribute("renderedResult", out.toString());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.addAttribute("status", "success");
&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.addAttribute("renderedResult", "渲染失败");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.addAttribute("status", "error");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.addAttribute("errorType", e.getClass().getName());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.addAttribute("errorMessage", e.getMessage());
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; return "review-result";
&nbsp; &nbsp; }
}
  • review.ftl
<!DOCTYPE html>
<html>
<head>
&nbsp; &nbsp; <meta charset="UTF-8">
&nbsp; &nbsp; <title>Review Lab</title>
</head>
<body>
<h1>FreeMarker 审计学习</h1>

<p><b>把用户输入直接当模板源码交给 FreeMarker 处理</b></p>

<form method="post" action="/review/render">
&nbsp; &nbsp; <textarea name="content" rows="8" cols="80">${r"Hello ${name}"}</textarea><br>
&nbsp; &nbsp; <button type="submit">提交</button>
</form>

</body>
</html>
  • review-result.ftl
<!DOCTYPE html>
<html>
<head>
&nbsp; &nbsp; <meta charset="UTF-8">
&nbsp; &nbsp; <title>Review Result</title>
</head>
<body>
<h1>Review Result</h1>

<p>状态:${status}</p>

<h2>提交的原始内容</h2>
<pre>${submittedContent!""}</pre>

<h2>渲染结果</h2>
<pre>${renderedResult!""}</pre>

<#if&nbsp;status == "error">
&nbsp; &nbsp; <h2>错误信息</h2>
&nbsp; &nbsp; <p>类型:${errorType!""}</p>
&nbsp; &nbsp; <p>消息:${errorMessage!""}</p>
</#if>

</body>
</html>

✨ 推荐的代码搜索关键词

new Template(
Template(
template.process(
StringReader(
Configuration
process(
render(
evaluate(
parseExpression(
getTemplate(
return page
return view
@RequestParam
@RequestBody
request.getParameter(
<#include
<#import
#evaluate(
th:utext
model.addAttribute(
model.put(

免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:走在网安路上的哥布林 GOWLSJ125 GOWLSJ125《Java 代码审计 – SSTI 模板注入》

评论:0   参与:  0