Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,37 +4,62 @@ import string
|
|
4 |
import time
|
5 |
import multiprocessing
|
6 |
import math
|
|
|
|
|
|
|
7 |
|
8 |
def generate_passwords(chars, length, start, end):
|
9 |
for attempt in itertools.islice(itertools.product(chars, repeat=length), start, end):
|
10 |
yield ''.join(attempt)
|
11 |
|
12 |
def crack_chunk(args):
|
13 |
-
chars, length, start, end,
|
14 |
attempts = 0
|
15 |
for attempt in generate_passwords(chars, length, start, end):
|
16 |
attempts += 1
|
17 |
-
if attempts % 1000000 == 0:
|
18 |
progress_queue.put((chunk_id, attempts))
|
19 |
-
if attempt ==
|
20 |
progress_queue.put((chunk_id, attempts))
|
21 |
return attempts, attempt
|
22 |
progress_queue.put((chunk_id, attempts))
|
23 |
return attempts, None
|
24 |
|
25 |
-
def optimized_brute_force(password, progress=gr.Progress()):
|
26 |
start_time = time.time()
|
27 |
total_attempts = 0
|
28 |
chars = string.ascii_letters + string.digits + string.punctuation
|
29 |
cpu_count = multiprocessing.cpu_count()
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
for length in range(1, len(password) + 1):
|
32 |
chunk_size = math.ceil(len(chars) ** length / cpu_count)
|
33 |
manager = multiprocessing.Manager()
|
34 |
progress_queue = manager.Queue()
|
35 |
|
36 |
chunks = [
|
37 |
-
(chars, length, i * chunk_size, (i + 1) * chunk_size,
|
38 |
for i in range(cpu_count)
|
39 |
]
|
40 |
|
@@ -56,21 +81,107 @@ def optimized_brute_force(password, progress=gr.Progress()):
|
|
56 |
total_attempts += attempts
|
57 |
if cracked:
|
58 |
end_time = time.time()
|
59 |
-
return
|
60 |
|
61 |
end_time = time.time()
|
62 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
def brute_force_interface(password):
|
65 |
-
return optimized_brute_force(password)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
iface = gr.Interface(
|
68 |
fn=brute_force_interface,
|
69 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
outputs="text",
|
71 |
-
title="
|
72 |
-
description="
|
73 |
)
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
if __name__ == "__main__":
|
76 |
-
|
|
|
4 |
import time
|
5 |
import multiprocessing
|
6 |
import math
|
7 |
+
import zxcvbn
|
8 |
+
import hashlib
|
9 |
+
import random
|
10 |
|
11 |
def generate_passwords(chars, length, start, end):
|
12 |
for attempt in itertools.islice(itertools.product(chars, repeat=length), start, end):
|
13 |
yield ''.join(attempt)
|
14 |
|
15 |
def crack_chunk(args):
|
16 |
+
chars, length, start, end, password_hash, hash_function, chunk_id, progress_queue = args
|
17 |
attempts = 0
|
18 |
for attempt in generate_passwords(chars, length, start, end):
|
19 |
attempts += 1
|
20 |
+
if attempts % 1000000 == 0:
|
21 |
progress_queue.put((chunk_id, attempts))
|
22 |
+
if hash_function(attempt.encode()).hexdigest() == password_hash:
|
23 |
progress_queue.put((chunk_id, attempts))
|
24 |
return attempts, attempt
|
25 |
progress_queue.put((chunk_id, attempts))
|
26 |
return attempts, None
|
27 |
|
28 |
+
def optimized_brute_force(password, hash_type='sha256', use_common_passwords=True, use_dictionary=True, progress=gr.Progress()):
|
29 |
start_time = time.time()
|
30 |
total_attempts = 0
|
31 |
chars = string.ascii_letters + string.digits + string.punctuation
|
32 |
cpu_count = multiprocessing.cpu_count()
|
33 |
|
34 |
+
hash_function = getattr(hashlib, hash_type)
|
35 |
+
password_hash = hash_function(password.encode()).hexdigest()
|
36 |
+
|
37 |
+
# Feature 1: Common password check
|
38 |
+
if use_common_passwords:
|
39 |
+
common_passwords = ['123456', 'password', 'qwerty', 'admin', '123456789', '12345', '1234', '12345678', '111111', '1234567']
|
40 |
+
for common_pwd in common_passwords:
|
41 |
+
total_attempts += 1
|
42 |
+
if hash_function(common_pwd.encode()).hexdigest() == password_hash:
|
43 |
+
end_time = time.time()
|
44 |
+
return create_result_output(common_pwd, total_attempts, start_time, end_time, password)
|
45 |
+
|
46 |
+
# Feature 2: Dictionary attack
|
47 |
+
if use_dictionary:
|
48 |
+
with open('english_words.txt', 'r') as f:
|
49 |
+
dictionary_words = f.read().splitlines()
|
50 |
+
for word in dictionary_words:
|
51 |
+
total_attempts += 1
|
52 |
+
if hash_function(word.encode()).hexdigest() == password_hash:
|
53 |
+
end_time = time.time()
|
54 |
+
return create_result_output(word, total_attempts, start_time, end_time, password)
|
55 |
+
|
56 |
for length in range(1, len(password) + 1):
|
57 |
chunk_size = math.ceil(len(chars) ** length / cpu_count)
|
58 |
manager = multiprocessing.Manager()
|
59 |
progress_queue = manager.Queue()
|
60 |
|
61 |
chunks = [
|
62 |
+
(chars, length, i * chunk_size, (i + 1) * chunk_size, password_hash, hash_function, i, progress_queue)
|
63 |
for i in range(cpu_count)
|
64 |
]
|
65 |
|
|
|
81 |
total_attempts += attempts
|
82 |
if cracked:
|
83 |
end_time = time.time()
|
84 |
+
return create_result_output(cracked, total_attempts, start_time, end_time, password)
|
85 |
|
86 |
end_time = time.time()
|
87 |
+
return create_result_output(None, total_attempts, start_time, end_time, password)
|
88 |
+
|
89 |
+
def create_result_output(cracked_password, attempts, start_time, end_time, original_password):
|
90 |
+
time_taken = round((end_time - start_time) * 1000, 2)
|
91 |
+
|
92 |
+
output = []
|
93 |
+
if cracked_password:
|
94 |
+
output.append(f"Password cracked: {cracked_password}")
|
95 |
+
else:
|
96 |
+
output.append("Password not found")
|
97 |
+
|
98 |
+
output.append(f"Attempts: {attempts:,}")
|
99 |
+
output.append(f"Time taken: {time_taken} ms")
|
100 |
+
|
101 |
+
# Feature 3: Password strength evaluation
|
102 |
+
strength = zxcvbn.zxcvbn(original_password)
|
103 |
+
output.append(f"Password strength score: {strength['score']}/4")
|
104 |
+
output.append(f"Estimated guesses: {strength['guesses']:,}")
|
105 |
+
|
106 |
+
# Feature 4: Entropy calculation
|
107 |
+
entropy = calculate_entropy(original_password)
|
108 |
+
output.append(f"Password entropy: {entropy:.2f} bits")
|
109 |
+
|
110 |
+
# Feature 5: Suggestions for improvement
|
111 |
+
if strength['score'] < 3:
|
112 |
+
output.append("Suggestions for improvement:")
|
113 |
+
for suggestion in strength['feedback']['suggestions']:
|
114 |
+
output.append(f"- {suggestion}")
|
115 |
+
|
116 |
+
return "\n".join(output)
|
117 |
+
|
118 |
+
def calculate_entropy(password):
|
119 |
+
char_set = set(password)
|
120 |
+
return len(password) * math.log2(len(char_set))
|
121 |
+
|
122 |
+
def generate_password(length=12, complexity='medium'):
|
123 |
+
if complexity == 'low':
|
124 |
+
chars = string.ascii_lowercase + string.digits
|
125 |
+
elif complexity == 'medium':
|
126 |
+
chars = string.ascii_letters + string.digits
|
127 |
+
else:
|
128 |
+
chars = string.ascii_letters + string.digits + string.punctuation
|
129 |
+
|
130 |
+
return ''.join(random.choice(chars) for _ in range(length))
|
131 |
|
132 |
+
def brute_force_interface(password, hash_type='sha256', use_common_passwords=True, use_dictionary=True):
|
133 |
+
return optimized_brute_force(password, hash_type, use_common_passwords, use_dictionary)
|
134 |
+
|
135 |
+
def analyze_password(password):
|
136 |
+
strength = zxcvbn.zxcvbn(password)
|
137 |
+
entropy = calculate_entropy(password)
|
138 |
+
|
139 |
+
output = [
|
140 |
+
f"Password strength score: {strength['score']}/4",
|
141 |
+
f"Estimated guesses: {strength['guesses']:,}",
|
142 |
+
f"Password entropy: {entropy:.2f} bits",
|
143 |
+
]
|
144 |
+
|
145 |
+
if strength['score'] < 3:
|
146 |
+
output.append("Suggestions for improvement:")
|
147 |
+
for suggestion in strength['feedback']['suggestions']:
|
148 |
+
output.append(f"- {suggestion}")
|
149 |
+
|
150 |
+
return "\n".join(output)
|
151 |
|
152 |
iface = gr.Interface(
|
153 |
fn=brute_force_interface,
|
154 |
+
inputs=[
|
155 |
+
gr.Textbox(label="Enter password to crack"),
|
156 |
+
gr.Dropdown(choices=['md5', 'sha1', 'sha256', 'sha512'], label="Hash type", value='sha256'),
|
157 |
+
gr.Checkbox(label="Use common passwords", value=True),
|
158 |
+
gr.Checkbox(label="Use dictionary attack", value=True),
|
159 |
+
],
|
160 |
+
outputs="text",
|
161 |
+
title="Advanced Brute-Force Password Cracker Simulator",
|
162 |
+
description="This simulator attempts to crack a given password using various methods. It utilizes multiple CPU cores for faster cracking. Note: This is for educational purposes only.",
|
163 |
+
)
|
164 |
+
|
165 |
+
password_analyzer = gr.Interface(
|
166 |
+
fn=analyze_password,
|
167 |
+
inputs=gr.Textbox(label="Enter password to analyze"),
|
168 |
outputs="text",
|
169 |
+
title="Password Strength Analyzer",
|
170 |
+
description="Analyze the strength of a given password without attempting to crack it.",
|
171 |
)
|
172 |
|
173 |
+
password_generator = gr.Interface(
|
174 |
+
fn=generate_password,
|
175 |
+
inputs=[
|
176 |
+
gr.Slider(minimum=8, maximum=32, step=1, label="Password Length", value=12),
|
177 |
+
gr.Dropdown(choices=['low', 'medium', 'high'], label="Complexity", value='medium'),
|
178 |
+
],
|
179 |
+
outputs=gr.Textbox(label="Generated Password"),
|
180 |
+
title="Secure Password Generator",
|
181 |
+
description="Generate a secure password based on specified length and complexity.",
|
182 |
+
)
|
183 |
+
|
184 |
+
demo = gr.TabbedInterface([iface, password_analyzer, password_generator], ["Brute-Force Simulator", "Password Analyzer", "Password Generator"])
|
185 |
+
|
186 |
if __name__ == "__main__":
|
187 |
+
demo.launch()
|