直接上代码:
import zipfile
import os
import string
symbols = string.punctuation # 通过string.punctuation获取所有的字符 如:'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
number = string.digits # 通过string.digits 获取所有的数字的字符串 如:'0123456789'
Letter = string.ascii_letters # 通过string.ascii_letters 获取所有因为字符的大小写字符串 'abc....zABC.....Z'
# passwd = symbols + number + Letter # 定义生成密码是组成密码元素的范围范例 字符+数字+大小写字母
passwd = number # 定义生成密码是组成密码元素的范围
words = str(passwd) # 将列表变为文字
def get_password(min_digits, max_digits, words):
"""
密码生成器
:param min_digits: 密码最小长度
:param max_digits: 密码最大长度
:param words: 密码可能涉及的字符
:return: 密码生成器
"""
import itertools as its
while min_digits <= max_digits:
pwds = its.product(words, repeat=min_digits)
for pwd in pwds:
yield ''.join(pwd)
min_digits += 1
return
key = get_password(6,6,words) #设定密码位数
list_key = []
for i in key:
list_key.append(i) #将迭代器转化为序列密码字典
#path = 'D:\\压缩包\\' #待破解文件夹示例
path = 'D:\\压缩包\\' #待破解文件夹
for i in os.listdir(path):
un_file = path + i
if un_file[:-5:-1] == '.zip'[:-5:-1]:
print('正在处理压缩包:',un_file)
for x in list_key:
f = zipfile.ZipFile(un_file)
try:
#print('try key:"{}"'.format(x))
f.extractall(path=path + '\destDir',pwd=str(x).encode())
print('Find password is "{}"'.format(x))
break
except Exception as e:
#print(i,'password error')
pass
else:
pass
基本原理是:
1、利用string生成自己要的密码范围,例如纯数字、字符、大小写英文。
2、然后用itertools生成迭代器,因为要批量文件,所以也要做转换成序列来处理。
3、最后使用zipfile模块来批量尝试密码。OS模块来批量获取文件路径。
执行效率比较低,初学者不是很懂,仅供笔记参考。