首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >joblib和Brute-Force PDF密码破解器自动化无聊的东西

joblib和Brute-Force PDF密码破解器自动化无聊的东西
EN

Stack Overflow用户
提问于 2021-03-19 17:48:14
回答 1查看 29关注 0票数 0

我正在尝试在我的代码中包含多处理支持。它现在工作得非常快,但字典迭代不会在找到密码时停止。我试着重置字典,但没用。

代码语言:javascript
复制
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')

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

EN

回答 1

Stack Overflow用户

发布于 2021-03-20 00:23:49

所以我自己想出了答案...我在decrypt()中更改了Parallel(n_jobs=numCores,timeout=1),并在iterDict()中添加了time.sleep(2)。

下面是我的最终代码:

代码语言:javascript
复制
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')

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66705871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档