Advertisement
Guest User

Untitled

a guest
Apr 13th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. import sys, socket, select, getpass
  2. import hashlib, random, string
  3. from GetRSA import getRSA
  4. from friedMess import *
  5.  
  6. PRIME_MINIMUM = 100
  7. HASHBYTES = 4
  8.  
  9. G_username = ""
  10. G_password = ""
  11. G_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  12. G_rsakey = []
  13.  
  14. KEY_PUBLIC = 0
  15. KEY_PRIVATE = 1
  16. KEY_N = 2
  17.  
  18. STATUS_NOTFOUND = "404"
  19. STATUS_UNAUTHORIZED = "403"
  20. STATUS_FOUND = "302"
  21. STATUS_OK = "200"
  22.  
  23. ServerKey_Public = 0
  24. ServerKey_N = 0
  25.  
  26. G_listCommands = ""
  27. for line in open("commands.txt", 'r'):
  28. G_listCommands += line
  29.  
  30. def printFriedMessLogo():
  31. print("\n" * 50)
  32. print(" ______ ____ ____ ______ ____ __ ___ ______ _____ _____")
  33. print(" / ____/ / __ \ / _/ / ____/ / __ \ / |/ / / ____/ / ___/ / ___/")
  34. print(" / /_ / /_/ / / / / __/ / / / / / /|_/ / / __/ \__ \ \__ \ ")
  35. print(" / __/ / _, _/ _/ / / /___ / /_/ / / / / / / /___ ___/ / ___/ / ")
  36. print("/_/ /_/ |_| /___/ /_____/ /_____/ /_/ /_/ /_____/ /____/ /____/ ")
  37. print("\n")
  38.  
  39. def printPreChar():
  40. print("> ", end="")
  41. sys.stdout.flush()
  42.  
  43. def sendMessage(message):
  44. message = str(message)
  45. print("Sending: " + message)
  46. G_socket.send(message.encode("ASCII"))
  47.  
  48. def formatMessage(command, recipient, message):
  49. nonce = generateNonce(16)
  50. payload = G_username + "," + command + "," + recipient + "," + message
  51. h_message = hashSHA(nonce + message + G_password)
  52. h_message = encodeRSA(h_message, ServerKey_Public, ServerKey_N)
  53. message = nonce + "," + str(h_message) + "," + payload
  54. return message
  55.  
  56. def generateNonce(length):
  57. return ''.join([random.choice(string.ascii_letters + string.digits) for n in range(length)])
  58.  
  59. def authenticateUser():
  60. nonce = generateNonce(16)
  61. message = G_username + ",AUTH," + str(G_rsakey[KEY_PUBLIC])+" "+str(G_rsakey[KEY_N]) + ","
  62. h_message = hashlib.sha256()
  63. h_message.update((nonce + message + G_password).encode("UTF-8"))
  64. h_message = str(int(h_message.hexdigest(), 16))[:HASHBYTES]
  65. message = nonce + "," + str(h_message) + "," + message
  66. sendMessage(message)
  67.  
  68. def validateMessage(incHash, salt, message, RSAK, RSAN):
  69. valid = False
  70. incHash = encodeRSA(incHash, RSAK, RSAN)
  71. checkHash = hashSHA(message+salt)
  72. if(str(checkHash) == str(incHash)):
  73. valid = True
  74. else:
  75. valid = False
  76. return valid
  77.  
  78. def validCommand(command):
  79. if(command in G_listCommands):
  80. return True
  81. return False
  82.  
  83. def chat_client():
  84. global G_username
  85. global G_password
  86. global G_socket
  87. global G_rsakey
  88. global ServerKey_Public
  89. global ServerKey_N
  90.  
  91. if(len(sys.argv) < 3) :
  92. print('Usage : python3 client.py <host> <port>')
  93. sys.exit()
  94.  
  95. HOST = sys.argv[1]
  96. PORT = int(sys.argv[2])
  97.  
  98. G_socket.settimeout(2)
  99.  
  100. # Try to connect to the chat server
  101. try :
  102. G_socket.connect((HOST, PORT))
  103. except Exception as ex :
  104. print('Unable to connect' + str(ex))
  105. sys.exit()
  106.  
  107. printFriedMessLogo()
  108.  
  109. G_rsakey = getRSA(PRIME_MINIMUM, PRIME_MINIMUM)
  110.  
  111. print("Please enter your zID (zXXXXXXX) and password (password will not echo):\n")
  112.  
  113. loginSuccess = False
  114.  
  115. while(loginSuccess == False):
  116. G_username = input("zID: ")
  117. G_password = getpass.getpass("Password: ")
  118.  
  119. authenticateUser()
  120. socket_list = [sys.stdin, G_socket]
  121. ready_to_read,ready_to_write,in_error = select.select(socket_list , [], [])
  122. for sock in ready_to_read:
  123. if sock == G_socket:
  124. data = sock.recv(4096).decode("ASCII")
  125. if(data == STATUS_OK):
  126. loginSuccess = True
  127. print("Login Successful!")
  128. printFriedMessLogo()
  129. print("Welcome back " + G_username + "!")
  130. print("Your connection is protected against replay attacks and data alteration\n")
  131. elif(data == STATUS_UNAUTHORIZED):
  132. print("Incorrect zID or password\n")
  133. while(1):
  134. socket_list = [sys.stdin, G_socket]
  135.  
  136. # Get the list sockets which are readable
  137. ready_to_read,ready_to_write,in_error = select.select(socket_list , [], [])
  138.  
  139. for sock in ready_to_read:
  140. if sock == G_socket:
  141. # incoming message from remote server, s
  142. data = sock.recv(4096).decode("ASCII")
  143. if not data :
  144. print('\nDisconnected from chat server')
  145. sys.exit()
  146. else :
  147. data = data.split(",")
  148. h_incMessage = data[0]
  149. sender = data[1]
  150. command = data[2]
  151. recipient = data[3]
  152. incMessage = data[4]
  153. if(validateMessage(h_incMessage, G_password, sender+","+command+","+recipient+","+incMessage, G_rsakey[KEY_PRIVATE], G_rsakey[KEY_N])):
  154. if(command == "ALLOCKEYS"):
  155. ServerKeys = incMessage.split(" ")
  156. ServerKey_Public = int(ServerKeys[0])
  157. ServerKey_N = int(ServerKeys[1])
  158. printPreChar()
  159.  
  160. else :
  161. # user entered a message
  162. msg = sys.stdin.readline()
  163. command = msg.strip().split(" ", 1)[0]
  164. if(validCommand(command)):
  165. if(command == "whoelse"):
  166. msg = formatMessage("whoelse", "", "")
  167. sendMessage(msg)
  168. else:
  169. print("Invalid command. Open the assignment to see list of commands")
  170. printPreChar()
  171.  
  172. if __name__ == "__main__":
  173.  
  174. sys.exit(chat_client())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement