Search code examples
pythonenumspython-pdfreader

Decrypting a pdf file


So I am trying to decrypt the pdf file by using brute force approach. The "pdfReader.decrypt(password)" returns a ENUM for type PasswordType. I am not able to figure out how do I compare this enum to print the message that the file is decrypted successfully.

The code below is says that the object returned by pdfReader.decrypt() is of type enum, but I do not know how to retreive and compare against the value

import PyPDF2 as pd

filename = input('Path to the file: ')
filename = filename.strip()
file = open(filename,'rb')
pdfReader = pd.PdfReader(file)

tried = 0

if not pdfReader.is_encrypted:
    print('The file is not password protected! You can successfully open it!')

else:
    wordListFile = open('wordlist.txt', 'r',errors='ignore')
    body = wordListFile.read().lower()
    words = body.split('\n')


    for i in range(len(words)):
        word = words[i]
        print('Trying to decode passowrd by: {}'.format(word))
        result = pdfReader.decrypt(word)
        print(result,type(result))
        if result == 0:
            print('Success! The password is: '+ word)
            break
        elif result == 1:
            tried += 1
            print('Passwords tried: ' + str(tried))
            continue

Output I recieved is:


Trying to decode passowrd by: hi
PasswordType.NOT_DECRYPTED <enum 'PasswordType'>
Passwords tried: 1
Trying to decode passowrd by: there
PasswordType.NOT_DECRYPTED <enum 'PasswordType'>
Passwords tried: 2
Trying to decode passowrd by: 0002
PasswordType.NOT_DECRYPTED <enum 'PasswordType'>
Passwords tried: 3
Trying to decode passowrd by: 0149
PasswordType.NOT_DECRYPTED <enum 'PasswordType'>
Passwords tried: 4
Trying to decode passowrd by: 0489
PasswordType.NOT_DECRYPTED <enum 'PasswordType'>
Passwords tried: 5
Trying to decode passowrd by: 0123                        # This is the password
PasswordType.OWNER_PASSWORD <enum 'PasswordType'> # I don't know how to check if it is OWER_PASSWORD or NOT_DECRYPTED
Trying to decode passowrd by: ghjk
PasswordType.NOT_DECRYPTED <enum 'PasswordType'>
Passwords tried: 6
Trying to decode passowrd by: dfgh
PasswordType.NOT_DECRYPTED <enum 'PasswordType'>
Passwords tried: 7
Trying to decode passowrd by: gfhj
PasswordType.NOT_DECRYPTED <enum 'PasswordType'>
Passwords tried: 8
Trying to decode passowrd by: 0125
PasswordType.NOT_DECRYPTED <enum 'PasswordType'>
Passwords tried: 9

I tried a bunch of soutions nothing seems to work. My if statement doesn't make sense I know,but I don't kow how to retreive

import PyPDF2 as pd

# from enum import PasswordType
import enum

filename = input('Path to the file: ')
filename = filename.strip()
file = open(filename,'rb')
pdfReader = pd.PdfReader(file)

tried = 0

if not pdfReader.is_encrypted:
    print('The file is not password protected! You can successfully open it!')

else:
    wordListFile = open('wordlist.txt', 'r',errors='ignore')
    body = wordListFile.read().lower()
    words = body.split('\n')


    for i in range(len(words)):
        word = words[i]
        print('Trying to decode passowrd by: {}'.format(word))
        result = pdfReader.decrypt(word)
        print(result,type(result))
        # try:
        if result == enum.Enum.PasswordType.OWNER_PASSWORD or result == enum.Enum.PasswordType.NOT_DECRYPTED :
            print('Success! The password is: '+ word)
            break
        # except:
        #     pass
        
        # try:
        elif result == enum.Enum.PasswordType.OWNER_PASSWORD or result == enum.Enum.PasswordType.NOT_DECRYPTED :
                    tried += 1
                    print('Passwords tried: ' + str(tried))
                    continue
        # except:
        #     pass
            



Contents of wordlist.txt

hi
there
0002
0149
0489
0123
ghjk
dfgh
gfhj
0125

Solution

  • The PasswordType enum in PyPDF is:

    class PasswordType(IntEnum):
        NOT_DECRYPTED = 0
        USER_PASSWORD = 1
        OWNER_PASSWORD = 2
    

    So the PasswordType members are both PasswordType and int -- so you have a choice:

    result = pdfReader.decrypt(word)
    
    # old way
    if result == 0:  # not decrypted
        ...
    
    # new way
    if result is PasswordType.NOT_DECRYPTED:
        ...