-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathZipCracker.py
166 lines (159 loc) · 5.19 KB
/
ZipCracker.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
import zipfile, itertools, sys
"""Character set is created."""
charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ~`!@#$%^&*()-_=+[{]}\|;:'\"/?.>,<"
minRep = 1
maxRep = 5
"""It generates the passwords."""
def genPassword(zfile):
back = '.' + zfile +'.data'
for x in xrange(minRep, maxRep + 1):
f = open(back,'w')
f.write(str(x))
f.close()
for l in itertools.product(charset,repeat = x):
yield ''.join(l)
"""extract all the files from the zip"""
def extract(zfile,password):
try:
zf = zipfile.ZipFile(zfile)
files = zf.infolist()
zf.read(files[0],pwd = password)
zf.close()
return True
except KeyboardInterrupt:
exit(0)
except:
return False
"""Function for bruteforce attack."""
def bruteforce(zfile):
back = '.' + zfile +'.data'
password = ''
try:
f = open(back,'r')
data = f.readline().strip()
if 'pwd' == data[:3]:
password = data[4:]
return password
else:
global minRep
minRep = int(data)
f.close()
except:
pass
try:
zf = zipfile.ZipFile(zfile)
files = zf.infolist()
data = zf.read(files[0],pwd = password)
zf.close()
flag = True
except KeyboardInterrupt:
exit(0)
except:
flag = False
if not flag:
for i in genPassword(zfile):
try:
flag = extract(zfile, i)
if flag:
password = i
break
except KeyboardInterrupt:
exit(0)
if not flag:
print 'Password Not Found!!!'
exit(0)
f = open(back,'w')
f.write('pwd:' + password)
f.close()
return password
"""Function for dictionary attack."""
def dictionary(zfile, dic):
back = '.' + zfile +'.data'
password = ''
try:
f = open(back,'r')
data = f.readline().strip()
if 'pwd' == data[:3]:
password = data[4:]
return password
else:
start = int(data)
f.close()
except:
start = 1
dictionaryFile = open(dic,'r')
try:
zf = zipfile.ZipFile(zfile)
files = zf.infolist()
data = zf.read(files[0],pwd = password)
zf.close()
flag = True
except KeyboardInterrupt:
exit(0)
except:
flag = False
if not flag:
dlist = dictionaryFile.readlines()
for i in xrange(start,len(dlist)):
try:
flag = extract(zfile, dlist[i])
if flag:
password = dlist[i]
break
except KeyboardInterrupt:
exit(0)
if not flag:
print 'Password Not Found!!!'
exit(0)
f = open(back,'w')
f.write('pwd:' + password)
f.close()
return password
if __name__ == '__main__':
arg = sys.argv[1:]
if arg[0] == '-h' or arg[0] == '--h' or arg[0] == '-help':
print "usage script.py filename [-b | -ab charset=charset | -m=[2,5] | -d file=dictionary.txt]"
print "option:"
print "-b\t\t\t:Bruteforce attack."
print "-ab -file=charset\t:Advance Bruteforce attack from the given charset."
print "-d file=dict.txt\t:Dictionary attack. Provide dictionary name in present working directory"
print "-m=[min, max]\t\t:Minimum and Maximum length of passwords to be consider."
exit(0)
elif arg[1] == '-b':
print 'Processing...'
if len(arg) > 2:
length = arg[2].split('=')[1].split(',')
minRep = int(length[0][1:])
maxRep = int(length[1][:-1])
password = bruteforce(arg[0])
elif arg[1] == '-ab':
print 'Processing...'
if len(arg) > 3:
length = arg[3].split('=')[1].split(',')
minRep = int(length[0][1:])
maxRep = int(length[1][:-1])
try:
charset = arg[2].split('=')[1]
except:
print "usage script.py filename [-b | -ab charset=charset | -m=[2,5] | -d file=dictionary.txt]"
print "option:"
print "-b\t\t\t:Bruteforce attack."
print "-ab -file=charset\t:Advance Bruteforce attack from the given charset."
print "-d file=dict.txt\t:Dictionary attack. Provide dictionary name in present working directory"
print "-m=[min, max]\t\t:Minimum and Maximum length of passwords to be consider."
exit(0)
password = bruteforce(arg[0])
elif arg[1] == '-d':
print 'Processing...'
fl = arg[2].split('=')[1]
password = dictionary(arg[0],fl)
else:
print "Invalid input"
print "usage script.py filename [-b | -ab charset=charset | -m=[2,5] | -d file=dictionary.txt]"
print "option:"
print "-b\t\t\t:Bruteforce attack."
print "-ab -file=charset\t:Advance Bruteforce attack from the given charset."
print "-d file=dict.txt\t:Dictionary attack. Provide dictionary name in present working directory"
print "-m=[min, max]\t\t:Minimum and Maximum length of passwords to be consider."
exit(0)
print 'Password is "%s" without quotes.' %password