Expert · updated

Server-side template injection

PortSwigger Web Security Academy · Expert

  • #SSTI

Server-Side Template Injection in a Sandboxed Environment

The vulnerability

You can edit Freemarker templates. They run in a sandbox, but the product object gives you access to Java classes, and with those you can read files.

Fix: Don’t give users access to objects they don’t need in a template.

The lab

Open the lab on PortSwigger

This lab uses the Freemarker template engine. It is vulnerable to server-side template injection due to its poorly implemented sandbox. To solve the lab, break out of the sandbox to read the file my_password.txtfrom Carlos's home directory. Then submit the contents of the file.

You can log in to your own account using the following credentials: content-manager:C0nt3ntM4n4g3r

Solution

1. Log In and Find the Template Editor: Log in to the application using the credentials content-manager:C0nt3ntM4n4g3r. Go to a product detail page and click the "Edit template" button.

2. Probe the Sandbox for Accessible Objects: Add a simple payload like ${object.getClass()} to the template and click "Preview". The response will confirm that you have access to some Freemarker objects.

3. Create the Sandbox Escape Payload: The goal is to escape the Freemarker sandbox and access the Java file system. Chain together a series of built-in functions to get a reference to the java.lang.Class object and then use it to read a local file. The payload will read the target file's content as a byte array.

${product.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().resolve('/home/carlos/my_password.txt').toURL().openStream().readAllBytes()?join(" ")}

4. Read the File and Decode Content: Paste the payload into the template editor and click “Preview”. The response will contain a series of numbers, which are the byte values of the my_password.txt file. Use an online tool to convert these bytes to ASCII characters. Submit the resulting text to solve the lab.

Server-Side Template Injection with a Custom Exploit

The vulnerability

Your display name is used in a Twig template, and there you can call functions of the user object. One sets your avatar to any file on the server, another deletes your avatar. Together they delete Carlos’s SSH key.

Fix: Don’t put user input into a template, and don’t show detailed error messages.

The lab

Open the lab on PortSwigger

This lab is vulnerable to server-side template injection. To solve the lab, create a custom exploit to delete the file /.ssh/id_rsa from Carlos's home directory.

You can log in to your own account using the following credentials: wiener:peter

Warning

As with many high-severity vulnerabilities, experimenting with server-side template injection can be dangerous. If you’re not careful when invoking methods, it is possible to damage your instance of the lab, which could make it unsolvable. If this happens, you will need to wait 20 minutes until your lab session resets.

Solution

1. Log In and Identify Features: Log in to the application using the provided credentials: wiener:peter. On the "My Account" page, note the three available functions: update mail, change the preferred name, and upload an avatar file.

2. Intercept Requests and Test for SSTI: The “update mail” function is not interesting. Go to Burp Suite’s Proxy → HTTP history and send both the POST /my-account/change-blog-post-author-display and POST /my-account/avatar requests to Repeater for further testing.

3. Find the Twig Template Engine: Go to a blog post and add a comment. Your username and avatar will be displayed. Now, in Repeater, change the value of the blog-post-author-display parameter to something unexpected (e.g., "user") and send the request. Reload the blog page. A broken page with a verbose error message will reveal that the Twig template engine is being used.

4. Discover the setAvatar Function: Try to upload a basic PHP web shell (<?php system($_GET['cmd']) ?>) via the avatar upload function. The upload will fail, but a verbose error message will be returned, which discloses internal function names, including the User.php file and the setAvatar() function.

PHP Fatal error:  Uncaught Exception: Uploaded file mime type is not an image: text/php in /home/carlos/User.php:28 
Stack trace: #0 /home/carlos/avatar_upload.php(19): User->setAvatar('/tmp/shell.php', 'text/php') 
#1 {main}   thrown in /home/carlos/User.php on line 28

5. Chain SSTI to Manipulate Avatar Path: Go back to Repeater and set the value of blog-post-author-display to an SSTI payload that calls the setAvatar() function with a path traversal payload. This will make the avatar path point to /etc/passwd. The payload is: user.setAvatar('/etc/passwd', 'image/png'). Send the request.

6. Read /etc/passwd and /home/carlos/User.php: Reload the blog page. The avatar will be a broken image. Right-click the broken image and open it in a new tab. In Burp Suite's Proxy → HTTP history, you will see a GET /avatar?avatar=wiener request. The response will contain the content of /etc/passwd, confirming the path manipulation works. Now, use the same technique to read the contents of /home/carlos/User.php (as revealed in the earlier error message) to find more information.

7. Discover and Call the gdprDelete() Function: The User.php source code reveals a function called gdprDelete() that deletes the user's avatar file.

public function gdprDelete() {     
    $this->rm(readlink($this->avatarLink));     
    $this->rm($this->avatarLink);     
    $this->delete(); 
}

8. Chain to Delete the Target File: Use the setAvatar() function one last time to change the avatar's path to the target file: /home/carlos/.ssh/id_rsa. Send the request and refresh the blog page to update the avatar link. Now, change the value of blog-post-author-display to user.gdprDelete() and send the request. Refresh the blog page. The gdprDelete() function will be called, deleting the id_rsa file and the lab is solved.