Python實(shí)現(xiàn)爆破ZIP文件(支持純數(shù)字,數(shù)字+字母,密碼本)
亂碼問題
破解壓縮包時(shí)候會(huì)存在中文亂碼問題!
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修改為gbk3:修改的第2段代碼
大概在1553行
這里也是與解決辦法2的鏈接中不一致的地方。if語句的內(nèi)容不一樣,可能是zipfile升級(jí)的緣故
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)工作在腳本所在的路徑,如需更改,請(qǐng)手動(dòng)修改第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)中傳遞的密碼長(zhǎng)度可能前者的值大于后者,這一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í)列對(duì)象
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ī)取選取一個(gè)值,并把選取幾次的結(jié)果拼接成一個(gè)字符,即一個(gè)密碼
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ù)的時(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ū)間長(zhǎng)度
password_max = 6 # 設(shè)置密碼區(qū)間長(zhǎng)度
os.chdir(r'C:\Users\asuka\Desktop\123') # 移動(dòng)到目標(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錯(cuò)誤密碼:', 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)會(huì)對(duì)腳本所在文件夾中的zip文件爆破
腳本存在一些缺陷,無法打印出extractfile函數(shù)中正確密碼
需要手動(dòng)去終端復(fù)制最后一行,那個(gè)才是正確密碼,用正確密碼手動(dòng)解壓文件即可
'''
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行,腳本會(huì)工作在,腳本所在位置
import zipfile
import os
import time
import sys
import random
'''
源代碼描述:
1:代碼中,變量統(tǒng)一使用zip代表壓縮包
2:關(guān)于空密碼
測(cè)試發(fā)現(xiàn),當(dāng)一個(gè)壓縮包是無密碼的時(shí)候,給extractall的pwd參數(shù)指定密碼,依然可以正確提取壓縮包
因此無需寫代碼檢查壓縮包時(shí)候有密碼
否則,檢查有無密碼報(bào)異常,有密碼爆破再爆異常,搞得代碼會(huì)很復(fù)雜
3:關(guān)于zfile.extractall
破解密碼用到的zfile.extractall中
extractall要求輸入的是字節(jié)類型,所以需要手動(dòng)轉(zhuǎn)
4:關(guān)于異常,前面的那段注釋
使用try/except時(shí)候,pycharm報(bào)告Exception太寬泛,我們應(yīng)該指定異常類型
如果不確定有可能發(fā)生的錯(cuò)誤類型
在 try 語句前加入 # noinspection PyBroadException 即可讓pycharm閉嘴
但是except Exception as e中的e仍然會(huì)被pycharm報(bào)告說,這個(gè)變量沒有使用
5:關(guān)于解壓亂碼
我這里是通過修改zipfile的源代碼來解決的
修改源代碼時(shí),如果無法找到【if zinfo.flag_bits & 0x800】
可能是zipfile版本的問題,請(qǐng)?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)行位置
由于我的代碼往往跟操作的文件不在一個(gè)地方,所以在run()函數(shù)中使用
#os.chdir(r'') # 此路徑是測(cè)試的時(shí)候使用,用來手動(dòng)設(shè)定壓縮包路徑
來手動(dòng)設(shè)定腳本工作目錄
'''
# 主備前提工作:檢查出當(dāng)前路徑中的壓縮包,并創(chuàng)建一個(gè)同名的文件夾
def ready_work():
files = os.listdir() # 獲取腳本工作路徑中的所有文件
for file in files: # 遍歷腳本工作路徑中的所有文件
if file.endswith('.zip'): # 找出腳本工作路徑中的所有zip文件
# 開始制造一個(gè)新文件夾的路徑,用來存放解壓結(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è)請(qǐng)輸入,否則請(qǐng)回車鍵跳過')
math_min_input = input('')
print('默認(rèn)數(shù)字上限是1000000(1百萬),如需重設(shè)請(qǐng)輸入,否則請(qǐng)回車鍵跳過')
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()返回的兩個(gè)變量
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('[-] 耗時(shí):{}秒'.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)中傳遞的密碼長(zhǎng)度可能前者的值大于后者,這一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í)列對(duì)象
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ī)取選取一個(gè)值,并把選取幾次的結(jié)果拼接成一個(gè)字符,即一個(gè)密碼
return result_word
def extract():
password_min = input('請(qǐng)輸入密碼長(zhǎng)度下限:') # 設(shè)置密碼區(qū)間長(zhǎng)度
if password_min.isdecimal():
# 上面input輸入數(shù)字,雖說input接收過來的數(shù)字是字符型,但是isdecimal認(rèn)為它是一個(gè)數(shù)字,這是isdecimal內(nèi)部的問題
# 但是password_min的值仍然是字符型,還是需要isdecimal判斷后,再手動(dòng)轉(zhuǎn)換為int型
password_min = int(password_min)
else:
print('請(qǐng)輸入數(shù)字!')
password_max = input('請(qǐng)輸入密碼長(zhǎng)度上限:') # 設(shè)置密碼區(qū)間長(zhǎng)度
if password_max.isdecimal():
password_max = int(password_max)
else:
print('請(qǐng)輸入數(shù)字!')
new_path, file = ready_work() # 用來接收ready_work()返回的兩個(gè)變量
print('爆破開始')
count = 0
with zipfile.ZipFile(file) as zip_file: # 讀取壓縮文件
for password in MyIter(password_min, password_max): # 隨機(jī)迭代出指定長(zhǎng)度區(qū)間內(nèi)的密碼,在不明確位數(shù)的時(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('[-] 耗時(shí):{}秒'.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()返回的兩個(gè)變量
with zipfile.ZipFile(file) as zip_file:
# 設(shè)置密碼本
# 用來判斷用戶是否正正確輸入數(shù)字1或者0
print('使用當(dāng)前工作路徑中的txt文件作為密碼本請(qǐng)輸入:1')
print('手動(dòng)指定密碼本路徑請(qǐng)輸入:0')
while True:
user_choice_mode = input()
if user_choice_mode == str(1) or user_choice_mode == str(0):
break
else:
print("請(qǐng)正確輸入數(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'請(qǐng)輸入密碼本的路徑:')
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('[-] 耗時(shí):{}秒'.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('你輸入的路徑有問題!請(qǐng)檢查,錯(cuò)誤信息是:')
print(f)
# 運(yùn)行程序
def run():
print('一個(gè)ZIP爆破工具')
print('需要把腳本和壓縮包放在同一個(gè)文件夾中,默認(rèn)對(duì)當(dāng)前路徑下所有的壓縮包逐個(gè)爆破')
os.chdir(r'C:\Users\asuka\Desktop\123') # 測(cè)試的時(shí)候使用,手動(dòng)修改腳本工作路徑
print('[+]輸入1:程序自動(dòng)進(jìn)行純數(shù)字爆破')
print('[+]輸入2:程序自動(dòng)進(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的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python自定義主從分布式架構(gòu)實(shí)例分析
這篇文章主要介紹了Python自定義主從分布式架構(gòu),結(jié)合實(shí)例形式分析了主從分布式架構(gòu)的結(jié)構(gòu)、原理與具體的代碼實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-09-09
python不同版本的_new_不同點(diǎn)總結(jié)
在本篇內(nèi)容里小編給大家整理了一篇關(guān)于python不同版本的_new_不同點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-12-12
使用python執(zhí)行shell腳本 并動(dòng)態(tài)傳參 及subprocess的使用詳解
這篇文章主要介紹了使用python執(zhí)行shell腳本 并動(dòng)態(tài)傳參 及subprocess的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
python獲取當(dāng)前git的repo地址的示例代碼
大家好,當(dāng)談及版本控制系統(tǒng)時(shí),Git是最為廣泛使用的一種,而Python作為一門多用途的編程語言,在處理Git倉(cāng)庫時(shí)也展現(xiàn)了其強(qiáng)大的能力,本文給大家介紹了python獲取當(dāng)前git的repo地址的方法,需要的朋友可以參考下2024-09-09
Pytorch中TensorDataset,DataLoader的聯(lián)合使用方式
這篇文章主要介紹了Pytorch中TensorDataset,DataLoader的聯(lián)合使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Python?異步如何使用等待有時(shí)間限制協(xié)程
這篇文章主要為大家介紹了Python?異步如何使用等待有時(shí)間限制協(xié)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

