使用Python对zip文件进行破解

使用Python破解zip文件

  1. 使用的主要模块为zipfile模块,该模块可以可以实现对zip文件的操作。
  2. 这里所说的破解是使用密码字典一个一个的进行尝试。
  3. 在windows系统上,使用python2进行编写的。

首先需要一个加密的zip文件,用来对写好的代码进行测试,这里使用7zip对文件压缩加密。
在这里插入图片描述
代码如下:

#coding:utf-8

import zipfile
import threading
import os
import time
import sys
#定义全局变量用来判断是否需要停止线程
flag = True
#破解密码函数
#在打开zip文件时如果密码错误会出现异常,我们可以根据这个来判断密码是否正确,密码错误出现异常不做任何处理,密码正确则输出正确密码
def pj(zfile,password):
try :
zfile.extractall(path=‘C:\Users\ASUS-PC\Desktop\’,pwd=password)
print(“password is :”+str(password))
global flag
flag = False
except :
pass

def main():
#num用来计算一共测试了多少次密码
num=0
#读取zip文件并判断改文件是否存在
file=raw_input(“zipfile:”)
if not(os.path.exists(file)):
print(‘zip文件不存在’.decode(‘utf-8’))
sys.exit()
zfile=zipfile.ZipFile(file)
#读取密码字典并判断文件是否存在
passwordfile=raw_input(“passwordfile:”)
if not(os.path.exists(passwordfile)):
print(‘password文件不存在’.decode(‘utf-8’))
sys.exit()
passwordfile=open(passwordfile,‘r’)
#开始时间
starttime=time.time()
print(“start time :”+str(starttime))
#使用多线程进行破解,破解成功后使用break结束循环不在进行后面的测试
for f in passwordfile.readlines():
password=f.strip()
if flag is True:
t=threading.Thread(target=pj,args=(zfile,password))
t.start()
t.join()
num=num+1
else:
break
#输出所需要的内容包括开始时间,结束时间,尝试的次数以及所花费的时间
print(“number is :”+str(num))
fintime=time.time()
print(“fin time :”+str(fintime))
print(“need time :”+str(fintime-starttime))

if name==‘main’:
main()

代码写好后对代码进行测试,这里所用的密码字典比较小只有1000多个密码
测试结果如图
在这里插入图片描述
程序写的比较简单希望各位大佬多多指点一下。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值