> For the complete documentation index, see [llms.txt](https://kruknight.gitbook.io/daemon-of-hacking/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kruknight.gitbook.io/daemon-of-hacking/writeups/portswigger-labs/server-side-template-injection/basic-server-side-template-injection-code-context.md).

# Basic server-side template injection (code context)

## Lab Description

<figure><img src="https://2387347627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F47EuhANOY5sIuDySBX97%2Fuploads%2FczZQw64PVweWOyQ1VrSa%2Fimage.png?alt=media&amp;token=0f5666b1-3f90-4b08-ac86-7b34f62bddb8" alt=""><figcaption></figcaption></figure>

## Walkthrough

### Step 1: Analyze the Functionality

**Observation:**

* After logging in with the provided credentials (`wiener:peter`), you navigate to the **My Account** page.
* There’s a dropdown labeled **"Preferred Name"**, which suggests that the user can select a display format for their name (e.g., full name, first name, nickname).
* Changing this setting sends a POST request with the parameter `blog-post-author-display` to configure how the user’s name will be displayed in comments.

<figure><img src="https://2387347627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F47EuhANOY5sIuDySBX97%2Fuploads%2FhwJpm4tnYbvvaSGeGaes%2Fimage.png?alt=media&amp;token=6e87fa71-42e0-4230-96f4-418675730da2" alt=""><figcaption></figcaption></figure>

* Since the hint mentions server-side template injection, the first step is to identify whether this functionality is vulnerable.
* Focus on the parameter `blog-post-author-display`, as it might process user input in an unsafe manner.
* A template engine is likely being used to generate the output dynamically.

<figure><img src="https://2387347627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F47EuhANOY5sIuDySBX97%2Fuploads%2FrwtQiUuYBRwtD416HOVx%2Fimage.png?alt=media&amp;token=580e7021-f630-4571-89ee-58d7080fb33c" alt=""><figcaption></figcaption></figure>

### **Step 2: Test for SSTI Vulnerability**

**Action:**

* Modify the `blog-post-author-display` parameter to include a basic template injection payload, such as `{{7*7}}`.
* Expected Behavior: If vulnerable, the application should evaluate this payload and return `49` instead of the string `{{7*7}}`.

**Result:**

* Post a comment on a blog post to see how the name is rendered.
* The username shows as `Peter0 {{49}}`, confirming the SSTI vulnerability.

**Thought Process:**

1. The successful result of `{{7*7}}` indicates the presence of a template engine.
2. The next step is to identify which template engine is in use to craft an appropriate payload for remote code execution (RCE).

<figure><img src="https://2387347627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F47EuhANOY5sIuDySBX97%2Fuploads%2F1QRt0uey0C7CJz25R3Ka%2Fimage.png?alt=media&amp;token=e8028afc-0243-437b-912b-df611a12bd04" alt=""><figcaption></figcaption></figure>

To see our username and check if the payload worked we need to post a comment on one of the posts, so we go ahead and do that and we can see that the payload worked

<figure><img src="https://2387347627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F47EuhANOY5sIuDySBX97%2Fuploads%2FBQOIsy9wS8715eCvWQQC%2Fimage.png?alt=media&amp;token=1e1b3205-157f-41ba-904a-dc3684006a3a" alt=""><figcaption></figcaption></figure>

### **Step 3: Identify the Template Engine**

**Action:**

* Use **trial-and-error** with payloads specific to common template engines (e.g., Jinja2, Tornado, Twig).
* Sites like **PayloadsAllTheThings** provide a list of test payloads for various engines.

After testing, the template engine is identified as **Python’s Jinja2**.

1. Knowing the template engine allows crafting payloads that execute Python code.
2. The next goal is to gain RCE by running arbitrary commands on the server.

#### **Step 4: Achieve Remote Code Execution**

**Action:**

* Use the Jinja2 payload to import the `os` module and run system command

This payload, when URL-encoded, executes the `ls` command to list files in the directory.

* The ability to execute commands confirms RCE.
* The lab objective is to delete the file `morale.txt`. Now, craft a payload to accomplish this.

```
user.first_name}}{%25+import+os+%25}{{os.system('ls')
```

<figure><img src="https://2387347627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F47EuhANOY5sIuDySBX97%2Fuploads%2F1trEU5UyWHRHFWGJwJlK%2Fimage.png?alt=media&amp;token=0e600377-561e-494e-864f-6c22b7fcb4af" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2387347627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F47EuhANOY5sIuDySBX97%2Fuploads%2FHSYQTO8JjnFZsVP4fUbC%2Fimage.png?alt=media&amp;token=79bb33f2-1538-48e2-943e-4d0bd0d28190" alt=""><figcaption></figcaption></figure>

#### **Step 5: Delete the File**

**Action:**

* Modify the payload to delete `morale.txt` using the `rm` command:

```
user.first_name}}{%25+import+os+%25}{{os.system('rm%20morale.txt')
```

* Deleting `morale.txt` satisfies the lab requirements.
* By leveraging SSTI and RCE, you have demonstrated the exploitability of improper input handling in template engines.

<figure><img src="https://2387347627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F47EuhANOY5sIuDySBX97%2Fuploads%2FvPU9Hlm5cfpJTiyfdy06%2Fimage.png?alt=media&amp;token=a5e826b8-b3d3-49fb-9d98-65b85b5478b1" alt=""><figcaption></figcaption></figure>
