You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.4 KiB

  1. # coding: utf-8
  2. import os, sys
  3. from z3 import *
  4. # 'hashed' password we have from the 'binary'
  5. hashed_password=168372036536695091
  6. # we are going to try different lengths of passwords
  7. for password_length in range(5,15):
  8. # init solver, this will wipe any residual constraints in each loop
  9. solver = Solver()
  10. # initialize constraints for each character of the password
  11. # we are only going to allow ascii values from 'a' to 'z'
  12. chars = [Int(c) for c in range(password_length)]
  13. # initialization value
  14. n = 7
  15. # the 'hashing' algorithm we have extracted from the binary
  16. for c in chars:
  17. # this essentially extends the symbolic constraints associated with each 'c'
  18. n = n * 27 + c * 5
  19. # the solver should return sat iff
  20. # the hashed input is equal the hashed_password
  21. solver.add(n == hashed_password)
  22. for c in chars:
  23. # each 'c' should be a lowercase ascii character
  24. solver.add(And(c >= ord('a'),
  25. c <= ord('z')))
  26. res = solver.check()
  27. if res != sat:
  28. print("No possible password found...")
  29. continue
  30. # print the model/password found
  31. m = solver.model()
  32. for d in m.decls():
  33. print("%s = %s -> %c" % (d.name(), m[d], chr(m[d].as_long())))
  34. password = ''.join(chr(m[c].as_long()) for c in chars)
  35. print("Trying '" + password + "' as password")
  36. os.system("./password_storage.sh {}".format(password))