Skip to content

Instantly share code, notes, and snippets.

@karmacoma-eth
Last active October 16, 2023 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karmacoma-eth/746441c3a9c992144ed01e847aa219a4 to your computer and use it in GitHub Desktop.
Save karmacoma-eth/746441c3a9c992144ed01e847aa219a4 to your computer and use it in GitHub Desktop.
enumerating possibles expression values with z3
from z3 import *
selector = BitVec('selector', 32)
expr = 107 + 4 * (selector % 16)
codesize = 128
s = Solver()
s.add(expr < codesize)
possible_jump_table_values = set([])
while True:
print(f"checking assertions [{', '.join([str(c) for c in s.assertions()])}]")
result = s.check()
print(f"result: {result}")
if result != sat:
break
model = s.model()
jump_table_value = model.evaluate(expr).as_long()
possible_jump_table_values.add(jump_table_value)
# make sure we get a different expr value next time
s.add(expr != jump_table_value)
print(f"possible jump table values: {possible_jump_table_values}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment