-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd5crack.py
106 lines (86 loc) · 3.08 KB
/
md5crack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
"""MD5 Crack
Crack an MD5 hash
Example hash > 5ebe2294ecd0e0f08eab7690d2a6ee69 : secret
Usage: $ python3 md5crack.py (-m <md5> | -f <file>) -w <wordlist>
python3 md5crack.py -m 5ebe2294ecd0e0f08eab7690d2a6ee69 -w resources/passwords.txt
python3 md5crack.py -f resources/hashlist.txt -w resources/passwords.txt
"""
import argparse
import hashlib
import re
import sys
import requests
def is_md5(hash: str) -> bool:
"""Checks for valid md5 hash
Args:
hash (str): A string to be checked
Returns:
bool: True for md5 valid else False
"""
check = re.findall(r"([a-fA-F\d]{32})", hash)
return len(check) != 0
def crack_md5_single(md5: str, wordlist: argparse.FileType) -> str:
"""Gets a list of information from a url
Args:
md5 (str): An MD5 encypted hash
wordlist (str): Path to wordlist
Returns:
str: Original password if found
"""
if not is_md5(md5):
print(f"Invalid md5 format for <{md5}>, exiting...")
return None
lines = [x.strip() for x in wordlist.readlines()]
for line in lines:
if hashlib.md5(bytes(line, encoding="utf-8")).hexdigest() == md5:
print(f"\t{md5}... {line}\nComplete.")
return line
print("Failed to crack the hash, try using a bigger dictionary!")
def crack_md5_file(file: str, wordlist: str) -> dict:
"""Gets a list of information from a url
Args:
file (str): Path to file of md5 hashes
wordlist (str): Path to wordlist
Returns:
dict: Dictionary of cracked passwords
"""
result = {}
hashes = [x.strip() for x in file.readlines()]
for h in hashes:
if not is_md5(h):
print(f"Invalid md5 format for <{h}>, skipping...")
else:
result[h] = None
if len(result):
lines = [x.strip() for x in wordlist.readlines()]
for h in result.keys():
for line in lines:
if hashlib.md5(line.encode()).hexdigest() == h:
print(f"\t{h}... {line}")
result[h] = line
else:
print("No valid hashes, exiting...")
return None
if any([result[h] == None for h in result.keys()]):
print("Unable to crack all hashes, try using a bigger dicitonary!")
return result
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Crack md5 passwords")
parser.add_argument(
"-w", "--wordlist", type=argparse.FileType("r"), help="wordlist", required=True
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"-f", "--file", type=argparse.FileType("r"), help="md5 text file"
)
group.add_argument("-m", "--md5", help="md5 hash")
args = parser.parse_args()
if args.file:
print(f"Cracking for file {args.file.name}:")
result = crack_md5_file(args.file, args.wordlist)
print(f"Result: {result}")
elif args.md5:
print(f"Cracking single hash {args.md5}:")
result = crack_md5_single(args.md5, args.wordlist)
print(f"Result: {result}")