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.