Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import crypt
- import itertools
- from time import gmtime, strftime
- import fileinput
- def menu():
- RUN = "yes"
- print "Password cracking tool for SECRYPT Coursework 2012"
- print "Group Members: 457040, 480864, 503350"
- print "Please do not distribute without prior permission from"
- print "the University of Portsmouth and group members"
- SOURCEFILE = raw_input("Please first enter the password file to crack: ")
- while RUN == "yes":
- #loop until user types exit
- print
- print "Please choose the desired crack from the following options:"
- print "1. User Data Attack"
- print "2. Dictionary Attacks"
- print "3. Generate Hash Tables"
- print "4. Test Hash Tables"
- print "5. Brute Force"
- print "all All Attacks"
- print "exit Exit the program"
- print
- CHOICE = raw_input("Option: ")
- if CHOICE == "1":
- SimpleUserAttack(SOURCEFILE)
- elif CHOICE == "2":
- DictionaryAttack(SOURCEFILE)
- elif CHOICE == "3":
- GenHashTables()
- elif CHOICE == "4":
- ChkHashTables(SOURCEFILE)
- elif CHOICE == "5":
- BruteForce(SOURCEFILE)
- elif CHOICE == "all":
- SimpleUserAttack(SOURCEFILE)
- DictionaryAttack(SOURCEFILE)
- GenHashTables()
- ChkHashTables(SOURCEFILE)
- BruteForce(SOURCEFILE)
- elif CHOICE == "exit":
- RUN = "no"
- def PASSWORDCHECK(TESTPHRASE, PASSWORD, USERNAME, WORD):
- if TESTPHRASE == PASSWORD:
- print USERNAME, "has the password:", WORD
- def DictionaryAttack(SOURCEFILE):
- print
- print "-----Select Source Files-----"
- DICT = raw_input("Please enter the name of the dictionary to use: ")
- SALT = "aa"
- print
- #Opens the dictionary and the password file
- DICTIONARY = open(DICT, 'r').readlines()
- PASSFILE = open(SOURCEFILE, 'r').readlines()
- #Will run the dictionary unaltered to check for any initial matches
- print "-----Testing Standard Dictionary-----"
- for WORD in DICTIONARY:
- WORD = WORD.rstrip('\n')
- TESTPHRASE = crypt.crypt(WORD, SALT)
- #Splits the single long string of the password file lines up into seperate lists so usernames
- #and passwords can be picked out
- for LINE in PASSFILE:
- TEMP = LINE.split(':')
- PASSWORD = TEMP[1]
- USERNAME = TEMP[0]
- #Passes all the necessary variables to the PASSWORDCHECK function to assess for any matches
- PASSWORDCHECK(TESTPHRASE, PASSWORD, USERNAME, WORD)
- #Will capitalize the start of each passphrase and then check for any further matches
- print
- print "-----Testing Modified Dictionaries-----"
- for WORD in DICTIONARY:
- WORD = WORD.rstrip('\n')
- WORD = WORD.capitalize()
- TESTPHRASE = crypt.crypt(WORD, SALT)
- for LINE in PASSFILE:
- TEMP = LINE.split(':')
- PASSWORD = TEMP[1]
- USERNAME = TEMP[0]
- PASSWORDCHECK(TESTPHRASE, PASSWORD, USERNAME, WORD)
- def SimpleUserAttack(SOURCEFILE):
- PASSFILE = open(SOURCEFILE, 'r').readlines()
- SALT = "aa"
- print
- print "-----Testing Stored User Data-----"
- for LINE in PASSFILE:
- TEMP = LINE.split(':')
- #Stores the hash of the password to be tested
- PASSWORD = TEMP[1]
- USERNAME = TEMP[0]
- #Extracts the additional information field from the password file
- ADDITIONALDATA = TEMP[4]
- #Splits the additional information into a list of useable strings
- TEMP = ADDITIONALDATA.split(',')
- #Stores the number found in the additional information field
- REFNUM = TEMP[1]
- #Stores the users first and last name to be used for testing
- FULLNAME = TEMP[0]
- TEMP = FULLNAME.split(' ')
- FIRSTNAME = TEMP[0]
- SURNAME = TEMP[1]
- TESTPHRASE = SURNAME.lower() + REFNUM
- #Trims the test phrase to an 8 character string as this
- #is the maximum LENGTH for DES encrypted passwords
- TESTPHRASE = TESTPHRASE[:8]
- WORD = TESTPHRASE
- TESTPHRASE = crypt.crypt(TESTPHRASE, SALT)
- #First test uses last name + number
- PASSWORDCHECK(TESTPHRASE, PASSWORD, USERNAME, WORD)
- TESTPHRASE = FIRSTNAME.lower() + SURNAME.lower()
- TESTPHRASE = TESTPHRASE[:8]
- WORD = TESTPHRASE
- TESTPHRASE = crypt.crypt(TESTPHRASE, SALT)
- #Second test uses first name + last name
- PASSWORDCHECK(TESTPHRASE, PASSWORD, USERNAME, WORD)
- TESTPHRASE = FIRSTNAME.lower() + REFNUM
- TESTPHRASE = TESTPHRASE[:8]
- WORD = TESTPHRASE
- TESTPHRASE = crypt.crypt(TESTPHRASE, SALT)
- #Third test uses first name + number
- PASSWORDCHECK(TESTPHRASE, PASSWORD, USERNAME, WORD)
- TESTPHRASE = SURNAME.lower() + FIRSTNAME.lower()
- TESTPHRASE = TESTPHRASE[:8]
- WORD = TESTPHRASE
- TESTPHRASE = crypt.crypt(TESTPHRASE, SALT)
- #Last test uses last name + first name
- PASSWORDCHECK(TESTPHRASE, PASSWORD, USERNAME, WORD)
- def GenHashTables():
- print
- print "-----Hash Table Generator-----"
- CHARSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
- #Allows user to chose start and end point for generation
- LENGTH = int(raw_input("First Table LENGTH to Generate: "))
- LAST = int(raw_input("Last Table LENGTH to Generate: "))
- TMPLENGTH = LENGTH
- SIZENEEDED = float(0)
- while TMPLENGTH <= LAST:
- #Calculates the amount of space needed to store the tables in bytes
- SIZENEEDED = SIZENEEDED + ((15+TMPLENGTH)*pow(62,TMPLENGTH))
- TMPLENGTH = TMPLENGTH + 1
- if SIZENEEDED >= 1073741824:
- #Warns the user if the space needed is more than 1 GB
- SIZENEEDED = '%.2f' % (SIZENEEDED / float(1073741824))
- print "WARNING!!! - This requires " + str(SIZENEEDED) + " GB of space."
- CONTINUE = raw_input("Type 'generate tables' to continue: ")
- if CONTINUE != "generate tables":
- #Only continues if the user types 'generate tables' correctly
- return
- while LENGTH <= LAST:
- #Loops though the tables to be generated
- print strftime("%d-%m-%Y %H:%M:%S - Generating "+str(LENGTH)+" Character Passwords.", gmtime())
- HASHFILE=open("HashTable_" + str(LENGTH), "a+")
- COMBOS = itertools.product(*[CHARSET]*LENGTH)
- for COMBO in COMBOS:
- #Converts the tuple to a string
- COMBO = ''.join(COMBO)
- #Hashes the password using the salt 'aa'
- CRYPT = crypt.crypt(COMBO, "aa")
- #Saves the hash in the format Password:Hash, 1 per line
- TOWRITE = COMBO + ":" + CRYPT + "\n"
- HASHFILE.write(TOWRITE)
- HASHFILE.close()
- LENGTH = LENGTH + 1
- print strftime("%d-%m-%Y %H:%M:%S - Finshed Generating Hash Tables.", gmtime())
- def ChkHashTables(FILE):
- print
- print "-----Hash Table Attack-----"
- #Gets the user to enter the first and last tables they have generated
- LENGTH = int(raw_input("First Hash Table Length: "))
- LAST = int(raw_input("Last Hash Table Length: "))
- FOUND = 0
- while LENGTH <= LAST:
- #loops though the first to last tables
- print strftime("%d-%m-%Y %H:%M:%S - Testing "+str(LENGTH)+" Character Passwords.", gmtime())
- for HASHLINE in fileinput.input("HashTable_" + str(LENGTH)):
- #seperates the password from the hash
- HASHITEMS = HASHLINE.split(':')
- #removes the newline character
- CRYPT = HASHITEMS[1].rstrip('\n')
- for USER in open(FILE, 'r').readlines():
- #Checks the current has agains all the hashes in the provided password file
- USERITEMS = USER.split(':')
- UCRYPT = USERITEMS[1]
- if UCRYPT == CRYPT:
- #If a match is found increment found, inform the user and write the match to file.
- FOUND = FOUND + 1
- print strftime("%d-%m-%Y %H:%M:%S - Found "+str(FOUND)+" Password(s).", gmtime())
- TOWRITE = USERITEMS[0] + ":" + HASHITEMS[0] + "\n"
- FOUNDFILE=open("FoundPasswordsHashTables.txt", "a+")
- FOUNDFILE.write(TOWRITE)
- FOUNDFILE.close()
- LENGTH = LENGTH + 1
- print strftime("%d-%m-%Y %H:%M:%S - Finshed Testing with Hash Tables.", gmtime())
- if FOUND > 0:
- #If matches found print out matches
- print "The following matches where found and saved to FoundPasswordsHashTables.txt"
- for PASSWORDMATCH in open("FoundPasswordsHashTables.txt", 'r').readlines():
- print PASSWORDMATCH.rstrip('\n')
- def BruteForce(FILE):
- print
- print "-----Brute Force Attack-----"
- CHARSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
- #allows the user to set min and max password lengths
- LENGTH = int(raw_input("Length to Start Attack From: "))
- LAST = int(raw_input("Stop Attack at: "))
- FOUND = 0
- while LENGTH <= LAST:
- #loops though Lengths
- print strftime("%d-%m-%Y %H:%M:%S - Testing "+str(LENGTH)+" Character Passwords.", gmtime())
- COMBOS = itertools.product(*[CHARSET]*LENGTH)
- LENGTH = LENGTH + 1
- for COMBO in COMBOS:
- #Converts the tuple to a string
- COMBO = ''.join(COMBO)
- #hashes the combination using the salt 'aa'
- CRYPT = crypt.crypt(COMBO, "aa")
- for USER in open(FILE, 'r').readlines():
- #test the current hash against all hashes in the password file
- USERITEMS = USER.split(':')
- UCRYPT = USERITEMS[1]
- if UCRYPT == CRYPT:
- #if a match is found increment counter, inform user and save the match to file
- FOUND = FOUND + 1
- print strftime("%d-%m-%Y %H:%M:%S - Found "+str(FOUND)+" Password(s).", gmtime())
- TOWRITE = USERITEMS[0] + ":" + COMBO + "\n"
- FOUNDFILE=open("FoundPasswordsBruteForce.txt", "a+")
- FOUNDFILE.write(TOWRITE)
- FOUNDFILE.close()
- print strftime("%d-%m-%Y %H:%M:%S - Finshed Testing with Brute Force.", gmtime())
- if FOUND > 0:
- #if passwords where found print them out
- print "The following matches where found and saved to FoundPasswordsBruteForce.txt"
- for PASSWORDMATCH in open("FoundPasswordsBruteForce.txt", 'r').readlines():
- print PASSWORDMATCH.rstrip('\n')
- if __name__ == "__main__":
- #autostart the menu system
- menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement