Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. from binascii import hexlify
  3. from binascii import unhexlify
  4. import socket
  5.  
  6.  
  7. # from curve25519 import Private, Public
  8. import nacl.secret
  9. import hmac
  10. import hashlib
  11.  
  12. SERVER = ('mitm.ctfcompetition.com', 1337)
  13. p = (2 ** 255) - 19
  14.  
  15.  
  16. def ReadLine(reader):
  17. data = b''
  18. while not data.endswith(b'\n'):
  19. cur = reader.recv(1)
  20. data += cur
  21. if cur == b'':
  22. return data
  23. return data[:-1]
  24.  
  25.  
  26. def WriteLine(writer, msg):
  27. writer.send(msg + b'\n')
  28.  
  29.  
  30. def ReadBin(reader):
  31. return unhexlify(ReadLine(reader))
  32.  
  33.  
  34. def WriteBin(writer, data):
  35. WriteLine(writer, hexlify(data))
  36.  
  37. my_key = (p-1).to_bytes(255, 'little').rstrip(b'\x00')
  38.  
  39. ss = socket.socket()
  40. cs = socket.socket()
  41. ss.connect(SERVER)
  42. cs.connect(SERVER)
  43.  
  44. WriteLine(ss, b's')
  45. WriteLine(cs, b'c')
  46.  
  47. server_public_key = ReadBin(ss)
  48. server_nonce = ReadBin(ss)
  49.  
  50. client_public_key = ReadBin(cs)
  51. client_nonce = ReadBin(cs)
  52.  
  53. print(my_key)
  54.  
  55. print(server_public_key, server_nonce)
  56. print(client_public_key, client_nonce)
  57.  
  58. WriteBin(ss, my_key)
  59. WriteBin(ss, client_nonce)
  60.  
  61. WriteBin(cs, my_key)
  62. WriteBin(cs, server_nonce)
  63.  
  64. server_proof = ReadBin(ss)
  65. client_proof = ReadBin(cs)
  66.  
  67. print(server_proof)
  68. print(client_proof)
  69.  
  70. WriteBin(cs, server_proof)
  71. WriteBin(ss, client_proof)
  72.  
  73. auth_data = ReadBin(ss)
  74. print(auth_data)
  75.  
  76. WriteBin(cs, auth_data)
  77.  
  78. print(ReadLine(cs))
  79.  
  80. mySecretBox = nacl.secret.SecretBox(my_key)
  81. print(mySecretBox.decrypt(auth_data))
  82.  
  83. ss.close()
  84. cs.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement