简体   繁体   English

破解PDF文件,Python 2.7

[英]Cracking a PDF File, Python 2.7

For an assignment I'm currently doing, I have to crack a locked PDF File. 对于当前正在执行的任务,我必须破解一个锁定的PDF文件。 I've got a while loop that creates potential passwords, one of which will crack the PDF File. 我有一个while循环,可以创建潜在的密码,其中一个会破解PDF文件。 I also have a program that cracks PDF Files through the use of a wordlist. 我也有一个程序可以通过使用单词表来破解PDF文件。 The code for both programs is as follows: 这两个程序的代码如下:

While Loop: While循环:

from random import shuffle
with open('randomwords.txt', 'r') as data:
    data = data.read().split()
    while(True):
        shuffle(data)
        password = ''
        for x in data[:3]:
            password += x
        print password.replace('o', '0')

PDF Cracker: PDF饼干:

import PyPDF2
import sys
import optparse

parser = optparse.OptionParser()
parser.add_option('-f', '--file', dest='file', help='encrypted file')
parser.add_option('-w', '--wordlist', dest='word', help='wordlist file')
(options, args) = parser.parse_args()
if options.file == None or options.word == None:
    print('')
    sys.exit()

file = options.file
word = options.word
wordlist = open(word)

pdf = PyPDF2.PdfFileReader(open(file,'rb'))
if not pdf.isEncrypted:
    print('This PDF has no password')
else:
    for line in wordlist.readlines():
        if pdf.decrypt(line.rstrip()):
            print('[+] Password: ' +line)
            sys.exit()
        print('[-] Password not found')

In order to complete this assignment, I have to crack the PDF File, is it possible to combine these two programs, so I can use the loop to crack the file instead of using a wordlist. 为了完成此任务,我必须破解PDF文件,是否可以合并这两个程序,因此我可以使用循环来破解文件,而不是使用单词表。

This is a bit beyond my current skill level with Python, have been struggling with this problem for a few days now. 这超出了我目前使用Python的技能水平,已经为这个问题苦苦挣扎了几天。

1) If you want the program to terminate, we'll have to get rid of the infinite while loop. 1)如果您希望程序终止,则必须摆脱无限的while循环。

We can use the itertools.permutations to get all permutations of selecting 3 words from the wordlist. 我们可以使用itertools.permutations获取从单词表中选择3个单词的所有排列。

For example 例如

wordlist = ['dog', 'cat', 'bat']
for p in itertools.permutations(wordlist, 2):
  print p

will output 将输出

('dog', 'cat')
('dog', 'bat')
('cat', 'dog')
('cat', 'bat')
('bat', 'dog')
('bat', 'cat')

So instead of 所以代替

while(True):
        shuffle(data)
        password = ''
        for x in data[:3]:
            password += x
        password.replace('o', '0')

we iterate over all the permutations 我们遍历所有排列

for perm in itertools.permutations(data, 3):
  password = "".join(perm)
  password.replace('o', '0')
  if pdf.decrypt(password):
      print('[+] Password: ' +line)
      sys.exit()
  print('[-] Password not found')

Then instead of attempting to decrypt inside this loop for line in wordlist.readlines(): , you attempt to decrypt inside for perm in itertools.permutations(data, 3): 然后,您尝试for perm in itertools.permutations(data, 3):内部解密,而不是尝试for line in wordlist.readlines():该循环内部for perm in itertools.permutations(data, 3):解密for perm in itertools.permutations(data, 3):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM