用python暴力破解rar加密文件(经过测试)

参考文章1
参考文章2

第一次使用csdn写文章,写得不好还请见谅。(运行环境:python3.6)

下了一个带密码的压缩包文件,作为一个刚学python的新手,想着能不能用python暴力破解它,于是在网上搜了很多资料,看着似乎并不是很麻烦,也想试着自己写一个可以暴力破解的程序,在写的过程中却遇到了各种各样的问题,希望大手们能带带我。遇到的问题如下:

  • zipfile和zipfile2似乎都不支持AES解密(https://bugs.python.org/issue9170)
  • 在用rarfile暴力破解时即使密码错误也不抛出异常,因此无法用try,except捕获密码

本来是想写一个可以同时暴力破解zip和rar的程序,在试了半天解密zip却一直提示密码错误之后放弃了zip,想着能不能写一个暴力破解rar的程序。

首先是生成字典:要用到itertools模块

import itertools as its
import string

def createDict(path,repeats,words):
	dict = its.product(words,repeat=repeats) 
	'''这里的words是要迭代的字符串,repeats是生成的密码长度,生成的dict是一个返回元组的迭代器'''
	f = open(path,'a')
	for cipher in dict:
		f.write(''.join(cipher) + '\n')
	f.close()

def main():
	numbers = string.digits  #包含0-9的字符串
	path = '输入你的字典路径'
	length = 你要迭代的密码长度
	for i in range(1,length):
		createDict(path,i,numbers)

if __name__=="__main__":
	main()

到这里我们的字典已经生成完毕了,接下来开始暴力破解rar

from threading import Thread
from unrar import rarfile
import os

'''首先我们要读取字典,字典可能太大因此我们采用迭代器'''

def get_pwd(dict_path):
	with open(dict_path,'r') as f:
		for pwd in f:
			yield pwd.strip()

def decode_rar(fp,pwd,extract_path):
	try:
		fp.extractall(extract_path,pwd=pwd) 
	except:
		pass
	else:
		print('the pwd is>',pwd)
'''
事实上我在尝试时似乎从来没有到达过else,这样可能是得不到解压密码的。我的
一种得到密码的想法如下,但是运行效率可能会降低


def decode_rar(fp,pwd,check_file,extract_path):
	fp.extractall(extract_path,pwd=pwd)
	if os.path.exists(check_file):
		print('The pwd is:',pwd)
		exit(0)
其中check_file可以设置为fp.namelist()[0]
并且该方法不能使用多线程,因此速度会降低很多
'''

def main():
	extract_path = '你要解压的路径'
	dict_path = '你的字典路径'
	filename = '你的rar路径'
	fp = rarfile.RarFile(filename)
	pwds = get_pwd(dict)
	'''使用多线程可提高速度'''
	for pwd in pwds:
		t = Thread(target
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值