Stratos Ally

Introduction to SSTI

**Note: The content in this article is only for educational purposes and understanding of cybersecurity concepts. It should enable people and organizations to have a better grip on threats and know how to protect themselves against them. Please use this information responsibly.** 

What is server-side template injection(SSTI)?

Server-Side Template Injection (SSTI) is a security flaw in web applications that occurs when user input is incorporated into templates to create dynamic content. If this input is inadequately validated or sanitized before being inserted into a template, an attacker might exploit the vulnerability by injecting malicious code. This code, written in the template engine’s syntax, can be executed on the server when the affected page is rendered.

Before moving forward we need to know what a template engine is ?

A template engine is a tool or library used in web development that helps developers create dynamic web applications. It enables the separation of presentation code (such as HTML/CSS) from business logic code (like database queries and algorithms). Different web application frameworks utilize various template engines based on the programming language and required functionality.

For example, Python-based web applications might use Jinja or Mako, while Ruby applications could use Embedded Ruby (ERB) or Slim. Other languages, including Go, PHP, and Java, also have their own template engines.

Using a template engine, developers can employ static template files within an application. These templates contain variables that are replaced with actual values when the page is generated, resulting in a dynamic web page for user interaction.

Creating dynamic content with templates 

For instance, consider building a blog. You can create a template to ensure a consistent look for each blog post without having to craft a unique HTML page for every post. The template might have a section like this, where each expression (the code within curly brackets) is used to retrieve and display the blog content:

This template only has to be made once, and each post displayed using this template will maintain a uniform look.

When a user opens a blog post, the application sends both the blog post content and the template to the template engine for processing. As the template engine constructs the web page using the provided template, it executes any expressions it encounters. These expressions retrieve specific elements of the blog post necessary for each section of the web page.

For example, in the template, the expression {{ post.title }} fetches the title of the post and inserts it into the title field as the page is rendered:

Regardless of the blog post being opened, the page formatting stays the same, with only the content changing dynamically each time.

How will the SSTI vulnerability occur?

The “Post #” field is derived from the post variable in the URL. The application takes this variable’s value and incorporates it into the template before it is processed by the template engine. When a user accesses a blog post through its number, this number is inserted into the template as expected.

However, if an expression is used instead of a number (e.g., {{10 + 10}}), it becomes part of the template:

The template engine doesn’t distinguish this injected expression from any other legitimate template code. It executes the code and includes the result in the page.

The same blog post, but with an expression injected into the post variable instead of a number, resulting in the display showing “Post #20.”

How impactful is SSTI ?

The impact of server-side template injection (SSTI) varies based on several factors, such as the template engine in use, the application’s architecture, and the server’s security measures. The most serious consequence of SSTI is the potential for remote code execution (RCE), effectively gaining full control over it. With such access, the attacker could use the compromised server as a stepping stone to launch further attacks on internal servers, turning the compromised server into a gateway for targeting additional systems.

Even if RCE is not achievable, an attacker can still leverage the SSTI vulnerability to access unauthorized data stored on the server. This can include user information, system data, or other sensitive information, posing a significant security risk.

more Related articles