-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_crack_script.py
More file actions
148 lines (112 loc) · 5.06 KB
/
Copy pathpdf_crack_script.py
File metadata and controls
148 lines (112 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import PyPDF2
import os
import sys
import glob
# ──────────────────────────────────────────────
# AUTO PATH DETECTION
# All files are expected to be in the same
# folder as this script. No editing required.
# ──────────────────────────────────────────────
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
import zipfile
WORDLIST_ZIP = "common_passwords.zip"
WORDLIST_NAME = "common_passwords.txt"
WORDLIST_ZIP_PATH = os.path.join(SCRIPT_DIR, WORDLIST_ZIP)
WORDLIST_PATH = os.path.join(SCRIPT_DIR, WORDLIST_NAME)
def extract_wordlist():
"""Extract wordlist automatically if not already extracted."""
if os.path.exists(WORDLIST_PATH):
return
if not os.path.exists(WORDLIST_ZIP_PATH):
print(f"[!] Missing {WORDLIST_ZIP}")
sys.exit(1)
print("[*] Extracting wordlist...")
try:
with zipfile.ZipFile(WORDLIST_ZIP_PATH, 'r') as zip_ref:
zip_ref.extractall(SCRIPT_DIR)
print("[+] Wordlist extracted successfully.")
except Exception as e:
print(f"[!] Failed to extract wordlist: {e}")
sys.exit(1)
def find_pdf():
"""Find a PDF file in the same folder as this script (excludes already-unlocked ones)."""
pdf_files = [
f for f in glob.glob(os.path.join(SCRIPT_DIR, "*.pdf"))
if not os.path.basename(f).startswith("unlocked_")
]
if len(pdf_files) == 0:
return None
if len(pdf_files) == 1:
return pdf_files[0]
# If multiple PDFs found, ask the user to pick one
print("\n[?] Multiple PDF files found in this folder:")
for i, f in enumerate(pdf_files):
print(f" [{i + 1}] {os.path.basename(f)}")
while True:
try:
choice = int(input("\nEnter the number of the PDF you want to unlock: "))
if 1 <= choice <= len(pdf_files):
return pdf_files[choice - 1]
except ValueError:
pass
print("[!] Invalid choice. Please try again.")
def crack_pdf(pdf_path, wordlist_path):
pdf_name = os.path.splitext(os.path.basename(pdf_path))[0]
output_path = os.path.join(SCRIPT_DIR, f"unlocked_{pdf_name}.pdf")
print(f"\n[*] Target PDF : {os.path.basename(pdf_path)}")
print(f"[*] Wordlist : {WORDLIST_NAME}")
print(f"[*] Output will : unlocked_{pdf_name}.pdf")
print("-" * 50)
try:
with open(pdf_path, "rb") as file:
reader = PyPDF2.PdfReader(file)
if not reader.is_encrypted:
print("[*] This PDF is not password-protected. Nothing to crack.")
return
print("[*] PDF is encrypted. Starting password search...\n")
with open(wordlist_path, "r", encoding="utf-8", errors="ignore") as wordlist:
for count, word in enumerate(wordlist):
password = word.strip()
try:
if reader.decrypt(password):
print(f"\n[+] Password found : {password}")
print(f"[+] Saving decrypted copy...")
writer = PyPDF2.PdfWriter()
for page in reader.pages:
writer.add_page(page)
with open(output_path, "wb") as out_file:
writer.write(out_file)
print(f"[+] Done! Saved as : unlocked_{pdf_name}.pdf")
return
except Exception:
continue
if count % 1000 == 0 and count > 0:
print(f"[*] Tried {count:,} passwords so far...")
print("\n[-] Password not found in the wordlist.")
print(" Try a different or larger wordlist.")
except FileNotFoundError:
print(f"[!] Error: Could not open '{os.path.basename(pdf_path)}'")
except Exception as e:
print(f"[!] Unexpected error: {e}")
# ──────────────────────────────────────────────
# MAIN
# ──────────────────────────────────────────────
if __name__ == "__main__":
print("=" * 50)
print(" PDF Password Cracker")
print("=" * 50)
extract_wordlist()
# Check wordlist exists
if not os.path.exists(WORDLIST_PATH):
print(f"\n[!] Wordlist not found: {WORDLIST_NAME}")
print(f" Make sure '{WORDLIST_NAME}' is in the same folder as this script.")
sys.exit(1)
# Find the PDF
pdf_file = find_pdf()
if pdf_file is None:
print("\n[!] No PDF file found in this folder.")
print(" Place your locked PDF in the same folder as this script and try again.")
sys.exit(1)
crack_pdf(pdf_file, WORDLIST_PATH)
print("\n[*] Finished.")
input(" Press Enter to close...")