【Python】zip文件密码破解

掌握基础语法后,尝试使用python的zipfile模块练手。

zipfile是Python里用来做zip格式编码的压缩和解压缩的。

这里将大体的思路分解成四段代码,逐一完善功能;

第一段代码:解压zip

首先了解python解压zip文件的库

import zipfile

# 定义通用解压函数
def tryZipPwd(zFile,savePath,pw =None):

    # 如果密码是空就直接解压,使用异常判断
    try:
        # 如果密码为空就直接解压
        if pw == None:
            zFile.extractall(path=savePath)
        else:
            # 将密码转换为utf-8编码
            zFile.extractall(path=savePath,pwd=pw.encode('utf-8'))
        print('[+] ZIp文件解压成功,密码:%s' %(pw))
        return True
    except:
        print('[-]Zip文件解压失败,密码:%s' % (pw))
        return False

第二段 解压zip函数的使用

将通用解压zip的函数,传入参数引用就可以了

# 指定密码打开Zip文件,密码是123qwer
#with zipfile.ZipFile('C:/Users/Windows32/Desktop/untitled/sucess.zip') as zFile:
#    tryZipPwd(zFile,'C:/Users/Windows32/Desktop/untitled/','1234qwer')

第三段代码 读取文件内容

python逐行读取文件内容的三种方法;

方法1:

f = open("foo.txt")             # 返回一个文件对象  
line = f.readline()             # 调用文件的 readline()方法  
while line:  
    print line,                 # 后面跟 ',' 将忽略换行符  
    # print(line, end = '')   # 在 Python 3中使用  
    line = f.readline()  

f.close()  

方法2:

for line in open("foo.txt"):  
    print line,  

方法3:

f = open("c:\\1.txt","r")  
lines = f.readlines()#读取全部内容  
for line in lines  
    print line  

第四段代码-读取密码文本,批量传入密码尝试解压zip文件

综合以上的代码就可以实现zip压缩包的密码破解了,pass.txt是CSDN泄露的TOP100常用密码,写了for循环与while循环的代码;

# -*- coding: utf-8 -*-
import zipfile

# 定义通用解压函数
def tryZipPwd(zFile,savePath,pw =None):

    try:
        if pw == None:
            zFile.extractall(path=savePath)
        else:
            zFile.extractall(path=savePath,pwd=pw.encode('utf-8'))
        print('[+] ZIp文件解压成功,密码:%s' %(pw))
        return True
    except:
       # print('[-]Zip文件解压失败,密码:%s' % (pw))
        return False

# 指定密码打开Zip文件
#with zipfile.ZipFile('C:/Users/Windows32/Desktop/untitled/sucess.zip') as zFile:
#    tryZipPwd(zFile,'C:/Users/Windows32/Desktop/untitled/','1234qwer')

# 逐行读取文本里的密码,然后传入通用解压函数中
passFile = open('C:/Users/Windows32/Desktop/untitled/pass1.txt')

# for循环
# 当破解成功后退出程序与关闭文件流
for i in open('C:/Users/Windows32/Desktop/untitled/pass1.txt'):
    # 将文本里的换行清除
    password = i.strip('\n')
    with zipfile.ZipFile('C:/Users/Windows32/Desktop/untitled/sucess.zip') as zFile:
        # 传回函数执行状态,如果返回结果为真,就代表解压zip文件成功,输出当前的密码
        flag = tryZipPwd(zFile,'C:/Users/Windows32/Desktop/untitled/',password)
        if flag:
            print("sucess pass is %s" % (i))
            exit(0)
            passFile.close()
    i = passFile.readline()
passFile.close()


# while循环
line =  passFile.readline()
while line:
    line = line.strip('\n')
    with zipfile.ZipFile('C:/Users/Windows32/Desktop/untitled/sucess.zip') as zFile:
        flag = tryZipPwd(zFile,'C:/Users/Windows32/Desktop/untitled/',line)
        if flag:
            print("sucess pass is %s" % (line))
            exit(0)
            passFile.close()
    line = passFile.readline()
passFile.close()

转载于:https://www.cnblogs.com/17bdw/p/6132540.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值