# -*- coding:utf-8 -*- import zipfile import os import string import random class crackzip(): def __init__(self, low=4, up=6): self.lownum = low self.upnum = up # 构造密码的字符串 self.model = string.ascii_lowercase + '0123456789' # ZIP文件位置 self.in_path = "C:\Users\10245\Desktop\test3.zip" # 解压位置 self.out_path = "C:\Users\10245\Desktop\re\" def __iter__(self): return self def __next__(self): s = '' for i in range(random.randrange(self.lownum, self.upnum)): s += random.choice(self.model) return s def dict_zip(self): # 字典文档位置 dic = "C:\Users\10245\Desktop\dict.txt" m = zipfile.ZipFile(self.in_path) names = [m.filelist[x].filename for x in range(len(m.filelist))] corr_name = [] for name in names: corr_name.append(name.encode('cp437').decode('gbk')) with open(dic) as f: passwords = f.readlines() for password in passwords: try: password = password.strip() m.extractall(path=self.out_path, pwd=password.encode('utf-8')) for n in range(len(names)): os.rename(self.out_path + names[n], self.out_path + corr_name[n]) print ('[+][Success]文件:{0} 密码:{1}'.format(corr_name, password)) exit(0) except Exception as e: print ('[-][Error]文件:{0} 密码:{1}'.format(corr_name, password)) def str_zip(self): m = zipfile.ZipFile(self.in_path) # zip中的文件 names = [m.filelist[x].filename for x in range(len(m.filelist))] corr_name = [] sec_num1, sec_num2 = map(int, input('请输入密码位数范围:').split()) for name in names: corr_name.append(name.encode('cp437').decode('gbk')) for password in crackzip(sec_num1, sec_num2): try: password = password.strip() m.extractall(path=self.out_path, pwd=password.encode('utf-8')) for n in range(len(names)): os.rename(self.out_path + names[n], self.out_path + corr_name[n]) print ('[+][Success]文件:{0} 密码:{1}'.format(corr_name, password)) exit(0) except Exception as e: print ('[-][Error]文件:{0} 密码:{1}'.format(corr_name, password)) m.close() if __name__ == '__main__': print (''' (1) 字典破解 (2) 常用构造破解 ''') m = crackzip() ch = int(input()) if ch == 1: m.dict_zip() elif ch == 2: m.str_zip()
字典破解,就是尝试常用密码文本中的密码。
暴力破解,使用字符串构造密码,Python性能不咋样,所以没法直接构造全部密码,再一一尝试,用了迭代器,每次随机取值。