Challenge
Welcome to hell, come on in, we eat what you feed with a devilish grin. But shadows creep and eyes may peer, so nothing’s quite what it seems down here.
Chant that spell hidden in
/flag, which opens the door to get out of this hell. Port: 1337.
Given: hell-jail (x86-64 PIE, stripped) and a per-team instance on port 1337.
1. Find out what the binary is
$ strings hell-jail # nothing useful
$ nm -D hell-jail # no libc I/O imports
It makes raw syscalls, and every string is XORed with 0xA5 and written into .bss byte by
byte at runtime. main is 16 KB and is a bytecode VM, so read its syscalls instead of its
code:
$ strace -f ./hell-jail hello
prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) = 0
prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, {len=55, filter=0x7ffe5b3b3688}) = 0
execve("hello", ["hello"], ...) = -1 ENOENT
It is a seccomp jail: it installs a 55-instruction filter and then runs argv[1]. The service
on port 1337 “eats what you feed” — it takes a program from you and runs it this way, with the
flag at /flag.
2. Dump the filter (and fall for the decoy)
$ seccomp-tools dump "./hell-jail x"
The filter is a whitelist: read, write, close, fstat, lseek, mmap, mprotect,
munmap, brk, ioctl, readv, writev, execve, exit, exit_group, a few
runtime-startup calls, and no open or openat. One more syscall is allowed conditionally:
0033: if (A != pread64) goto KILL
count >> 32 == 0, pos >> 32 == 0
0038: A = count
A = rol32(A, 21) ^ 0xa5c3f00d
A = A * 0xcc9e2d51 + 0x0badbeef
A = ((A ^ 0x11223344) - 0x0badcafe) ^ 0xdeadc0de
0052: if (A != 0x230226d9) goto KILL
Inverting it gives count = 0xf00dface. The obvious reading is “the service passes an open fd
for the flag, pread64 it with the magic count”.
3. Notice the payload only works while traced
A test payload that calls pread64(3, buf, 0xf00dface, 0):
$ strace -f ./hell-jail ./exp 3<fakeflag # prints fakeflag
$ ./hell-jail ./exp 3<fakeflag
Bad system call (core dumped)
Same program, same input, different result depending on whether something is watching. That is
the “eyes may peer, nothing’s quite what it seems” hint. The function at 0x11e1 opens
/proc/self/status and reads TracerPid:. If a tracer is attached, the binary installs the
filter from step 2 — a decoy. The server runs untraced and uses a different one.
4. Dump the real filter
The filter constants are computed at runtime by a second small VM (0x153e), so they can’t be
carved out of the file. Lie to the tracer check instead, then grab the filter at the prctl
call:
# gdb -q --batch -x dump_filter.py
import gdb, struct
for c in ("set pagination off", "set confirm off", "set disable-randomization on"):
gdb.execute(c)
BASE = 0x555555554000 # PIE base with ASLR off
gdb.execute("file ./hell-jail")
gdb.execute("break *%#x" % (BASE + 0x11e1)) # TracerPid check
gdb.execute("run /bin/true")
gdb.execute("return (long)0") # "nobody is tracing"
gdb.execute("catch syscall prctl")
for _ in range(20):
gdb.execute("continue")
orig = int(gdb.parse_and_eval("$orig_rax"))
rdi = int(gdb.parse_and_eval("$rdi"))
rax = int(gdb.parse_and_eval("$rax"))
if orig == 157 and rdi == 22 and rax == -38: # PR_SET_SECCOMP, syscall entry
inf = gdb.inferiors()[0]
prog = bytes(inf.read_memory(int(gdb.parse_and_eval("$rdx")), 16))
n, ptr = struct.unpack("<H", prog[:2])[0], struct.unpack("<Q", prog[8:])[0]
open("real_filter.bin", "wb").write(bytes(inf.read_memory(ptr, n * 8)))
break
gdb.execute("kill")
$ seccomp-tools disasm real_filter.bin
The same filter also falls out statically by emulating both VMs with the tracer check forced to “clean”; the GDB route is just faster.
5. Read the real filter
The whitelist is the same as the decoy. The conditional tail is not:
0033: if (A != openat) goto KILL
0034: flags >> 32 == 0
0036: flags == 0 ; exactly O_RDONLY
0038: mode >> 32 == 0
0040: A = mode
0041: A ^= 0x5a5a5a5a
0042: A *= 0x9e3779b1
0043: A += 0x1badc0de
0044: A = (A << 13) | (A >> 19) ; rol32(A, 13)
0051: A ^= 0xa5a5a5a5
0052: if (A != 0x7665438e) goto KILL
0053: return ALLOW
openat is allowed if the flags are exactly O_RDONLY and the hash of mode equals
0x7665438e. The path is not checked. The kernel ignores mode unless O_CREAT is set, so it
is a free 32-bit slot — that is the “spell”.
6. Invert the hash
Xor, add, rotate and multiplication by an odd number are all invertible mod 2³², so undo them in reverse order:
M = 0xffffffff
rol = lambda v, n: ((v << n) | (v >> (32 - n))) & M
ror = lambda v, n: rol(v, 32 - n)
t = ror(0x7665438e ^ 0xa5a5a5a5, 13)
t = (t - 0x1badc0de) & M
t = t * pow(0x9e3779b1, -1, 1 << 32) & M
mode = t ^ 0x5a5a5a5a
print(hex(mode)) # 0xf289daa3
7. Write the payload
It has to be static and libc-free, because normal program startup makes syscalls the filter kills.
; nasm -f elf64 open.asm -o open.o && ld -static -nostdlib open.o -o open_flag
BITS 64
default rel
section .text
global _start
_start:
mov eax, 257 ; openat
mov rdi, -100 ; AT_FDCWD
lea rsi, [path]
xor edx, edx ; flags = O_RDONLY
mov r10d, 0xf289daa3 ; mode = the spell
syscall
mov r12, rax
.loop:
xor eax, eax ; read(fd, buf, 0x1000)
mov rdi, r12
lea rsi, [buf]
mov edx, 0x1000
syscall
test rax, rax
jle .done
mov rdx, rax
mov eax, 1 ; write(1, buf, n)
mov edi, 1
lea rsi, [buf]
syscall
jmp .loop
.done:
mov eax, 60 ; exit(0)
xor edi, edi
syscall
path: db "/flag",0
section .bss
buf: resb 0x1000
8. Test locally, untraced
Build a second copy with path pointing at a test file and run it without any tracer:
$ ./hell-jail ./open_my
csawctf{local_openat_spell_ok}
$ ./hell-jail /bin/cat testfile # control: ordinary openat, no magic mode
Bad system call (core dumped)
The payload gets through and an ordinary program does not, so this is the real filter.
9. Send it
The service prints no banner or prompt. Send the raw ELF, half-close the socket so it sees EOF, and read what comes back:
import socket
s = socket.create_connection(("<host>", 1337), timeout=10)
s.sendall(open("open_flag", "rb").read())
s.shutdown(socket.SHUT_WR)
out = b""
while (d := s.recv(4096)):
out += d
print(out.decode())
Flag
csaw{uNC0v3rd_t#e_Dev1l$_0rDe&l__$uch_&_e^v1l_M0nst3r__t#aNk$_f0r_v1s1Tng}
Takeaways
- A seccomp filter dumped under
ptraceis only what the program chose to show a tracer. If a payload passes understraceand dies without it, look for aTracerPidcheck. - Forcing the anti-debug result and dumping at
prctlis far cheaper than reversing two nested VMs. - BPF argument “hashes” built from xor, add, rotate and odd multiplies can always be inverted.