在日常工作生活中,经常用到压缩文件,有些为了安全保密,还专门设置了密码,如果忘记密码要怎么破,这时暴力破解就派上了用场,本文以一个简单的小例子,简述如何通过Python中的zipfile模块进行破解,仅供学习分享使用,如有不足之处,还请指正。
准备工作
在本例中,首先准备一个带密码的zip压缩包,采用winrar进行压缩,如下所示:
设置zip文件密码,如下所示:
注意:一定要采用zip传统加密,否则python的zipfile模块将无法解压成功。
破解步骤
1. 下载密码字典
本例采用密码字典+多线程方式进行破解,首先需要下载密码字典,密码字典包含常用的密码,有多个文件,所有需要采用多线程方式,以提高破解效率。密码字典格式如下:
2. 导入模块文件
需要导入zipfile模块,及多线程相关和文件目录相关模块,如下所示:
1 import zipfile 2 time 3 threading 4 os.path 5 import os
3. 单个密码解压函数
通过zipfile对象的extractall可以进行解压,解压成功,则停止;否则,继续。如下所示:
1 def extract(self,file,password): 2 try: 3 self.threadLock.acquire() 4 if self.is_running: 5 # password = str(password) 6 zfile = zipfile.ZipFile(file,mode='r') 7 zfile.extractall(path=".",pwd=password.encode(encoding=utf-8)) 8 print(the password is {}".format(password)) 9 end_time = time.time() 10 the end time is {}'.format(time.strftime(%Y-%m-%d %H:%M:%S,time.localtime(time.time())))) 11 spend time is {}".format(end_time - self.start_time)) 12 成功解压其余线程终止 13 self.is_running = False 14 zfile.close() 15 except Exception as e: 16 尝试密码:{},不对17 print(e) 18 finally19 self.threadLock.release()
4. 遍历单个密码字典文件
遍历单个密码字典文件,并调用解压函数进行解压,如下所示:
single_pwd_file(self,root,pwd_file): 2 """单个密码本破解""" 3 file = os.path.abspath(django.zip 4 print(file) 5 pwd_file = os.path.abspath(os.path.join(root,pwd_file)) 6 遍历{}文件.format(pwd_file)) 7 8 with open(pwd_file,1)">',encoding=) as f: 9 pwd = f.readline() while pwd: 11 12 self.extract(file,pwd.strip()) 13 else14 break 15 pwd =16 pass
5. 遍历所有密码字典文件
遍历所有密码字典文件,每一个字典文件,采用一个线程,如下所示:
start(self): 通过密码本破解 3 self.start_time =the start time is {} 5 root = rwpa2pojiezidian" for root,dirs,files in os.walk(root,topdown=True): 7 for pwd_file in files: if pwd_file.endswith(.txt') or pwd_file.endswith(.TXT): 9 10 t = threading.Thread(target=self.single_pwd_file,args=(root,pwd_file,)) 11 t.start() 12 t.join() break
破解示例
本例为了测试,采用的密码比较简单,所以破解比较快,如下所示:
源码
为何一定要勾选传统加密?
默认情况下,WinRAR在CTR模式下使用AES-256加密ZIP存档。虽然AES-256比ZIP 2.0传统加密算法安全得多,但它可能与一些较旧的解压软件不兼容。如果需要与这些工具兼容,可以在密码对话框中启用“ZIP传统加密”选项,或在命令行模式下使用-mezl开关。
Python标准库中的zipfile模块仅支持CRC32加密的zip文件。
一定能暴力破解吗?
采用密码字典文件的方式进行破解,密码字典只是收录了常规的密码,如果加密密码正好不在密码字典文件中,则无法破解。暴力破解,通俗的讲就是逐个密码取尝试,有可能需要破解几天,甚至更长时间,才可能会成功。所以技术理论上可行,但实际上可行性并不高。
备注
苏幕遮·燎沉香
【作者】周邦彦【朝代】宋
燎沉香,消溽暑。鸟雀呼晴,侵晓窥檐语。叶上初阳干宿雨、水面清圆,一一风荷举。
故乡遥,何日去。家住吴门,久作长安旅。五月渔郎相忆否。小楫轻舟,梦入芙蓉浦。