wk3 - structs, SAT solvers
Theme: Reverse Engineering, Structures, SAT Solvers
Knapsack (150)
from z3 import BitVec, Solver, sat
# declare three integers, the goal of our solver
# make sure the type is correct
# here, all are int32_t
# order should match the order in which they are entered in the program
in_1 = BitVec('in_1', 32)
in_2 = BitVec('in_2', 32)
in_3 = BitVec('in_3', 32)
in_4 = BitVec('in_4', 32)
in_5 = BitVec('in_5', 32)
in_6 = BitVec('in_6', 32)
# create solver and enforce constraints per the program control flow
s = Solver()
# give the solver requirements s.t. when fulfilled, output is TRUE
s.add(in_1 >= 0x0, in_2 >= 0x0, in_3 >= 0x0, in_4 >= 0x0, in_5 >= 0x0, in_6 > 0x0)
var_0 = 0x645
var_1 = 0xd7
var_2 = 0x113
var_3 = 0x14f
var_4 = 0x163
var_5 = 0x1a4
var_6 = 0x244
process_result = var_1 * in_1 + var_2 * in_2 + var_3 * in_3 + var_4 * in_4 + var_5 * in_5 + var_6 * in_6
s.add(process_result == var_0)
# solve!
assert s.check() == sat, "Error, not satisfiable!"
print(s.model()) # print out the inputs I needFlips (200)
Last updated