connector.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import socket
  2. import threading
  3. import os
  4. import config as cfg
  5. from connection.socket import SocketServer
  6. from connection.socket import SocketHTTP
  7. from tcp import models
  8. import time
  9. from tcp import utils
  10. import tcp as t
  11. from types import SimpleNamespace
  12. class Connector():
  13. def __init__(self, parser,socket):
  14. self.parser = parser
  15. self.server = None
  16. self.serverDomain = None
  17. self.serverIP = None
  18. self.s = SocketServer(socket,self.parser,"Connector")
  19. self.buffer = []
  20. self.handshaked = False
  21. self.t = threading.Thread(target=self.tcp)
  22. print("\n\n Connector\n\n")
  23. def recv(self,data):
  24. try:
  25. self.buffer.append(data)
  26. if not self.t.is_alive():
  27. self.t.start()
  28. except:
  29. print("\33[41m\33[41mAn exception occurred recv\33[0m" )
  30. def oracleInitial(self,data):
  31. try:
  32. while len(self.s.connected) < 2:
  33. time.sleep(.5)
  34. if(len(self.s.connected) == 2):
  35. # if self.s.connected[0].addr[0] == data["bearer"] and self.s.connected[1].addr[0] == data["cryptor"]:
  36. self.bearer = self.s.connected[1]
  37. self.cryptor = self.s.connected[0]
  38. # elif self.s.connected[1].addr[0] == data["bearer"] and self.s.connected[0].addr[0] == data["cryptor"]:
  39. # self.bearer = self.s.connected[1]
  40. # self.cryptor = self.s.connected[0]
  41. # else:
  42. # raise Exception("Error")
  43. self.serverDomain = data["server"]
  44. if type(self.serverDomain) == str:
  45. self.serverDomain = [self.serverDomain]
  46. # self.serverIP = socket.gethostbyname(data["server"])
  47. self.serverIP = "216.58.205.228"
  48. self.server = SocketHTTP(self.serverIP,443,self)
  49. except Exception as e:
  50. print("\33[41m\33[41mAn exception occurred oracleInitial Connector ",e,"\33[0m" )
  51. def keyExchange(self,data):
  52. try:
  53. while not self.server:
  54. time.sleep(.5)
  55. self.key_exchange =bytes.fromhex( data["keyExchange"])
  56. self.server.send(bytes.fromhex(self.client_hello()))
  57. except NameError:
  58. print(NameError)
  59. print("\33[41mAn exception occurred keyExchange Connector,\33[0m")
  60. # def toCryptor(self,data):
  61. # try:
  62. # self.key_exchange =bytes.fromhex( data["keyExchange"])
  63. # self.server.send(bytes.fromhex(self.client_hello()))
  64. # except NameError:
  65. # print(NameError)
  66. # print("\33[41mAn exception occurred keyExchange Connector,\33[0m")
  67. def client_hello(self):
  68. try:
  69. print("Done 1")
  70. extensions = [
  71. models.ClientExtension.server_names(self.serverDomain),
  72. models.ClientExtension.supported_versions([b"\x03\x04"]),
  73. models.ClientExtension.key_share(
  74. [models.KeyShareEntry(models.NamedGroup.x25519, self.key_exchange)]
  75. ),
  76. models.ClientExtension.signature_algorithms(list(models.SignatureScheme)),
  77. models.ClientExtension.supported_groups([models.NamedGroup.x25519]),
  78. ]
  79. compatibility_mode = True
  80. handshake = models.Handshake(
  81. models.HandshakeType.client_hello,
  82. models.ClientHello(
  83. ...,
  84. os.urandom(32),
  85. os.urandom(32) if compatibility_mode else b"",
  86. list(models.CipherSuite),
  87. ...,
  88. extensions,
  89. ),
  90. )
  91. self.client_hello_data = handshake.binary
  92. self.handshake_context = bytearray(self.client_hello_data)
  93. self.parser.parserSend(bytes(self.client_hello_data).hex(),self.cryptor,"fromContoCryHelloCLient")
  94. data = models.ContentType.handshake.tls_plaintext(self.client_hello_data)
  95. return bytes.hex(data)
  96. except Exception as exception:
  97. print("\33[41mAn exception occurred client_hello Connector,\33[0m",exception)
  98. def tcp(self):
  99. while 1:
  100. time.sleep(0.5)
  101. while self.buffer and self.buffer[0] and len(self.buffer[0])>=10:
  102. s = self.buffer[0][6:10]
  103. print(s)
  104. l = int(s,16)*2+10
  105. a = self.lenCalc(l)
  106. if not self.handshaked:
  107. self.parser.parserSend(a,self.cryptor,"fromContoCry")
  108. self.t.exit()
  109. def lenCalc(self, len2):
  110. if len(self.buffer[0]) > len2:
  111. a = self.buffer[0][:len2]
  112. self.buffer[0] = self.buffer[0][len2:]
  113. return a
  114. elif len(self.buffer[0]) == len2 :
  115. return self.buffer.pop(0)
  116. elif len(self.buffer[0]) < len2 :
  117. if self.buffer[1]:
  118. self.buffer[0] = self.buffer[0] + self.buffer[1]
  119. self.buffer.pop(1)
  120. return lenCalc(self.buffer,len2)
  121. else:
  122. print("Data is not copmlete1 ")
  123. time.sleep(1)
  124. lenCalc(self.buffer,len2)
  125. else:
  126. print("Data is not copmlet2e")
  127. time.sleep(1)
  128. lenCalc(self.buffer,len2)
  129. def MPC(self,data):
  130. try:
  131. print("TODO") # TODO
  132. except Exception as exception :
  133. print("\33[41mAn exception occurred MPC Connector " ,exception,"\33[0m" )
  134. def fromCrytoCon(self,data):
  135. try:
  136. print("TODO fromCrytoCon") # TODO
  137. except Exception as exception :
  138. print("\33[41mAn exception occurred fromCrytoCon Connector " ,exception,"\33[0m" )