Python Forum
Brute force password breaker
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Brute force password breaker
#1
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?
Reply
#2
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.
Reply
#3
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)
Reply
#4
Loop over the contents of helloDictCont, split each item, and extend liDict instead of appending.

liDict = []
for words in helloDictCont:
    liDict.extend(words.split())
Reply
#5
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.
Reply
#6
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.
Reply
#7
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?
Reply
#8
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Brute force for CTF (easy one) JokerTux 1 2,292 Feb-18-2020, 08:05 PM
Last Post: JokerTux
  Why can't I force an int to be a string? fad3r 4 3,317 Feb-13-2018, 11:17 PM
Last Post: fad3r
  brute force skriff 4 3,807 Sep-12-2017, 06:23 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020