Binary Exploitation
Easy
150 points
Canary Intro
Recuite 2025 - HCMUS
6 tháng 10, 2025
Stack Canary
Information Leak
Buffer Overflow

Binary Exploitation
Canary Intro - Writeup
Challenge Information
- Category: Pwn
- Difficulty: Beginner
- Protections: Full (PIE, NX, Canary, RELRO, SHSTK, IBT)
Analysis
Stack Canary Protection
The program demonstrates the stack canary mechanism:
- Prints
winfunction address to screen - Calls
dumpstack()displaying the canary value! - Receives input with
scanf("%s")→ can cause buffer overflow due to unlimited length mainfunction finishes and returns
Stack Layout
[rbp-0x20] buffer[24 bytes] ← where scanf writes data
[rbp-0x08] canary [8 bytes] ← MUST be preserved!
[rbp+0x00] saved rbp [8]
[rbp+0x08] return addr [8] ← target: return address → win
Vulnerability & Leaks
- Buffer overflow:
scanf("%s")has no length limit → can overwrite buffer - Canary Leak:
dumpstack()prints the canary value, so attacker knows the actual canary - PIE Leak:
winfunction address is printed (PIE leaked or program has PIE disabled)
Key idea: When canary is leaked, we can bypass the check by writing back that exact value in the payload.
Exploitation Technique
Payload Structure
payload = b"A" * 24 # Fill buffer
payload += p64(canary) # Write correct canary to avoid triggering __stack_chk_fail
payload += b"B" * 8 # Saved RBP (doesn't matter)
payload += p64(win_addr) # Return address -> win
Exploitation Steps
- Read (parse)
winaddress from program output. - Read (parse) canary value from stack dump (line saying "<=== Stack canary" or similar).
- Build payload with the exact leaked canary value.
- Send payload to program.
- When canary is preserved, the check succeeds → program does not call
__stack_chk_fail. - When
mainreturns, return address points towin()→ spawn shell. - Get flag (e.g.
cat flag.txt).
Flag
BPCTF{canary_guards_your_stack_f1994a7d1cb2e0aaf69241ba160693ab}
Key Takeaways
- Stack canaries are effective against overflows without leaks.
- Information Leaks can destroy canary effectiveness — if attacker knows the canary value, bypass is trivial.
- Defense in depth: Canary is just one layer of protection — need to combine with ASLR/RELRO/NX/...
- Absolutely do not print/expose sensitive data (like stack dump) in production environment.
Technical Details
- Canary on x86-64 Linux is usually fetched from
fs:[0x28]. - Before returning, it checks:
cmp rax, fs:[0x28](orsub rax, fs:[0x28]thenje ok) — if wrong call__stack_chk_fail. - Therefore payload must preserve the canary value intact to pass the check.
150
Points
Easy
Difficulty
Binary Exploitation
Category