I made this challenge.
Writeup is going to be how i envisioned you solve it.
And im going to be at an advantage because i wrote the c code :)
This is a super detailed writeup because it is for DDC introduction
How does ./jokes function?
A quick look
We get a few files
jokes -> a binary
reversed_flag -> weird file
logs.txt -> what has been done to get to this situation before you got the binary
logs.txt
the logs writes a flag.c file - so the flag is code?
flag is compiled and ran as an argument with the jokes binary.
What does ./jokes do?
Lets run it!
So we run it and the program tells 4 jokes and then dumps the core. But we do get a pretty good error message. It needs a file. the jokes binary before was ran with a flag. Maybe we provide it with a file as well.
Running it with a file does not give an error. The file is left untouched but it seems like reversed_flag was overwritten.
So reversed flag is a shuffling of test now.
Lets figure out how it is shuffled
We know that after the forth time we get new joke something different happens.
Ok so we call the_funniest_joke function the forth time. Lets see what it does
Disassembling the function gives us a lot of stuff, but this is the very first bit of code after allocating for variables.
Another function open_file_from_args. Seems like a util function
Also srand(0x539) which is the same as srand(1337) hmmmm.
Lets look at open_file_from_args
Actually not that exiting. Just seems to load in the file and provide a pointer to it.
Lets keep looking at the_funniest_joke
Its running a for loop 10000 times… Thats sus - lets zoom in on that
I have renamed a few variables so it is easier to see what is going on.
It looks like we pick two random variables based on the srand(1337)
Then we modify some values using these values inside the while loop
Lets guess what it does by writing it in some basic python where we dont care about syntax
So we pick two points a, b
Now we actually have a very nice idea about what the shuffling actually does.
while a is less than b
Go through an array backwards from b down to a and put those values somewhere
Go through those values and put them into the original array starting from a
Visualuzing looks like this
This happens a cool 10000 times and probably shuffles the file very much.
Now the program needs to write it back into a new file
Knowing how fwrite works we can guess that k must be the size of file we are writing. But we are writing this shuffled mess into a file again.
Since we now know how to shuffle the array, and the fact that we use a seeded random. Meaning that if we just use the same seed again we will get the same output.
I slow method of piecing together this file would to be making an array of [0,1,2,3...n] and using that same shuffling method on that array.
Then we weill have a recipe on what index was put where. Then we can loop through the new array and put the pieces back where they belong.
Since we need srand from libc. Its easier just to write this solvescript in c. Its almost the same as python… or something..
My solvescript looks like this
I also did write one in python. But it was inconsistent because of some libc issues
Properly running the solvescript creates a binary file which actually runs!