Skip to content

Instantly share code, notes, and snippets.

@dhondta
Last active April 22, 2024 01:22
Show Gist options
  • Save dhondta/efe84a92e4dfae3b6c14932c73ab2577 to your computer and use it in GitHub Desktop.
Save dhondta/efe84a92e4dfae3b6c14932c73ab2577 to your computer and use it in GitHub Desktop.
Tinyscript tool to bruteforce the password of a PDF

PDF password bruteforcer

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

$ pip install pypdf tinyscript
$ tsm pdf-password-bruteforcer
#!/usr/bin/python3
from tinyscript import *
try:
import pikepdf
BACKEND = "pikepdf"
except ImportError:
import pypdf
BACKEND = "pypdf"
__author__ = "Alexandre D'Hondt"
__version__ = "1.4"
__copyright__ = ("A. D'Hondt", 2020)
__license__ = "gpl-3.0"
__examples__ = ["secret.pdf -p '[a-z0-9]{5}'"]
__doc__ = """
*PDF Password Bruteforcer* allows to execute a bruteforce attack on a given PDF file,
setting a regular expression pattern for the target password.
"""
BANNER_FONT = "tombstone"
BANNER_STYLE = {'fgcolor': "lolcat"}
def bruteforce_pdf_password(path, regex):
if BACKEND == "pypdf":
with open(path, 'rb') as f:
reader = pypdf.PdfReader(f)
for p in ts.bruteforce_re(regex):
logger.debug(p)
try:
reader.decrypt(p)
len(reader.pages)
logger.success("FOUND: " + p)
return True
except pypdf.errors.FileNotDecryptedError:
pass
except Exception as e:
logger.exception(e)
break
else:
for p in ts.bruteforce_re(regex):
logger.debug(p)
try:
with pikepdf.open(path, password=p) as f:
logger.success("FOUND: " + p)
return True
except pikepdf._qpdf.PasswordError:
pass
except Exception as e:
logger.exception(e)
break
return False
if __name__ == '__main__':
parser.add_argument("file", type=ts.file_exists, help="encrypted PDF file")
parser.add_argument("-p", "--pattern", default="^[0-9a-zA-Z!-_]{1,8}", help="password pattern")
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.p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3PExV00o7xE87
HtYdxzB9zXIc0+rNYJ9SVfFVzTStFHFNz6CKmtIlsTSMidUrb/wwY7Juo2wh2E/rjY8uNOEiOgqs
Gibo6OgbzIF/AMArx2CPBgAA
@bcjarrett
Copy link

bcjarrett commented Nov 20, 2023

Here's a little dockerfile if anyone wants it. Just need to put the script in the same folder as this Dockerfile

# Build
FROM centos:7.9.2009

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

ENV APP_HOME=/app
WORKDIR $APP_HOME

# install system dependencies
RUN yum update -y && \
    yum install -y gcc python3 python3-pip python3-devel
RUN pip3 install --upgrade pip

# Install reqs and copy over files from context
COPY . $APP_HOME
RUN chmod +x $APP_HOME/*
RUN pip3 install pypdf tinyscript
docker build -t pdftool .
docker run --rm -it pdftool /bin/bash
>> python3 pdf-password-bruteforcer.py <filename>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment