-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
HashKiller.py
205 lines (152 loc) · 6.44 KB
/
HashKiller.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python3
import concurrent.futures
import psutil
import argparse
import hashlib
from itertools import product
import time
from passlib.hash import sha512_crypt, nthash
import threading
from colorama import Fore, init
print_lock = threading.Lock()
init(autoreset=True)
shutdown_event = threading.Event()
match_found_event = threading.Event()
threads = []
def generate_combinations(chars, min_length, max_length=22):
for length in range(min_length, max_length + 1):
for combo in product(chars, repeat=length):
yield ''.join(combo)
def md5_hash(string):
return hashlib.md5(string.encode()).hexdigest()
def sha1_hash(string):
return hashlib.sha1(string.encode()).hexdigest()
def sha256_hash(string):
return hashlib.sha256(string.encode()).hexdigest()
def sha512_unix_hash(string):
return sha512_crypt.using(rounds=5000).hash(string)
def nt_hash(string):
return nthash.hash(string)
def print_current_password(hash_type, target_hash, current_word):
with print_lock:
print(f'\rTYPE: {hash_type} | TARGET: {target_hash} | TRYING: {current_word}', end='')
print("\033[K", end='', flush=True)
def brute_force(target_hash, hash_type, chars, min_length, max_length, success_event, safety_pause=None):
counter = 0
for word in generate_combinations(chars, min_length, max_length):
if shutdown_event.is_set() or success_event.is_set():
return
print_current_password(hash_type, target_hash, word)
if hash_type == 'md5':
computed_hash = md5_hash(word)
elif hash_type == 'sha1':
computed_hash = sha1_hash(word)
elif hash_type == 'sha256':
computed_hash = sha256_hash(word)
elif hash_type == 'sha512_unix':
if sha512_crypt.verify(word, target_hash):
success_event.set()
with print_lock:
print(f"\nMatch found for hash {target_hash}:{Fore.LIGHTBLUE_EX} {word}")
time.sleep(2)
return word
elif hash_type == 'nt':
computed_hash = nt_hash(word)
else:
raise ValueError("Unsupported hash type")
if computed_hash == target_hash:
success_event.set()
match_found_event.set()
time.sleep(1)
with print_lock:
print(f"\nMatch found for hash {target_hash}:{Fore.LIGHTBLUE_EX} {word}")
return word
counter += 1
if safety_pause:
if safety_pause == 1 and counter % 699999 == 0:
time.sleep(1.35)
elif safety_pause == 2 and counter % 699999 == 0:
time.sleep(2)
elif safety_pause == 3 and counter % 199999 == 0:
time.sleep(1.5)
def resource_printer():
while not shutdown_event.is_set():
cpu, mem = resource_usage()
with print_lock:
print(f'\n\rCPU Usage: {cpu}% Memory Usage: {mem}%', end='', flush=True)
print("\033[F", end='', flush=True)
time.sleep(1)
if match_found_event.is_set():
break
def resource_usage():
cpu_percent = psutil.cpu_percent(interval=1)
memory_info = psutil.virtual_memory()
return cpu_percent, memory_info.percent
def crack_hash(target_hash, hash_type, chars, min_length, max_length, safety_pause=None):
global threads
results = []
t_printer = None # Initialize t_printer variable
try:
t_printer = threading.Thread(target=resource_printer)
t_printer.start()
for _ in range(args.threads):
t = threading.Thread(target=brute_force,
args=(target_hash, hash_type, chars, min_length, max_length, success_event,
safety_pause))
t.start()
threads.append(t)
for t in threads:
t.join()
except KeyboardInterrupt:
print("\nInitiating graceful shutdown. Please wait...")
shutdown_event.set()
for t in threads:
t.join()
if t_printer:
t_printer.join()
finally:
if not success_event.is_set():
print(f"\nNo match found for hash {target_hash}.")
if t_printer:
t_printer.join()
return results
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Brute force a hash from a file. Supports MD5, SHA-1, SHA-256, SHA-512(UNIX), and Windows NT.")
parser.add_argument("--hash", required=True, help="File containing the hash to be brute-forced.")
parser.add_argument("--hash-type", required=True, choices=['md5', 'sha1', 'sha256', 'sha512_unix', 'nt'],
help="Type of hashing algorithm.")
parser.add_argument("--length", type=int, default=4, choices=range(4, 9),
help="Minimum password length to start brute-forcing. Default is 4, can be set between 4 and 8.")
parser.add_argument("--threads", type=int, default=1, choices=[1, 2, 3, 4],
help="Number of threads to use for brute-forcing. Default is 1. Max is 4.")
parser.add_argument("--safety", type=int, choices=[1, 2, 3], default=None,
help="Choose a safety level to reduce CPU usage during brute-forcing. Safety levels 1, 2, 3")
args = parser.parse_args()
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*() -_+=?<>'
min_length = args.length
safety_pause = args.safety
try:
with open(args.hash, 'r') as file:
target_hash = file.readline().strip()
except FileNotFoundError:
print("Error: Specified file not found.")
exit(1)
except PermissionError:
print("Error: No permission to read the file.")
exit(1)
if not target_hash:
print("Error: File is empty or contains no valid hash.")
exit(1)
try:
success_event = threading.Event() # success event to signal threads to stop
results = crack_hash(target_hash, args.hash_type, chars, min_length, 22, safety_pause)
if results:
print(f"\nMatch found for hash {target_hash}:{Fore.LIGHTBLUE_EX} {results}")
except KeyboardInterrupt:
print("\nInitiating graceful shutdown. Please wait...")
success_event.set() # success event to stop threads
for t in threads:
t.join()
if not success_event.is_set():
print(f"\nNo match found for hash {target_hash}.")