https://ctftime.org/event/1066
I did not solve the vast majority of challenges I am writing up here, but instead I am doing it to consolidate my own understanding of it. I also hope the writeups you find here help you understand the challenge, and if they did then it's fulfilled it's purpose and whether or not I originally completed them is irrelevant :)
A ret2libc with a given leak
Running the binary prints and hex value and prompts for input:
We can definitely cause it to segfault:
So let's work out what this value is and how we can use it.
We chuck the binary into GHidra and get a simple disassembly. main
calls vuln
and does almost nothing else. vuln
, however, has some interesting stuff:
It prints the address of system
! Awesome.
Let's run the binary on the remote serevr to leak the libc version.
So now we essentially have a libc leak, we head over to find the libc version.
Annoyingly, there are 4 possible libc versions, and we can only get it from trial and error. Aside from the libc version itself, the exploit is quite simple - subtract the offset of system
from the leaked address to get libc
base, then use that to get the location of /bin/sh
.
The correct libc version is 2.30-0ubuntu2.1_i386
.
Messing with the XOR
Let's try running the file:
Perhaps it wants a flag.txt file? Let's create one with the words FwordCTF{flag_flaggety_flag}
:
This isn't quite counting the number of letters we enter. Let's see if the disassembly can shed any light on it.
First thing we notice is that every libc
function is built into the binary due to the stripped names. We can confirm this with rabin2
:
Many of the functions can be handled using the return address and the general context. Some of the decompilation - especially the references to strings - may not have loaded in yet; make sure GHidra finishes analysing. We don't even need the exact C names, as long as we get the general gist it's all fine.
The python equivalent of this is roughly
In short, it's an XOR function.
To calculate the length of the string it uses
The key here is strlen
stops at a null byte. If you input a character with the same value as the flag character in that position, it will XOR to become \x00
.
We can test every possible character. If the returned value is one less than the length of the string, the last character is correct as it XORed to create a null byte.
To test different offsets we can pad using a value definitely not in the flag, such as #
.
Now we can just switch out the process type on the remote server.
Flag: NuL1_Byt35?15_IT_the_END?Why_i_c4nT_h4ndl3_That!