This repository has been archived by the owner on Apr 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Password_Crack.py
147 lines (133 loc) · 6.89 KB
/
Password_Crack.py
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
import itertools
import msoffcrypto
import pikepdf
import string
import subprocess
def main(): # main function to select function
print("Which file would you like to decrypt?")
which = int(input(
"1 = .pdf, 2 = .docx, 3 = PGP, 11 =simple brute-force a password (only passwords with 1-5 letters)\n99 = "
"Stop! : "))
if which == 1:
try: # start functions, if error returned print error-message
file_pdf = str(input("Which PDF-File?\n")) # enter file to search password
decrypt_pdf(file_pdf)
except:
print("Error!")
elif which == 2:
try:
file_docx = str(input("Which DOCX/XLSX-File?\n"))
decrypt_docx(file_docx)
except:
print("Error!")
elif which == 3:
try:
file_gpg = str(input("Which File?\n"))
decrypt_gpg(file_gpg)
except:
print("Error!")
elif which == 11:
try:
print(brute_force())
except:
print("Error!")
elif which == 99: # if you still know the password. :D
print("Stopping...")
exit(0)
else:
print("not implemented yet") # if you're stupid :D
input("Press any key to continue...")
def brute_force(): # to proof how long (attempts) the algorithm need to find password, use this.
password = input("which password do you want to brute-force? ") # input searched password
chars = string.ascii_letters + string.digits # all ascii-letters and digits
attempts = 0 # count attempts
if (len(password) <= 5): # proof password-size
for plen in range(1, 6): # all passwords with 1 to 5 letters
for guess in itertools.product(chars, repeat=plen): # guess = generated password
attempts += 1 # increment counter
guess = ''.join(guess)
# print(guess,attempts) #for debug, print generated password and attempt
if guess == password: # proof guess = password
return ("[BRUTE-FORCE]: found password! password: {} "
"with {} attempts".format(guess, attempts)) # if true print password and attempts
else:
print("password too long!")
def decrypt_pdf(file_pdf):
chars = string.ascii_letters + string.digits
attempts = 0
print("Searching for password!\nThis may take long time...") # print that you can go shopping :D
for plen in range(1, 6): # brute-force procedure already the same
for guess in itertools.product(chars, repeat=plen):
attempts += 1
guess = ''.join(guess)
# print(guess,attempts) #Debug
try:
pdf = pikepdf.open(file_pdf, password=guess) # try start pikepdf with open the file (declared as
# file_pdf) and generated password
pdf.save('decrypted.pdf') # save opened pdf-file decrypted in new file
print("[PDF BRUTE-FORCE]: found password! "
"password: {} with {} attempts".format(guess, attempts)) # print that you've won :D
return True
except:
# print(str(attempts)+" not correct!") #Debug
continue # if open failed, continue with next password
def decrypt_docx(file_docx):
chars = string.ascii_letters + string.digits
attempts = 0
print("Searching for password!\nThis may take long time...") # print that you can go shopping :D
for plen in range(1, 6): # already the same
for guess in itertools.product(chars, repeat=plen):
attempts += 1
guess = ''.join(guess)
# print(guess,attempts) #Debug
try:
file = msoffcrypto.OfficeFile(open(file_docx, "rb")) # try start msoffcrypto-tool as OfficeFile with
# file-name and read-access only
file.load_key(password=guess) # if password required, take the generated
file.decrypt(open("decrypted.docx",
"wb")) # if password correct, open new file with write-access and copy content in it
print("[DOCX, XLSX BRUTE-FORCE]: found password! password: {} with {} attempts".format(guess, attempts))
return True
except:
# print(str(attempts)+"not correct!") #Debug
continue # otherwise continue with next password
def decrypt_gpg(file_gpg):
chars = string.ascii_letters + string.digits
attempts = 0
print("Searching for password!\nThis may take long time...") # print that you can go shopping :D
for plen in range(1, 6): # already the same
for guess in itertools.product(chars, repeat=plen):
attempts += 1
guess = ''.join(guess)
# print(guess,attempts) #Debug
try:
if checkPassword(file_gpg, guess): # try get true by using function checkPassword which use the file
# as file_gpg and generated password
print("[GPG BRUTE-FORCE]: found password! "
"password: {} with {} attempts".format(guess, attempts)) # print success!
return True
except:
# print(str(attempts)+" not correct!") #Debug
continue # otherwise next password
def checkPassword(filename, password): # function to check password from gpg-encrypted files
output = ""
try: # try create new subprocess with check_output function. Execute command at shell.
# gpg = start gpg, --pinentry-mode loopback = send a password directly to GnuPG,
# rather than GnuPG itself prompting for the password.
# --output decrypted_gpg.txt = after decryption save it decrypted in txt-file
# --batch --yes = execute int batch true
# --passphrase password = generated password from function decrypt_gpg()
# -d filename = encrypted file to decrypt
# shell = True --> open in shell
subprocess.check_output(
"gpg --pinentry-mode loopback --output decrypted_gpg.txt --batch --yes --passphrase " + password +
" -d " + filename + " 2>&1", shell=True)
return True # if executed without errors return True and password was correct
except subprocess.CalledProcessError as e: # if subprocess-error you can print it out
# out = str(e.output) #Debug
# print(out) #Debug
return False # password wasn´t correct
except:
return False # if other error return False --> next password
if __name__ == "__main__": # declare function main() as first executed function
main()