> For the complete documentation index, see [llms.txt](https://ganesha-hk.gitbook.io/cybersecurity-writeups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ganesha-hk.gitbook.io/cybersecurity-writeups/port-swigger-ssti-labs/lab-2.md).

# lab-2

## Basic Server-Side Template Injection (Code Context — Tornado)

**Difficulty:** Practitioner

### Lab Description

This lab is vulnerable to server-side template injection due to the unsafe use of a Tornado template.

**Objective:** Review the Tornado documentation to discover how to execute arbitrary code, then delete the `morale.txt` file from Carlos's home directory.

**Credentials:** `wiener:peter`

### Solution

**Step 1 — Look for a suspect parameter**

Since the lab description tells us the backend uses the **Tornado** template engine, we can test its syntax directly once a candidate parameter is found.

**Step 2 — Locate the injection point**

Go to **My account**, select an option under **Preferred name**, submit, and capture the request in Burp. A hidden parameter, `blog-post-author-display`, is revealed — this is the value used to display a name (first name, nickname, etc.) when posting a comment on a product. At this stage, SSTI isn't confirmed yet.

**Step 3 — Confirm template injection**

Set the parameter to:

```jinja
{{ 7*7 }}
```

URL-encode and send it. The current request/response doesn't reflect the result directly — but posting a *new* comment on a product afterward shows the output `49` rendered on the page, confirming SSTI.

**Step 4 — Enumerate available built-ins**

To find which dangerous functions are reachable, submit:

```jinja
{{ [k for k in __builtins__.keys() if any(x in k for x in ['exec', 'eval', 'system'])] }}
```

(Remember: a new comment must be posted each time to see the freshly rendered output.)

This returns:

```
['execfile', 'eval']
```

The presence of `execfile` confirms the backend is running **Python 2**, since this builtin was removed in Python 3.

**Step 5 — Exploit to delete the file**

Submit the following payload (URL-encoded) as a new comment to execute an OS command via `eval`:

```jinja
{{ __builtins__['eval']("__import__('os').popen('rm /home/carlos/morale.txt').read()") }}
```

### Conclusion

The Tornado template engine evaluated user-controlled input as code. By enumerating accessible Python builtins, an `eval`-based command execution primitive was found and used to delete the target file.


---

# 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://ganesha-hk.gitbook.io/cybersecurity-writeups/port-swigger-ssti-labs/lab-2.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.
