#!/usr/bin/env python3 """Ranger Desk: runs ONE trekker expression, then exits. This exact file is shown at /source. The web server starts a fresh copy for every request (stdin = your expression, stdout = the answer) and kills it after a few seconds. The resource limits and the syscall filter below only keep the desk up for everybody; they are not part of the puzzle. """ import os import resource import sys MAX_LEN = 400 BANNED_CHARS = set("_'\"\\#`") BANNED_WORDS = ("format", "import", "eval", "exec", "open", "compile", "globals", "getattr", "breakpoint", "input") # ---------------------------------------------------------------- house-keeping (not the puzzle) def limits(): for res, val in ((resource.RLIMIT_CPU, 2), (resource.RLIMIT_AS, 96 << 20), (resource.RLIMIT_NPROC, 0), (resource.RLIMIT_FSIZE, 0), (resource.RLIMIT_NOFILE, 32), (resource.RLIMIT_CORE, 0)): try: resource.setrlimit(res, (val, val)) except (ValueError, OSError): pass try: # if memory runs out, the kernel should pick a desk copy, never the web server with open("/proc/self/oom_score_adj", "w") as f: f.write("1000") except OSError: pass def syscall_filter() -> bool: """seccomp: no processes, signals to others, sockets, file writes or tracing.""" import ctypes import struct if os.uname().machine != "x86_64": return False deny = [41, 42, 43, 44, 45, 46, 47, 49, 50, 53, 288, 299, 307, # sockets 56, 57, 58, 59, 322, 435, # clone/fork/exec 62, 200, 234, 129, 297, 424, 434, 438, # signals, pidfd 101, 310, 311, 312, # ptrace & friends 76, 77, 82, 83, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 133, # file changes 258, 259, 260, 263, 264, 265, 266, 268, 316, 437, 452, 425, 426, 427, 135, 155, 161, 165, 166, 248, 249, 250, 272, 298, 308, 321, 323] write_flags = 0o1 | 0o2 | 0o100 | 0o1000 | 0o2000 # O_WRONLY|O_RDWR|O_CREAT|O_TRUNC|O_APPEND ALLOW, EPERM, KILL = 0x7FFF0000, 0x00050001, 0x80000000 prog = [(0x20, 0, 0, 4), (0x15, 1, 0, 0xC000003E), (0x06, 0, 0, KILL), # arch == x86_64 (0x20, 0, 0, 0), (0x35, 0, 1, 0x40000000), (0x06, 0, 0, KILL)] # no x32 ABI for nr in deny: prog += [(0x15, 0, 1, nr), (0x06, 0, 0, EPERM)] for nr, arg in ((2, 1), (257, 2)): # open / openat prog += [(0x15, 0, 4, nr), (0x20, 0, 0, 16 + 8 * arg), (0x45, 0, 1, write_flags), (0x06, 0, 0, EPERM), (0x06, 0, 0, ALLOW)] prog += [(0x06, 0, 0, ALLOW)] raw = b"".join(struct.pack(" bool: """The ranger never posts the summit code, not even backwards.""" code = logbook.__closure__[0].cell_contents return any(code[i:i + 6] in text or code[::-1][i:i + 6] in text for i in range(len(code) - 5)) def desk(src: str) -> str: src = src.strip() if not src: return MOTD if len(src) > MAX_LEN: return f"Too long: the desk takes at most {MAX_LEN} characters." if not (src.isascii() and src.isprintable()): return "Plain ASCII on one line, please." if set(src) & BANNED_CHARS or any(w in src.lower() for w in BANNED_WORDS): return "The ranger frowns at that. (banned character or word)" try: code = compile(src, "", "eval") result = eval(code, {"__builtins__": {}}, {"ranger": Ranger()}) out = result if isinstance(result, str) else repr(result) except BaseException as e: # noqa: BLE001 - show trekkers what went wrong out = f"{type(e).__name__}: {e}" out = out[:2000] if tore_up(out): return "The ranger tore up your postcard: it quoted the summit logbook!" return out if __name__ == "__main__": limits() filtered = syscall_filter() if "--selftest" in sys.argv: import socket probes = [] for name, fn in (("fork", lambda: os.fork() or os._exit(0)), ("socket", socket.socket), ("write", lambda: open("/tmp/x", "w")), ("kill", lambda: os.kill(1, 0))): try: fn() probes.append(f"{name}=ALLOWED") except OSError: probes.append(f"{name}=blocked") print(f"seccomp={'on' if filtered else 'OFF'} " + " ".join(probes)) sys.exit(0) print(desk(sys.stdin.read(4096)))