暴力破解rar和zip加密压缩包

前言

最近老是遇到一些加密的压缩包,还不给密码……其中最过分的就是Adobe Audition CC贴吧吧主分享盗版软件压缩版,还搞收费,交钱才能知道解压密码,这操作真是秀到我了……

话不多说,直接开始尝试暴力破解

源码

###
# Winrar 加密: 源文件压缩成数据段;将数据段加密
# 对于同一个源文件而言,不加密,只压缩获取的数据段是一样的;
# 但加密时,就算密码一致加密完rar文件的数据段是不一样的,这是由于加密的密钥依赖于一个Salt(8字节的密钥,用来加密时使用,存放在rar文件头中)
# 加密流程: 将明文密码与salt一起,通过HASH算法,生成两个16字节的密钥(AES参数和initVector) 循环加密
# 解密流程: 将解密后的数据块进行解压缩,然后解压成源文件,对该文件进行CRC校验,存在rar文件中的源文件CRC校验码比较,相同则密码正确,不相同则密码错误

import threading
from unrar import rarfile
import zipfile
import os
import itertools
import sys
import time
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("file")
parser.add_argument("extract")

def decode_rar(rar_file, pwd, extract_path):
    try:
        rar_file.extractall(extract_path, pwd = pwd)
    except:
        #print('wrong')
        pass
    else:
        print('password is', pwd)
        sys.exit(0)
        
        
def decode_zip(zip_file, pwd, extract_path):
    try:
        zip_file.extractall(extract_path, pwd=pwd)
    except:
        #print('wrong')
        pass
    else:
        print('password is', pwd)
        sys.exit(0)
        
        
        
def create_pwd(words, repeatNum):
    dict = itertools.product(words, repeat = repeatNum)
    with open('./password.txt', 'w') as f:
        for it in dict:
            f.write(''.join(it) + '\n')
        
        
def get_pwd():
    with open('./password.txt','r') as f:
        for pwd in f:
            yield pwd.strip()
        
        
def main(file, extract_path):
    # 创建密码
    if not (os.path.exists('./password.txt')):
        create_pwd('./password.txt')
    pwds = get_pwd()
    
    suffix = os.path.splitext(file)[1][1:]
    if suffix == 'zip':
        zip_file = zipfile.ZipFile(file, 'r')
        for pwd in pwds:
            t = threading.Thread(target=decode_zip, kwargs={'zip_file': zip_file, 'pwd': pwd, 'extract_path': extract_path})
            t.start()
            
    elif suffix == 'rar':
        rar_file = rarfile.RarFile(file)
        for pwd in pwds:
            t = threading.Thread(target=decode_rar, kwargs={'rar_file': rar_file, 'pwd': pwd, 'extract_path': extract_path})
            t.start()
            
    else:
        print("暂不支持该格式")
    
if __name__ == '__main__':
    args = parser.parse_args()
    start_time = time.time()
    
    #密码创建 默认为4位纯数字
    create_pwd('1234567890', 4)
    
    print('create password time: %fs' % (time.time() - start_time))
    main(args.file, args.extract)
    print('run time: %fs' % (time.time() - start_time))

使用方法

python myunrar.py 压缩文件路径 解压路径

另外,密码需要自己改。
改create_pwd(‘1234567890’, 4)就好了, 第一个参数为密码的字符构成,第二个参数为密码位数,这是做排列组合的,因此字符构成中不需要出现重复字符。

感想

遇到加密者用中文加密,或者位数很长的密码,着实难以破解,毕竟现在只能通过遍历密码尝试暴力破解。

或许了解了加密原理,可以找到巧妙的破解方法,降低时间复杂度O(N)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值