S-H-Hausarbeit/teil2.py
2025-06-07 20:06:19 +02:00

72 lines
2.0 KiB
Python

import csv
from faker import Faker
from hashlib import sha256
from tabulate import tabulate
from os import system
from sys import argv
from time import sleep
ROUNDS = 10
fake = Faker('de_DE')
def print_output_table(data):
system('clear')
headers = ["username" , "password", "hash-pwd (short)", "tries"]
print(tabulate(data, headers=headers, tablefmt="rounded_grid", numalign="center", stralign="center", showindex="always") + "\n\n")
def load_dict(filename) -> dict:
dummy_dict = {}
with open(filename, 'r', encoding='utf-8', newline='') as head:
reader = csv.reader(head)
next(reader)
for row in reader:
dummy_dict[row[1]] = row[0]
return dummy_dict
#loaded dict
def bruteforce_pwd(dummy_dict) -> list:
data = []
success = False
roundsum = 0
for _ in range(ROUNDS):
success = False
roundsum = 0
while not success:
roundsum += 1
guess_password = fake.password(length=5,special_chars=False,digits=False,upper_case=False,lower_case=True)
hash_func = sha256()
hash_func.update(guess_password.encode('utf-8'))
hash_value = hash_func.hexdigest()
if hash_value in dummy_dict:
wert = dummy_dict[hash_value]
data.append([ wert, guess_password, hash_value[:5] + "[...]" + hash_value[-5:], str(roundsum)])
del dummy_dict[hash_value]
#print(f"{wert} | {guess_password} > {roundsum} tries")
success = True
print_output_table(data)
sleep(0.3)
return data
def main():
global ROUNDS
try:
if len(argv) == 2:
ROUNDS = int(argv[1])
except ValueError as e:
print(e)
exit(-1)
filename = "dummy_file.csv"
dummy_dict = load_dict(filename)
data = bruteforce_pwd(dummy_dict)
#print_output_table(data)
if __name__ == "__main__":
main()