py_gadgets/rar_crack.py
2024-09-17 17:09:27 +08:00

39 lines
1.0 KiB
Python

import rarfile
import itertools
import string
# pip install rarfile
# 设置RAR文件的路径
rar_file_path = r'path'
# 设置密码字符集和最大长度
charset = string.ascii_letters + string.digits # 字母和数字
max_length = 10 # 设置最大密码长度
def extract_rar(password):
try:
with rarfile.RarFile(rar_file_path) as rf:
rf.extractall(pwd=password)
print(f'密码正确:{password}')
return True
except rarfile.BadRarFile:
return False
except rarfile.RarWrongPassword:
return False
# 暴力破解
def brute_force():
for length in range(1, max_length + 1):
for password in itertools.product(charset, repeat=length):
password = ''.join(password)
print(f'尝试密码:{password}')
if extract_rar(password):
return password
if __name__ == '__main__':
found_password = brute_force()
if found_password:
print(f'找到密码:{found_password}')
else:
print('未能找到密码')