Open In App

How to Crack PDF Files in Python?

Prerequisite: tqdm

In this article, we will learn How to Crack a Protected PDF File Using Python. Here we will use the Brute force Method, to Crack a PDF File using pikepdf module in Python.



Required Modules :

pip install pikepdf
pip install tqdm

We won’t be using pikepdf for that though, we just gonna need to open the password-protected PDF file, if it succeeds, that means it’s a correct password, and it’ll raise a PasswordError exception otherwise.



Understand step by step Implementation:

Below is the full implementation:




# Import Required Module
import pikepdf
from tqdm import tqdm
  
# Empty password list
passwords = []
  
# Contain passwords in text file
password_text_file = "Password Text File"
  
# Iterate through each line
# and store in passwords list
for line in open(password_text_file):
    passwords.append(line.strip())
      
# iterate over passwords
for password in tqdm(passwords, "Cracking PDF File"):
    try:
        
        # open PDF file and check each password
        with pikepdf.open("Protected PDF File",
                          password = password) as p:
            
            # If password is correct, break the loop
            print("[+] Password found:", password)
            break
              
    # If password will not match, it will raise PasswordError
    except pikepdf._qpdf.PasswordError as e:
        
        # if password is wrong, continue the loop
        continue

Output:


Article Tags :