Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
6005CEM-Security-CW2/bruteforce-delay-checker.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
62 lines (46 sloc)
3.73 KB
This file contains 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 json | |
from datetime import datetime | |
def readFile(filepath): #writes the given data to the given JSON file | |
with open(filepath, "r") as jsonFile: #opens the file as a JSON file | |
data = json.load(jsonFile) #reads and stores the data within the file | |
return data | |
def writeFile(filepath, data): #writes the given data to the given JSON file | |
with open(filepath, "w") as jsonFile: #opens the file as a JSON file | |
json.dump(data, jsonFile) #writes the given data to the file | |
def getCurrentDelay(data): #finds the current delay | |
start_delay = stringtimeToTime(getLastAttempt(data)) #finds the time of the last attempt | |
now = datetime.now() #gets the current time | |
difference = (now - start_delay).total_seconds() #finds the difference between the two times in seconds | |
return difference | |
def getLastAttempt(data): #gets the time of the last attempt | |
if None in data['attempt_times']: #checks to see if there are current empty slots | |
last_attempt_index = data['attempt_times'].index(None) - 1 #finds the time before the next empty slot | |
else: | |
last_attempt_index = len(data['attempt_times']) - 1 #otherwise returns the last time in the array | |
return data['attempt_times'][last_attempt_index] | |
def checkDelay(data): #checks to see if the currently delay meets requirements | |
return (getCurrentDelay(data) >= data['required_delay']) #requirements are stored externally in the file | |
def resetGuesses(data): #resets the current set of guess slots | |
data['attempt_times'] = [None, None, None, None, None] #sets all the guess slots to emtpy | |
return data | |
def updateAttempts(data): #updates the next guess slot with the current time | |
attempt_index = data['attempt_times'].index(None) #gets the index of the next emtpy slot | |
data['attempt_times'][attempt_index] = timeToString(datetime.now()) #fills the slot with the current time | |
return data | |
def timeToString(time): #converts time from datetime to string | |
return str(time) | |
def stringtimeToTime(stringtime): #converts the time from string to datetime | |
return datetime.strptime(stringtime, "%Y-%m-%d %H:%M:%S.%f") | |
filepath = 'bruteforce-data.json' #defines the given filepath used in the program | |
data = readFile(filepath) #reads and stores all the data currently in the given file | |
password = input("please enter the password: ") #takes the users password input | |
if None in data['attempt_times']: #checks to see if there are empty guess slots | |
updateAttempts(data) #updates the next guess slot with the current time | |
elif checkDelay(data): #checks for sufficient time delay between a set of incorrect guesses | |
data = resetGuesses(data) #resets the current set of guess slots | |
data = updateAttempts(data) #updates the next guess slot with the current time | |
else: #prints a message to the user indicating they must delay their guesses | |
print("im sorry please wait before trying again") | |
print("there is a required delay of {} seconds".format(data['required_delay'])) | |
print("you only waited {} seconds".format(int(getCurrentDelay(data)))) | |
writeFile(filepath, data) #writes the changes made back to the given file |