PYTHON LANGUAJE A cyber-security company called UltraHackz is interested in find
ID: 3745835 • Letter: P
Question
PYTHON LANGUAJE
A cyber-security company called UltraHackz is interested in finding talented computer science students. To do so, they post a challenge you find very interesting. They upload a txt file that contains 100 records containing information about 100 system accounts. Each record has a username, a salt value, and a hashed password.
Each record is stored as follows: ,, That is, there is one line per record in the file, where the three values are separated by commas. Your job is to find the real password associated with each of the accounts. They tell you that all passwords contain only numbers (0-9), and that each password is at least 3 characters long, and at most 7 characters long. Your task is to implement a recursive method to generate all possible passwords (brute force). To make it interesting, UltraHackz posted the following rules: The method MUST be recursive Numbers 3 and 7 are not be hard-coded inside the method that generates the passwords; they should be parameters of the method You can have at most two nested loops inside this method (less than two is perfectly fine as well). Every time you generate a string s, you need to check if s is the password of any of the usernames in the file. To do so, concatenate s with a user’s salt value, and apply the hashlib.sha256 method to the resulting string. If the output generated by sha256 matches the hashed string for that account, string s is the real account’s password!
----------------------------------------------------------------------------------------------
CODE GIVEN
import hashlib
def hash_with_sha256(str):
hash_object = hashlib.sha256(str.encode('utf-8'))
hex_dig = hash_object.hexdigest()
return hex_dig
def main():
hex_dig = hash_with_sha256('this is how you hash a string with sha 256')
print(hex_dig)
main()
---------------------------------------------------------------------
Explanation / Answer
import hashlib
from itertools import product
user_details = {}
comList = "0123456789"
list_of_passwords =[]
def hash_with_sha256(str):
hash_object = hashlib.sha256(str.encode('utf-8'))
hex_dig = hash_object.hexdigest()
return hex_dig
def checkForPassword(gen_pass):
for key, value in user_details.items():
if value[2] == hash_with_sha256(value[1].join(gen_pass)):
print("Password Matches user : " + key)
def processFileLine(record):
values = record.split(',')
user_details[values[0]] = values
def generatePassword(lowerLimit, upperLimit):
list_of_passwords = product(comList, repeat=lowerLimit)
lowerLimit += 1
for i in list(list_of_passwords):
gen_pass = ''.join(i)
print("Generated Password : " + gen_pass)
checkForPassword(gen_pass)
if lowerLimit <= upperLimit:
generatePassword(lowerLimit, upperLimit)
def main():
with open('content_file.txt') as openfileobject:
for record in openfileobject:
processFileLine(record)
generatePassword(3,7)
main()
#Provide content_file.txt in the running directory.
#Assumed all usernames will be unique.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.