python暴力解压rar加密文件过程详解


Posted in Python onJuly 05, 2019

第一次使用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=rar_file,args=(fp,pwd,extract_path))
		t.start()

以上是写程序的思路和遇到的各种坑,代码是手敲的,可能有一些错误,希望能得到谅解和帮助。

下面是一个图形界面的rar解密源代码:(图形只是想练习,运行较慢,建议直接运行上面的函数)

import tkinter as tk
import os
from tkinter import messagebox
from unrar import rarfile
from threading import Thread

def getPwd(dict):
  with open(dict,'r') as f:
    for pwd in f:
      yield pwd.strip()
def slowerDecode(fp,pwd,check_file,extract_path):
  fp.extractall(extract_path,pwd=pwd)
  if os.path.exists(check_file):
    messagebox.showinfo(message="密码:"+pwd)
    messagebox.showinfo(message="程序结束")
    messagebox.showinfo(message="密码:"+pwd)
    exit(0)
	
def quickDecode(fp,pwd,extract_path):
  fp.extractall(extract_path,pwd=pwd)


def check(obs):
  flag = 1
  for ob in obs:
    if not ob.checkExist():
      flag = 0
      ob.showError()  

  if(not flag):
    return 0
  else:
    for ob in obs:
      if not ob.check():
        flag = 0
        ob.showError()
		
  if (not flag):
    return 0
  else:
    for ob in obs:
      ob.right()
    return 1
		
def main(obs):
  extract_path = obs[0].path_input.get()
  rar_path = obs[1].path_input.get()
  txt_path = obs[2].path_input.get()
  pwds = getPwd(txt_path)
  global var1
  global var2
  if(check(obs)):
    if(var1.get() == 0 and var2.get() == 0):
      messagebox.showerror(message="选择一个选项!!!")
    elif(var1.get() == 0 and var2.get() == 1):
      fp = rarfile.RarFile(rar_path)
      check_file = fp.namelist()[0]
      for pwd in pwds:
        slowerDecode(fp,pwd,check_file,extract_path)
    elif(var1.get() == 1 and var2.get() == 0):
      fp = rarfile.RarFile(rar_path)
      for pwd in pwds:
        t = Thread(target=quickDecode,args=(fp,pwd,extract_path))
        t.start()
      exit(0)
    else:
      messagebox.showerror(message="只选择一个!!!")
    

class FolderPath:
  
  def __init__(self,y=0,error_message="Not exists!",path_input="",text=''):
    self.y = y
    self.error_message = error_message
    self.path_input = path_input
    self.text = text
	
  def createLabel(self):
    label = tk.Label(window,bg="white",font=("楷体",13),width=20,text=self.text)
    cv.create_window(100,self.y,window=label)
  
  def createEntry(self):
  	entry = tk.Entry(window,fg="blue",width="40",bg="#ffe1ff",textvariable=self.path_input)
  	cv.create_window(330,self.y,window=entry)
  
  def show(self):
    self.createLabel()
    self.createEntry()
	
  def showError(self,color="red"):
    label = tk.Label(window,bg="white",fg=color,font=("楷体",13),width="10",text=self.error_message)
    cv.create_window(530,self.y,window=label)
  
  def checkExist(self):
    self.error_message = 'Not exists!'
    if not os.path.exists(self.path_input.get()):
      return 0
    return 1
  
  def check(self):
    if not os.path.isdir(self.path_input.get()):
      self.error_message = 'Not a dir!'
      return 0
    else:
      return 1
  
  def right(self):
    self.error_message = "right path!"
    self.showError('#00FFFF')
	
  
class FilePath(FolderPath):
  def check(self):
    if (self.path_input.get().split('.')[-1] == self.suffix):
      return 1
    else:
      self.error_message = "Not "+self.suffix + '!'
      return 0
  

window = tk.Tk()
window.title('made by qiufeng')
window.geometry('600x300')
cv = tk.Canvas(window,width=600,height=300,bg='white')
cv.pack()

folderpath = FolderPath(y=140,path_input=tk.StringVar(),text="请输入解压路径")
folderpath.show()


rarpath = FilePath(y=60,path_input=tk.StringVar(),text="请输入rar路径")
rarpath.suffix = 'rar'
rarpath.show()

txtpath = FilePath(y=100,path_input=tk.StringVar(),text="请输入字典路径")
txtpath.suffix = 'txt'
txtpath.show()
obs = [folderpath,rarpath,txtpath]

