Python script to check challenge availability at SlashRoot CTF 3.0
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.

174 lines
4.4 KiB

5 years ago
  1. #!/usr/bin/python2
  2. import time
  3. import socket
  4. import requests
  5. import threading
  6. from colorama import Fore, Back, Style
  7. TIMEOUT = 0.1
  8. SLEEP = 0
  9. BUFF = 8192
  10. HOST = "192.168.1.2"
  11. TEAM_NAME = [
  12. "RevID.CTF",
  13. "Youtube",
  14. "Flag Earth",
  15. "UMN",
  16. "Pengen Mandi",
  17. "Gundar Cysec",
  18. "gaboet berfaedah",
  19. "Fake Team",
  20. "UNAIR FT SHL",
  21. "Jangan Dibaca tJOY",
  22. ]
  23. CHALLENGE = [
  24. "MTP - Crypto",
  25. "Sophisticated Calculator - Pwn",
  26. "My Secure Web App - Web",
  27. "Save HTML Design - Web",
  28. "Name Generator - Pwn",
  29. "Web Image Search - Web",
  30. ]
  31. EXPECTED_ANSWER = {
  32. 1: " ! +-+-+-+-+-+-+-+-+-+ +-+-+-+ +-+-+-+\n ^ |S|l|a|s|h|R|o|o|t| |C|T|F| |3|.|0|\n / \\ +-+-+-+-+-+-+-+-+-+ +-+-+-+ +-+-+-+\n /___\\\n |= =| +-+-+-+-+-+-+-+ +-+-+ +-+-+-+-+-+-+-+-+\n | B | |M|i|s|s|i|o|n| |T|o| |D|y|s|n|o|m|i|a|\n | A | +-+-+-+-+-+-+-+ +-+-+ +-+-+-+-+-+-+-+-+\n | L |\n /| I |\\ [G] Get Session\n / |##!##| \\ [S] Set Session\n| / ^ | ^ \\ | [T] Generate Token\n|/ (( | )) \\| [L] Let's Go\n (( )) [C] Cancel\n ( )\n .\n \n",
  33. 2: "Welcome to Online Calculator v3.1\n\nOperator supported: + - * /\nex: 2 + 2\nex: 2 + a //'a' to use last results\n\n------------------------------------\n>",
  34. 5: ">> Your Nick: ",
  35. }
  36. LOCK = threading.Lock()
  37. class Challenge(object):
  38. down = 0
  39. stat = 1
  40. def __init__(self, challPort, challName):
  41. self.name = challName
  42. self.port = challPort
  43. def set_down(self):
  44. if self.stat:
  45. self.stat = 0
  46. self.down = time.time()
  47. def set_up(self):
  48. self.stat = 1
  49. self.down = 0
  50. def get_downtime(self):
  51. return int(round(time.time() - self.down))
  52. class Team(object):
  53. def __init__(self, port, teamName):
  54. self.name = teamName
  55. self.port = port
  56. self.challengeList = [Challenge(index, challName)
  57. for index, challName in enumerate(CHALLENGE, start=1)]
  58. def main():
  59. teamList = [Team(port, teamName)
  60. for port, teamName in enumerate(TEAM_NAME, start=1)]
  61. while True:
  62. for team in teamList:
  63. for chall in team.challengeList:
  64. #threading._start_new_thread(test_challenge, (team, chall))
  65. test_challenge(team, chall)
  66. time.sleep(SLEEP)
  67. def success(team, chall):
  68. msg = now() + "[6" + str(team.port).zfill(2) + str(chall.port).zfill(2) + \
  69. "] " + team.name + " " + chall.name + " is UP"
  70. if not chall.stat:
  71. msg += " again total down time = " + str(chall.get_downtime()) + "s"
  72. with open("new_check.log", "a") as log:
  73. log.write(msg + "\n")
  74. chall.set_up()
  75. LOCK.acquire()
  76. print(Fore.GREEN + msg + Style.RESET_ALL)
  77. LOCK.release()
  78. def failed(team, chall):
  79. chall.set_down()
  80. msg = now() + "[6" + str(team.port).zfill(2) + str(chall.port).zfill(2) + "] " + \
  81. team.name + " " + chall.name + " is DOWN for " + \
  82. str(chall.get_downtime()) + "s"
  83. with open("new_check.log", "a") as log:
  84. log.write(msg + "\n")
  85. LOCK.acquire()
  86. print(Fore.RED + msg + Style.RESET_ALL)
  87. LOCK.release()
  88. def now():
  89. return time.strftime("%X ")
  90. def test_challenge(team, chall):
  91. port = 60000+team.port*100+chall.port
  92. '''
  93. if chall.port == 3:
  94. return
  95. '''
  96. if chall.name[-3:] == "Web":
  97. chk = test_web(port)
  98. else:
  99. chk = test_socket(port)
  100. if chk:
  101. success(team, chall)
  102. else:
  103. failed(team, chall)
  104. def test_socket(port):
  105. try:
  106. socket.setdefaulttimeout(TIMEOUT)
  107. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  108. s.connect((HOST, port))
  109. r = ""
  110. t = EXPECTED_ANSWER[port % 100]
  111. chk = False
  112. while not chk:
  113. r += s.recv(BUFF)
  114. chk = r == t
  115. return chk
  116. except KeyboardInterrupt:
  117. exit(0)
  118. except:
  119. return False
  120. def test_web(port):
  121. try:
  122. r = requests.get("http://"+HOST+":"+str(port), timeout=TIMEOUT)
  123. chk1 = True # r.text == EXPECTED_ANSWER[get_chall(port)]
  124. chk2 = r.status_code == requests.codes.ok
  125. if chk1 and chk2:
  126. return True
  127. else:
  128. return False
  129. except KeyboardInterrupt:
  130. exit(0)
  131. except:
  132. return False
  133. if __name__ == '__main__':
  134. main()