700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Python实战-暴力破解zip文件解压密码

Python实战-暴力破解zip文件解压密码

时间:2021-06-23 09:52:43

相关推荐

Python实战-暴力破解zip文件解压密码

简介

使用的核心模块是python标准库中的zipfile模块。这个模块可以实现zip文件的各种功能,具体可以查看官方参考文档。这里的暴力破解的意思是对密码可能序列中的值一个一个进行密码尝试,这对人来说是很难的,可是对计算机而言并不难。有时候我们下载的zip文件需要密码解压而我们不知道,需要付费才知道。所有这里主要介绍两种暴力破解的密码:纯数字密码和英文数字组合密码。

文件创建

首先测试文件为test.txt(仅包含单行文本),压缩后文件为test.zip,压缩密码为2340,压缩后删除目录下的txt文件。。

上图注意勾选传统加密。

纯数字密码

指的是不用0开头的数字密码,0开头见后面的字母组合。原理就是zipfile模块解压压缩文件时,一旦密码不正确,程序会终止,在try语句只有成功解压的密码才会执行到extract函数调用后面的语句。

代码:

import zipfileimport timeimport threadingstartTime = time.time()# 判断线程是否需要终止flag = Truedef extract(password, file):try:password = str(password)file.extractall(path='.', pwd=password.encode('utf-8'))print("the password is {}".format(password))nowTime = time.time()print("spend time is {}".format(nowTime-startTime))global flag# 成功解压其余线程终止flag = Falseexcept Exception as e:print(e)def do_main():zfile = zipfile.ZipFile("test.zip", 'r')# 开始尝试for number in range(1, 9999):if flag is True:t = threading.Thread(target=extract, args=(number, zfile))t.start()t.join()if __name__ == '__main__':do_main()

显然,解压成功,这里提一下这种编码 密码的方式只适用于传统zip加密,winrar有一种新式的默认加密方式,是不可以的。

字母数字混合密码

这里情况密码组合太多,为了防止内存溢出,改用迭代器。这种情况费时很久,可以闲来无事挂着脚本。这里再次压缩文件,密码为python。

import zipfileimport randomimport timeimport sysclass MyIterator():# 单位字符集合letters = 'abcdefghijklmnopqrstuvwxyz012345678'min_digits = 0max_digits = 0def __init__(self, min_digits, max_digits):# 实例化对象时给出密码位数范围,一般4到10位if min_digits < max_digits:self.min_digits = min_digitsself.max_digits = max_digitselse:self.min_digits = max_digitsself.max_digits = min_digits# 迭代器访问定义def __iter__(self):return selfdef __next__(self):rst = str()for item in range(0, random.randrange(self.min_digits, self.max_digits+1)):rst += random.choice(MyIterator.letters)return rstdef extract():start_time = time.time()zfile = zipfile.ZipFile("test.zip")for p in MyIterator(5, 6):try:zfile.extractall(path=".", pwd=str(p).encode('utf-8'))print("the password is {}".format(p))now_time = time.time()print("spend time is {}".format(now_time-start_time))sys.exit(0)except Exception as e:passif __name__ == '__main__':extract()

字符的序列组合很多,需要等待。

补充说明

很多人反馈这个方法无效,这主要是因为文件路径不正确(我的代码是默认该Python脚本所在目录下的zip文件进行解压的)和加密方式并非传统加密。这种暴力破解方法只在自己大致记得密码位数和密码格式(如只有字母等)时比较实用,完全的暴力破解是不现实的,毕竟做加密的也不是白做的。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。