Last active
March 1, 2017 14:56
-
-
Save fortable1999/8309b870e822e0e8d3f6a3d6669d6b1b to your computer and use it in GitHub Desktop.
password attack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import zipfile | |
from itertools import product | |
PASSWORD_MAX_LENGTH = 8 | |
ALPHABETS = [chr(i) for i in range(ord('a'), ord('z')+1)] | |
def open_dict_file(filename): | |
''' | |
this function will remind to open the txt file | |
''' | |
with open(filename, 'r') as fp: | |
return [p.strip() for p in fp.readlines()] | |
def open_zip_file(filename, password): | |
''' | |
this function will remind to open the zip file | |
''' | |
zipfile_obj = zipfile.ZipFile(filename) | |
cracked = False | |
try: | |
zipfile_obj.extractall(pwd=password.encode()) | |
return True | |
except RuntimeError: | |
pass | |
except: | |
cracked = True | |
finally: | |
zipfile_obj.close() | |
return cracked | |
def brute_force_attack(filename): | |
''' | |
try password on zip_file object | |
raise exception when failed. | |
''' | |
for length in range(1,PASSWORD_MAX_LENGTH+1): | |
for items in product(ALPHABETS, repeat=length): | |
password = "".join(items) | |
print('try password', password) | |
if open_zip_file(filename, password): | |
return password | |
raise Exception("brute force attack failed!") | |
def dictionary_attack(filename, dictionary): | |
'''using dictionary way to find the password''' | |
for password in dictionary: #read each line of the txt file | |
if open_zip_file(filename, password): | |
return password | |
raise Exception("dictionary attack failed!") | |
def crack_bruteforce(): | |
print("Brute Force Cracking") | |
filename = input("Enter zipfile name: ") | |
start = time.process_time() | |
password = None | |
try: | |
password = brute_force_attack(filename) #the function | |
except RuntimeError as e: | |
print(e) | |
end = time.process_time() | |
print("Elapsed time (%s):" % str(end - start)) #print the time | |
return password | |
def crack_dictionary(): | |
print("Dictionary Cracking") | |
filename = input("Enter zipfile name: ") | |
dictionary_path = input("Input dictionary file name") | |
dictionary = open_dict_file(dictionary_path) | |
start = time.process_time() | |
password = None | |
try: | |
password = dictionary_attack(filename, dictionary) #the function | |
except RuntimeError as e: | |
print(e) | |
end = time.process_time() | |
print("Elapsed time (%s):" % str(end - start)) #print the time | |
return password | |
if __name__ == '__main__': | |
print(""" | |
Cracking zip files. | |
Warning cracking passwords is illegal due to law Computer Fraud and Abuse Act (CFAA) and has a prison term of 20 years | |
""") | |
while True: | |
algorithm = input("What type of cracking ('brute force','dictionary','both','q'): ") | |
if algorithm == 'brute force': #using brute force to find the password | |
print(crack_bruteforce()) | |
elif algorithm == 'dictionary': #using dictionary to find the password | |
print(crack_dictionary()) | |
elif algorithm == 'both': | |
password = crack_dictionary() | |
if not password: | |
password = crack_bruteforce() | |
print(password) | |
elif algorithm == 'q': | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import time
import zipfile
from itertools import product
PASSWORD_MAX_LENGTH = 8
ALPHABETS = [chr(i) for i in range(ord('a'), ord('z')+1)]
def open_dict_file(filename):
'''
this function will remind to open the txt file
'''
with open(filename, 'r') as fp:
return [p.strip() for p in fp.readlines()]
def open_zip_file(zipfile_obj, password):
'''
this function will remind to open the zip file
'''
try:
zipfile_obj.extractall(pwd=password.encode())
return True
except RuntimeError:
return False
def brute_force_attack(zip_file):
'''
try password on zip_file object
raise exception when failed.
'''
for length in range(1,PASSWORD_MAX_LENGTH+1):
#print('try repeat')
for items in product(ALPHABETS, repeat=length):
password = "".join(items)
#print('try password', password)
if open_zip_file(zip_file, password):
return password
raise Exception("brute force attack failed!")
def dictionary_attack(zip_file, dictionary):
'''using dictionary way to find the password'''
for password in dictionary: #read each line of the txt file
if open_zip_file(zip_file, password):
return password
raise Exception("No password found!")
def crack_bruteforce():
print("Brute Force Cracking")
filename = input("Enter zipfile name: ")
zipfile_object = zipfile.ZipFile(filename, 'r')
start = time.process_time()
password = None
try:
password = brute_force_attack(zipfile_object) #the function
except RuntimeError as e:
print(e)
finally:
end = time.process_time()
zipfile_object.close()
print("Elapsed time (%s):" % str(end - start)) #print the time
return password
def crack_dictionary():
print("Dictionary Cracking")
if name == 'main':
print("""
Cracking zip files.
Warning cracking passwords is illegal due to law Computer Fraud and Abuse Act (CFAA) and has a prison term of 20 years
""")
while True: