python怎么破解压缩包密码

发布时间:2022-05-15 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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')

更多Python相关技文章,请访问Python教程栏目进行学习!

以上就是python怎么破解压缩包密码的详细内容,更多请关注脚本宝典其它相关文章!

脚本宝典总结

以上是脚本宝典为你收集整理的python怎么破解压缩包密码全部内容,希望文章能够帮你解决python怎么破解压缩包密码所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: