From f765a0b6f9de9de8fe2d1d82643fae259ebe865f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Gro=C3=9F?= Date: Sun, 8 Jul 2018 19:13:41 -0400 Subject: [PATCH] Imported challenge checks and helper classes Added .gitignore file Added python server example as used in the "Basically Gambling" challenge Imported automatic chellange checks and ansicolor helper class --- .gitignore | 6 + check_challenges.py | 138 ++++++++++++++++++ python_helper/__init__.py | 0 .../__pycache__/__init__.cpython-34.pyc | Bin 0 -> 128 bytes .../__pycache__/ansicolors.cpython-34.pyc | Bin 0 -> 305 bytes python_helper/ansicolors.py | 8 + server.py | 38 +++++ 7 files changed, 190 insertions(+) create mode 100644 .gitignore create mode 100755 check_challenges.py create mode 100644 python_helper/__init__.py create mode 100644 python_helper/__pycache__/__init__.cpython-34.pyc create mode 100644 python_helper/__pycache__/ansicolors.cpython-34.pyc create mode 100644 python_helper/ansicolors.py create mode 100644 server.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..34ba226 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +#Ignore virtualenv +venv/ + +#Ignore editor specific files +.vimrc +.vscode/ diff --git a/check_challenges.py b/check_challenges.py new file mode 100755 index 0000000..88ce6cf --- /dev/null +++ b/check_challenges.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python3 +import requests, socket, errno, json +import base64 as b64 +from python_helper import ansicolors as colors + +''' +This script checks if all the challenges are online and operational. +Currently these challenges are being checked: +Basically Gambling +Business Inquiry +Looking Good +''' +CTF_IP = '84.200.106.95' +CTF_DOMAIN = 'ctf.minzkraut.com' + +def print_ok(str_out, symbol=u'\u2714', symcol=colors.OKGREEN, str_color=colors.OKBLUE): + print("{sym_color}| {sym} |{str_color}{string}{endc}".format( + sym_color=symcol, sym=symbol, str_color=str_color, string=str_out, endc=colors.ENDC + )) +def print_fail(str_out, symbol=u'\u2716', symcol=colors.FAIL, str_color=colors.OKBLUE): + print("{sym_color}| {sym} |{str_color}{string}{endc}".format( + sym_color=symcol, sym=symbol, str_color=str_color, string=str_out, endc=colors.ENDC + )) +def print_info(str_out, symbol=u'\u21B3', symcol=colors.OKBLUE, str_color=colors.OKBLUE): + print_message("-- {}".format(str_out), symbol, symcol, str_color) +def print_message(str_out, symbol=u'\u2610', symcol=colors.HEADER, str_color=colors.OKBLUE): + print("{sym_color}| {sym} |{str_color}{string}{endc}".format( + sym_color=symcol, sym=symbol, str_color=str_color, string=str_out, endc=colors.ENDC + )) +def chk_port(ip, port): + success = True + client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + try: + client.connect((ip, port)) + except socket.error as e: + return (False, e) + finally: + client.close() + return (True) + +def check_server(): + #Check for SSH + client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + try: + client.connect((CTF_IP, 22)) + except socket.error as e: + print_fail("Server Offline! (SSH) {} - {}".format(e.errno, e.strerror)) + return False + finally: + client.close() + print_ok("Server Online! (SSH)") + + #Check if webserver is ok + try: + requests.get("http://{}".format(CTF_DOMAIN)) + except requests.exceptions.ConnectionError as e: + print_info("Webserver: FAILED at {}! (Connection Error!)".format(CTF_DOMAIN), symcol=colors.FAIL) + except requests.exceptions.HTTPError as e: + print_info("Webserver: FAILED with HTTPError {} at {}".format(e.message, CTF_DOMAIN)) + else: + print_info("Webserver: OK") + + #Check if FTP is ok + ftp_chk = chk_port(CTF_IP, 21) + if ftp_chk[0] == True: + print_info("FTP: Ok") + else: + print_info("FTP: FAILED! {}".format(ftp_chk[1].strerror), symcol=colors.FAIL) + + return True + +def check_business_inquiry(): + try: + response = requests.get("http://loremcorp.{}".format(CTF_DOMAIN)) + html = response.text + if "challenge_chk" in html: + print_ok("LoremCorp challenge: OK") + return True + except requests.exceptions.RequestException: + print_fail("LoremCorp challenge: Connection Error!") + return False + print_fail("LoremCorp Challenge: Not working, chk flag not found!") + return False + +def check_looking_good(): + try: + response = requests.get("http://loremcorp.{}:8787/login.php".format(CTF_DOMAIN)) + html = response.text + response = requests.post("http://loremcorp.{}:8787/view.php".format(CTF_DOMAIN), data={'username':'factory_admin', 'password': 'CHANGE_THIS_BEFORE_SHIPPING!'}) + html = html + response.text + if not "challenge_chk_login" in html: + print_fail("LookingGood Challenge: challenge_chk_login flag not found!") + if not "challenge_chk_view" in html: + print_fail("LookingGood Challenge: challenge_chk_view flag not found!") + else: + print_ok("LookingGood challenge: OK") + return True + except requests.exceptions.RequestException: + print_fail("LookingGood challenge: Connection Error!") + return False + return False + +def check_basically_gambling(): + client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + data = "" + try: + client.connect((CTF_IP, 6646)) + data = client.recv(512) + data=data.decode() + data = data.replace('\n', '') + if b64.b64encode(b64.b64decode(data)) == data.encode(): + print_ok("LuckyNr64 challenge: OK") + return True + else: + print_fail("LuckyNr64 challenge not returning valid base64!") + print_info(data) + except socket.error as e: + print_fail("LuckyNr64 challenge: Socket error! ({} - {})".format(e.errno, e.strerror)) + except UnicodeDecodeError: + print_fail("LuckyNr64 challenge returning non unicode characters!") + print_info(data) + return False + +def run_checks(): + print("Checking Server Status") + if not check_server(): + return False + print("\n") + print("Checking Challenges...") + challenges = [check_business_inquiry, check_looking_good, check_basically_gambling] + working = 0 + for challenge in challenges: + if challenge(): + working += 1 + print("{}/{} challenges working".format(working, len(challenges))) + +if __name__ == "__main__": + run_checks() \ No newline at end of file diff --git a/python_helper/__init__.py b/python_helper/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python_helper/__pycache__/__init__.cpython-34.pyc b/python_helper/__pycache__/__init__.cpython-34.pyc new file mode 100644 index 0000000000000000000000000000000000000000..769390ac2927d21e430e21241df118c0c57dbc86 GIT binary patch literal 128 zcmaFI!^t DZ9g81 literal 0 HcmV?d00001 diff --git a/python_helper/__pycache__/ansicolors.cpython-34.pyc b/python_helper/__pycache__/ansicolors.cpython-34.pyc new file mode 100644 index 0000000000000000000000000000000000000000..583df6178b55b63849c4c560906f673b85c17127 GIT binary patch literal 305 zcmYk2%}T>S5XUF&=A)v%K%bx&*N=D*L`==vhK3D9im*9I=|)n_WfHzEMb`wx8Rqxbx36SxJ;0&WAdfjjUt8MWp(xw#gd++LTxu6kXF7LAkhj(v6oojA&x6VE3xv!cZ` zToPqRi&;zvRra$N(}2ztdh~)&AwvBJg{D#Hv(b|3l`x>>11ekJ+~I(6CPj+HrfA=) v)waRw<`tK_CX>ZWmgZ$z;Z;#@)-sn>eOB(2b1Nj@=IPz<1L{)^%lO|vw$Vsz literal 0 HcmV?d00001 diff --git a/python_helper/ansicolors.py b/python_helper/ansicolors.py new file mode 100644 index 0000000..92b135f --- /dev/null +++ b/python_helper/ansicolors.py @@ -0,0 +1,8 @@ +HEADER = '\033[95m' +OKBLUE = '\033[94m' +OKGREEN = '\033[92m' +WARNING = '\033[93m' +FAIL = '\033[91m' +ENDC = '\033[0m' +BOLD = '\033[1m' +UNDERLINE = '\033[4m' \ No newline at end of file diff --git a/server.py b/server.py new file mode 100644 index 0000000..cbd5ae0 --- /dev/null +++ b/server.py @@ -0,0 +1,38 @@ +import threading +import SocketServer + +class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler): + + def handle(self): + data = self.request.recv(1024) + cur_thread = threading.current_thread() + print "Received: {}".format(data) + response = "{}".format(cur_thread.name) + self.request.sendall(response) + +class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): + pass + +if __name__ == "__main__": + HOST, PORT = "localhost", 6667 + + server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler) + ip, port = server.server_address + + # Start a thread with the server -- that thread will then start one + # more thread for each request + server_thread = threading.Thread(target=server.serve_forever) + # Exit the server thread when the main thread terminates + server_thread.daemon = True + server_thread.start() + print "Server loop running in thread:", server_thread.name + #server.shutdown() + #server.server_close() + while ( True ): + try: + server.get_request() + except KeyboardInterrupt: + print "Server Stopping" + server.shutdown() + server.server_close() + break