finalizando trabalho 4

This commit is contained in:
José Henrique 2023-10-31 18:31:12 -03:00
parent 7b1b7860b6
commit 583aca1960
5 changed files with 112 additions and 36 deletions

View File

@ -1,23 +1,23 @@
from common import * from common import *
from hashlib import sha256 from hashlib import sha256
import socket
from AES import AESCipher from AES import AESCipher
import socket
import os import os
import base64 import base64
import time
HOST = "127.0.0.1" HOST = "127.0.0.1"
PORT = AS_PORT PORT = AS_PORT
def process_message(message): def process_message(message):
print(message)
message = message.split("||") message = message.split("||")
if message[0] == "register": if message[0] == "register":
body = message[1].split("<>") body = message[1].split("<>")
username = body[0] username = body[0]
password = body[1] password = body[1]
password = sha256(password.encode()).hexdigest() password = sha256(password.encode()).hexdigest()
key = os.urandom(32) key = os.urandom(32)
print(f"Registering user [{username}]")
return register_user(username, password, key) return register_user(username, password, key)
elif message[0] == "login": elif message[0] == "login":
body = message[1].split("<>") body = message[1].split("<>")
@ -35,8 +35,8 @@ def process_message(message):
body = message[1].split("<>") body = message[1].split("<>")
ID_C = body[0] ID_C = body[0]
key = get_ktgs() Kc = get_user_key(ID_C)
AES = AESCipher(key) AES = AESCipher(Kc)
inner_message = AES.decrypt(body[1]) inner_message = AES.decrypt(body[1])
inner_message = inner_message.split("<>") inner_message = inner_message.split("<>")
@ -76,6 +76,18 @@ def login(username, password):
print(f"Login failed! [{username}]") print(f"Login failed! [{username}]")
return "failure" return "failure"
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
def get_ktgs(): def get_ktgs():
message = f"getkey" message = f"getkey"
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
@ -95,7 +107,6 @@ def main():
while True: while True:
conn, addr = s.accept() conn, addr = s.accept()
with conn: with conn:
print(f"Connected by {addr}")
data = conn.recv(4096) data = conn.recv(4096)
data = data.decode() data = data.decode()

View File

