Search code examples
pythonpypdf

Brute force password breaker


create a list of word strings by reading this file. Then loop over each word in this list, passing it to the decrypt() method. If this method returns the integer 0, the password was wrong and your program should continue to the next password. If decrypt() returns 1, then your program should break out of the loop and print the hacked password. You should try both the uppercase and lower-case form of each word. This dictionary.txt file contains words in capital letters.

> import PyPDF2

pdfFile = open('reverse.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFile)
pdfWriter = PyPDF2.PdfFileWriter()
for pageNum in range(pdfReader.numPages):
    pdfWriter.addPage(pdfReader.getPage(pageNum))
wrd = input('Please enter one word as a password: ')
pdfWriter.encrypt(wrd)
resultPdf = open('encryptedreverse.pdf', 'wb')
pdfWriter.write(resultPdf)
resultPdf.close()
print(pdfReader.isEncrypted)

helloDict = open('dictionary.txt')
helloDictCont = helloDict.read().splitlines()

liDict = []
for word in helloDictCont:
    liDict.extend(word.split())

PdfFile2 = open('encryptedreverse.pdf', 'rb')
pdfReader2 = PyPDF2.PdfFileReader(PdfFile2)
print(pdfReader2.isEncrypted)

for word in liDict:
    if pdfReader2.decrypt(word) == 1:
        break
        print(word)
    elif pdfReader2.decrypt(word.lower()) == 1:
        break
        print(word)

After a few minutes processing ends and I neither get a password printed nor the pdf file is decrypted. Any idea what am I doing wrong?


Solution

  • This works fine for me:

    import PyPDF2
    
    pdfFile = open('reverse.pdf', 'rb')
    pdfReader = PyPDF2.PdfFileReader(pdfFile)
    pdfWriter = PyPDF2.PdfFileWriter()
    for pageNum in range(pdfReader.numPages):
        pdfWriter.addPage(pdfReader.getPage(pageNum))
    wrd = input('Please enter one word as a password: ')
    pdfWriter.encrypt(wrd)
    resultPdf = open('encryptedreverse.pdf', 'wb')
    pdfWriter.write(resultPdf)
    resultPdf.close()
    print(pdfReader.isEncrypted)
    
    helloDict = open('t.txt')
    helloDictCont = helloDict.read().splitlines()
    
    liDict = []
    for word in helloDictCont:
        liDict.extend(word.split())
    
    PdfFile2 = open('encryptedreverse.pdf', 'rb')
    pdfReader2 = PyPDF2.PdfFileReader(PdfFile2)
    print(pdfReader2.isEncrypted)
    
    for word in liDict:
        if pdfReader2.decrypt(word) == 1:
            print('The correct PWD as upper case: ' + word)
            break
        elif pdfReader2.decrypt(word.lower()) == 1:
            print('The correct PWD as lower case: ' + word)
            break
        else:
            print('PWD is not correct: ' + word)