import os
import subprocess
import sys # 用于终止程序
def crack_rar_password(rar_path):
# WinRAR命令行工具路径
rar_exe_path = r"C:\Users\a\Desktop\ctf\WinRAR\Rar.exe"
# 检查 Rar.exe 路径
if not os.path.exists(rar_exe_path):
print("Rar.exe 工具未找到,请检查路径是否正确。")
return
# 解压到可写目录
output_dir = r"C:\Temp"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 开始尝试密码
for i in range(10000): # 4位数字范围
password = str(i).zfill(4) # 生成4位数字密码
print(f"尝试密码:{password}") # 打印正在尝试的密码
try:
# 构造解压命令
command = [
rar_exe_path, "x", "-y", f"-p{password}",
f'"{rar_path}"', f'"{output_dir}"'
]
# 执行解压命令
result = subprocess.run(
" ".join(command), # 拼接成字符串以避免路径问题
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
shell=True, # 使用 shell 模式以处理复杂路径
timeout=5 # 设置超时 5 秒
)
# 打印解压程序的输出
print(result.stdout)
# 检查解压是否成功
if "全部正常" in result.stdout or "All OK" in result.stdout:
print(f"密码正确:{password}")
sys.exit(0) # 直接退出程序
except subprocess.TimeoutExpired:
print(f"密码 {password} 尝试超时,跳过...")
continue # 继续下一次尝试
except Exception as e:
print(f"尝试密码 {password} 时出错:{e}")
break # 遇到不可预知错误时退出
print("未找到正确的密码")
return None
if __name__ == "__main__":
rar_file_path = r"C:\Users\a\Desktop\it\基础破解.rar" # 修改为你的RAR文件路径
crack_rar_password(rar_file_path)
温馨提示:
1.需要再终端pip install subprocess
2.需要使用到rar工具,修改代码的路径为你电脑的路径
3.需要修改rar包的位置路径,如果和代码在同一文件夹下可以使用rar_file_path="基础破解.rar"
4.最终结果会在控制台显示并且暂停