Injecting into template engines for code execution
Learning Objectives
Understand template injection
Identify SSTI vulnerabilities
Exploit common template engines
Achieve remote code execution
When Templates Attack
Template engines are supposed to make web development easier - separate your logic from your presentation, they said. It'll be clean, they said. What they didn't mention is that if user input reaches the template engine, attackers can execute arbitrary code on your server.
Server-Side Template Injection (SSTI) occurs when user input is embedded into a template in an unsafe way. Instead of being treated as data, it's processed as template code. The result? Remote code execution in many cases.
SSTI often leads directly to Remote Code Execution (RCE). Unlike XSS which runs in browsers, SSTI runs on the server with full system access.
How Template Engines Work
Template engines allow developers to create dynamic HTML by embedding variables and logic. Here's the safe way vs the dangerous way:
Safe: Data in Template Context
python
1606070;"># Python/Jinja2 - SAFE
2from flask import render_template_string
3
4template = 606070;">#a5d6ff;">"Hello, {{ name }}!"
A web application reflects your input in a "Welcome" message:
URL: /welcome?name=John
Response: "Welcome, John!"
You try: /welcome?name={{7*7}}
Response: "Welcome, 49!"
What template engine is likely being used, and what should you try next?
Check if result is 49 (evaluated) or literal (safe)
Try multiple syntaxes to identify the engine
3
Identify the Template Engine
Use error messages (often reveal engine name)
Try engine-specific syntax
Check technology stack (Python = likely Jinja2)
4
Explore Capabilities
Can you access configuration? {{config}}
Can you access object methods? {{''.__class__}}
Are there built-in dangerous functions?
5
Achieve Code Execution
Use known exploitation chains for the engine
Read files first (less intrusive PoC)
Escalate to command execution if needed
Practice Challenges
Basic SSTI
Challenge
🔥 medium
A web application has a greeting page:
GET /greet?name=John
Response: "<h1>Hello, John!</h1>"
You notice the name parameter is reflected. Test for SSTI and read /etc/passwd.
The application is built with Python/Flask.
Need a hint? (4 available)
Filtered SSTI
Challenge
🔥 medium
Same application, but now with filters:
- "config" is blocked
- Underscores are blocked
- "os" is blocked
Find a way to still execute commands.
Need a hint? (4 available)
Identify and Exploit
Challenge
🔥 medium
A Java web application has a PDF generation feature. Users can customize
the header text.
POST /generate-pdf
Content-Type: application/x-www-form-urlencoded
header=Company Report
You notice the header appears in the generated PDF. Test for SSTI,
identify the template engine, and achieve code execution.
Need a hint? (4 available)
Knowledge Check
SSTI Quiz
Question 1 of 5
What's the key difference between SSTI and XSS?
Key Takeaways
SSTI = user input as template code, leading to code execution on the server
Each engine has unique syntax: {{}} for Jinja2/Twig, ${} for Freemarker, <%= %> for ERB
Detection: Try {{7*7}} or ${7*7} - if you see 49, it's vulnerable
Exploitation chains vary by engine - learn the specific payloads for common engines
Defense: Pass user data as template variables, never concatenate into template strings