python暴力解壓rar加密文件過程詳解
第一次使用csdn寫文章,寫得不好還請見諒。(運(yùn)行環(huán)境:python3.6)
下了一個(gè)帶密碼的壓縮包文件,作為一個(gè)剛學(xué)python的新手,想著能不能用python暴力破解它,于是在網(wǎng)上搜了很多資料,看著似乎并不是很麻煩,也想試著自己寫一個(gè)可以暴力破解的程序,在寫的過程中卻遇到了各種各樣的問題,希望大手們能帶帶我。遇到的問題如下:
- zipfile和zipfile2似乎都不支持AES解密(https://bugs.python.org/issue9170)
- 在用rarfile暴力破解時(shí)即使密碼錯(cuò)誤也不拋出異常,因此無法用try,except捕獲密碼
本來是想寫一個(gè)可以同時(shí)暴力破解zip和rar的程序,在試了半天解密zip卻一直提示密碼錯(cuò)誤之后放棄了zip,想著能不能寫一個(gè)暴力破解rar的程序。
首先是生成字典:要用到itertools模塊
import itertools as its
import string
def createDict(path,repeats,words):
dict = its.product(words,repeat=repeats)
'''這里的words是要迭代的字符串,repeats是生成的密碼長度,生成的dict是一個(gè)返回元組的迭代器'''
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()
到這里我們的字典已經(jīng)生成完畢了,接下來開始暴力破解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)
'''
事實(shí)上我在嘗試時(shí)似乎從來沒有到達(dá)過else,這樣可能是得不到解壓密碼的。我的
一種得到密碼的想法如下,但是運(yùn)行效率可能會降低
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可以設(shè)置為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()
以上是寫程序的思路和遇到的各種坑,代碼是手敲的,可能有一些錯(cuò)誤,希望能得到諒解和幫助。
下面是一個(gè)圖形界面的rar解密源代碼:(圖形只是想練習(xí),運(yùn)行較慢,建議直接運(yùn)行上面的函數(shù))
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="程序結(jié)束")
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="選擇一個(gè)選項(xiàng)!??!")
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="只選擇一個(gè)?。?!")
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="確認(rèn)",command=lambda: main(obs))
cv.create_window(90,260,window=button)
window.mainloop()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用python批量讀取word文檔并整理關(guān)鍵信息到excel表格的實(shí)例
今天小編就為大家分享一篇使用python批量讀取word文檔并整理關(guān)鍵信息到excel表格的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
Python報(bào)錯(cuò)KeyError: ‘missing_key‘的有效解決方法
在 Python 編程中,報(bào)錯(cuò)信息常常讓開發(fā)者感到困擾,其中,“KeyError: ‘missing_key’”是一個(gè)較為常見的報(bào)錯(cuò),它可能在各種數(shù)據(jù)處理和字典操作的場景中出現(xiàn),本文將深入探討這個(gè)報(bào)錯(cuò)的原因,并提供多種有效的解決方法,幫助開發(fā)者快速解決此類問題2024-10-10
Numpy創(chuàng)建數(shù)組和隨機(jī)數(shù)組的方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了Numpy創(chuàng)建數(shù)組和隨機(jī)數(shù)組的方法小結(jié),文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Python有一定幫助,具有一定的參考價(jià)值,需要的可以參考一下2023-11-11
python?包?requests?實(shí)現(xiàn)請求操作
這篇文章主要介紹了python?包?requests?實(shí)現(xiàn)請求操作,文章介紹內(nèi)容包括帶參數(shù)請求、自定義headers,文章內(nèi)容詳細(xì)具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-04-04
Python編程語言的35個(gè)與眾不同之處(語言特征和使用技巧)
這篇文章主要介紹了Python編程語言的35個(gè)與眾不同之處,Python編程語言的語言特征和使用技巧,需要的朋友可以參考下2014-07-07
把JSON數(shù)據(jù)格式轉(zhuǎn)換為Python的類對象方法詳解(兩種方法)
本文通過兩種方法給大家介紹了把JSON數(shù)據(jù)格式轉(zhuǎn)換為Python的類對象,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-06-06
接口自動(dòng)化多層嵌套json數(shù)據(jù)處理代碼實(shí)例
這篇文章主要介紹了接口自動(dòng)化多層嵌套json數(shù)據(jù)處理代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11

