Microsoft's Theorem Prover
Z3 is an SMT Solver, a program that can test whether a set of conditions can hold or not, and eventually find a valid solution. This is super helpful in the context of reverse engineering, where you sometimes want to find a valid input or reverse engineer a checker function.
Follow these instructions:
Wait for that to compile. Make sure you also install the Python bindings, which allow you to use Z3 from within Python:
Let's take the example of Hack The Box's Hissss challenge. Once you do the whole python decompilation step, you end up with a large if
statement for the password:
So we have a bunch of relations. Some of them are easy - password[0] == 48
, password[10] == str(3)
. Some, however, would require some thinking - the different characters are interrelated and appear in multiple statements. Sometimes, you might have other relations - for example, password[4] > 68
.
What we really want is to be able to plug all of this into a program that will solve the system of statements. Enter Z3!
First, set up the solver:
A BitVec
is just a binary number. In this case, we are making them 8
bits long (like a character!) and also calling them char0
, char1
, etc. Each charX
is the X
th character of the flag, so password
is really an array of characters which are represented in the form of BitVec
so that Z3 understands them.
Now we add the conditions to the Solver
object:
We then grab the solution as well as setting an answer
array of the correct length to populate:
The values returned by sol
are not in the simplest form possible, but we can extract the values for each index as so:
We get 0p3n_s3sam3!