首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Python破解ZIP或RAR文件密码

Python破解ZIP或RAR文件密码

作者头像
Python小屋屋主
发布2018-04-16 14:30:27
发布2018-04-16 14:30:27
5.1K0
举报
文章被收录于专栏:Python小屋Python小屋

基本原理在于Python标准库zipfile和扩展库unrar提供的解压缩方法extractall()可以指定密码,这样的话首先(手动或用程序)生成一个字典,然后依次尝试其中的密码,如果能够正常解压缩则表示密码正确。

import os

import sys

#zipfile是Python标准库

import zipfile

#尝试导入扩展库unrar,如果没有就临时安装

try:

from unrar import rarfile

except:

path = '"'+os.path.dirname(sys.executable)+'\\scripts\\pip" install --upgrade pip'

os.system(path)

path = '"'+os.path.dirname(sys.executable)+'\\scripts\\pip" install unrar'

os.system(path)

from unrar import rarfile

def decryptRarZipFile(filename):

#根据文件扩展名,使用不同的库

if filename.endswith('.zip'):

fp = zipfile.ZipFile(filename)

elif filename.endswith('.rar'):

fp = rarfile.RarFile(filename)

#解压缩的目标文件夹

desPath = filename[:-4]

if not os.path.exists(desPath):

os.mkdir(desPath)

#先尝试不用密码解压缩,如果成功则表示压缩文件没有密码

try:

fp.extractall(desPath)

fp.close()

print('No password')

return

#使用密码字典进行暴力破解

except:

try:

fpPwd = open('pwddict.txt')

except:

print('No dict file pwddict.txt in current directory.')

return

for pwd in fpPwd:

pwd = pwd.rstrip()

try:

if filename.endswith('.zip'):

for file in fp.namelist():

#对zip文件需要重新编码再解码,避免中文乱码

fp.extract(file, path=desPath, pwd=pwd.encode())

os.rename(desPath+'\\'+file, desPath+'\\'+file.encode('cp437').decode('gbk'))

print('Success! ====>'+pwd)

fp.close()

break

elif filename.endswith('.rar'):

fp.extractall(path=desPath, pwd=pwd)

print('Success! ====>'+pwd)

fp.close()

break

except:

pass

fpPwd.close()

if __name__ == '__main__':

filename = sys.argv[1]

if os.path.isfile(filename) and filename.endswith(('.zip', '.rar')):

decryptRarZipFile(filename)

else:

print('Must be Rar or Zip file')

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2016-09-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python小屋 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档