Skip to content
RCE Write-up Intermediate

Server-Side Template Injection in a Sandboxed Environment: Escaping Freemarker Template…

A Freemarker template sandbox was improperly configured, allowing an attacker to escape restrictions and execute system commands. The attacker could read a sensitive file containing a password.

infosecwriteups.com

Published on infosecwriteups.com

This lab demonstrates a server-side template injection (SSTI) issue within the Freemarker template engine, which has been configured with a restrictive sandbox. At first glance, the sandbox is intended to prevent malicious template expressions from accessing sensitive functions or the underlying system.

Bash Overflow, on infosecwriteups.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 sandbox did not fully isolate untrusted template input, allowing access to dangerous objects and methods.

Impact

An attacker could read sensitive files such as configuration files, secret keys, API keys, authentication tokens, or database credentials, and bypass intended security controls.

What the attacker needed

Access to the lab environment; ability to edit templates

How it was found

  1. Log in to the lab and navigate to an article, then select 'Edit template' to inject Freemarker code.
  2. Inject a snippet to print the Freemarker version, revealing version 2.3.29.
  3. Find version-specific payloads for Freemarker 2.3.29.
  4. Test object references; an injection into the 'article' object fails, but 'product' works.
  5. Modify the payload to use 'product.class.protectionDomain.classLoader' instead of 'article.class.protectionDomain.classLoader' to escape the sandbox.
  6. Execute system commands to list files and read 'my_password.txt'.

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.

A template engine lets websites create dynamic pages by mixing fixed text with placeholders. A sandbox is a security feature that tries to stop those placeholders from running dangerous code. Here, the sandbox was not strict enough, so an attacker could break out and run commands on the server, like reading a private file.

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.