How to Crack PDF Files in Python?


Python has a rich collection of libraries that are used for multiple purposes like creating and developing applications, web development, scientific computation, software testing, machine learning, and many more. Python is also used for testing and developing system applications in terms of information security. There are several other libraries and tools available that contain specific scripts used for creating hashes, information gathering, information retrieval, encryption and decryption, web crawling, spoofing, and many more.

In this article, we will create a program that will decrypt a password protected PDF document. For decryption, we will use a word list that contains some common passwords and it will help to decrypt the pdf document.

In order to create a pdf cracker, we will import the pikepdf library. Once downloaded, we can include it in our notebook. For reference, we will use this wordlist as an example that contains 5000 common passwords in it.

Example

import pikepdf
from tqdm import tqdm

#Loading password list
password = [line.strip() for line in open("wordlist.txt")]

#iterate over all the passwords
for paswrd in tqdm(password, "Cracking PDF"):
   try:
      #open PDF file
      with pikepdf.open("protected.pdf", password=paswrd) as pdf:
#If password matches then break the loop and print the output
         print("Password found:", paswrd)
         break
   except pikepdf._qpdf.PasswordError as e:
      #If password not found then continue
      continue

Output

Running the above code will first find the password and then print it as the output.

Updated on: 04-Mar-2021

959 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements