-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
396 lines (332 loc) · 12.1 KB
/
main.py
File metadata and controls
396 lines (332 loc) · 12.1 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# Bibliotek
import bcrypt
import time
import string
from hashlib import sha256, md5
import multiprocessing
import pickle
import random
import hashcat
# Alla tecken
chars = string.printable
chars = chars.strip("\n")
chars = chars.strip()
# chars = random.sample(chars, len(chars))
lowercase = string.ascii_lowercase
uppercase = string.ascii_uppercase
digits = string.digits
special_characters = string.punctuation
hash_types = ["sha256", "bcrypt", "md5"]
hash_type = int()
weights = [0.0, 4.5]
def hash_password(password, algo):
"""Hashar lösenord som skickas in."""
if algo == "sha256":
hashed_password = sha256(password.encode('utf-8')).hexdigest()
elif algo == "bcrypt":
hashed_password = bcrypt.hashpw(
password.encode('utf-8'),
bcrypt.gensalt(rounds=4)
)
elif algo == "md5":
hashed_password = md5(password.encode('utf-8')).hexdigest()
return hashed_password
def index_to_password(index, length, chars, vals):
"""Omvandlar tal till lösenordskombinationer."""
passwd = []
for i in reversed(range(length)):
val = vals[i]
passwd.append(chars[index // val])
index = index % val
return "".join(passwd)
def brute_force(hash, chars, indexes, len, hash_algo, efound, qresult):
"""
Utför simulation av ren brute force-attack i intervallet indexes.
Använder efound och qresult för att synka med huvudprocess.
"""
vals = []
for i in range(10):
vals.append(94 ** i)
for i in range(indexes[0], indexes[1]):
if i % 100 == 0:
if efound.is_set():
return
candidate = index_to_password(i, len, chars, vals)
if hash_algo == "bcrypt":
if bcrypt.checkpw(candidate.encode(), hash):
qresult.put(candidate)
efound.set()
return
else:
hashed = hash_password(candidate, hash_algo)
if hashed == hash:
qresult.put(candidate)
efound.set()
return
def benchmark(str, hash_algo, qcounter):
"""Ren benchmark för hashningshastighet, ger teoretisk maxhastighet."""
count = 0
time_start = time.monotonic()
if hash_algo == "bcrypt":
iterations = 100
else:
iterations = 10000
while True:
hash_password(str, hash_algo)
count += 1
if count % iterations == 0:
if time.monotonic() - time_start >= 10:
qcounter.put(count)
break
def save_results(password, algo, charset, length, time, tool):
files = ["all.pkl", picklename]
for file in files:
data = []
try:
with open("results/" + file, "rb") as f:
data = pickle.load(f)
except:
pass
data.append((password, algo, charset, length, time, tool))
with open("results/" + file, "wb") as f:
pickle.dump(data, f)
def get_color_weight(password):
color_weight = 0.0
if not set(password).isdisjoint(special_characters):
color_weight += 2.0
if not set(password).isdisjoint(digits):
color_weight += 0.5
if not set(password).isdisjoint(uppercase):
color_weight += 1.0
if not set(password).isdisjoint(lowercase):
color_weight += 1.0
return color_weight
def generate_password(length, charset):
password = ""
for i in range(length):
password += random.choice(charset)
return password
def main(mode, hash, proc, hash_algo):
if mode == 0 or mode == 2:
print("Using " + str(proc) + " processes to brute force...")
base = len(chars)
efound = multiprocessing.Event()
qresult = multiprocessing.Queue()
time_start = time.monotonic()
for i in range(1, 10):
if(efound.is_set()):
break
print("Trying passwords with length " + str(i))
total = base ** i
chunk = total // proc
processes = []
ranges = []
for j in range(proc):
first = chunk*j
if j == proc - 1:
last = total
else:
last = chunk*(j+1)
ranges.append([first, last])
for k in range(proc):
p = multiprocessing.Process(target=brute_force, args=(
hash,
chars,
ranges[k],
i,
hash_algo,
efound,
qresult
))
p.start()
processes.append(p)
while True:
if not qresult.empty():
guess = qresult.get()
time_end = time.monotonic()
elapsed = time_end - time_start
print("Found password: " + guess)
print("Elapsed time: " + format_time(elapsed))
print("----")
if mode == 0:
color_weight = get_color_weight(password)
else:
color_weight = weights[entropy]
save_results(
guess,
hash_algo,
color_weight,
len(password),
elapsed,
"python"
)
for p in processes:
# Stäng ned allt
p.terminate()
break
if all(not p.is_alive() for p in processes):
break
time.sleep(0.01)
else:
measured = []
for i in range(10):
print("Benchmarking using " + str(proc) + " processes...")
bstring = "AAAAA"
processes = []
qcounter = multiprocessing.Queue()
total = 0
for i in range(proc):
p = multiprocessing.Process(
target=benchmark,
args=(bstring, hash_algo, qcounter)
)
p.start()
processes.append(p)
time.sleep(11)
for p in processes:
p.terminate()
while not qcounter.empty():
total += qcounter.get()
efficiency = total / 10
measured.append(efficiency)
efficiency_sum = sum(measured) / 10
print("Estimated pure hashing efficiency: " + str(efficiency_sum) + " H/s")
def format_time(time):
if time > 60 and time > (60 * 60) and time > (60 * 60 * 24):
return str(round(time / (60 * 60 * 24), 2)) + "d"
elif time > 60 and time > (60 * 60):
return str(round(time / (60 * 60), 2)) + "h"
elif time > 60:
return str(round(time / 60, 2)) + "m"
else:
return str(round(time, 2)) + "s"
def hashcat_main(hash, hash_algo):
if mode == 0 or mode == 2:
result = hashcat.crack(hash, hashcat.hash_map[hash_algo])
print(result[0], format_time(result[1]))
print("----")
if mode == 0:
color_weight = get_color_weight(password)
else:
color_weight = weights[entropy]
save_results(
result[0],
hash_algo,
color_weight,
len(password),
result[1],
"hashcat"
)
else:
list = []
for i in range(10):
result = hashcat.benchmark(hashcat.hash_map[hash_algo])
list.append(result[0])
print("Test " + str(i + 1) + ":")
print(str(result[0]) + " " + str(result[1]))
print("----")
print("Average: " + str(sum(list) / 10))
# Huvudprocess
if __name__ == '__main__':
# Skicka in lösenord och kör brute force
mode = int(input("Mode (Brute force <0>, Benchmark <1>, Fixed <2>): "))
enable_random = False
test_all_hashes = False
length = None
password = None
iterate = False
repeat = 1
charset = ""
hash = None
tool = int(input("Tool (python <0>, hashcat <1>): "))
if mode == 0:
enable_random = bool(int(input("Use random password? (0/1): ")))
if enable_random:
length = int(input("Password length: "))
if input("Use special characters? (0/1): ") == "1":
charset += special_characters
if input("Use digits? (0/1): ") == "1":
charset += digits
if input("Use uppercase? (0/1): ") == "1":
charset += uppercase
if input("Use lowercase? (0/1): ") == "1":
charset += lowercase
iterate = bool(int(input("Iterate through length? (0/1): ")))
repeat = int(input("Repeat? (0/1): "))
if repeat == 1:
repeat = int(input("Repeat amount: "))
else:
repeat = 1
else:
password = input("Password: ")
test_all_hashes = bool(int(input("Test all hashes? (0/1): ")))
if not test_all_hashes:
hash_algo = hash_types[int(input("Hash type (sha256 <0>, bcrypt <1>, md5 <2>): "))]
else:
hash_algo = hash_types[int(input("Hash type (sha256 <0>, bcrypt <1>, md5 <2>): "))]
if mode == 2:
floor = int(input("Min character count (1-6): "))
roof = int(input("Max character count (1-6): "))
entropy = int(input("Select entropy (Numbers only <0>, All characters <1>): "))
if tool == 0:
proc = int(input("Amount of processes to be used: "))
if mode != 1:
picklename = input("Separate file to write to: ") + ".pkl"
if mode != 2:
for j in range(repeat):
if test_all_hashes and iterate:
for hashtype in hash_types:
for i in range(1, length + 1):
if mode == 0:
if enable_random:
password = generate_password(i, charset)
hash = hash_password(password, hashtype)
if tool == 0:
main(mode, hash, proc, hashtype)
else:
hashcat_main(hash, hashtype)
elif test_all_hashes:
for hashtype in hash_types:
if mode == 0:
if enable_random:
password = generate_password(length, charset)
hash = hash_password(password, hashtype)
if tool == 0:
main(mode, hash, proc, hashtype)
else:
hashcat_main(hash, hashtype)
elif iterate:
for i in range(1, length + 1):
if mode == 0:
if enable_random:
password = generate_password(i, charset)
hash = hash_password(password, hash_algo)
if tool == 0:
main(mode, hash, proc, hash_algo)
else:
hashcat_main(hash, hash_algo)
else:
if mode == 0:
if enable_random:
password = generate_password(length, charset)
hash = hash_password(password, hash_algo)
if tool == 0:
main(mode, hash, proc, hash_algo)
else:
hashcat_main(hash, hash_algo)
else:
if entropy == 0:
filename = "passwords/digits.txt"
elif entropy == 1:
filename = "passwords/special.txt"
passwords = open(filename, "r")
lines = passwords.readlines()
for i in range(floor, roof + 1):
for j in range(10):
line = (i - 1) * 10 + j
password = lines[line].strip()
hash = hash_password(password, hash_algo)
if tool == 0:
main(mode, hash, 8, hash_algo)
else:
hashcat_main(hash, hash_algo)