Server-side template injection
What is Server-Side Template Injection?
Server-Side Template Injection (SSTI) occurs when an attacker is able to inject malicious payloads into a server-side template. These payloads are then executed on the server due to improper handling of user input.
How Does It Happen?
SSTI arises when user input is directly concatenated into a template rather than being passed as sanitized data.
Example of safe code (user input is treated as data)
Example of vulnerable code (user input is concatenated):
Attack URL:
Impact of SSTI
At the severe end, SSTI can lead to:
Remote Code Execution (RCE): Execute arbitrary commands on the server.
Sensitive Data Exposure: Gain access to environment variables or sensitive files.
Other Attacks: Use SSTI as a basis for XSS, SSRF, or further escalation.
Constructing an SSTI Attack
The process can be divided into four phases:
Detect
Identify
Exploit
Escalate (Read, Explore, Attack)
1. Detect
Detecting SSTI is challenging but critical. Use fuzzing or specific payloads to observe how the input is processed.
How to Detect SSTI
Inject special characters used in template syntax (
${{<%[%'"}}%
).If an exception or error message is triggered, it might indicate SSTI.
Example: Mathematical Operation
Inject the payload:
If the output is:
This confirms SSTI as the server interpreted the template syntax.
Code Context Detection
If the server uses template syntax for rendering:
Test for XSS:
Inject template syntax to escape the statement:
If output is:
Hello Mary<tag>
→ SSTI is present.If an error occurs → Possibly no SSTI or a different engine.
2. Identify the Template Engine
Once a potential SSTI is detected, the next step is identifying the template engine.
Techniques for Identification
Invalid Syntax Testing
Submit invalid syntax and observe the error message:
{{7/0}}
Example Error (Ruby-based ERB engine):
Test Engine-Specific Payloads
Different template engines interpret payloads differently:
Common Syntax Examples:
3. Exploit
After identifying SSTI, the next step is exploitation. The level of exploitation depends on the template engine and server context.
Read
Study Documentation:
Understand the template engine’s syntax and features.
Example for Python-based Mako template engine
Read About Security Implications:
Identify known vulnerabilities and exploit techniques.
Explore
Many template engines expose powerful objects such as
self
orenvironment
.Example in Java:
Attack
Escalate from SSTI to RCE or sensitive data access.
Example RCE:
Steps to Test for SSTI
Inject payloads containing special characters:
Examples:
${{<%[%'"}}%
Look for exceptions or anomalies in the server response.
Test template syntax-specific payloads to confirm exploitation potential.
Advanced SSTI Techniques
1. SSTI in Plaintext Context
Output rendered as text (e.g.,
Hello Mary
).Often mistaken for XSS vulnerabilities.
2. SSTI in Code Context
Exploit by escaping template syntax and injecting malicious code.
3. Error-Based SSTI
Trigger parsing errors to reveal template engine details or sensitive data.
Mitigations for SSTI
Avoid Direct Concatenation:
Always pass user input as data, not as part of the template string.
Use Secure Template Engines:
Use engines with built-in sanitization or restrict access to dangerous objects.
Validate and Sanitize User Input:
Ensure that user input cannot contain template syntax.
Restrict Template Functionality:
Limit access to objects, functions, or system calls in the template engine.
Error Handling:
Do not expose detailed error messages that could reveal sensitive information.
Last updated
Was this helpful?