python - joblib 和 Brute-Force PDF Password Breaker 自动化无聊的东西
问题描述
我正在尝试在我的代码中包含多处理支持。它现在工作得非常快,但是找到密码后字典迭代不会停止。我试图重置字典,但没有帮助。
import PyPDF2
import time
import os
import sys
from joblib import Parallel, delayed
import multiprocessing
def iterDict(password):
password1 = password.lower()
if pdfReader.decrypt(password) or pdfReader.decrypt(password1):
if pdfReader.decrypt(password1):
password = password1
print(f'Password: {password}')
passwords = [1] # list reset don't help to stop dictionary iteration
return passwords
else:
return False
def decrypt():
if pdfReader.isEncrypted:
print(f'Working... {time.asctime()}')
start = time.time()
numCores = multiprocessing.cpu_count()
if Parallel(n_jobs=numCores)(
delayed(iterDict)(password) for password in passwords):
end = time.time()
hours = int((end - start) / 3600)
minutes = int((end - start - hours * 3600) / 60)
secondes = int(end - start - (hours * 3600) - (minutes * 60))
print(f'{pdf} has been decrypted in {hours}H:{minutes}M:{secondes}S!')
else:
print(f'{pdf} hasn\'t been decrypted... Maybe need a better dictionary?')
else:
print(f'{pdf} isn\'t encrypted')
if len(sys.argv) == 3:
dictionary, pdf = sys.argv[1], sys.argv[2]
if os.path.isfile(dictionary) and dictionary.endswith('.txt'):
if os.path.isfile(pdf) and pdf.endswith('.pdf'):
global passwords
passwords = open(dictionary).read().split('\n')
pdfReader = PyPDF2.PdfFileReader(pdf)
decrypt()
else:
print('Invalid path to pdf or pdf file')
else:
print('Invalid path to dictionary or dictionary file')
else:
print('Please enter arguments as example:\
\ndictionaryName.txt pdfName.pdf')
有什么建议吗?非常感谢!
解决方案
所以我自己想出了答案......我在decrypt()中更改了Parallel(n_jobs=numCores, timeout=1),并在iterDict()中添加了time.sleep(2)。
这是我的最终代码:
import PyPDF2
import time
import os
import sys
from joblib import Parallel, delayed
import multiprocessing
def iterDict(password):
password1 = password.lower()
if pdfReader.decrypt(password) or pdfReader.decrypt(password1):
if pdfReader.decrypt(password1):
password = password1
print(f'Password: {password}')
time.sleep(2)
return True
else:
return False
def decrypt():
if pdfReader.isEncrypted:
print(f'Working... {time.asctime()}')
start = time.time()
numCores = multiprocessing.cpu_count()
try:
Parallel(n_jobs=numCores, timeout=1)(
delayed(iterDict)(password) for password in passwords)
except multiprocessing.context.TimeoutError:
wTime = time.strftime('%H:%M:%S', time.gmtime(time.time() - start))
print(f'{pdf} has been decrypted in {wTime}!')
else:
print(f'{pdf} hasn\'t been decrypted... Maybe need a better dictionary?')
else:
print(f'{pdf} isn\'t encrypted')
if len(sys.argv) == 3:
dictionary, pdf = sys.argv[1], sys.argv[2]
if os.path.isfile(dictionary) and dictionary.endswith('.txt'):
if os.path.isfile(pdf) and pdf.endswith('.pdf'):
passwords = open(dictionary).read().split('\n')
pdfReader = PyPDF2.PdfFileReader(pdf)
decrypt()
else:
print('Invalid path to pdf or pdf file')
else:
print('Invalid path to dictionary or dictionary file')
else:
print('Please enter arguments as example:\
\ndictionaryName.txt pdfName.pdf')
但也许有人有更好的解决方案或任何建议来改进我的代码?谢谢!
推荐阅读
- primefaces - PrimeFaces 日历聚焦时不显示日历
- r - 从R中的列表创建数据框
- html - 溢出隐藏导致 div 在浮动 div 下方而不是在其后面
- python - 无法从 oracle 数据库验证用户(python 3.8.3 和 django 3.0)
- c# - 泛型方法的 C# 奇怪的内联行为 - 可能的错误
- swiftui - SwiftUI 2.0 / Xcode 12 中父视图和子视图的警报
- machine-learning - 如果我的 Dropout 太高会怎样?在我的 2048 神经元密集层上使用什么 Dropout?(数据很少)
- c - 声明一个函数,c99 的问题
- php - 如何按变量之一的数值对此 JSON 进行排序
- node.js - 为什么我的递归函数(涉及循环中的异步调用)不返回调用者函数以继续循环?