decrypt-db.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python2
  2. # -*- coding: UTF-8 -*-
  3. # File: decrypt-db.py
  4. # Author: Yuxin Wu <ppwwyyxx@gmail.com>
  5. from argparse import ArgumentParser
  6. from pysqlcipher import dbapi2 as sqlite
  7. from hashlib import md5
  8. import sys
  9. import os
  10. DEFAULT_OUTPUT_DB_NAME = 'decrypted.db'
  11. def get_args():
  12. parser = ArgumentParser()
  13. parser.add_argument('db', help='path to EnMicroMsg.db')
  14. parser.add_argument('imei', help='15 digit IMEI of your phone')
  15. parser.add_argument('uin', help='WeChat UIN')
  16. parser.add_argument('--output', help='output decrypted database',
  17. default=DEFAULT_OUTPUT_DB_NAME)
  18. args = parser.parse_args()
  19. return args
  20. def get_key(imei, uin):
  21. a = md5(imei + uin)
  22. return a.hexdigest()[:7]
  23. if __name__ == '__main__':
  24. args = get_args()
  25. output = args.output
  26. if os.path.abspath(os.path.dirname(output)) != os.path.abspath('.'):
  27. print "Output file must be in current directory"
  28. sys.exit(1)
  29. if os.path.isfile(output):
  30. print "{} already exists. Remove? (y/n)".format(args.output),
  31. ans = raw_input()
  32. if ans not in ['y', 'Y']:
  33. print "Bye!"
  34. sys.exit()
  35. os.unlink(output)
  36. key = get_key(args.imei, args.uin)
  37. print "KEY: {}".format(key)
  38. print "Decrypt and dump database to {} ... ".format(output)
  39. conn = sqlite.connect(args.db)
  40. c = conn.cursor()
  41. c.execute("PRAGMA key = '" + key + "';")
  42. c.execute("PRAGMA cipher_use_hmac = OFF;")
  43. c.execute("PRAGMA cipher_page_size = 1024;")
  44. c.execute("PRAGMA kdf_iter = 4000;")
  45. c.execute("ATTACH DATABASE '" + args.output + "' AS db KEY '';")
  46. c.execute("SELECT sqlcipher_export('db');" )
  47. c.execute("DETACH DATABASE db;" )
  48. c.close()