|
| 1 | +# Security system |
| 2 | + |
| 3 | +# imports |
| 4 | +import os |
| 5 | +import hashlib |
| 6 | +import binascii |
| 7 | +import json |
| 8 | +import time |
| 9 | +import sys |
| 10 | + |
| 11 | +sys.path.append(os.path.abspath('..\\inc\\')) |
| 12 | + |
| 13 | +import screen as s |
| 14 | +import helpers as h |
| 15 | + |
| 16 | +# classes |
| 17 | +class User: |
| 18 | + min_len = 8 |
| 19 | + allow_spaces = 0 |
| 20 | + allow_leading_number = 0 |
| 21 | + locked = 0 |
| 22 | + lockouts = 0 |
| 23 | + lockout_release = 0 |
| 24 | + attempts = 0 |
| 25 | + attempt_limit = 3 |
| 26 | + lockout_period = 60 |
| 27 | + loggedin = 0 |
| 28 | + |
| 29 | + def __init__(self, username): |
| 30 | + self.username=username |
| 31 | + if username in a_users.keys(): |
| 32 | + self.passwordhash=a_users[username] |
| 33 | + else: |
| 34 | + self.passwordhash="" |
| 35 | + |
| 36 | + if username in a_lockout.keys(): |
| 37 | + if time.time() < a_lockout[username]["r"]: |
| 38 | + self.locked=1 |
| 39 | + self.lockout_release = a_lockout[username]["r"] |
| 40 | + self.lockouts= a_lockout[username]["a"] |
| 41 | + |
| 42 | + |
| 43 | + def check_password_complexity(self, password): |
| 44 | + valid_pass = 1 |
| 45 | + s_err="" |
| 46 | + try: |
| 47 | + if self.min_len > 0: |
| 48 | + if len(password) < self.min_len: |
| 49 | + s_err += f"* Minimum length {self.min_len}\n" |
| 50 | + valid_pass = 0 |
| 51 | + if self.allow_spaces == 0: |
| 52 | + if " " in password: |
| 53 | + s_err += f"* No spaces" |
| 54 | + valid_pass = 0 |
| 55 | + if self.allow_leading_number == 0: |
| 56 | + if password[0].isdigit(): |
| 57 | + s_err += f"* Cannot begin with a number" |
| 58 | + valid_pass = 0 |
| 59 | + except: |
| 60 | + pass |
| 61 | + |
| 62 | + if valid_pass == 0: |
| 63 | + print ("Invalid Password. Please ensure it meets the minimum requirements: ") |
| 64 | + print ( s_err ) |
| 65 | + |
| 66 | + return valid_pass |
| 67 | + |
| 68 | + def set_password(self, password): |
| 69 | + if self.check_password_complexity(password) == 1: |
| 70 | + self.passwordhash=self.hash_password(password) |
| 71 | + |
| 72 | + |
| 73 | + def hash_password(self,password): |
| 74 | + salt =hashlib.sha256(os.urandom(60)).hexdigest().encode('ascii') |
| 75 | + pwdhash = hashlib.pbkdf2_hmac('sha512', password.encode('utf-8'), |
| 76 | + salt, 200000) |
| 77 | + pwdhash = binascii.hexlify(pwdhash) |
| 78 | + return (salt + pwdhash).decode('ascii') |
| 79 | + |
| 80 | + def verify_password(self,provided_password): |
| 81 | + f_return = False |
| 82 | + if self.is_locked_out(): |
| 83 | + t_release = time.strftime("%d/%m/%Y %H:%M:%S", time.gmtime(self.lockout_release)) |
| 84 | + print ( f"Sorry your account is locked out until {t_release}.\nPlease try again later") |
| 85 | + else: |
| 86 | + salt = self.passwordhash[:64] |
| 87 | + pwdhash = hashlib.pbkdf2_hmac('sha512', |
| 88 | + provided_password.encode('utf-8'), |
| 89 | + salt.encode('ascii'), |
| 90 | + 200000) |
| 91 | + stored_password = self.passwordhash[64:] |
| 92 | + pwdhash = binascii.hexlify(pwdhash).decode('ascii') |
| 93 | + f_return = (pwdhash == stored_password) |
| 94 | + if f_return==True: |
| 95 | + self.loggedin = 1 |
| 96 | + elif f_return == False: |
| 97 | + print ( "Sorry that username / password combination wasn't recognised. \nPlease try again") |
| 98 | + self.attempts += 1 |
| 99 | + if self.attempts >= self.attempt_limit: |
| 100 | + # reset attempts |
| 101 | + self.attempts = 0 |
| 102 | + # unlock account |
| 103 | + self.locked = 1 |
| 104 | + # increment the punishment counter |
| 105 | + self.lockouts +=1 |
| 106 | + # set the release time |
| 107 | + self.lockout_release = int(time.time()) + (self.lockout_period * self.lockouts) |
| 108 | + a_lockout[self.username] = {"r": self.lockout_release, "a": self.lockouts} |
| 109 | + |
| 110 | + return f_return |
| 111 | + |
| 112 | + def is_locked_out(self): |
| 113 | + f_locked=0 |
| 114 | + if self.locked == 1: |
| 115 | + t_now = time.time() |
| 116 | + t_lockout = self.lockout_release |
| 117 | + if t_now < t_lockout: |
| 118 | + f_locked = 1 |
| 119 | + else: |
| 120 | + self.locked=0 |
| 121 | + self.lockout_release=0 |
| 122 | + return f_locked |
| 123 | + |
| 124 | + |
| 125 | +def load_file(): |
| 126 | + global a_users |
| 127 | + |
| 128 | + if os.path.isfile(s_filename): |
| 129 | + with open(s_filename) as sec_file: |
| 130 | + a_users=json.load(sec_file) |
| 131 | + pass |
| 132 | + |
| 133 | + else: |
| 134 | + # nothing to do |
| 135 | + pass |
| 136 | + |
| 137 | +def write_file(): |
| 138 | + with open(s_filename, "w") as sec_file: |
| 139 | + json.dump(a_users,sec_file) |
| 140 | + pass |
| 141 | + |
| 142 | +# definitions |
| 143 | +s_filename = "security_checker_users.json" |
| 144 | +a_users = {} |
| 145 | +a_lockout = {} |
| 146 | + |
| 147 | +# Load the security file first. |
| 148 | +load_file() |
| 149 | + |
| 150 | +# debug |
| 151 | +#print (a_users) |
| 152 | + |
| 153 | +while True: |
| 154 | + print ( "Password Verifier 2000 " ) |
| 155 | + print ( "-----------------------") |
| 156 | + print ( "1. New User" ) |
| 157 | + print ( "2. Existing User" ) |
| 158 | + print ( "3. Print Users" ) |
| 159 | + print ( "0. Exit" ) |
| 160 | + print ( "-----------------------") |
| 161 | + print ( "" ) |
| 162 | + i_menu = h.get_int("Please enter your selection",1,3) |
| 163 | + |
| 164 | + if i_menu == 0: |
| 165 | + break |
| 166 | + elif i_menu == 1: |
| 167 | + s_usr = input( "Please enter your username [Enter to quit]: ") |
| 168 | + if s_usr=="": |
| 169 | + break |
| 170 | + |
| 171 | + if s_usr in a_users.keys(): |
| 172 | + print ( f"{s.REDON}Username {s_usr} is already taken.{s.COLOUROFF}") |
| 173 | + time.sleep(1) |
| 174 | + break |
| 175 | + |
| 176 | + usr = User(s_usr) |
| 177 | + |
| 178 | + while True: |
| 179 | + print ( f"Please enter a password for new user {s_usr}") |
| 180 | + s_p_1 = input ( "Please enter the new password: ") |
| 181 | + if s_p_1 == "": |
| 182 | + break |
| 183 | + s_p_2 = input ( "Please re-enter the new password: ") |
| 184 | + |
| 185 | + if s_p_1 == s_p_2: |
| 186 | + if usr.check_password_complexity(s_p_1) == 1: |
| 187 | + usr.set_password(s_p_1) |
| 188 | + a_users[usr.username] = usr.passwordhash |
| 189 | + write_file() |
| 190 | + print ( f"{s.GREENON}User {usr.username} added successfully.{s.COLOUROFF} ") |
| 191 | + time.sleep (1) |
| 192 | + break |
| 193 | + else: |
| 194 | + print (f"{s.REDON}Password complexity rules not met.{s.COLOUROFF}\nPlease try again") |
| 195 | + else: |
| 196 | + print (f"{s.REDON}Passwords do not match.{s.COLOUROFF}\nPlease try again") |
| 197 | + elif i_menu == 2: |
| 198 | + |
| 199 | + s_usr = input( "Please enter your username [Enter to quit]: ") |
| 200 | + if s_usr=="": |
| 201 | + break |
| 202 | + |
| 203 | + print ( f"Welcome back {s_usr}.") |
| 204 | + if s_usr in a_users.keys(): |
| 205 | + usr = User(s_usr) |
| 206 | + |
| 207 | + if usr.is_locked_out(): |
| 208 | + t_release = time.strftime("%d/%m/%Y %H:%M:%S", time.gmtime(usr.lockout_release)) |
| 209 | + print ( f"{s.REDON}Sorry your account is locked out until {t_release}.{s.COLOUROFF}\nPlease try again later") |
| 210 | + time.sleep(1) |
| 211 | + continue |
| 212 | + else: |
| 213 | + f_loggedin=0 |
| 214 | + while usr.locked == 0 and usr.loggedin == 0: |
| 215 | + s_pwd_try = input ( f"Please enter password for user {usr.username}: " ) |
| 216 | + if usr.verify_password(s_pwd_try) == True: |
| 217 | + break |
| 218 | + |
| 219 | + if usr.loggedin == 1: |
| 220 | + print ( f"{s.GREENON}Welcome to the Password Verifier 2000.{s.COLOUROFF}\n{s.ORANGEON}You successfully entered your username and password!{s.COLOUROFF}" ) |
| 221 | + else: |
| 222 | + print ( f"{s.REDON}{s.REVON}Password attempts limit exceeded.{s.REVOFF}{s.COLOUROFF} \nYour account has been locked for 60 seconds." ) |
| 223 | + else: |
| 224 | + print ( f"{s.REDON}Username not found. Please try again{s.COLOUROFF}" ) |
| 225 | + |
| 226 | + elif i_menu == 3: |
| 227 | + print ("System Users") |
| 228 | + print ("------------") |
| 229 | + |
| 230 | + for this_user in a_users: |
| 231 | + usr = User(this_user) |
| 232 | + if usr.is_locked_out(): |
| 233 | + s_colour = s.REDON |
| 234 | + else: |
| 235 | + s_colour = s.GREENON |
| 236 | + |
| 237 | + print ( f"{s_colour}{usr.username}{s.COLOUROFF}" ) |
| 238 | + |
| 239 | + print ("------------") |
| 240 | + print ( "" ) |
0 commit comments