Skip to content

Commit 4783b9b

Browse files
committed
add hash cracker tutorial
1 parent fa4cbf6 commit 4783b9b

File tree

5 files changed

+5069
-0
lines changed

5 files changed

+5069
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
4040
- [How to Make a Ransomware in Python](https://www.thepythoncode.com/article/make-a-ransomware-in-python). ([code](ethical-hacking/ransomware))
4141
- [How to Perform DNS Enumeration in Python](https://www.thepythoncode.com/article/dns-enumeration-with-python). ([code](ethical-hacking/dns-enumeration))
4242
- [How to Geolocate IP addresses in Python](https://www.thepythoncode.com/article/geolocate-ip-addresses-with-ipinfo-in-python). ([code](ethical-hacking/geolocating-ip-addresses))
43+
- [How to Crack Hashes in Python](https://thepythoncode.com/article/crack-hashes-in-python). ([code](ethical-hacking/hash-cracker))
4344

4445
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
4546
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# [How to Crack Hashes in Python](https://thepythoncode.com/article/crack-hashes-in-python)
2+
To run this:
3+
- `pip install -r requirements.txt`
4+
- Get usage: `python crack_hashes.py --help`
5+
- Crack a SHA-256 hash using `wordlist.txt`:
6+
```bash
7+
$ python crack_hashes.py 6ca13d52ca70c883e0f0bb101e425a89e8624de51db2d2392593af6a84118090 wordlist.txt --hash-type sha256
8+
```
9+
**Output:**
10+
```
11+
[*] Cracking hash 6ca13d52ca70c883e0f0bb101e425a89e8624de51db2d2392593af6a84118090 using sha256 with a list of 14344394 words.
12+
Cracking hash: 96%|███████████████████████████████████████████████████████████████████████████████████████████▉ | 13735317/14344394 [00:20<00:00, 664400.58it/s]
13+
[+] Found password: abc123
14+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tqdm

0 commit comments

Comments
 (0)