首页 > 解决方案 > 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')

有什么建议吗?非常感谢!

标签: python

解决方案


所以我自己想出了答案......我在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')

但也许有人有更好的解决方案或任何建议来改进我的代码?谢谢!


推荐阅读