@ -1,13 +1,15 @@
from common import *
import os
import socket
from AES import AESCipher from AES import AESCipher
from random import randint from random import randint
from common import *
import socket
import time
HOST = "127.0.0.1" HOST = "127.0.0.1"
CLIENT_ID = None CLIENT_ID = None
KEY = None KEY = None
SAVED_INFO = {}
def register_user(username, password): def register_user(username, password):
global CLIENT_ID, KEY global CLIENT_ID, KEY
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
@ -38,7 +40,7 @@ def login(username, password):
CLIENT_ID = username CLIENT_ID = username
KEY = eval(data[1]) KEY = eval(data[1])
def print_something(): def send_messages():
global CLIENT_ID, KEY global CLIENT_ID, KEY
if CLIENT_ID is None: if CLIENT_ID is None:
@ -48,9 +50,8 @@ def print_something():
AES = AESCipher(KEY) AES = AESCipher(KEY)
ID_C = CLIENT_ID ID_C = CLIENT_ID
ID_S = "PRINTER" ID_S = "DOOR"
T_R = int(input("Enter the ticket lifetime (minutes): ")) T_R = int(input("Enter the ticket lifetime (seconds): "))
T_R = T_R * 60
N1 = randint(0, 1000000) N1 = randint(0, 1000000)
M2 = None M2 = None
@ -63,7 +64,7 @@ def print_something():
message = f"request||{M1}" message = f"request||{M1}"
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, AS_PORT)) s.connect((HOST, AS_PORT))
print('Sending M1', message) print('Sending M1', f'{ID_C}<>[{ID_S}<>{T_R}<>{N1}]')
s.sendall(message.encode()) s.sendall(message.encode())
data = s.recv(4096) data = s.recv(4096)
@ -71,6 +72,8 @@ def print_something():
print('Received M2', data) print('Received M2', data)
M2 = data M2 = data
time.sleep(3)
''' '''
M2 = [{K_c_tgs + N_1}Kc + T_c_tgs] M2 = [{K_c_tgs + N_1}Kc + T_c_tgs]
M3 = [{ID_C + ID_S + T_R + N2}K_c_tgs + T_c_tgs] M3 = [{ID_C + ID_S + T_R + N2}K_c_tgs + T_c_tgs]
@ -80,7 +83,12 @@ def print_something():
M2_inner = M2_inner.split("<>") M2_inner = M2_inner.split("<>")
K_c_tgs = eval(M2_inner[0]) K_c_tgs = eval(M2_inner[0])
N1 = M2_inner[1] M2_N1 = int(M2_inner[1])
if M2_N1 != N1:
print("Invalid ticket [N1 mismatch]")
return
T_c_tgs = M2[1] T_c_tgs = M2[1]
M2_AES = AESCipher(K_c_tgs) M2_AES = AESCipher(K_c_tgs)
@ -92,7 +100,7 @@ def print_something():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, TGS_PORT)) s.connect((HOST, TGS_PORT))
message = f"request||{M3}" message = f"request||{M3}"
print('Sending M3', message) print('Sending M3', f'[{ID_C}<>{ID_S}<>{T_R}<>{N2}]<>[T_c_tgs]')
s.sendall(message.encode()) s.sendall(message.encode())
data = s.recv(4096) data = s.recv(4096)
@ -100,6 +108,8 @@ def print_something():
print('Received M4', data) print('Received M4', data)
M4 = data M4 = data
time.sleep(3)
''' '''
M4 = [{K_c_s + T_A + N2}K_c_tgs + T_c_s] M4 = [{K_c_s + T_A + N2}K_c_tgs + T_c_s]
T_c_s = {ID_C + T_A + K_c_s}K_s T_c_s = {ID_C + T_A + K_c_s}K_s
@ -112,7 +122,11 @@ def print_something():
K_c_s = eval(M4_inner[0]) K_c_s = eval(M4_inner[0])
T_A = M4_inner[1] T_A = M4_inner[1]
N2 = M4_inner[2] M4_N2 = int(M4_inner[2])
if M4_N2 != N2:
print("Invalid ticket [N2 mismatch]")
return
T_c_s = M4[1] T_c_s = M4[1]
@ -123,6 +137,46 @@ def print_something():
M5_inner = M5_AES.encrypt(M5_inner) M5_inner = M5_AES.encrypt(M5_inner)
M5 = f"{M5_inner}<>{T_c_s}" M5 = f"{M5_inner}<>{T_c_s}"
SAVED_INFO['M5'] = M5
SAVED_INFO['N3'] = N3
SAVED_INFO['K_c_s'] = K_c_s
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, SERVICE_PORT))
message = f"request||{M5}"
print('Sending M5', f'[{ID_C}<>{T_A}<>{ID_S}<>{N3}]<>[T_c_s]')
s.sendall(message.encode())
data = s.recv(4096)
data = data.decode()
print('Received M6', data)
M6 = data
time.sleep(3)
'''
M6 = [{Resposta, N3}K_c_s]
'''
M6 = M6.split("<>")
M6_AES = AESCipher(K_c_s)
M6_inner = M6_AES.decrypt(M6[0])
M6_inner = M6_inner.split("<>")
resposta = M6_inner[0]
M6_N3 = int(M6_inner[1])
if M6_N3 != N3:
print("Invalid ticket [N3 mismatch]")
return
print(f"Resposta: [{resposta}]")
print("Finished!")
def resend_service_message():
M5 = SAVED_INFO['M5']
N3 = SAVED_INFO['N3']
K_c_s = SAVED_INFO['K_c_s']
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, SERVICE_PORT)) s.connect((HOST, SERVICE_PORT))
message = f"request||{M5}" message = f"request||{M5}"
@ -142,23 +196,26 @@ def print_something():
M6_inner = M6_AES.decrypt(M6[0]) M6_inner = M6_AES.decrypt(M6[0])
M6_inner = M6_inner.split("<>") M6_inner = M6_inner.split("<>")
resposta = M6_inner[0] resposta = M6_inner[0]
N3 = M6_inner[1] M6_N3 = int(M6_inner[1])
if M6_N3 != N3:
print("Invalid ticket [N3 mismatch]")
return
print(f"Resposta: [{resposta}]") print(f"Resposta: [{resposta}]")
print("Finished!!!") print("Finished!")
def main(): def main():
global CLIENT_ID global CLIENT_ID
while True: while True:
# os.system("clear")
print(f"Hello world! Logged as [{CLIENT_ID}]") print(f"Hello world! Logged as [{CLIENT_ID}]")
print("1. Register") print("1. Register")
print("2. Login") print("2. Login")
if CLIENT_ID is not None: if CLIENT_ID is not None:
print("3. Print something") print("3. Open door [M1 -> M6]")
print("4. Open door [M5 -> M6]")
print("0. Exit") print("0. Exit")
choice = input("Enter your choice: ") choice = input("Enter your choice: ")
@ -172,7 +229,9 @@ def main():
password = input("Enter your password: ") password = input("Enter your password: ")
login(username, password) login(username, password)
elif choice == "3": elif choice == "3":
print_something() send_messages()
elif choice == "4":
resend_service_message()
elif choice == "0": elif choice == "0":
break break
else: else:

