2017-02-07 5 views
-1

현재 진행중인 과제에 대해 잠긴 PDF 파일을 해독해야합니다. 잠재적 인 암호를 생성하는 while 루프가 있는데 그 중 하나가 PDF 파일을 해독 할 것입니다. 또한 단어 목록을 사용하여 PDF 파일을 크랙하는 프로그램도 있습니다. 루프PDF 파일 크래킹 2.7, 파이썬 2.7

동안 :

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 크래커가 :

이 과제를, I는 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') 

, 그것을이다 다음과 같이 두 프로그램의 코드는 이 두 프로그램을 결합 할 수 있으므로 루프를 사용하여 단어 목록을 사용하는 대신 파일을 크랙 할 수 있습니다.

이것은 현재 나의 숙련도 인 파이썬을 약간 넘어 섰고, 며칠 동안이 문제로 어려움을 겪어 왔습니다.

답변

1

1) 프로그램을 종료하려면 무한 while 루프를 제거해야합니다.

itertools.permutations을 사용하여 단어 목록에서 3 단어를 선택하는 모든 순열을 얻을 수 있습니다. 예를 들어

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

출력됩니다

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

그래서 대신

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

우리가 모든 순열 반복을

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') 

그런 다음이 대신 루프 for line in wordlist.readlines(): 내부에 해독을 시도, 당신은 내가 올바른 암호를 찾을 때까지 while 루프가 생성하는 각 암호를 시도하고 종료 할 수있는 프로그램을 싶습니다

+0

for perm in itertools.permutations(data, 3): 내부의 암호를 해독하려고 시도. 나는 itertools.permutations을 완전히 이해하지 못한다. 당신이 작성한 코드가 내 코드에서 내가 원하는 방식으로 실행되도록 허용합니까? 아니면 거기에 조금 더 있습니까? – Quarismo

+0

itertools.permutations 예제로 내 대답을 업데이트하고 루프가 어떻게 생겼는지를 추가했습니다. 이런 순열을 사용하는 것이 의도 한 것과 다른지 말해봐. – avodo