Context
The TrendMicro CTF was a blast (apart for some Shakespeare guesswork), and I solved a challenge using radare2, so I thought this would be a good opportunity to present a challenge which can be solved using emulation.
The challenge
We’re provided with a file called dataloss
.
OK, so file
doesn’t know what it is. Time to open it in radare2.
If we look around in visual mode, we can see that some instructions make sense. We can tell r2 to auto-analyze the data and identify functions.
Plenty of functions found. We can switch to visual mode and cycle through them using n/N
. Most of them look like garbage, until we stumble upon the following:
It has a single argument, which it loads in the ecx
register, and then it moves all sorts of hardcoded values onto the stack. There are two directions we can go from here:
- Dump this function into an assembly source file, call it from main, assemble the file and run it in a debugger.
- ESIL.
We’ll obviously go with the second option, since it’s faster.
ESIL will need to perform some writes in memory, and since we opened the file in read-only mode, we’re going to need to enable caching. Then we will initialize the ESIL VM. All ESIL-related commands are preceded by ae
. You can view them by inputing ae?
Now we can emulate the function by stepping until 0x00000460
and print ebp
at that point.
Hmm, this doesn’t look like a flag. Luckily, we know what the flag should look like: TMCTF{...}
. Remember that this function receives an argument, which is assigned to ecx
. Then, the value 0x4b
, corresponding to the letter K
is added to it. We can figure out that ecx
needs to be the value 0x9
in order to get T
as the first letter of the flag.
Let’s rewind a bit and set the argument for our function at ebp+0x8
to 0x9
.
And there’s our flag!