dictionary 파일을 이용하여 zip파일의 패스워드를 풀어보자.


  1. import zipfile  
  2. from threading import Thread  
  3.   
  4. def crackzip(zipfile, passwd):  
  5.     try:  
  6.         zfile.extractall(pwd = passwd)  
  7.         print "zip file extracted successfully!! PASS = [%s]" %passwd.decode()  
  8.         return True  
  9.     except:  
  10.         pass  
  11.     return False  
  12.   
  13. if __name__ == '__main__':  
  14.     dictfile = 'dictionary.txt'  
  15.     zipfilename = 'zipfile.zip'  
  16.     zfile = zipfile.ZipFile(zipfilename, 'r')  
  17.     f = open(dictfile, 'r')  
  18.   
  19.     for line in f.readlines():  
  20.         passwd = line.strip('\n')  
  21.         t = Thread(target = crackzip, args = (zfile, passwd.encode('utf-8')))  
  22.         t.start()  


[ 코드 1 ] zip 파일 패스워드 크래킹 - zipcracker.py



위의 코드는 파일 이름이 'zipfile.zip' 인 압축 파일을 

dictionary 공격을 이용하여 압축 해제하고 패스워드를 추출한다.


  1. import zipfile  
  2. from threading import Thread  


zip 압축파일을 다루기 위해 zipfile 모듈을 import 한다. 

zipfile 모듈은 zip 파일을 생성하고 읽고 기록하고 압축풀기와 같은 zip파일 관련 다양한 메소드를 제공한다.

스레드를 구동하여 구현할 것이므로 threading 모듈의 Thread 모듈을 import 한다.


  1. def crackzip(zipfile, passwd):  
  2.     try:  
  3.         zfile.extractall(pwd = passwd)  
  4.         print "zip file extracted successfully!! PASS = [%s]" %passwd.decode()  
  5.         return True  
  6.     except:  
  7.         pass  
  8.     return False  


crackzip( zfile, passwd )는 인자로 zipfile.ZipFile 객체 zfile과 사전에 선택한 패스워드 passwd를 인자로 받아

해당 zip파일의 압축 해제를 시도하고 성공하면 압축 해제와 함께 패스워드를 화면에 출력한다.


ZipFile 객체인 zfile의 extractall( pwd = passwd )는 passwd를 패스워드로 하여 zfile의 모든 내용에 대해

압축 해제를 시도한다. 오류가 발생할 때 프로그램이 중단되지 않도록 적절한 예외 처리를 수행한다.


  1. if __name__ == '__main__':  
  2.     dictfile = 'dictionary.txt'  
  3.     zipfilename = 'zipfile.zip'  
  4.     zfile = zipfile.ZipFile(zipfilename, 'r')  
  5.     f = open(dictfile, 'r')  


dictfile은 dictionary 공격을 위한 사전 파일이며 zipfile.zip은 우리가 패스워드 크래킹을 수행할 zip파일이다. 사전파일과 같은 일반 파일을 다루기 위해서 open( )을 사용하여 파일 핸들러를 얻은 후 이를 읽고 쓰는 작업을 수행하지만 zip파일은 ZipFile 객체를 생성하여 zip파일을 다루게된다.


※dictionary.txt는 구글 검색을 통해 쉽게 찾을수 있다.


  1.     for line in f.readlines():  
  2.         passwd = line.strip('\n')  
  3.         t = Thread(target = crackzip, args = (zfile, passwd.encode('utf-8')))  
  4.         t.start()


사전파일에서 한 라인씩 읽어 줄바꿈 문자를 제거하고 변수 passwd에 할당한다.

이후 crackzip( )함수를 독립된 스레드로 호출한다.


스레드를 호출하는 이유는 crackzip( )이 연산하는 동안 for 문을 계속 수행하여 보다 효율적으로 패스워드 크래킹을 하기 위함이다. 










johntheripper 쓰자.........

'Crypto' 카테고리의 다른 글

공개키 암호  (0) 2017.02.05
대칭키 암호  (0) 2017.02.02

+ Recent posts