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

# Basic server-side template injection

## Lab Description

<figure><img src="https://2387347627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F47EuhANOY5sIuDySBX97%2Fuploads%2FAxrofIjTk799OhF9or3a%2Fimage.png?alt=media&amp;token=db51806c-fb85-4803-851c-9fb595983d99" alt=""><figcaption></figcaption></figure>

## Walkthrough

Homepage

<figure><img src="https://2387347627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F47EuhANOY5sIuDySBX97%2Fuploads%2F6mRYS196u63fdZatrENE%2Fimage.png?alt=media&amp;token=a6b661cf-5fad-4be8-903e-035412f22206" alt=""><figcaption></figcaption></figure>

### **Step 1: Analyzing the Application**

Before testing for vulnerabilities, it’s essential to understand the application’s functionality. The homepage displays a shopping interface with products. When clicking on a product, a message stating, "Unfortunately, this product is out of stock," is displayed.

<figure><img src="https://2387347627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F47EuhANOY5sIuDySBX97%2Fuploads%2FDILhi5VREpmYDHAWPlLa%2Fimage.png?alt=media&amp;token=376ae976-328f-480a-9b9a-8d89e1836caa" alt=""><figcaption></figcaption></figure>

Using Burp send the request to the repeater and examine the parameters passed when interacting with a product.

* The `message` parameter is present in the request. This parameter might be used to dynamically render content on the webpage.

<figure><img src="https://2387347627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F47EuhANOY5sIuDySBX97%2Fuploads%2Fr9RAXomn9d3y0jdrEFU1%2Fimage.png?alt=media&amp;token=41a86cbb-f967-4f90-af0b-63202b5825e9" alt=""><figcaption></figcaption></figure>

### **Step 2: Testing for Template Injection**

Server-side template injection occurs when user inputs are passed directly into the server's template engine without proper sanitization. By crafting specific payloads, we can test if the `message` parameter is vulnerable.

1. Inject a basic payload: `{{7*7}}`.
   * **Expected Output:** If vulnerable, the result should display `49` (calculated value).
   * **Observation:** The output displays `{{7*7}}`, meaning this specific syntax did not trigger the vulnerability.
2. Exploring other template engines.
   * Attempted payload: `<%= 7*7 %>` (Ruby's Embedded Ruby, ERB syntax).
   * **Result:** Successfully evaluated to `49`. This confirms the application uses the ERB engine, making it vulnerable to Ruby-based template injection.

<figure><img src="https://2387347627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F47EuhANOY5sIuDySBX97%2Fuploads%2FugtkZi3bJbCjzt0vIXG2%2Fimage.png?alt=media&amp;token=72c2b1d8-4f5b-4a33-bc7e-931e5f10ffd9" alt=""><figcaption></figcaption></figure>

### **Step 3: Establishing Remote Code Execution (RCE)**

**Thought Process:**

After confirming template injection, the next step is to assess whether arbitrary code can be executed.

```
<%= system('id') %>
```

The response contains system information (`uid=12002(carlos) gid=12002(carlos)`), confirming RCE.

<figure><img src="https://2387347627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F47EuhANOY5sIuDySBX97%2Fuploads%2FtxCdP0yx0ylVVpE3phUM%2Fimage.png?alt=media&amp;token=c4037033-b04a-4e35-b3d9-97b11832731d" alt=""><figcaption></figcaption></figure>

### **Step 4: Locating the Target File**

**Thought Process:**

The goal is to delete the `morale.txt` file from Carlos's home directory. First, we need to confirm its presence in the current directory.

* The output lists `morale.txt` as a file in the current directory.

<figure><img src="https://2387347627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F47EuhANOY5sIuDySBX97%2Fuploads%2FbPxyiJaQ98vHn6IwCwdO%2Fimage.png?alt=media&amp;token=1c5c9241-7a9c-4d01-8217-0841114cea41" alt=""><figcaption></figcaption></figure>

#### **Step 5: Deleting the File**

Now that the target file is identified, the next step is to delete it using the `rm` command.

```
<%= system('rm morale.txt') %>
```

After executing the payload, the lab confirms successful completion.

<figure><img src="https://2387347627-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F47EuhANOY5sIuDySBX97%2Fuploads%2FLbznZtg7fVuu4y3HSiTV%2Fimage.png?alt=media&amp;token=c753564d-36ce-46d0-9def-5e48015adea7" alt=""><figcaption></figcaption></figure>
