Skip to content
Related Articles

Related Articles

How to Crack PDF Files in Python?
  • Last Updated : 26 Dec, 2020
GeeksforGeeks - Summer Carnival Banner

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 :

  • pikepdf is a Python module, which is used to create, manipulate, and repair PDF Files.
pip install pikepdf
  • tqdm is one of the more comprehensive packages for progress bars with python and is handy for those instances you want to build scripts that keep the users informed on the status of your application.
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:



  • Here we will use a text file that contains a password list, get it here.
  • We will iterate through all password lists and try to open the file with each password, bypassing the password argument to open() method, if the password will not match it will raise PasswordError. 
  • The last step is progress, like how many words are remaining, for showing the progress we will use tqdm module.

Below is the full implementation:

Python3




# 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:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :