如何使用PYPDF2检查PDF密码是否正确



我正在从python中自动化无聊的东西的挑战问题。任务是编写一个程序,该程序将使用提供的Dictionary.txt文件"蛮力" PDF密码。

使用我加密并知道密码的文件,以及包含该密码的字典文件,我无法让我的代码"弄清楚"它是密码。取而代之的是,它运行到字典文件的末尾,然后停止。

我以为我可能会误解pdfobject.decrypt((如何在" if"语句中起作用,因此我制作了一个小的测试程序来使用该语法,但是它在该程序中使用与我的"我的"我的"相同的语法"中起作用。主要程序。在"测试"程序中,我提供了来自Input((而不是ARGV和列表的密码,但是我看不出是否会影响它。

#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')剥离了它们,并且该程序按预期工作(快的速度大约是两倍(。

如果使用;

word_list = open("dictionary.txt").readlines()

然后列表仅包含单词,您可以通过用;

从列表中打印出每一行来测试它。

使项目工作我使用.Strip方法以删除' n',因为我在列表中迭代

相关内容

最新更新