404 CTF Sans Protection Writeup
Before we start you need to download the binary from here : Binary .
Here we go let’s start by running the binary and see what we have :
1 | (ironbyte㉿IronByte)-[/mnt/c/Users/IR0NYTE/Desktop/ctf] |
As you can see the binary gave us some random memory address with a beautiful hello in french. Why don’t we start by checking the architecture of the binary with the file command :
1 | (ironbyte㉿IronByte)-[/mnt/c/Users/IR0NYTE/Desktop/ctf] |
We got :
1 | fragile: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=6a457609506482cdebb144dbacd9c1f6fba34955, stripped |
As we can see the architecture of the binary is 64 bit, it’s also dynamically linked and not striped. Let’s try checking out the mitigations that is ON :
1 | (ironbyte㉿IronByte)-[/mnt/c/Users/IR0NYTE/Desktop/ctf] |
We got :
1 | [*] '/mnt/c/Users/IR0NYTE/Desktop/ctf/fragile' |
So as you can see we have no stack canary protecting the stack from overriding the return of the program, we also have the right to execute code in the stack since the NX is disabled. Since this two mitagations are off i was curious a little of bit to know more about this random address that the binary was throwing at me so i fired up my ghidra and started making some analysis on it, i tried to decompile the main. Here’s what i got :
After a quick look at the main, i noticed that there was a call to the gets function wich was a little of bit wired since the gets function have no limit on the size of the input which can lead to a buffer overflow attack. After that, i have noticed also that the address that the program was displaying was the address of the buffer. The idea is quite simple, we will try to inject a 64 bit shellcode in the beginning of the buffer from Shell Storm which will spawn for us a shell (system(“bin/bash”) Syscall), after that we will try to overflow the buffer until we get to the return address of the program then just redirect the return to the address of the buffer to execute our malicious shellcode. so we need to calculate the offset between the buffer and the return address. Let’s fire up the gdb and set a break point after that gets call than i just typed in “mike” and then tried to calculate the offset from there :
1 | gef➤ search-pattern "mike" |
Offset = 0x7fffffffe138 - 0x7fffffffe0f0 = 0x48.
Here’s the final exploit :
1 | from pwn import * |