2023-10-22 19:56:01 +00:00
|
|
|
from common import *
|
|
|
|
from hashlib import sha256
|
|
|
|
from AES import AESCipher
|
2023-10-31 21:31:12 +00:00
|
|
|
import socket
|
2023-10-22 19:56:01 +00:00
|
|
|
import os
|
2023-10-23 20:26:05 +00:00
|
|
|
import base64
|
2023-10-31 21:31:12 +00:00
|
|
|
import time
|
2023-10-22 19:56:01 +00:00
|
|
|
|
|
|
|
HOST = "127.0.0.1"
|
|
|
|
PORT = AS_PORT
|
|
|
|
|
|
|
|
def process_message(message):
|
|
|
|
message = message.split("||")
|
|
|
|
if message[0] == "register":
|
2023-10-22 20:15:12 +00:00
|
|
|
body = message[1].split("<>")
|
2023-10-22 19:56:01 +00:00
|
|
|
username = body[0]
|
|
|
|
password = body[1]
|
|
|
|
password = sha256(password.encode()).hexdigest()
|
2023-10-23 20:26:05 +00:00
|
|
|
key = os.urandom(32)
|
2023-10-31 21:31:12 +00:00
|
|
|
print(f"Registering user [{username}]")
|
2023-10-22 19:56:01 +00:00
|
|
|
return register_user(username, password, key)
|
|
|
|
elif message[0] == "login":
|
2023-10-22 20:15:12 +00:00
|
|
|
body = message[1].split("<>")
|
2023-10-22 19:56:01 +00:00
|
|
|
username = body[0]
|
|
|
|
password = body[1]
|
|
|
|
password = sha256(password.encode()).hexdigest()
|
|
|
|
return login(username, password)
|
|
|
|
elif message[0] == "request":
|
|
|
|
"""
|
|
|
|
M1 = [ID_C + {ID_S + T_R + N1}Kc]
|
|
|
|
|
|
|
|
T_c_tgs = {ID_C + T_R + K_c_tgs}K_tgs
|
|
|
|
M2 = [{K_c_tgs + N_1}Kc + T_c_tgs]
|
|
|
|
"""
|
2023-10-22 20:15:12 +00:00
|
|
|
body = message[1].split("<>")
|
2023-10-22 19:56:01 +00:00
|
|
|
|
|
|
|
ID_C = body[0]
|
2023-10-31 21:31:12 +00:00
|
|
|
Kc = get_user_key(ID_C)
|
|
|
|
AES = AESCipher(Kc)
|
2023-10-22 19:56:01 +00:00
|
|
|
|
|
|
|
inner_message = AES.decrypt(body[1])
|
2023-10-22 20:15:12 +00:00
|
|
|
inner_message = inner_message.split("<>")
|
2023-10-22 19:56:01 +00:00
|
|
|
T_R = inner_message[1]
|
|
|
|
N1 = inner_message[2]
|
|
|
|
|
2023-10-23 20:26:05 +00:00
|
|
|
# generate m2
|
|
|
|
K_tgs = get_ktgs()
|
|
|
|
M2_AES = AESCipher(K_tgs)
|
2023-10-22 19:56:01 +00:00
|
|
|
K_c_tgs = os.urandom(16)
|
2023-10-22 20:15:12 +00:00
|
|
|
T_c_tgs = f"{ID_C}<>{T_R}<>{K_c_tgs}"
|
2023-10-22 19:56:01 +00:00
|
|
|
T_c_tgs = M2_AES.encrypt(T_c_tgs)
|
|
|
|
|
2023-10-22 20:15:12 +00:00
|
|
|
M2 = f"{K_c_tgs}<>{N1}"
|
2023-10-22 19:56:01 +00:00
|
|
|
M2 = AES.encrypt(M2)
|
2023-10-22 20:15:12 +00:00
|
|
|
M2 = f"{M2}<>{T_c_tgs}"
|
2023-10-22 19:56:01 +00:00
|
|
|
return M2
|
|
|
|
else:
|
|
|
|
print("Invalid message")
|
|
|
|
|
|
|
|
def register_user(username, password, key):
|
|
|
|
with open("users.data", "a") as f:
|
2023-10-23 20:26:05 +00:00
|
|
|
f.write(f"{username}<>{password}<>{base64.b64encode(key)}\n")
|
2023-10-22 20:15:12 +00:00
|
|
|
return f"success<>{key}"
|
2023-10-22 19:56:01 +00:00
|
|
|
|
|
|
|
def login(username, password):
|
|
|
|
with open("users.data", "r", newline='\n') as f:
|
|
|
|
for line in f:
|
|
|
|
line = line.strip()
|
|
|
|
line = line.replace("\n", "")
|
2023-10-22 20:15:12 +00:00
|
|
|
user, pw, key = line.split("<>")
|
2023-10-22 19:56:01 +00:00
|
|
|
if user == username and pw == password:
|
|
|
|
key = eval(key)
|
2023-10-23 20:26:05 +00:00
|
|
|
key = base64.b64decode(key)
|
|
|
|
print(f"Login succeeded [{username}]")
|
2023-10-22 20:15:12 +00:00
|
|
|
return f"success<>{key}"
|
2023-10-23 20:26:05 +00:00
|
|
|
print(f"Login failed! [{username}]")
|
2023-10-22 19:56:01 +00:00
|
|
|
return "failure"
|
|
|
|
|
2023-10-31 21:31:12 +00:00
|
|
|
def get_user_key(username):
|
|
|
|
with open("users.data", "r", newline='\n') as f:
|
|
|
|
for line in f:
|
|
|
|
line = line.strip()
|
|
|
|
line = line.replace("\n", "")
|
|
|
|
user, pw, key = line.split("<>")
|
|
|
|
if user == username:
|
|
|
|
key = eval(key)
|
|
|
|
key = base64.b64decode(key)
|
|
|
|
return key
|
|
|
|
return None
|
|
|
|
|
2023-10-23 20:26:05 +00:00
|
|
|
def get_ktgs():
|
|
|
|
message = f"getkey"
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
|
|
s.connect((HOST, TGS_PORT))
|
|
|
|
s.sendall(message.encode())
|
|
|
|
|
|
|
|
data = s.recv(4096)
|
|
|
|
data = data.decode()
|
|
|
|
key = eval(data)
|
|
|
|
key = base64.b64decode(key)
|
|
|
|
return key
|
2023-10-22 19:56:01 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
|
|
s.bind((HOST, PORT))
|
|
|
|
s.listen()
|
|
|
|
while True:
|
|
|
|
conn, addr = s.accept()
|
|
|
|
with conn:
|
|
|
|
data = conn.recv(4096)
|
|
|
|
data = data.decode()
|
|
|
|
|
|
|
|
response = process_message(data)
|
|
|
|
|
|
|
|
conn.sendall(response.encode())
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|