Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hashlib
- import math
- import string
- import argparse
- import re
- import functools
- def main():
- parser = argparse.ArgumentParser(description='Brute force a string from a hex-digest.',
- epilog='Only "works" for strings with only lowercase letters and spaces.')
- parser.add_argument('-A', '--algorithm',
- help='The hash algorithm used to generate the hex digest',
- choices=hashlib.algorithms_available,
- default='sha256')
- parser.add_argument('-H', '--hexdigest',
- help='The hex encoded digest of the text we are brute-forcing.',
- default='2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824')
- args = parser.parse_args()
- digest_size_map = {}
- constructor_map = {}
- for algo in hashlib.algorithms_available:
- h = hashlib.new(algo)
- digest_size_map[algo] = 2*h.digest_size
- try:
- constructor_map[algo] = getattr(hashlib, algo)
- except AttributeError:
- constructor_map[algo] = functools.partial(hashlib.new, algo)
- if len(args.hexdigest) != digest_size_map[args.algorithm]:
- print(f'Invalid hexdigest length for {args.algorithm}; required length is {digest_size_map[args.algorithm]}.')
- target_digest = args.hexdigest.lower()
- if re.match(r'^[0-9a-f]+$', target_digest) is None:
- print(f'Invalid hexdigest characters: must hex digits only.')
- symbols = (string.ascii_lowercase + ' ').encode()
- base = len(symbols)
- constructor = constructor_map[args.algorithm]
- i = 0
- text_i = symbols[:1]
- hash_i = constructor(text_i)
- digest_i = hash_i.hexdigest()
- while target_digest != digest_i:
- print('HAXING FAILED, TRYING AGAIN!~:', text_i.decode())
- i += 1
- text_i_length = int(math.log(i, base)) + 1
- text_i = bytearray(text_i_length)
- n = i
- for j in range(text_i_length - 1, -1, -1):
- n, m = divmod(n, base)
- text_i[j] = symbols[m]
- hash_i = constructor(text_i)
- digest_i = hash_i.hexdigest()
- print('HAXING SUCCESS!~:', text_i.decode())
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement