-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathZCracker.py
221 lines (175 loc) · 9.58 KB
/
ZCracker.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#ZIP password cracker written in python by MCoury AKA python-scripter and 2u6z3r0
import zipfile as zf
from unrar import rarfile as rf
import pickle
from itertools import product
import os
import sys
class CrackZip:
'''a CrackZip object
Use: mission = ZCracker.CrackZip(path to the file you want to attack,
*optional*path where you want the contents saved)
mission.dict_attack()
or: mission.brute_attack()'''
def __init__(self, file_path=None, target_path=None):
self.file_path = file_path
self.target_path = target_path
self.space_selector = {1: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 2: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 3: 'abcdefghijklmnopqrstuvwxyz', 4: '0123456789', 5: '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 6: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 7: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 8: 'abcdefghijklmnopqrstuvwxyz0123456789', 9: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 10: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 11: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 12: 'abcdefghijklmnopqrstuvwxyz!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 13: '0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 14: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 15: 'abcdefghijklmnopqrstuvwxyz0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'}
def dict_attack(self, dict_path=r'pass_dict.pickle', save=True):
'''Starts a dictionary attack on the target file
takes 2 optional arguments, dict_path,
which allows th user to look in a custom dictionary,
and save defaults to True and if set to False,
the found password won't be saved as a txt file'''
if not (save == True or save == False):
raise ValueError ('save must be either True or False!')
with open(r'' + dict_path, 'rb') as dict_f:
content = pickle.load(dict_f)
z = zf.ZipFile(self.file_path)
found = []
print('Working...')
for pas in content:
b = str.encode(pas)
try:
if self.target_path:
z.extractall(path=self.target_path, pwd=b)
else:
z.extractall(pwd=b)
except Exception:
pass
else:
found.append(pas)
break
if found:
if save:
with open(self.file_path + r'_pass.txt', 'w') as pfile:
pfile.write(pas)
return found[0]
if not found:
return None
def brute_attack(self, min_length=4, max_length=8, space_n=3, save=True):
'''Starts a Brute-Force attack on the target file
takes 3 optional arguments,
min_length=4 the minimum length of the password,
max_length=8 the maximum length of the password,
space_n=3 a number related to the characters of the password,
call the selector() method for more details on space_n argument,
and save defaults to True and if set to False,
the found password won't be saved as a txt file'''
if not (save == True or save == False):
raise ValueError ('save must be either True or False!')
space = self.space_selector[space_n]
z = zf.ZipFile(self.file_path)
found = []
print('Working...')
for length in range(min_length, max_length + 1):
attempts = product(space, repeat=length)
for pas in attempts:
pas = ''.join(pas)
b = str.encode(pas)
try:
if self.target_path:
z.extractall(path=self.target_path, pwd=b)
else:
z.extractall(pwd=b)
except Exception:
pass
else:
found.append(pas)
break
if found:
if save:
with open(self.file_path + r'_pass.txt', 'w') as pfile:
pfile.write(pas)
return found[0]
if not found:
return None
class CrackRar:
def __init__(self, file_path=None, target_path=None):
self.file_path = file_path
self.target_path = target_path
self.space_selector = {1: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 2: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 3: 'abcdefghijklmnopqrstuvwxyz', 4: '0123456789', 5: '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 6: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 7: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 8: 'abcdefghijklmnopqrstuvwxyz0123456789', 9: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 10: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 11: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 12: 'abcdefghijklmnopqrstuvwxyz!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 13: '0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 14: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 15: 'abcdefghijklmnopqrstuvwxyz0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'}
def dict_attack(self, dict_path=r'pass_dict.pickle', save=True):
'''Starts a dictionary attack on the target file
takes 2 optional arguments, dict_path,
which allows th user to look in a custom dictionary,
and save defaults to True and if set to False,
the found password won't be saved as a txt file'''
if not (save == True or save == False):
raise ValueError ('save must be either True or False!')
with open(r'' + dict_path, 'rb') as dict_f:
content = pickle.load(dict_f)
r = rf.RarFile(self.file_path)
found = []
print('Working...')
for pas in content:
try:
if self.target_path:
r.extractall(path=self.target_path, pwd=pas)
else:
r.extractall(pwd=pas)
except Exception:
pass
else:
found.append(pas)
break
if found:
if save:
with open(self.file_path + r'_pass.txt', 'w') as pfile:
pfile.write(pas)
return found[0]
if not found:
return None
def brute_attack(self, min_length=4, max_length=8, space_n=3, save=True):
'''Starts a Brute-Force attack on the target file
takes 3 optional arguments,
min_length=4 the minimum length of the password,
max_length=8 the maximum length of the password,
space_n=3 a number related to the characters of the password,
call the selector() method for more details on space_n argument,
and save defaults to True and if set to False,
the found password won't be saved as a txt file'''
if not (save == True or save == False):
raise ValueError ('save must be either True or False!')
space = self.space_selector[space_n]
r = rf.RarFile(self.file_path)
found = []
print('Working...')
for length in range(min_length, max_length + 1):
attempts = product(space, repeat=length)
for pas in attempts:
pas = ''.join(pas)
try:
if self.target_path:
r.extractall(path=self.target_path, pwd=pas)
else:
r.extractall(pwd=pas)
except Exception:
pass
else:
found.append(pas)
break
if found:
if save:
with open(self.file_path + r'_pass.txt', 'w') as pfile:
pfile.write(pas)
return found[0]
if not found:
return None
def selector():
'''Lets the user take a look at the characters
used in brute-force attacks'''
return {1: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 2: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 3: 'abcdefghijklmnopqrstuvwxyz', 4: '0123456789', 5: '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 6: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 7: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 8: 'abcdefghijklmnopqrstuvwxyz0123456789', 9: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 10: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 11: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 12: 'abcdefghijklmnopqrstuvwxyz!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 13: '0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 14: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 15: 'abcdefghijklmnopqrstuvwxyz0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'}
def gen_dict(txt_path=None, target_name='pass_dict2.pickle', separator='\n'):
'''For performance related reasons, this module requires the dictionary list
to be saved in a pickle file. Use this function to convert a .txt password
dictionary to pickle'''
if os.path.exists(r'' + target_name):
sys.exit('Target file already exists! Rename or move it to avoid losing your data...')
with open(r'' + txt_path) as src:
pwds = src.readlines()
for i in range(len(pwds)):
pwds[i] = pwds[i].strip(separator)
with open(r'' + target_name, 'wb') as tgt:
pickle.dump(pwds, tgt)
return 'The given dictionary has been saved as pickle successfully!'