Reversing Hero Level 4
# Initial opinions
Opening up the file, we are greeted with the start function. It calls a function and checks if it returns an exit code 0 and gives us the flag if that is the case:
|
|
# f_maincheck
This function has a bunch of calls.
|
|
Now, the f_some_weirdshit
function is important, and I have spent a bunch of
time trying to reverse it. However, it is as complicated as it is useful. And
after solving the challenge, I still don’t know what it does. The function that
matters here is f_bit_manipulator
, since if it alters a2
so that is equal to
a3
, then the function returns 0 and we get the flag. In our case:
|
|
# f_bit_manipulator
Looking at the IDA decompilation, we see the following.
|
|
I don’t know about you, but that hurts my eyes. Instead, let’s take a look at the disassembly:
|
|
After a quick examination, we can make the following inferences:
- The function only accepts
0
,1
or2
characters as the input. As you can see from the initial cmp and jmp statements which correspond to a switch. - When the character is 0, the counter at [rdi+8], which is set to 64 by
f_maincheck
in linev11 = 64
(Reference to the assembly to understand how that variable is accessed in another function) is moved to rcx. Afterwards,a2
’s RCX’th most significant bit is set to 0. - When the char is 1, the counter is incremented.
- When the char is 2, the counter is incremented and the reverse of 0 occurs, i.e. RCXth bit is set to 1.
# How is the RCXth bit set to 0.
If you look the assembly below:
|
|
Rotating left by RCX means that RCXth most
significant bit is at the leftmost point in the register. Then, shifting right
and left means that bit is set to 0. That is equivalent to anding with
0xFFFFFFFFFFFFFFFE
# The python program
It is easier to first set the contents of a2
to 0
and then setting each
necessary bit to 1
:
|
|