Kickstart your coding journey with ourPython Code Assistant. An AI-powered assistant that's always ready to help. Don't miss out!
Let us assume that you got a password-protected PDF file and it's your top priority job to access it, but unfortunately, you overlooked the password. So, at this stage, you will look for an utmost way that can give you an instant result. In this tutorial, you will learn how to:
pikepdf library in Python.To get started, install the required dependencies:
pip3 install pikepdf tqdmpikepdf is a Python library that allows us to create, manipulate and repair PDF files. It provides a Pythonic wrapper around theC++ QPDF library.
We won't be usingpikepdf 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 aPasswordError exception otherwise:
import pikepdffrom tqdm import tqdm# load password listpasswords = [ line.strip() for line in open("wordlist.txt") ]# iterate over passwordsfor password in tqdm(passwords, "Decrypting PDF"): try: # open PDF file with pikepdf.open("foo-protected.pdf", password=password) as pdf: # Password decrypted successfully, break out of the loop print("[+] Password found:", password) break except pikepdf._core.PasswordError as e: # wrong password, just continue in the loop continueFirst, we load a password list fromwordlist.txt file in the current directory, get ithere. You can userockyou list or any other large wordlists as well.You can also use the Crunch tool to generate your own custom wordlist.
Next, we iterate over the list and try to open the file with each password, by passingpassword argument topikepdf.open() method, this will raisepikepdf._qpdf.PasswordError if it's an incorrect password.
We usedtqdm here just to print the progress on how many words are remaining. Check out my result:
Decrypting PDF: 43%|████████████████████████████████████████▏ | 2137/5000 [00:06<00:08, 320.70it/s][+] Password found: abc123The password was found after2137 trials, which took about 6 seconds. As you can see, it's going for about320 word/s; we'll see how to boost this rate.
John the Ripper is a free and fast password-cracking software tool that is available on many platforms. However, we'll be using Kali Linux operating system here, as it already comes pre-installed.
First, we gonna need a way to extract the password hash from the PDF file to be suitable for cracking in john utility. Luckily for us, there is a Python scriptpdf2john.py that does that. Let's download it:
Put your password-protected PDF in the current directory; mine is called
foo-protected.pdf, and run the following command:
root@rockikz:~/pdf-cracking# python3 pdf2john.py foo-protected.pdf | sed "s/::.*$//" | sed "s/^.*://" | sed -r 's/^.{2}//' | sed 's/.\{1\}$//' > hashThis will extract the PDF password hash into a new file namedhash, here is my result:
After I saved the password hash into
hash file, I usedcat command to print it to the screen.
Finally, we use this hash file to crack the password:
We simply use the command"john [hashfile]". As you can see, the password is012345 and was found with the speed of4503p/s.
Related: How to Use Hashing Algorithms in Python using hashlib.
Not all users are comfortable with coding in Python or using commands in Linux. So, if you're looking for an effective PDF password cracking program on Windows, theniSeePassword Dr.PDF is one of the best choices.

This PDF password cracking has an easy-to-understand UI so even the novices know how to use this program. Besides, it offers three powerful password cracking algorithms, includingDictionary,Brute-force, and Brute-force with Mask. You're free to set several types of parameters to boost the performance.

Currently, the password cracking speed is up to 100K per second, making it one of the fastest programs for cracking PDF passwords.
So that's it, our job is done, and we have successfully cracked the PDF password using three methods:pikepdf, John The Ripper, and iSeePassword Dr.PDF. The first method takes a lot of time to break the password but is quite intuitive for Python programmers, whereas the other methods are the ultimate method to get the password of a PDF file in a short period of time. Make sure you use this for ethical and own use.
Finally, inour Ethical hacking with Python book, we have built 24 hacking tools (including password crackers) from scratch using Python. Make sure to check it outhere if you're interested!
Learn also: How to Brute Force ZIP File Passwords in Python.
Happy Cracking ♥
Want to code smarter? OurPython Code Assistant is waiting to help you. Try it now!
View Full Code Create Code for MeGot a coding query or need some guidance before you comment? Check out thisPython Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!


