掌握基础语法后,尝试使用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()

【Python】zip文件密码破解的更多相关文章

  1. python zip文件密码爆破

    #!/usr/bin/env # coding=UTF-8 import zipfile import threading import os import sys class CrackZip: d ...

  2. 有个计算机专业的学妹问我:我这个zip文件密码破解运行起来为什么内存爆了?

    1.这篇博文的由来 2.跑下错误代码,找病根 先把学妹发给我的错误代码放上,能发现他为了提高速度加了多线程的代码,很聪明哦: import zipfile import itertools from ...

  3. Python与Hack之Zip文件口令破解

    1.需要的库: **import zipfile**import optparse**from threading import Thread(1)zipfile:1.1 zipfile.ZipFil ...

  4. 12.持久性后门----Ettercap之ARP中毒----RAR/ZIP & linux密码破解----kali上检测rootkits

    持久性后门 生成PHP shell weevely generate 密码 /root/Desktop/404.php 靶机IP/404.php weevely http://192.168.1.10 ...

  5. 暴力破解ZIP文件密码

    Python 的标准库提供了 ZIP 文件的提取压缩模块 zipfile,现在让我们试着用这个模块,暴力破解出加密的 ZIP 文件!我们可以用 extractall()这个函数抽取文件,密码正确则返回 ...

  6. 【pyhon】黑客用字典暴力破解Zip文件密码原理性展示

    基本原理:用程序把字典文件里拟定好的密码一个个提取出来,去测试zip能否打开 字典文件pass.txt内容: 1224 2121 asdf abcd1234 dwsdsd dssds 程序代码: # ...

  7. zip压缩包密码破解

    有一种破解方法叫做Known plaintext attack.市面上的密码破解软件几乎都带有这个功能.操作方法就是找到加密压缩包中的任意一个文件,用同样的压缩软件同样的压缩方式压缩成一个不加密的包, ...

  8. office文件密码破解方法及软件

    今天会用到3个软件 1.Office Password Remover 说明:这个软件可以很快破解.doc   .xls的密码 使用方法:参考百度经验里面的文章http://jingyan.baidu ...

  9. python zip文件压缩和解压

    压缩 import shutil zipOutputName = "1234" # 输出1234.zip fileType = "zip" # 文件类型zip ...

随机推荐

  1. 自学Zabbix12.3 Zabbix命令-zabbix_agentd

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix12.3 Zabbix命令-zabbix_agentd 1. zabbix_age ...

  2. 【BZOJ1816】[CQOI2010]扑克牌(二分,贪心)

    [BZOJ1816][CQOI2010]扑克牌(二分,贪心) 题面 BZOJ 题解 看了一眼这题,怎么这么眼熟?woc,原来\(xzy\)的题目是搬的这道啊... 行,反正我考的时候也切了,这数据范围 ...

  3. 洛谷 P1984 [SDOI2008]烧水问题 解题报告

    P1984 [SDOI2008]烧水问题 题目描述 把总质量为1kg的水分装在n个杯子里,每杯水的质量均为(1/n)kg,初始温度均为0℃.现需要把每一杯水都烧开.我们可以对任意一杯水进行加热.把一杯 ...

  4. 洛谷 P3225 [HNOI2012]矿场搭建 解题报告

    P3225 [HNOI2012]矿场搭建 题目描述 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤 ...

  5. poj1236/luogu2746 Network of Schools (tarjan)

    tarjan缩点后,第一问答案显然是入度为零的点得个数第二问:考虑到 没有入度或出度为0的点 的图强连通, 所以答案就是max{入度为零的个数,出度为零的个数} (把出度为零的连到入度为零的点,然后剩 ...

  6. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

  7. 走进Java中的持有对象(容器类)【二】Collection

    概述 通过前文的学习,我们对容器的分类及常用容器类的作用有了基本的认识.本文将针对Collection容器的功能与使用进行细致分析. 基本操作 Collection集合抽象出的目的是为存放独立元素的序 ...

  8. python - how to sort

    python - how to sort overview Key function (★★★★★) OPerator module functions asc and desc 升序和降序 Over ...

  9. How the Bitcoin protocol actually works

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  10. GoLang基础数据类型--->数组(array)详解

    GoLang基础数据类型--->数组(array)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Golang数组简介 数组是Go语言编程中最常用的数据结构之一.顾名 ...