Spaces:
Sleeping
Sleeping
Create components/passbeaker.py
Browse files- components/passbeaker.py +125 -0
components/passbeaker.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import hashlib
|
2 |
+
import argparse
|
3 |
+
import itertools
|
4 |
+
from multiprocessing import Pool
|
5 |
+
|
6 |
+
|
7 |
+
class PasswordCracker:
|
8 |
+
def __init__(self, password_hash, wordlist_file, algorithm, salt=None, parallel=False, complexity_check=False):
|
9 |
+
self.password_hash = password_hash
|
10 |
+
self.wordlist_file = wordlist_file
|
11 |
+
self.algorithm = algorithm
|
12 |
+
self.salt = salt
|
13 |
+
self.parallel = parallel
|
14 |
+
self.complexity_check = complexity_check
|
15 |
+
self.total_passwords = 0
|
16 |
+
self.matched_password = None
|
17 |
+
|
18 |
+
def crack_hash(self, word):
|
19 |
+
if self.salt:
|
20 |
+
word_with_salt = f'{self.salt}{word}'
|
21 |
+
else:
|
22 |
+
word_with_salt = word
|
23 |
+
hashed_word = hashlib.new(self.algorithm, word_with_salt.encode()).hexdigest()
|
24 |
+
if hashed_word == self.password_hash:
|
25 |
+
self.matched_password = word
|
26 |
+
return True
|
27 |
+
return False
|
28 |
+
|
29 |
+
def generate_passwords(self, min_length, max_length, character_set):
|
30 |
+
passwords = []
|
31 |
+
for length in range(min_length, max_length + 1):
|
32 |
+
for combination in itertools.product(character_set, repeat=length):
|
33 |
+
password = ''.join(combination)
|
34 |
+
passwords.append(password)
|
35 |
+
return passwords
|
36 |
+
|
37 |
+
def evaluate_complexity(self, password):
|
38 |
+
has_lowercase = False
|
39 |
+
has_uppercase = False
|
40 |
+
has_digit = False
|
41 |
+
has_special = False
|
42 |
+
|
43 |
+
for char in password:
|
44 |
+
if char.islower():
|
45 |
+
has_lowercase = True
|
46 |
+
elif char.isupper():
|
47 |
+
has_uppercase = True
|
48 |
+
elif char.isdigit():
|
49 |
+
has_digit = True
|
50 |
+
else:
|
51 |
+
has_special = True
|
52 |
+
|
53 |
+
if len(password) >= 8 and has_lowercase and has_uppercase and has_digit and has_special:
|
54 |
+
return True
|
55 |
+
return False
|
56 |
+
|
57 |
+
def crack_passwords(self, passwords):
|
58 |
+
for password in passwords:
|
59 |
+
self.total_passwords += 1
|
60 |
+
if self.crack_hash(password):
|
61 |
+
break
|
62 |
+
|
63 |
+
def crack_passwords_parallel(self, passwords):
|
64 |
+
pool = Pool()
|
65 |
+
pool.map(self.crack_password, passwords)
|
66 |
+
pool.close()
|
67 |
+
|
68 |
+
def crack_password(self, password):
|
69 |
+
if self.complexity_check and not self.evaluate_complexity(password):
|
70 |
+
return
|
71 |
+
if self.matched_password is None:
|
72 |
+
if self.crack_hash(password):
|
73 |
+
return
|
74 |
+
|
75 |
+
def crack_passwords_with_wordlist(self):
|
76 |
+
with open(self.wordlist_file, 'r', encoding="latin-1") as wordlist:
|
77 |
+
passwords = wordlist.read().splitlines()
|
78 |
+
if self.parallel:
|
79 |
+
self.crack_passwords_parallel(passwords)
|
80 |
+
else:
|
81 |
+
self.crack_passwords(passwords)
|
82 |
+
|
83 |
+
def crack_passwords_with_brute_force(self, min_length, max_length, character_set):
|
84 |
+
passwords = self.generate_passwords(min_length, max_length, character_set)
|
85 |
+
if self.parallel:
|
86 |
+
self.crack_passwords_parallel(passwords)
|
87 |
+
else:
|
88 |
+
self.crack_passwords(passwords)
|
89 |
+
|
90 |
+
def print_statistics(self):
|
91 |
+
print(f"Total Number of Passwords Tried: {self.total_passwords}")
|
92 |
+
if self.matched_password:
|
93 |
+
print(f"Password Cracked! Password: {self.matched_password}")
|
94 |
+
else:
|
95 |
+
print("Password Failed.")
|
96 |
+
|
97 |
+
|
98 |
+
def main():
|
99 |
+
parser = argparse.ArgumentParser(description='Password cracking source PassBreaker')
|
100 |
+
parser.add_argument('password_hash', help='Password hash')
|
101 |
+
parser.add_argument('wordlist_file', help='Wordlist File')
|
102 |
+
parser.add_argument('--algorithm', choices=hashlib.algorithms_guaranteed, required=True, help='Hash algorithm')
|
103 |
+
parser.add_argument('-s', '--salt', help='Salt Value')
|
104 |
+
parser.add_argument('-p', '--parallel', action='store_true', help='Use parallel processing')
|
105 |
+
parser.add_argument('-c', '--complexity', action='store_true', help='Check for password complexity')
|
106 |
+
parser.add_argument('-b', '--brute-force', action='store_true', help='Perform a brute force attack')
|
107 |
+
parser.add_argument('--min-length', type=int, default=1, help='Minimum password length for brute force attack')
|
108 |
+
parser.add_argument('--max-length', type=int, default=6, help='Minimum password length for brute force attack')
|
109 |
+
parser.add_argument('--character-set', default='abcdefghijklmnopqrstuvwxyz0123456789',
|
110 |
+
help='Character set for brute force attack')
|
111 |
+
|
112 |
+
args = parser.parse_args()
|
113 |
+
|
114 |
+
cracker = PasswordCracker(args.password_hash, args.wordlist_file, args.algorithm, args.salt, args.parallel, args.complexity)
|
115 |
+
|
116 |
+
if args.brute_force:
|
117 |
+
cracker.crack_passwords_with_brute_force(args.min_length, args.max_length, args.character_set)
|
118 |
+
else:
|
119 |
+
cracker.crack_passwords_with_wordlist()
|
120 |
+
|
121 |
+
cracker.print_statistics()
|
122 |
+
|
123 |
+
|
124 |
+
if __name__ == '__main__':
|
125 |
+
main()
|