Skip to content

Instantly share code, notes, and snippets.

@dhondta

dhondta/README.md

Last active Jun 7, 2020
Embed
What would you like to do?
Tinyscript tool to bruteforce the password of a PDF

PFD password bruteforcer

This is a small tool using Tinyscript and PyPDF2 to bruteforce the password of a PDF given an alphabet (defaults to printables) and a length (default is 8).

#!/usr/bin/env python
import PyPDF2
from tinyscript import *
__author__ = "Alexandre D'Hondt"
__email__ = "alexandre.dhondt@gmail.com"
__examples__ = ["secret.pdf -a xyzZ1 -l 5 -v"]
def bruteforce_pdf_password(path, alphabet, length):
with open(path, 'rb') as f:
reader = PyPDF2.PdfFileReader(f)
for p in ts.bruteforce(length, alphabet, minlen=length):
logger.debug(p)
try:
reader.decrypt(p)
reader.getNumPages()
logger.success("FOUND: " + p)
return True
except:
pass
return False
if __name__ == '__main__':
parser.add_argument("file", type=ts.file_exists, help="encrypted PDF file")
parser.add_argument("-a", "--alphabet", default=string.printable.replace("%", "%%"), # .replace(...) avoids string
help="alphabet to be used for bruteforce") # interpolation error
parser.add_argument("-l", "--length", type=ts.pos_int, default=8, help="password length")
initialize(add_time=True, noargs_action="demo", action_at_interrupt="confirm")
logger.info("Starting PDF password bruteforce...")
logger.handlers[-1].terminator = ""
if not bruteforce_pdf_password(args.file, args.alphabet, args.length):
logger.failure("Password not found")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment