Server Side Template Injection
Template injection allows an attacker to include template code into an existing (or not) template. A template engine makes designing HTML pages easier by using static template files which at runtime replaces variables/placeholders with actual values in the HTML pages
Summary
Tools
-
Hackmanit/TInjA - An effiecient SSTI + CSTI scanner which utilizes novel polyglots
-
epinna/tplmap - Server-Side Template Injection and Code Injection Detection and Exploitation Tool
-
vladko312/SSTImap - Automatic SSTI detection tool with interactive interface based on epinna/tplmap
Methodology
Identify the Vulnerable Input Field
The attacker first locates an input field, URL parameter, or any user-controllable part of the application that is passed into a server-side template without proper sanitization or escaping.
For example, the attacker might identify a web form, search bar, or template preview functionality that seems to return results based on dynamic user input.
TIP: Generated PDF files, invoices and emails usually use a template.
Inject Template Syntax
The attacker tests the identified input field by injecting template syntax specific to the template engine in use. Different web frameworks use different template engines (e.g., Jinja2 for Python, Twig for PHP, or FreeMarker for Java).
Common template expressions:
{{7*7}}
for Jinja2 (Python).#{7*7}
for Thymeleaf (Java).
Find more template expressions in the page dedicated to the technology (PHP, Python, etc).
In most cases, this polyglot payload will trigger an error in presence of a SSTI vulnerability:
The Hackmanit/Template Injection Table is an interactive table containing the most efficient template injection polyglots along with the expected responses of the 44 most important template engines.
Enumerate the Template Engine
Based on the successful response, the attacker determines which template engine is being used. This step is critical because different template engines have different syntax, features, and potential for exploitation. The attacker may try different payloads to see which one executes, thereby identifying the engine.
- Python: Django, Jinja2, Mako, ...
- Java: Freemarker, Jinjava, Velocity, ...
- Ruby: ERB, Slim, ...
The post "template-engines-injection-101" from @0xAwali summarize the syntax and detection method for most of the template engines for JavaScript, Python, Ruby, Java and PHP and how to differentiate between engines that use the same syntax.
Escalate to Code Execution
Once the template engine is identified, the attacker injects more complex expressions, aiming to execute server-side commands or arbitrary code.
Labs
- Root Me - Java - Server-side Template Injection
- Root Me - Python - Server-side Template Injection Introduction
- Root Me - Python - Blind SSTI Filters Bypass
References
- A Pentester's Guide to Server Side Template Injection (SSTI) - Busra Demir - December 24, 2020
- Gaining Shell using Server Side Template Injection (SSTI) - David Valles - August 22, 2018
- Template Engines Injection 101 - Mahmoud M. Awali - November 1, 2024
- Template Injection On Hardened Targets - Lucas 'BitK' Philippe - September 28, 2022