如何使用pypdf2检查pdf密码是否正确

How to use pypdf2 to check if a pdf password is correct

我正在用 Python 做一个来自 Automate the Boring Stuff 的挑战题。任务是编写一个程序,使用提供的 dictionary.txt 文件 "brute force" pdf 密码。

使用我加密并知道密码的文件,以及包含该密码的字典文件,我无法让我的代码"figure out"知道它是密码。相反,它会一直运行到字典文件的末尾,然后停止。

我想我可能误解了 pdfObject.decrypt() 在 "if" 语句中的工作方式,所以我制作了一个小测试程序来使用语法,但它在该程序中使用与我的 "main" 程序相同的语法。在 "test" 程序中,我提供来自 input() 的密码而不是 argv 和一个列表,但我看不到影响它的 if/how。

#My Program (that blitzes right past the correct password):

#/usr/bin/python

# bruteForce.py - Uses a dictionary attack to crack the password of an
# encrypted pdf

from sys import argv
import PyPDF2

# Take the argument for the file you want to attack and open it
if len(argv) > 1:
    pdfFilename = argv[1]
else:
    print('User must specify a file.')
    exit()

# Open the pdf as an object
pdfFile = open(pdfFilename, 'rb')
pdfObject = PyPDF2.PdfFileReader(pdfFile)

# Add the contents of the dictionary file to a data structure
dictionaryList = []
dictionaryFile = open('dictionary.txt')
for word in dictionaryFile.readlines():
    dictionaryList.append(word)
    wordLower = word.lower()
    dictionaryList.append(wordLower)
dictionaryFile.close()

# Iterate over the data structure, trying each password as lowercase
print('Trying passwords...')
for word in dictionaryList:
    password = str(word)
    print('Trying ' + password)
    if pdfObject.decrypt(password) == 1:
        print('Password is ' + word)
        exit()
    else:
        continue

print('Password not found')

##My Test (that works and returns 'Yup, that's it!'):

#!/usr/bin/python
# Figuring out how to deal with foo.decrypt to get a value of 1 or 0
# This first test returns the correct result with the correct password,
# so presumably bruteForce is not feeding it the right password somehow

import PyPDF2

filename = input('What file?')
password = input('What password?')

pdfFile = open(filename, 'rb')
pdfObject = PyPDF2.PdfFileReader(pdfFile)

if pdfObject.decrypt(password) == 1:
    print('Yup, that\'s it!')
else:
    print('Nope!')



I expect the program to arrive at the correct word in the dictionary, try it, and stop. Instead, it runs to the end of the list and says "Password not found."

字典条目包含文本文件中的换行符,因此它们与密码不匹配。在将它们添加到字典之前,我用 wordStripped = word.strip('\n') 删除了它们,程序按预期运行(速度大约是原来的两倍)。