SHARE
TWEET

craxor.py

a guest Nov 19th, 2019 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import hashlib
  2. import math
  3. import string
  4. import argparse
  5. import re
  6. import functools
  7.  
  8.  
  9. def main():
  10.     parser = argparse.ArgumentParser(description='Brute force a string from a hex-digest.',
  11.                                      epilog='Only "works" for strings with only lowercase letters and spaces.')
  12.     parser.add_argument('-A', '--algorithm',
  13.                         help='The hash algorithm used to generate the hex digest',
  14.                         choices=hashlib.algorithms_available,
  15.                         default='sha256')
  16.     parser.add_argument('-H', '--hexdigest',
  17.                         help='The hex encoded digest of the text we are brute-forcing.',
  18.                         default='2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824')
  19.     args = parser.parse_args()
  20.  
  21.     digest_size_map = {}
  22.     constructor_map = {}
  23.     for algo in hashlib.algorithms_available:
  24.         h = hashlib.new(algo)
  25.         digest_size_map[algo] = 2*h.digest_size
  26.         try:
  27.             constructor_map[algo] = getattr(hashlib, algo)
  28.         except AttributeError:
  29.             constructor_map[algo] = functools.partial(hashlib.new, algo)
  30.  
  31.     if len(args.hexdigest) != digest_size_map[args.algorithm]:
  32.         print(f'Invalid hexdigest length for {args.algorithm}; required length is {digest_size_map[args.algorithm]}.')
  33.  
  34.     target_digest = args.hexdigest.lower()
  35.     if re.match(r'^[0-9a-f]+$', target_digest) is None:
  36.         print(f'Invalid hexdigest characters: must hex digits only.')
  37.  
  38.     symbols = (string.ascii_lowercase + ' ').encode()
  39.     base = len(symbols)
  40.     constructor = constructor_map[args.algorithm]
  41.  
  42.     i = 0
  43.     text_i = symbols[:1]
  44.     hash_i = constructor(text_i)
  45.     digest_i = hash_i.hexdigest()
  46.  
  47.     while target_digest != digest_i:
  48.         print('HAXING FAILED, TRYING AGAIN!~:', text_i.decode())
  49.  
  50.         i += 1
  51.         text_i_length = int(math.log(i, base)) + 1
  52.         text_i = bytearray(text_i_length)
  53.  
  54.         n = i
  55.         for j in range(text_i_length - 1, -1, -1):
  56.             n, m = divmod(n, base)
  57.             text_i[j] = symbols[m]
  58.  
  59.         hash_i = constructor(text_i)
  60.         digest_i = hash_i.hexdigest()
  61.  
  62.     print('HAXING SUCCESS!~:', text_i.decode())
  63.  
  64.  
  65. if __name__ == '__main__':
  66.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top