View File

@ -1,10 +1,10 @@
import socket
from common import * from common import *
import socket
from AES import AESCipher from AES import AESCipher
import os
from random import randint from random import randint
import socket
import os
import base64 import base64
import time
HOST = "127.0.0.1" HOST = "127.0.0.1"
PORT = SERVICE_PORT PORT = SERVICE_PORT
@ -34,6 +34,11 @@ def process_message(message):
T_c_s = T_c_s.split("<>") T_c_s = T_c_s.split("<>")
ID_C = T_c_s[0] ID_C = T_c_s[0]
T_A = T_c_s[1] T_A = T_c_s[1]
if int(T_A) < int(time.time()):
print("Invalid ticket [expired]")
return "Invalid ticket [expired]"
K_c_s = eval(T_c_s[2]) K_c_s = eval(T_c_s[2])
M5_inner_AES = AESCipher(K_c_s) M5_inner_AES = AESCipher(K_c_s)
@ -43,10 +48,13 @@ def process_message(message):
N3 = M5_inner[3] N3 = M5_inner[3]
# generate M6 # generate M6
M6_inner = f"{randint(0, 100)}<>{N3}" message = "OPEN"
M6_inner = f"{message}<>{N3}"
M6_inner_AES = AESCipher(K_c_s) M6_inner_AES = AESCipher(K_c_s)
M6_inner = M6_inner_AES.encrypt(M6_inner) M6_inner = M6_inner_AES.encrypt(M6_inner)
print("Sending M6", f"[{message}<>{N3}]")
M6 = f"{M6_inner}" M6 = f"{M6_inner}"
return M6 return M6
@ -57,7 +65,6 @@ def main():
while True: while True:
conn, addr = s.accept() conn, addr = s.accept()
with conn: with conn:
print(f"Connected by {addr}")
all_data = "" all_data = ""
data = conn.recv(4096) data = conn.recv(4096)
all_data += data.decode() all_data += data.decode()

View File

@ -1,9 +1,9 @@
import socket
from common import * from common import *
import socket
from AES import AESCipher from AES import AESCipher
import socket
import os import os
import base64 import base64
import time
HOST = "127.0.0.1" HOST = "127.0.0.1"
PORT = TGS_PORT PORT = TGS_PORT
@ -15,6 +15,7 @@ def process_message(message):
message = message.split("||") message = message.split("||")
if message[0] == "getkey": if message[0] == "getkey":
print('ktgs', TGS_KEY)
return f"{base64.b64encode(TGS_KEY)}" return f"{base64.b64encode(TGS_KEY)}"
if message[0] == "request": if message[0] == "request":
""" """
@ -48,6 +49,7 @@ def process_message(message):
# generate M4 # generate M4
K_c_s = os.urandom(16) K_c_s = os.urandom(16)
T_A = 600 if T_R > 600 else T_R T_A = 600 if T_R > 600 else T_R
T_A = int(time.time()) + T_A
T_c_s = f"{ID_C}<>{T_A}<>{K_c_s}" T_c_s = f"{ID_C}<>{T_A}<>{K_c_s}"
K_s = get_ks() K_s = get_ks()
@ -58,6 +60,8 @@ def process_message(message):
M4_inner_AES = AESCipher(K_c_tgs) M4_inner_AES = AESCipher(K_c_tgs)
M4_inner = M4_inner_AES.encrypt(M4_inner) M4_inner = M4_inner_AES.encrypt(M4_inner)
print("Sending M4", f"[{K_c_s}<>{T_A}<>{N2}]<>[T_c_s] | T_c_s = [{ID_C}<>{T_A}<>K_c_s]")
M4 = f"{M4_inner}<>{T_c_s}" M4 = f"{M4_inner}<>{T_c_s}"
return M4 return M4
@ -80,7 +84,6 @@ def main():
while True: while True:
conn, addr = s.accept() conn, addr = s.accept()
with conn: with conn:
print(f"Connected by {addr}")
all_data = "" all_data = ""
data = conn.recv(4096) data = conn.recv(4096)
all_data += data.decode() all_data += data.decode()
@ -90,8 +93,4 @@ def main():
conn.sendall(response.encode()) conn.sendall(response.encode())
if __name__ == "__main__": if __name__ == "__main__":
if not os.path.exists("tgs_db.data"):
with open("tgs_db.data", "w") as f:
pass
main() main()

View File

@ -1 +1 @@
fake<>a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3<>b'zl+psilWmCU547E7BGr4b8Edq7fX8XVkY7J8MfpVMS0=' fake<>a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3<>b'b9x8hZlmLGC19xOG22DrzWGJoYFCQlzm0W2VCX9Tkoo='