Sitemap
Aardvark Infinity

Welcome to Aardvark Infinity Embark on a journey to the future with cutting-edge technology. AI, SCRIPTING, CYBERSECURITY

Step 1: Import Necessary Libraries

2 min readSep 25, 2024

--

Let’s proceed with creating the script. We’ll need PyPDF2 for working with the PDF and itertools for generating brute-force combinations.

python
Copy code
import PyPDF2
import itertools
import string

Step 2: Define the Character Set and Load Dictionary

We’ll define our character set for brute-force attempts and load a dictionary of common words or phrases.

python
Copy code
characters = string.ascii_lowercase + string.digits
dictionary_file = "common_passwords.txt" # A file containing common passwords or phrases, one per line
def load_dictionary(file_path):
with open(file_path, 'r') as file:
return [line.strip() for line in file.readlines()]

Step 3: Create the PDF Password Recovery Function

We’ll create a function that first attempts to unlock the PDF using dictionary words, and if that fails, it switches to brute-force.

python
Copy code
def recover_pdf_password(pdf_file, max_length):
pdf_reader = PyPDF2.PdfReader(pdf_file)
dictionary = load_dictionary(dictionary_file)

# Attempt dictionary attack
for word in dictionary:
if pdf_reader.decrypt(word) == 1:
print(f"Password found: {word}")
return word

# If dictionary attack fails, perform brute-force attack
for length in range(1, max_length + 1):
for guess in itertools.product(characters, repeat=length):
password = ''.join(guess)
if pdf_reader.decrypt(password) == 1:
print(f"Password found: {password}")
return password
    print("Password not found")
return None

Step 4: Run the Script

Finally, you can run the script by specifying the PDF file’s path and the maximum length for brute-force attempts.

python
Copy code
pdf_file = "path/to/your/encrypted.pdf"
max_length = 6 # Adjust based on the complexity of the password
recover_pdf_password(pdf_file, max_length)

Explanation:

  1. Dictionary Attack: The script first tries every word or phrase in the dictionary file. This is usually faster and more effective if the password is a common word.
  2. Brute-Force Fallback: If the dictionary attack fails, the script generates all possible combinations of lowercase letters and digits up to the specified maximum length.
  3. Success Check: The decrypt() method returns 1 if the correct password is found, at which point the script prints and returns the password.

Use Case:

This script is useful when you suspect that a password might be a common word but want to ensure all possibilities are covered by also attempting a brute-force attack.

Final Thoughts:

This demonstration combines the efficiency of a dictionary attack with the thoroughness of brute-force, offering a robust solution for recovering passwords from encrypted PDF files.

Get Aardvark Infinity’s stories in your inbox

Join Medium for free to get updates from this writer.

Here are five related hashtags:

  1. #PDFEncryption
  2. #PasswordRecovery
  3. #DictionaryAttack
  4. #CyberSecurity
  5. #DecryptPDF

--

--

Written by Aardvark Infinity

Welcome to Aardvark Infinity Embark on a journey to the future with cutting-edge technology.

Published in Aardvark Infinity

Welcome to Aardvark Infinity Embark on a journey to the future with cutting-edge technology. AI, SCRIPTING, CYBERSECURITY