|
| 1 | +import hashlib |
| 2 | +from tqdm import tqdm |
| 3 | + |
| 4 | +# List of supported hash types |
| 5 | +hash_names = [ |
| 6 | + 'blake2b', |
| 7 | + 'blake2s', |
| 8 | + 'md5', |
| 9 | + 'sha1', |
| 10 | + 'sha224', |
| 11 | + 'sha256', |
| 12 | + 'sha384', |
| 13 | + 'sha3_224', |
| 14 | + 'sha3_256', |
| 15 | + 'sha3_384', |
| 16 | + 'sha3_512', |
| 17 | + 'sha512', |
| 18 | +] |
| 19 | + |
| 20 | +def crack_hash(hash, wordlist, hash_type=None): |
| 21 | + """Crack a hash using a wordlist. |
| 22 | +
|
| 23 | + Args: |
| 24 | + hash (str): The hash to crack. |
| 25 | + wordlist (str): The path to the wordlist. |
| 26 | +
|
| 27 | + Returns: |
| 28 | + str: The cracked hash. |
| 29 | + """ |
| 30 | + hash_fn = getattr(hashlib, hash_type, None) |
| 31 | + if hash_fn is None or hash_type not in hash_names: |
| 32 | + # not supported hash type |
| 33 | + raise ValueError(f'[!] Invalid hash type: {hash_type}, supported are {hash_names}') |
| 34 | + # Count the number of lines in the wordlist to set the total |
| 35 | + total_lines = sum(1 for line in open(wordlist, 'r')) |
| 36 | + print(f"[*] Cracking hash {hash} using {hash_type} with a list of {total_lines} words.") |
| 37 | + # open the wordlist |
| 38 | + with open(wordlist, 'r') as f: |
| 39 | + # iterate over each line |
| 40 | + for line in tqdm(f, desc='Cracking hash', total=total_lines): |
| 41 | + if hash_fn(line.strip().encode()).hexdigest() == hash: |
| 42 | + return line |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + import argparse |
| 47 | + parser = argparse.ArgumentParser(description='Crack a hash using a wordlist.') |
| 48 | + parser.add_argument('hash', help='The hash to crack.') |
| 49 | + parser.add_argument('wordlist', help='The path to the wordlist.') |
| 50 | + parser.add_argument('--hash-type', help='The hash type to use.', default='md5') |
| 51 | + args = parser.parse_args() |
| 52 | + print() |
| 53 | + print("[+] Found password:", crack_hash(args.hash, args.wordlist, args.hash_type)) |
0 commit comments