You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.9 KiB

5 years ago
  1. #!/usr/bin/env python
  2. import socket
  3. from thread import start_new_thread
  4. from datetime import datetime
  5. from os import urandom
  6. from random import randrange as random
  7. import RSAvulnerableKeyGenerator as keygen
  8. FLAG = 'SlashRootCTF{W13nn3r_w1nn3r_RSA_d1nn3r}'
  9. HOST = '0.0.0.0'
  10. PORT = 6070
  11. BUFF = 1024
  12. BIT = 256
  13. MAX = 10
  14. def banner():
  15. return '''\
  16. _______________________________________________
  17. | ____ ____ ____ ___ ____ _ _ ____ _ _ |
  18. | |__/ [__ |__| | | | |_/ |___ |\ | |
  19. | | \ ___] | | | |__| | \_ |___ | \| |
  20. | ____ ____ _ _ ____ ____ ____ ___ ____ ____ |
  21. | | __ |___ |\ | |___ |__/ |__| | | | |__/ |
  22. | |__] |___ | \| |___ | \ | | | |__| | \ |
  23. |_____________________________________________|
  24. | [1] Generate RSA |
  25. | [2] Generate Token |
  26. | [3] Generate Flag |
  27. |_____________________________________________|
  28. >>> '''
  29. def log(message, address, filename='RSATG.log'):
  30. with open(filename, 'a') as log:
  31. timestamp = datetime.now().strftime('%d/%m/%Y %H:%M:%S')
  32. log.write('[%s][%s:%d] %s\n' % (timestamp, address[0], address[1], str(message)))
  33. def serve_client(client, address, receive=''):
  34. try:
  35. client.send(banner())
  36. token = []
  37. while True:
  38. data = ''
  39. receive = client.recv(BUFF).strip()
  40. if receive == '1':
  41. log('<<< Generating RSA', address)
  42. data = genRSA(client, address)
  43. elif receive == '2':
  44. log('<<< Generating Token', address)
  45. data, token = genToken(client, address)
  46. log('>>> Sending Token: %s' % format(token), address)
  47. elif receive == '3':
  48. log('<<< Generating flag', address)
  49. if token:
  50. client.send('Token : ')
  51. if format(token) == client.recv(BUFF).strip():
  52. ID = urandom(16).encode('hex')
  53. log('<-> RSA ID: %s[%s]' % (ID, format(token)), address)
  54. log('>>> Sending Flag: %s[%s]' % (ID, format(token)), address)
  55. client.send('''\
  56. RSA ID\t : %s
  57. FLAG\t : %s
  58. *Sertakan TOKEN dan RSA ID pada writeup agar poin dihitung!\n''' % (ID, FLAG))
  59. break
  60. else:
  61. log('>-< Wrong Token: %s|%s' % (receive, format(token)), address)
  62. data = 'Try Again!\nYour token is %s\n' % format(token)
  63. else:
  64. log('>-< Empty Token!', address)
  65. data = 'Generate your token!\n'
  66. token = []
  67. client.send(data + '[1|2|3]>>> ')
  68. log('Disconnected', address)
  69. except Exception as message:
  70. log(message, address, 'error.log')
  71. log('>-< Disconnected because error: %s' % message, address)
  72. finally:
  73. client.close()
  74. log('--- Disconnected', address)
  75. def format(token):
  76. return '-'.join(token)
  77. def RSA(bit):
  78. e, n, d = keygen.generateKeys(bit)
  79. p = random(1000, 9999)
  80. c = pow(p, e, n)
  81. return e, n, d, p, c
  82. def genRSA(client, address):
  83. e, n ,d, p, c = RSA(BIT)
  84. client.send('e = %s \nn = %s \nc = %s \np = ' % (e, n, c))
  85. r = client.recv(BUFF).strip()
  86. if r == str(p):
  87. log('<-> Correct %s{e:%s,n:%s,d:%s,p:%s,c:%s)' % (r, e, n, d, p, c), address)
  88. return '\n\m/ Correct \m/\n'
  89. else:
  90. log('>-< Wrong %s{e:%s,n:%s,d:%s,p:%s,c:%s)' % (r, e, n, d, p, c), address)
  91. return '\nWrong :P is %s\n' % p
  92. def genToken(client, address):
  93. token = []
  94. data = 'Token has been generated!\n'
  95. for i in range(1, 6):
  96. e, n ,d, p, c = RSA(BIT)
  97. client.send('Token #%i\ne = %s \nn = %s \nc = %s \np = ' % (i, e, n, c))
  98. r = client.recv(BUFF).strip()
  99. if r == str(p):
  100. log('<-> Correct #%d: %s{e:%s,n:%s,d:%s,p:%s,c:%s)' % (i, r, e, n, d, p, c), address)
  101. token.append(str(p))
  102. else:
  103. token = []
  104. data = '\nWrong :P is %s\n' % p
  105. log('>-< Wrong #%d: %s{e:%s,n:%s,d:%s,p:%s,c:%s)' % (i, r, e, n, d, p, c), address)
  106. break
  107. return data, token
  108. def main():
  109. server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  110. server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  111. server.bind((HOST, PORT))
  112. server.listen(MAX)
  113. log('(+) Power On Server', [HOST, PORT])
  114. while True:
  115. try:
  116. client, address = server.accept()
  117. log('<<< Client connected from IP %s with PORT %d' % (address), address)
  118. start_new_thread(serve_client, (client, address))
  119. except Exception as message:
  120. log(message, [HOST, PORT], 'error.log')
  121. except KeyboardInterrupt:
  122. log('(-) Power Off Server', [HOST, PORT])
  123. break
  124. server.close()
  125. if __name__ == '__main__':
  126. main()