使用Python解压rar文件,暴力破解rar加密文件

使用Python解压rar文件,暴力破解rar加密文件

 在Python的环境中,可以设置数字,小写字母,大写字母,特殊符号来进行破解。

网上寻求了很久Python暴力破解rar文件的代码,始终无法成功。参考了上面这个链接的做了写修改,供参考!

使用的插件:unrar,下面详细介绍如何设置。

Windows

下载安装 unrar library,按照默认安装路径安装

下载地址:http://www.rarlab.com/rar/UnRARDLL.exe

 安装后的目录:

 

 

将安装后文件夹中的 X64 文件夹加入环境变量Path

C:\Program Files (x86)\UnrarDLL\x64

 

 

 

 新建系统变量UNRAR_LIB_PATH,注意不是用户变量哦

C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll

 

 

 

CentOS7

下载地址

https://www.rarlab.com/rar_add.htm 
curl -L -o unrarsrc-6.0.7.tar.gz  https://www.rarlab.com/rar/unrarsrc-6.0.7.tar.gz

 

安装依赖

yum install gcc-c++ -y

 

编译安装

tar zxvf unrarsrc-6.0.7.tar.gz -C /usr/local

cd /usr/local/unrar

# 编译库文件
make lib

# 生成libunrar.so 文件
make install-lib

echo "export UNRAR_LIB_PATH=/usr/lib/libunrar.so" >> /etc/profile

source /etc/profile

 

安装python3

yum install python3 -y

 

其他操作步骤

安装 python的unrar 模块 

pip3 install unrar
或者 
pip3 install unrar -i https://pypi.douban.com/simple

 

单进程代码:

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 import itertools as its
 5 import os,sys
 6 from unrar import rarfile
 7 import time
 8 # import rarfile
 9 
10 numbers = '1234567890'
11 lowerLetters = 'abcdefghijklmnopqrstuvwxyz'
12 upperLetters = lowerLetters.upper()
13 symbols = '~!@#$%^&*()_-=+{}[]\\|;:\'",.<>/?`'
14 words = numbers#+lowerLetters+upperLetters+symbols # 涉及到生成密码的参数
15 
16 flag = True  # 是否关闭线程的标志
17 
18 
19 def append_on_file(password):
20     # 把解析出的密码写入到文件中
21     with open('password.txt', 'a', encoding='utf8') as f:
22         text = password + '\n'
23         f.write(text)
24         
25         
26 def get_password(min_digits, max_digits, words):
27     """
28     :param min_digits: 密码最小长度
29     :param max_digits: 密码最大长度
30     :param words: 密码可能涉及的字符
31     :return: 密码生成器
32     """
33     while min_digits <= max_digits:
34         pwds = its.product(words, repeat=min_digits)
35         for pwd in pwds:
36             yield ''.join(pwd)
37         min_digits += 1
38   
39   
40 def extract(file_name):
41     """
42     若线程关闭标志为True,就执行循环,从密码生成器中取出密码,验证密码是否正确
43     密码正确,则把密码写入文件中,并将线程关闭标志flag设定为False,通知其他线程关闭
44     """
45     global flag
46     
47     File = rarfile.RarFile(file_name)
48     
49     while flag:
50         p = next(passwords)
51         try:
52             File.extractall(pwd=p)  # 打开压缩文件,提供密码...
53             flag = False
54             print("password is " + p)  ###破解到密码
55             
56             append_on_file(p)
57             
58             break
59         except:
60             print(p)
61  
62     
63 if __name__=='__main__':
64     filename = 'test.rar'
65     start_time = time.time()
66     passwords = get_password(1, 4, words)
67     extract(filename)
68     end_time = time.time()
69     change_time = end_time - start_time
70     print("破解耗时:{}s".format(change_time))
单进程破解代码

在Python环境下运行,4位全部 数字密码的话,单进程破解大概25秒左右,这要看你电脑的配置啦

 

多线程代码:

  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 
  4 import os
  5 import sys
  6 import multiprocessing
  7 import itertools
  8 import time
  9 
 10 from unrar import rarfile
 11 
 12 numbers = '1234567890'
 13 lowerLetters = 'abcdefghijklmnopqrstuvwxyz'
 14 upperLetters = lowerLetters.upper()
 15 symbols = '~!@#$%^&*()_-=+{}[]\\|;:\'",.<>/?`'
 16 words = numbers#+lowerLetters+upperLetters+symbols # 涉及到生成密码的参数
 17  
 18 filename = 'test.rar'
 19 # filename = r'zh.rar'
 20 File = rarfile.RarFile(filename)
 21 
 22 
 23 index = 0
 24    
 25 def append_on_file(password):
 26     # 把解析出的密码写入到文件中
 27     with open('password.txt', 'a', encoding='utf8') as f:
 28         text = password + '\n'
 29         f.write(text)
 30         
 31         
 32 def get_password(min_digits, max_digits, words):
 33     """
 34     :param min_digits: 密码最小长度
 35     :param max_digits: 密码最大长度
 36     :param words: 密码可能涉及的字符
 37     :return: 密码生成器
 38     """
 39     while min_digits <= max_digits:
 40         pwds = itertools.product(words, repeat=min_digits)
 41         for pwd in pwds:
 42             yield ''.join(pwd)
 43         min_digits += 1
 44   
 45   
 46 def extract(password):
 47  
 48     global index
 49     
 50     index = index + 1
 51     
 52     print(f"尝试第{index}次,密码:{password}")
 53  
 54     
 55     try:
 56         File.extractall(pwd=password)  # 打开压缩文件,提供密码...
 57  
 58         print("password is " + password)  ###破解到密码
 59         
 60         append_on_file(password)
 61         
 62         return True
 63     except Exception as e:
 64         # print(e)
 65         return False
 66 
 67 
 68 class FoundException(Exception): pass
 69 
 70  
 71 if __name__=='__main__':
 72     start_time = time.time()
 73     print(multiprocessing.cpu_count())
 74     pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
 75     
 76     passwords = get_password(1, 4, words) #设置密码的长度1-4
 77  
 78     N = 11
 79     
 80     end_flag = 0 
 81 
 82     try:
 83         while True: 
 84             g2 = pool.imap(extract, itertools.islice(passwords, N)) # 对迭代器做切片操作
 85             if g2:
 86                 
 87                 for i in g2:
 88                     end_flag = 0
 89                     if i:
 90                         raise FoundException()
 91                 else:
 92                     end_flag = end_flag + 1
 93                     if end_flag > 1:
 94                         break
 95             else:
 96                 break
 97     except Exception as e:
 98         print(e)
 99         
100     print("破解结束")
101     end_time = time.time()
102     change_time = end_time - start_time
103     print("破解耗时:{}s".format(change_time))
多线程代码

在Python环境下运行,4位全部 数字密码的话,多线程破解大概9秒左右,这要看你电脑的配置啦

 

 

 参考:https://www.wanghaiqing.com/article/c214a9f7-7f43-4c4c-9713-08dda2f1a71f/

 

posted @ 2022-11-14 15:26  JackyXu_阿西  阅读(3207)  评论(0编辑  收藏  举报