Skip to content

Update to add prefix #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions crack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import argparse
import re

def crack_password(digits, filename, pattern):
def crack_password(digits, filename, pattern, prefix=''):
n = 1
n_max = 9999999999999999999 % (10**digits)
while n < n_max + 1:
pn = str(n).zfill(digits)
pn = prefix + str(n).zfill(digits) # Add prefix to the password
if pattern is None or pattern.match(pn):
try:
Pdf.open(filename_or_stream = filename, password = pn)
Pdf.open(filename_or_stream=filename, password=pn)
print("Password is " + pn)
return True
except:
Expand All @@ -24,6 +24,7 @@ def crack_password(digits, filename, pattern):
parser.add_argument('-m', '--min-digits', help="Minimum digits in the password", type=int, default="1")
parser.add_argument('-n', '--max-digits', help="Maximum digits in the password", type=int, default="-1")
parser.add_argument('-r', '--matching-regex', help="Skip passwords not matching regex", type=str, default=None)
parser.add_argument('-p', '--prefix', help="Character to add before the numeric password", type=str, default='') # New argument
args = parser.parse_args()

# Check and fix min and max digit values
Expand All @@ -38,13 +39,17 @@ def crack_password(digits, filename, pattern):
pattern = re.compile(args.matching_regex)
print('Skipping passwords not matching:', args.matching_regex)

# Print prefix if provided
if args.prefix:
print('Using prefix:', args.prefix)

# Iterate
digits = min_digits
found_password = False
print("Cracking. Please wait...")
while not found_password and digits <= max_digits:
print("Trying to crack using " + str(digits) + " digit passwords")
found_password = crack_password(digits, args.filename, pattern)
found_password = crack_password(digits, args.filename, pattern, args.prefix) # Pass prefix to function
digits += 1
if not found_password:
print("Could not crack the password")
print("Could not crack the password")