Skip to content

Commit

Permalink
fix: fixes on submitter-test in xfarm
Browse files Browse the repository at this point in the history
  • Loading branch information
domysh committed Jun 1, 2024
1 parent de2584a commit 5761f37
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 46 deletions.
6 changes: 5 additions & 1 deletion client/exploitfarm/utils/reqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
14 changes: 11 additions & 3 deletions client/xfarm
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)",
Expand All @@ -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"
Expand Down Expand Up @@ -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}[/]")
48 changes: 6 additions & 42 deletions scripts/ccit_auto_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand Down
41 changes: 41 additions & 0 deletions scripts/submitters/ccit_submitter.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 5761f37

Please sign in to comment.