Python Forum
Brute force password breaker - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Brute force password breaker (/thread-15507.html)



Brute force password breaker - Truman - Jan-20-2019

create a list of word strings by reading this file. Then loop over each word in this list, passing it to the decrypt() method. If this method returns the integer 0, the password was wrong and your program should continue to the next password. If decrypt() returns 1, then your program should break out of the loop and print the hacked password. You should try both the uppercase and lower-case form of each word.
This dictionary.txt file contains words in capital letters.

my code:
import PyPDF2

pdfFile = open('reverse.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFile)
pdfWriter = PyPDF2.PdfFileWriter()
for pageNum in range(pdfReader.numPages):
	pdfWriter.addPage(pdfReader.getPage(pageNum))
pdfWriter.encrypt('inside')
resultPdf = open('encryptedreverse.pdf', 'wb')
pdfWriter.write(resultPdf)
resultPdf.close()
print(pdfReader.isEncrypted)

helloDict = open('dictionary.txt')
helloDictCont = helloDict.read().splitlines()

liDict = []
for word in helloDictCont:
	liDict.append(word)

PdfFile2 = open('encryptedreverse.pdf', 'rb')
pdfReader2 = PyPDF2.PdfFileReader(PdfFile2)
print(pdfReader2.isEncrypted)
	
for word in liDict:
    if pdfReader2.decrypt(word) == 1:
        break
        print(word)
    elif pdfReader2.decrypt(word.lower()) == 1:
        break
        print(word)
    else:
        pass 
As you can see I first encrypted a file and then wrote a code that should decrypt it. The output is that it prints True and then after minutes of work it doesn't give anything. What am I doing wrong?


RE: Brute force password breaker - stullis - Jan-20-2019

I suspect the problem is lines 17 through 19. liDict is identical to helloDictCont. Try printing one of those to see what data are there. I believe that each item in either of those contains multiple, space-separated words that you still need to split out.

Also, lines 32 and 33 are superfluous; the code looks better without them.


RE: Brute force password breaker - Truman - Jan-21-2019

Your belief is correct. Both are lists.
When I try to split liDict get this:
Error:
AttributeError: 'list' object has no attribute 'split'
Suggestion how to do a split is appreciated.

Now I removed unnecessary list:
import PyPDF2

pdfFile = open('reverse.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFile)
pdfWriter = PyPDF2.PdfFileWriter()
for pageNum in range(pdfReader.numPages):
	pdfWriter.addPage(pdfReader.getPage(pageNum))
pdfWriter.encrypt('inside')
resultPdf = open('encryptedreverse.pdf', 'wb')
pdfWriter.write(resultPdf)
resultPdf.close()

helloDict = open('dictionary.txt')
helloDictCont = helloDict.read().splitlines()

PdfFile2 = open('encryptedreverse.pdf', 'rb')
pdfReader2 = PyPDF2.PdfFileReader(PdfFile2)
print(pdfReader2.isEncrypted)

for word in helloDictCont:
	if pdfReader2.decrypt(word) == 1:
		break
		print(word)
	elif pdfReader2.decrypt(word.lower()) == 1:
		break
		print(word)



RE: Brute force password breaker - stullis - Jan-21-2019

Loop over the contents of helloDictCont, split each item, and extend liDict instead of appending.

liDict = []
for words in helloDictCont:
    liDict.extend(words.split())



RE: Brute force password breaker - Truman - Jan-21-2019

while my old machine is looking for a match I found this on a difference between append and extend:
http://thomas-cokelaer.info/blog/2011/03/post-2/

This execution was endless and my hard was seriously overheated so I decided to stop it.


RE: Brute force password breaker - Truman - Jan-22-2019

import PyPDF2

pdfFile = open('reverse.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFile)
pdfWriter = PyPDF2.PdfFileWriter()
for pageNum in range(pdfReader.numPages):
	pdfWriter.addPage(pdfReader.getPage(pageNum))
wrd = input('Please enter one word as a password: ')
pdfWriter.encrypt(wrd)
resultPdf = open('encryptedreverse.pdf', 'wb')
pdfWriter.write(resultPdf)
resultPdf.close()
print(pdfReader.isEncrypted)

helloDict = open('dictionary.txt')
helloDictCont = helloDict.read().splitlines()

liDict = []
for word in helloDictCont:
	liDict.extend(word.split())

PdfFile2 = open('encryptedreverse.pdf', 'rb')
pdfReader2 = PyPDF2.PdfFileReader(PdfFile2)
print(pdfReader2.isEncrypted)
	
for word in liDict:
    if pdfReader2.decrypt(word) == 1:
        break
        print(word)
    elif pdfReader2.decrypt(word.lower()) == 1:
        break
        print(word)
	
I added input() to my code. Now after a couple of minutes of proccessing it ends but it doesn't print a password and it doesn't encrypt a pdf file. Still wondering why.


RE: Brute force password breaker - stullis - Jan-22-2019

A better way to slow it down and decrease memory usage is to use time.sleep(0.01). That will halt execution for 0.01 seconds each iteration; it has a surprisingly significant impact.

In dictionary.txt, are the words separated by just spaces or are there commas as well?


RE: Brute force password breaker - Truman - Jan-23-2019

One line one word.

But this is actually the solution:
for word in liDict:
    if pdfReader2.decrypt(word) == 1:
        print('The correct PWD as upper case: ' + word)
        break
    elif pdfReader2.decrypt(word.lower()) == 1:
        print('The correct PWD as lower case: ' + word)
        break
My mistake was that I put print statement after break.