Lab 02: Server-Side Template Injection (SSTI)

Objective

Exploit Jinja2 Server-Side Template Injection vulnerabilities to achieve Remote Code Execution from Kali Linux:

  1. Detect SSTI with mathematical probes ({{7*7}}49)

  2. Escalate to arbitrary Python evaluation via cycler.__init__.__globals__

  3. Execute OS commands: id, cat /etc/passwd, env

  4. Chain SSTI → RCE → full server compromise


Background

SSTI occurs when user input is embedded in a template and evaluated by the template engine. Unlike XSS (client-side execution), SSTI executes on the server with the web process's privileges.

Real-world examples:

  • 2016 Uber — SSTI in a marketing email template editor; researcher achieved RCE by injecting {{7*7}} in the subject line of a test campaign. Paid $10,000 bug bounty.

  • 2019 HackerOne platform — SSTI in a custom Markdown processor rendered Python expressions; security researcher achieved file read on production servers.

  • Pebble/FreeMarker/Velocity/Twig — every major template engine has been exploited via SSTI when templates are constructed from user input.

OWASP: A03:2021 Injection


Architecture

Time

45 minutes


Lab Instructions

Step 1: Setup


Step 2: Launch Kali and Detect SSTI

📸 Verified Output:


Step 3: Read Server Config and Globals


Step 4: SSTI → RCE via cycler

📸 Verified Output:

💡 cycler is a Jinja2 built-in helper object. Its __init__.__globals__ gives us the Python module's global namespace, which includes os. From os, we call popen() to spawn a shell. This works because Jinja2 templates run in the same Python process as the web server — they have full access to the Python runtime.


Step 5: SSTI via Email Preview (POST endpoint)

📸 Verified Output:


Step 6: Alternative RCE Payloads


Step 7: Write a Reverse Shell Payload

📸 Verified Output:


Step 8: Cleanup


Remediation

Further Reading

Last updated