Skip to content
RCE Write-up Intermediate

Server-side Template Injection with a Custom Exploit in PHP Twig

The application unsafely embedded user input into a server-side template, allowing an attacker to invoke internal PHP methods. The attacker used this to read files and delete an SSH private key.

medium.com

Published on medium.com

This lab highlights a classic case of Server-Side Template Injection (SSTI), a vulnerability that arises when user-supplied input is unsafely embedded into a server-side template. Instead of being treated as harmless data, the input is interpreted as executable code by the underlying template engine.

Bash Overflow, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the application failed to sanitise user input before passing it into the template engine, allowing arbitrary method calls.

Impact

An attacker could read or delete sensitive files and potentially execute arbitrary system commands, leading to full server compromise.

What the attacker needed

Valid user account; ability to upload files; ability to update profile

How it was found

  1. Log in to the lab with the provided test credentials.
  2. Upload a file with a PHP backdoor and set the Content-Type header to image/png to bypass MIME filtering.
  3. Trigger an error to learn the setAvatar() method signature from the logs.
  4. Replace the user.first_name parameter with a call to user.setAvatar() to read files.
  5. Attempt to read the SSH key but receive a defensive message.
  6. Use the gdprDelete() function via the injection point to delete the SSH key.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

Server-side template injection happens when a website takes user input and puts it directly into a template that the server processes. Instead of just displaying the input as text, the server may execute it as code. Here, the attacker could call internal functions to read and delete files on the server.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.