Python實(shí)現(xiàn)爆破ZIP文件(支持純數(shù)字,數(shù)字+字母,密碼本)
亂碼問題
破解壓縮包時候會存在中文亂碼問題!
1:直接使用Everything搜索出要修改的庫文件 zipfile.py ,并用notepad++打開
2:修改的第一段代碼
大概位置在1374行
if flags & 0x800: # UTF-8 file names extension filename = filename.decode('utf-8') else: # Historical ZIP filename encoding filename = filename.decode('gbk') # 把cp437修改為gbk
3:修改的第2段代碼
大概在1553行
這里也是與解決辦法2的鏈接中不一致的地方。if語句的內(nèi)容不一樣,可能是zipfile升級的緣故
if fheader[_FH_GENERAL_PURPOSE_FLAG_BITS] & 0x800: # UTF-8 filename fname_str = fname.decode("utf-8") else: fname_str = fname.decode("gbk") # 把原來的cp437更改為gbk
4:保存退出即可
單線程純數(shù)字爆破
需要指定一下第6行(壓縮包的位置),第20行(密碼區(qū)間)
import zipfile import os import time import sys os.chdir(r'C:\Users\asuka\Desktop\123') start_time = time.time() # 獲取zip文件 def get_zipfile(): files = os.listdir() for file in files: if file.endswith('.zip'): return file # 用來提取zip文件 def extract(): file = get_zipfile() zfile = zipfile.ZipFile(file) # 讀取壓縮文件 for num in range(1, 1000000): # 設(shè)置數(shù)字密碼區(qū)間 try: pwd = str(num) zfile.extractall(path='.', pwd=pwd.encode('utf-8')) print('解壓密碼是:', pwd) end_time = time.time() print('單線程破解壓縮包花了%s秒' % (end_time - start_time)) sys.exit(0) # 讓程序在得到結(jié)果后,就停止運(yùn)行,正常退出 except Exception as e: pass if __name__ == "__main__": extract()
單線程數(shù)字字母爆破
腳本默認(rèn)工作在腳本所在的路徑,如需更改,請手動修改第49行
import zipfile import time import sys import random import os class MyIter(object): word = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" # 原始的密碼本 def __init__(self, min_len, max_len): # 迭代器實(shí)現(xiàn)初始方法,傳入?yún)?shù) # 下面的if-else是為了解決extract函數(shù)中,for循環(huán)中傳遞的密碼長度可能前者的值大于后者,這一bug if min_len < max_len: self.min_len = min_len self.max_len = max_len else: self.min_len = max_len self.max_len = min_len def __iter__(self): # 直接返回self實(shí)列對象 return self def __next__(self): # 通過不斷地輪循,生成密碼 result_word = '' for i in range(0, random.randint(self.min_len, self.max_len)): # randint取值為[]左右閉區(qū)間 result_word += random.choice(MyIter.word) # 從word中隨機(jī)取選取一個值,并把選取幾次的結(jié)果拼接成一個字符,即一個密碼 return result_word def extract(): start_time = time.time() zip_file = zipfile.ZipFile('1.zip', 'r') for password in MyIter(password_min, password_max): # 隨機(jī)迭代出1~4位數(shù)的密碼,在不明確位數(shù)的時候做相應(yīng)的調(diào)整 if zip_file: try: zip_file.extractall(path='.', pwd=str(password).encode('utf-8')) print("壓縮密碼為:", password) end_time = time.time() print('破解壓縮包花了%s秒' % (end_time - start_time)) sys.exit(0) except Exception as e: print('pass密碼:', password) pass if __name__ == "__main__": password_min = 5 # 設(shè)置密碼區(qū)間長度 password_max = 6 # 設(shè)置密碼區(qū)間長度 os.chdir(r'C:\Users\asuka\Desktop\123') # 移動到目標(biāo)所在文件夾 extract()
多線程爆破密碼本
腳本存在一些缺陷,無法打印出extractfile函數(shù)中正確密碼
第38行指定工作路徑
第39行指定密碼本
import zipfile from threading import Thread import sys import os ''' 多線程爆破完成 ''' def extractfile(zip_file, password): try: zip_file.extractall(path='.', pwd=str(password).encode('utf-8')) print('[+]' + zip_file + ' 解壓密碼是:', password) sys.exit(0) except Exception as e: # print('pass錯誤密碼:', password) pass def main(password_file): files = os.listdir() for file in files: # 遍歷當(dāng)前路徑下的所有文件 if file.endswith('.zip'): # 爆破zip文件 zip_file = zipfile.ZipFile(file) pass_file = open(password_file) for line in pass_file.readlines(): password = line.strip('\n') t = Thread(target=extractfile, args=(zip_file, password)) t.start() if __name__ == '__main__': ''' 腳本默認(rèn)會對腳本所在文件夾中的zip文件爆破 腳本存在一些缺陷,無法打印出extractfile函數(shù)中正確密碼 需要手動去終端復(fù)制最后一行,那個才是正確密碼,用正確密碼手動解壓文件即可 ''' os.chdir(r'C:\Users\asuka\Desktop\123') password_file = r'C:\Users\asuka\Desktop\123\password.txt' # 用來指定密碼本的路徑 main(password_file)
單線程爆破3合1版(支持?jǐn)?shù)字,數(shù)字+字母、密碼本)
第213行,可以指定腳本工作路徑
如果刪除第213行,腳本會工作在,腳本所在位置
import zipfile import os import time import sys import random ''' 源代碼描述: 1:代碼中,變量統(tǒng)一使用zip代表壓縮包 2:關(guān)于空密碼 測試發(fā)現(xiàn),當(dāng)一個壓縮包是無密碼的時候,給extractall的pwd參數(shù)指定密碼,依然可以正確提取壓縮包 因此無需寫代碼檢查壓縮包時候有密碼 否則,檢查有無密碼報(bào)異常,有密碼爆破再爆異常,搞得代碼會很復(fù)雜 3:關(guān)于zfile.extractall 破解密碼用到的zfile.extractall中 extractall要求輸入的是字節(jié)類型,所以需要手動轉(zhuǎn) 4:關(guān)于異常,前面的那段注釋 使用try/except時候,pycharm報(bào)告Exception太寬泛,我們應(yīng)該指定異常類型 如果不確定有可能發(fā)生的錯誤類型 在 try 語句前加入 # noinspection PyBroadException 即可讓pycharm閉嘴 但是except Exception as e中的e仍然會被pycharm報(bào)告說,這個變量沒有使用 5:關(guān)于解壓亂碼 我這里是通過修改zipfile的源代碼來解決的 修改源代碼時,如果無法找到【if zinfo.flag_bits & 0x800】 可能是zipfile版本的問題,請?jiān)囍阉鳌緄f fheader[_FH_GENERAL_PURPOSE_FLAG_BITS] & 0x800】,或者【fname_str = fname.decode("utf-8")】 定位的方式是多樣的,不必拘泥 詳情參見: https://secsilm.blog.csdn.net/article/details/79829247 https://wshuo.blog.csdn.net/article/details/80146766?spm=1001.2014.3001.5506 6:關(guān)于代碼運(yùn)行位置 由于我的代碼往往跟操作的文件不在一個地方,所以在run()函數(shù)中使用 #os.chdir(r'') # 此路徑是測試的時候使用,用來手動設(shè)定壓縮包路徑 來手動設(shè)定腳本工作目錄 ''' # 主備前提工作:檢查出當(dāng)前路徑中的壓縮包,并創(chuàng)建一個同名的文件夾 def ready_work(): files = os.listdir() # 獲取腳本工作路徑中的所有文件 for file in files: # 遍歷腳本工作路徑中的所有文件 if file.endswith('.zip'): # 找出腳本工作路徑中的所有zip文件 # 開始制造一個新文件夾的路徑,用來存放解壓結(jié)果 a = os.path.splitext(file) # 分離壓縮包的名字和后綴 new_path = os.path.join(os.path.abspath('.'), str(a[0])) # 把當(dāng)前腳本運(yùn)行的路徑和壓縮包的文件名拼接成新的路徑 # 檢查新文件夾的路徑是否已存在,如果有就直接使用,如果沒有就創(chuàng)建它 if os.path.exists(new_path): pass else: os.makedirs(new_path) return new_path, file # 純數(shù)字爆破 def get_zipfile(): math_max = 1000000 # 設(shè)置數(shù)字密碼的上限 math_min = 1 # 設(shè)置數(shù)字密碼是下限 print('默認(rèn)輸入數(shù)字下限是1,如需重設(shè)請輸入,否則請回車鍵跳過') math_min_input = input('') print('默認(rèn)數(shù)字上限是1000000(1百萬),如需重設(shè)請輸入,否則請回車鍵跳過') math_max_input = input('') if len(math_max_input): math_max = int(math_max_input) else: pass if len(math_min_input): math_min = int(math_min_input) else: pass new_path, file = ready_work() # 用來接收ready_work()返回的兩個變量 print('爆破開始') count = 0 # 開始解壓文件 with zipfile.ZipFile(file) as zip_file: # 讀取壓縮文件 for num in range(math_min, math_max): # 設(shè)置數(shù)字密碼區(qū)間 start_time = time.time() # noinspection PyBroadException try: pwd = str(num) zip_file.extractall(path=new_path, pwd=pwd.encode('utf-8')) print('[+]' + str(file) + ' 解壓密碼是:', pwd) end_time = time.time() print('[-] 耗時:{}秒'.format(str(end_time - start_time))) count = count + 1 # count最后加1一次,用來算成成功的這次 print('[-] 累計(jì)嘗試{}'.format(count)) sys.exit(0) # 讓程序在得到結(jié)果后,就停止運(yùn)行,正常退出 except Exception as e: count += 1 # 統(tǒng)計(jì)總共失敗了多少次 pass # 實(shí)現(xiàn)數(shù)字字母組合爆破 class MyIter(object): word = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" # 原始的密碼本 def __init__(self, min_len, max_len): # 迭代器實(shí)現(xiàn)初始方法,傳入?yún)?shù) # 下面的if-else是為了解決extract函數(shù)中,for循環(huán)中傳遞的密碼長度可能前者的值大于后者,這一bug if min_len < max_len: self.min_len = min_len self.max_len = max_len else: self.min_len = max_len self.max_len = min_len def __iter__(self): # 直接返回self實(shí)列對象 return self def __next__(self): # 通過不斷地輪循,生成密碼 result_word = '' for i in range(0, random.randint(self.min_len, self.max_len)): # randint取值為[]左右閉區(qū)間 result_word += random.choice(MyIter.word) # 從word中隨機(jī)取選取一個值,并把選取幾次的結(jié)果拼接成一個字符,即一個密碼 return result_word def extract(): password_min = input('請輸入密碼長度下限:') # 設(shè)置密碼區(qū)間長度 if password_min.isdecimal(): # 上面input輸入數(shù)字,雖說input接收過來的數(shù)字是字符型,但是isdecimal認(rèn)為它是一個數(shù)字,這是isdecimal內(nèi)部的問題 # 但是password_min的值仍然是字符型,還是需要isdecimal判斷后,再手動轉(zhuǎn)換為int型 password_min = int(password_min) else: print('請輸入數(shù)字!') password_max = input('請輸入密碼長度上限:') # 設(shè)置密碼區(qū)間長度 if password_max.isdecimal(): password_max = int(password_max) else: print('請輸入數(shù)字!') new_path, file = ready_work() # 用來接收ready_work()返回的兩個變量 print('爆破開始') count = 0 with zipfile.ZipFile(file) as zip_file: # 讀取壓縮文件 for password in MyIter(password_min, password_max): # 隨機(jī)迭代出指定長度區(qū)間內(nèi)的密碼,在不明確位數(shù)的時候做相應(yīng)的調(diào)整 start_time = time.time() # noinspection PyBroadException try: zip_file.extractall(path=new_path, pwd=str(password).encode('utf-8')) print('[+]' + str(file) + ' 解壓密碼是:' + password) end_time = time.time() print('[-] 耗時:{}秒'.format(str(end_time - start_time))) count = count + 1 # count最后加1一次,用來算成成功的這次 print('[-] 累計(jì)嘗試{}'.format(count)) sys.exit(0) except Exception as e: count += 1 pass # 實(shí)現(xiàn)密碼本爆破 def password_file_baopo(): new_path, file = ready_work() # 用來接收ready_work()返回的兩個變量 with zipfile.ZipFile(file) as zip_file: # 設(shè)置密碼本 # 用來判斷用戶是否正正確輸入數(shù)字1或者0 print('使用當(dāng)前工作路徑中的txt文件作為密碼本請輸入:1') print('手動指定密碼本路徑請輸入:0') while True: user_choice_mode = input() if user_choice_mode == str(1) or user_choice_mode == str(0): break else: print("請正確輸入數(shù)字 1 或者 0!") continue user_choice_mode = int(user_choice_mode) if user_choice_mode: # 如果用戶選擇了模式1 all_files = os.listdir() for all_file in all_files: if all_file.endswith('.txt'): password_file = str(all_file) else: password_file = input(r'請輸入密碼本的路徑:') print('爆破開始') count = 0 start_time = time.time() try: with open(password_file, 'r', encoding='utf8') as pwdfile: # 逐行讀取密碼本 word = pwdfile.readlines() for w in word: w = w.replace('\n', '') # 嘗試破解zip文件 # noinspection PyBroadException try: zip_file.extractall(path=new_path, pwd=str(w).encode('utf-8')) print('[+]' + str(file) + ' 解壓密碼是:', str(w)) end_time = time.time() print('[-] 耗時:{}秒'.format(str(end_time - start_time))) count = count + 1 # count最后加1一次,用來算成成功的這次 print('[-] 累計(jì)嘗試{}'.format(count)) sys.exit(0) except Exception as e: count += 1 pass except Exception as f: print('你輸入的路徑有問題!請檢查,錯誤信息是:') print(f) # 運(yùn)行程序 def run(): print('一個ZIP爆破工具') print('需要把腳本和壓縮包放在同一個文件夾中,默認(rèn)對當(dāng)前路徑下所有的壓縮包逐個爆破') os.chdir(r'C:\Users\asuka\Desktop\123') # 測試的時候使用,手動修改腳本工作路徑 print('[+]輸入1:程序自動進(jìn)行純數(shù)字爆破') print('[+]輸入2:程序自動進(jìn)行字母數(shù)字組合爆破,效率低下不推薦') print('[+]輸入3:使用密碼本進(jìn)行爆破') print() user_choice = int(input('[-]輸入:')) if user_choice == 1: print('純數(shù)字爆破模式--->') get_zipfile() elif user_choice == 2: print('數(shù)字字母爆破模式--->') extract() else: print('密碼本爆破模式--->') password_file_baopo() if __name__ == '__main__': run()
模式1:
模式2:
模式3:
模式3的健壯性:
以上就是Python實(shí)現(xiàn)爆破ZIP文件(支持純數(shù)字,數(shù)字+字母,密碼本)的詳細(xì)內(nèi)容,更多關(guān)于Python爆破ZIP的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
如何在Django中添加沒有微秒的 DateTimeField 屬性詳解
這篇文章主要給大家介紹了關(guān)于如何在Django中添加沒有微秒的 DateTimeField 屬性的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01Django rest framework分頁接口實(shí)現(xiàn)原理解析
這篇文章主要介紹了Django rest framework分頁接口實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08如何使用PyCharm將代碼上傳到GitHub上(圖文詳解)
這篇文章主要介紹了如何使用PyCharm將代碼上傳到GitHub上(圖文詳解),文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Python import自己的模塊報(bào)錯問題及解決
這篇文章主要介紹了Python import自己的模塊報(bào)錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Python利用selenium建立代理ip池訪問網(wǎng)站的全過程
selenium控制瀏覽器也是可以使用代理ip的,下面這篇文章主要給大家介紹了關(guān)于Python利用selenium建立代理ip池訪問網(wǎng)站的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03Python3+OpenCV2實(shí)現(xiàn)圖像的幾何變換(平移、鏡像、縮放、旋轉(zhuǎn)、仿射)
這篇文章主要介紹了Python3+OpenCV2實(shí)現(xiàn)圖像的幾何變換(平移、鏡像、縮放、旋轉(zhuǎn)、仿射),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05利用Python的tkinter模塊實(shí)現(xiàn)界面化的批量修改文件名
這篇文章主要介紹了利用Python的tkinter模塊實(shí)現(xiàn)界面化的批量修改文件名,用Python編寫過批量修改文件名的腳本程序,代碼很簡單,運(yùn)行也比較快,詳細(xì)內(nèi)容需要的小伙伴可以參考一下下面文章內(nèi)容2022-08-08