I'm new to python so below is a (poorly written) script which aims to find the password of a zip using a kind of mix between "brute force" and "dictionnary".
ex: "thisisastringdictionnary" => dictionnary; password=>"astring" the script will test any possible subschain within a certain limit (for example 500 caracters).
The script below works fine but it's very slow. That's why I would like to use pool/multiprocessing.
script without multiprocessing (working):
import zipfile
import thread
import sys
zipfilename = 'toopen.zip'
dictionary = 'dictionnary.txt'
zip_file = zipfile.ZipFile(zipfilename)
def openZip(sub, start, stringLen):
try:
zip_file.extractall(pwd=sub)
sub = 'Password found: %s' % sub
print sub
sys.exit(0)
except SystemExit:
sys.exit(0)
except:
print str((start/float(stringLen))*100)+"%"
pass
def main():
password = None
zip_file = zipfile.ZipFile(zipfilename)
with open(dictionary, 'r') as f:
for line in f.readlines():
password = line.strip('\n')
for start in range(len(password)):
for index in range(500):
sub = password[start:index+1]
openZip(sub, start, len(password));
if __name__ == '__main__':
main()
With the tries I have done with multiprocessing I encountered several problem:
- the script won't stop/exit when the password is found
- the printing inside the try catch displays weirdly (like every process is printing with no order) => So the progress indicator is not workign anymore
- I'm not even sure I'm doing this right :/
Below my try:
import zipfile
import thread
import sys
from multiprocessing import Pool
zipfilename = 'toopen.zip'
dictionary = 'dictionnary.txt'
zip_file = zipfile.ZipFile(zipfilename)
def openZip(sub):
try:
zip_file.extractall(pwd=sub[0])
sub = 'Password found: %s' % sub[0]
print sub[0]
sys.exit(0)
except SystemExit:
sys.exit(0)
except:
print str((sub[1]/float(sub[2]))*100)+"%"
pass
def main():
p = Pool(4)
password = None
zip_file = zipfile.ZipFile(zipfilename)
with open(dictionary, 'r') as f:
for line in f.readlines():
password = line.strip('\n')
pwdList = []
for start in range(len(password)):
for index in range(500):
sub = password[start:index+1]
pwdList.append([sub, start, len(password)])
p.map(openZip, pwdList)
if __name__ == '__main__':
main()
I'm probably missing something trivial but I'm having a hard time to catch the way to use multiprocessing properly. Any help would be greatly appreciated. :)