#多选框
var1 = tk.IntVar()
var2 = tk.IntVar()
ck1 = tk.Checkbutton(window,text="直接破解(无法获得密码)",variable=var1)
cv.create_window(150,200,window=ck1)
ck2 = tk.Checkbutton(window,text="慢速(可获得密码)",variable=var2)
cv.create_window(132,230,window=ck2)
button = tk.Button(window,text="确认",command=lambda: main(obs))
cv.create_window(90,260,window=button)

window.mainloop()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中使用动态变量名的方法
May 06 Python
python base64 decode incorrect padding错误解决方法
Jan 08 Python
Python实现给文件添加内容及得到文件信息的方法
May 28 Python
pandas DataFrame实现几列数据合并成为新的一列方法
Jun 08 Python
python顺序的读取文件夹下名称有序的文件方法
Jul 11 Python
django celery redis使用具体实践
Apr 08 Python
Python发展简史 Python来历
May 14 Python
Python3 requests文件下载 期间显示文件信息和下载进度代码实例
Aug 16 Python
Python实现直播推流效果
Nov 26 Python
使用Pycharm分段执行代码
Apr 15 Python
python实现凯撒密码、凯撒加解密算法
Jun 11 Python
python opencv将多个图放在一个窗口的实例详解
Feb 28 Python
Python 使用folium绘制leaflet地图的实现方法
Jul 05 #Python
Python 给定的经纬度标注在地图上的实现方法
Jul 05 #Python
python 自动轨迹绘制的实例代码
Jul 05 #Python
python实现ip代理池功能示例
Jul 05 #Python
解决yum对python依赖版本问题
Jul 05 #Python
python写入文件自动换行问题的方法
Jul 05 #Python
Python Numpy计算各类距离的方法
Jul 05 #Python
You might like
Protoss魔法科技
2020/03/14 星际争霸
长波有什么东西
2021/03/01 无线电
PHP连接SQLServer2005的实现方法(附ntwdblib.dll下载)
2012/07/02 PHP
php中session过期时间设置及session回收机制介绍
2014/05/05 PHP
php中error与exception的区别及应用
2014/07/28 PHP
php过滤HTML标签、属性等正则表达式汇总
2014/09/22 PHP
PHP输出缓冲控制Output Control系列函数详解
2015/07/02 PHP
Zend Framework入门知识点小结
2016/03/19 PHP
js 页面刷新location.reload和location.replace的区别小结
2009/12/24 Javascript
javascript数组的使用
2013/03/28 Javascript
js 控制页面跳转的5种方法
2013/09/09 Javascript
JavaScript中的6种运算符总结
2014/10/16 Javascript
AngularJS操作键值对象类似java的hashmap(填坑小结)
2016/11/12 Javascript
用Vue.extend构建消息提示组件的方法实例
2017/08/08 Javascript
BootStrap模态框不垂直居中的解决方法
2017/10/19 Javascript
详解如何运行vue项目
2019/04/15 Javascript
Vue-resource安装过程及使用方法解析
2020/07/21 Javascript
Vue+element-ui添加自定义右键菜单的方法示例
2020/12/08 Vue.js
[01:00:44]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#1COL VS Alliance第三局
2016/03/04 DOTA
Python有序字典简单实现方法示例
2017/09/28 Python
深入理解Django中内置的用户认证
2017/10/06 Python
hmac模块生成加入了密钥的消息摘要详解
2018/01/11 Python
Python创建一个空的dataframe,并循环赋值的方法
2018/11/08 Python
python实现各种插值法(数值分析)
2019/07/30 Python
pytorch1.0中torch.nn.Conv2d用法详解
2020/01/10 Python
pyinstaller打包单文件时--uac-admin选项不起作用怎么办
2020/04/15 Python
太阳镜仓库,售价20美元或更少:Sunglass Warehouse
2016/09/28 全球购物
bonprix荷兰网上商店:便宜的服装、鞋子和家居用品
2020/07/04 全球购物
年终自我鉴定
2013/10/09 职场文书
在校硕士自我鉴定
2014/01/23 职场文书
微电影大赛策划方案
2014/06/05 职场文书
软件研发工程师岗位职责
2014/09/30 职场文书
2014年基层党建工作总结
2014/11/11 职场文书
2015年高一班主任工作总结
2015/05/13 职场文书
2016年教师学习教师法心得体会
2016/01/20 职场文书
学习党史心得体会2016
2016/01/23 职场文书