From 5761f379c36c7541375e2aa9bd2b676ecdd28812 Mon Sep 17 00:00:00 2001 From: Domingo Dirutigliano Date: Sun, 2 Jun 2024 00:44:47 +0200 Subject: [PATCH] fix: fixes on submitter-test in xfarm --- client/exploitfarm/utils/reqs.py | 6 +++- client/xfarm | 14 ++++++-- scripts/ccit_auto_setup.py | 48 ++++------------------------ scripts/submitters/ccit_submitter.py | 41 ++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 46 deletions(-) create mode 100644 scripts/submitters/ccit_submitter.py diff --git a/client/exploitfarm/utils/reqs.py b/client/exploitfarm/utils/reqs.py index 377ad56..0f3bab0 100644 --- a/client/exploitfarm/utils/reqs.py +++ b/client/exploitfarm/utils/reqs.py @@ -33,7 +33,11 @@ class EMPTY:pass def requests_check(res: Dict) -> Any: if res["status"] != "ok": - raise ReqsError(res["message"]) + msg = res.get("message", None) + if isinstance(msg, str): + raise ReqsError(msg) + else: + raise ReqsError("Unknown error") else: return res["response"] diff --git a/client/xfarm b/client/xfarm index 0f98526..6fe926a 100755 --- a/client/xfarm +++ b/client/xfarm @@ -19,6 +19,8 @@ from exploitfarm.model import Language from exploitfarm.utils.config import EXPLOIT_CONFIG_REGEX from multiprocessing import Manager from exploitfarm.utils.cmd.startxploit import start_exploit_tui +from exploitfarm.utils.reqs import ReqsError +from requests.exceptions import Timeout as RequestsTimeout import traceback @@ -216,7 +218,7 @@ def submitter_test( if len(flags) == 0: print(f"[bold red]No flags extracted from output! REGEX: {escape(g.config.status['config']['FLAG_REGEX'])}") return - + submitter_id = None try: submitter_id:int = g.config.reqs.new_submitter({ "name": "TEST_SUBMITTER (Will be deleted soon)", @@ -229,7 +231,8 @@ def submitter_test( print(g.config.reqs.test_submitter(submitter_id, flags)) print("[bold yellow]----- TEST RESULTS -----") finally: - g.config.reqs.delete_submitter(submitter_id) + if submitter_id: + g.config.reqs.delete_submitter(submitter_id) class StatusWhat(Enum): status = "status" @@ -334,4 +337,9 @@ def main(interactive: bool = typer.Option(True, help="Interactive configuration g.interactive = interactive if __name__ == "__main__": - app() + try: + app() + except ReqsError as e: + print("[bold red]The server returned an error: {e}[/]") + except RequestsTimeout as e: + print(f"[bold red]The server has timed out: {e}[/]") diff --git a/scripts/ccit_auto_setup.py b/scripts/ccit_auto_setup.py index 294a4b8..2b6a53e 100644 --- a/scripts/ccit_auto_setup.py +++ b/scripts/ccit_auto_setup.py @@ -5,9 +5,15 @@ from exploitfarm.model import AttackMode from dateutil.parser import parse as date_parser from rich import print +from os.path import join as pjoin +from os.path import dirname CCIT_SERVER = "10.10.0.1" +with open(pjoin(dirname(__file__), "submitters", "ccit_submitter.py")) as f: + SUBMITTER = f.read() +print(SUBMITTER) + try: general_info = requests.get(f"http://{CCIT_SERVER}/api/status", timeout=5).json() except Exception as e: @@ -33,48 +39,6 @@ pass print("Invalid team id") -SUBMITTER = """ -import requests - -class FlagStatus: - ok = 'ok' - wait = 'wait' - timeout = 'timeout' - invalid = 'invalid' - -RESPONSES = { - FlagStatus.wait: ['game not started', 'try again later', 'game over', 'is not up', 'no such flag'], - FlagStatus.timeout: ['timeout'], - FlagStatus.ok: ['accepted', 'congrat'], - FlagStatus.invalid: ['bad', 'wrong', 'expired', 'unknown', 'your own', - 'too old', 'not in database', 'already', 'invalid', 'nop team'], -} - - -def submit(flags, token:str = None, http_timeout:int=30, url:str="http://10.10.0.1:8080/flags"): - r = requests.put(url, headers={'X-Team-Token': token}, json=flags, timeout=http_timeout) - if r.status_code == 429: - for flag in flags: - yield (flag, FlagStatus.wait, "Too many requests. Error 429") - else: - for i, item in enumerate(r.json()): - if not isinstance(item, dict): - yield (flags[i], FlagStatus.wait, "Unexpected response. Error 429") - - response = item['msg'].strip() - response = response.replace('[{}] '.format(item['flag']), '') - - response_lower = response.lower() - for status, substrings in RESPONSES.items(): - if any(s in response_lower for s in substrings): - found_status = status - break - else: - found_status = FlagStatus.wait - - yield (item['flag'], found_status, response) -""" - submitter_id = config.reqs.new_submitter({ "name": "CCIT submitter", "code": SUBMITTER, diff --git a/scripts/submitters/ccit_submitter.py b/scripts/submitters/ccit_submitter.py new file mode 100644 index 0000000..940d9d1 --- /dev/null +++ b/scripts/submitters/ccit_submitter.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +import requests + +class FlagStatus: + ok = 'ok' + wait = 'wait' + timeout = 'timeout' + invalid = 'invalid' + +RESPONSES = { + FlagStatus.wait: ['game not started', 'try again later', 'game over', 'is not up', 'no such flag'], + FlagStatus.timeout: ['timeout'], + FlagStatus.ok: ['accepted', 'congrat'], + FlagStatus.invalid: ['bad', 'wrong', 'expired', 'unknown', 'your own', + 'too old', 'not in database', 'already', 'invalid', 'nop team'], +} + + +def submit(flags, token:str = None, http_timeout:int=30, url:str="http://10.10.0.1:8080/flags"): + r = requests.put(url, headers={'X-Team-Token': token}, json=flags, timeout=http_timeout) + if r.status_code == 429: + for flag in flags: + yield (flag, FlagStatus.wait, "Too many requests. Error 429") + else: + for i, item in enumerate(r.json()): + if not isinstance(item, dict): + yield (flags[i], FlagStatus.wait, "Unexpected response. Error 429") + + response = item['msg'].strip() + response = response.replace('[{}] '.format(item['flag']), '') + + response_lower = response.lower() + for status, substrings in RESPONSES.items(): + if any(s in response_lower for s in substrings): + found_status = status + break + else: + found_status = FlagStatus.wait + + yield (item['flag'], found_status, response) \ No newline at end of file