Hack This Site - Application #17
Not just from the fact that this was marked as hard, this one definitely looks difficult in the sense that our password (or key) is unique to the username. As usual we start with the Intermodular References to look for reading the console input:
We put a breakpoint on this call and then follow it up:
Here Iâve discovered a loop of sorts which will read one character at a time from the input. Following it up further weâre inside a larger loop:
I donât really need to know the details of this loop, except to know from debugging that we exit all the loops after pressing âEnterâ. Now this returns to another function (Iâve conveniently already debugged it all and labelled):
So basically we read in the username, assembly a âPassword :â string and print it, then read in a password. Then we call a function which appears to verify the password. Iâll go into the contents of this in more detail:
The first important thing in this function is that we are grabbing the password input and comparing the first 4 characters to a code - namely [0x120, 0x150, 0x14C, 0xB4]. This involves bitshifting the input character left by 2 first - therefore we can reverse this code by bit shifting it to the right which gives us [âHâ, âTâ, âSâ, â-â]. So basically weâre just checking it is of the required format.
The next bit seems to be referring to the section beyond the âHTS-â in the userâs password. It seems to be expecting this length to be of 13 (based on the loop length) so passwords should be of the form âHTS-XXXX-XXXX-XXXXâ. In this loop (as commented) it basically checks that the dashes are all in the correct positioning. In the next bit we are removing the dashes:
I didnât bother commenting it as itâs easier to just observe the debugging output and see it places the resultant string after the loop:
So now we have a string with the âHTS-â and other â-â removed. Then next part Iâve gone through and debugging quite detailed:
Basically weâre verifying that the extracted password (without dashes and âHTSâ) is exactly 2 times the length of the username. We also check the username isnât 0 and then extract the hexadecimal value of the first 2 characters in the given password.
The next section is quite confusing however Iâve documented what is happening alongside the assembly code. (if you want to read) Iâll try and summarise what is happening:
Loop over the length of the username (for each i)
shl eax, cl => username[i] << (code & 0xFF)
sub esi, esx => username[i] - code
sar esi, 1 => (username[i] - code) >> 0x01
not eax => ~(username[i] << (code & 0xFF))
and esi eax => (username[i] - code) &Â ~(username[i] << (code & 0xFF))
Set the code to this result
ModifiedKey += Hex(Code) (2 character value)
Add the âHTS-â to front of modified key and add a â-â after every 4 characters
So basically I only really needed the first section; the second half of the above image didnât really matter at all. In the end I implemented this in a keygen in python:
Running the program we get the following:
And running in the program we get the result:
And we have completed the challenge! This one was definitely complex and took forever, however it was pretty cool to get more experience with x86 assembly.