コード例 #1
0
ファイル: problem.py プロジェクト: easyctf/openctf-docker
def problem_submit():
    params = utils.flat_multi(request.form)
    pid = params.get("pid")
    flag = params.get("flag")
    tid = session.get("tid")
    _user = user.get_user().first()
    username = _user.username

    problem = Problems.query.filter_by(pid=pid).first()
    team = Teams.query.filter_by(tid=tid).first()
    solved = Solves.query.filter_by(pid=pid, tid=tid, correct=1).first()
    if solved:
        raise WebException("You already solved this problem.")

    flag_tried = Solves.query.filter_by(pid=pid, flag=flag).first()
    if flag_tried:
        raise WebException("Your team has already tried this solution.")

    if problem:
        if problem.category == "Programming":
            raise WebException(
                "Please submit programming problems using the Programming interface."
            )
        grader = imp.load_source("grader", problem.grader)
        random = None
        if problem.autogen:
            random = autogen.get_random(pid, tid)
        correct, response = grader.grade(random, flag)

        solve = Solves(pid, _user.uid, tid, flag, correct)
        db.session.add(solve)
        db.session.commit()

        if correct:
            # Wait until after the solve has been added to the database before adding bonus
            solves = get_solves(pid)
            solve.bonus = [-1, solves][solves < 4]

            cache.invalidate_memoization(get_solves, pid)

            if _user:
                activity = UserActivity(_user.uid, 3, tid=tid, pid=pid)
                db.session.add(activity)

            db.session.commit()
            logger.log(__name__,
                       "%s has solved %s by submitting %s" %
                       (team.teamname, problem.title, flag),
                       level=logger.WARNING)
            return {"success": 1, "message": response}
        else:
            logger.log(__name__,
                       "%s has incorrectly submitted %s to %s" %
                       (team.teamname, flag, problem.title),
                       level=logger.WARNING)
            raise WebException(response)

    else:
        raise WebException("Problem does not exist!")
コード例 #2
0
ファイル: problem.py プロジェクト: EasyCTF/openctf-docker
def clear_solves():
	params = utils.flat_multi(request.form)

	pid = params.get("pid")
	Solves.query.filter_by(pid=pid).delete()
	ProgrammingSubmissions.query.filter_by(pid=pid).delete()
	cache.invalidate_memoization(get_solves, pid)
	db.session.commit()

	return { "success": 1, "message": "Submissions cleared." }
コード例 #3
0
ファイル: problem.py プロジェクト: easyctf/openctf-docker
def clear_solves():
    params = utils.flat_multi(request.form)

    pid = params.get("pid")
    Solves.query.filter_by(pid=pid).delete()
    ProgrammingSubmissions.query.filter_by(pid=pid).delete()
    cache.invalidate_memoization(get_solves, pid)
    db.session.commit()

    return {"success": 1, "message": "Submissions cleared."}
コード例 #4
0
ファイル: problem.py プロジェクト: EasyCTF/openctf-docker
def problem_submit():
	params = utils.flat_multi(request.form)
	pid = params.get("pid")
	flag = params.get("flag")
	tid = session.get("tid")
	_user = user.get_user().first()
	username = _user.username

	problem = Problems.query.filter_by(pid=pid).first()
	team = Teams.query.filter_by(tid=tid).first()
	solved = Solves.query.filter_by(pid=pid, tid=tid, correct=1).first()
	if solved:
		raise WebException("You already solved this problem.")

	flag_tried = Solves.query.filter_by(pid=pid, flag=flag).first()
	if flag_tried:
		raise WebException("Your team has already tried this solution.")

	if problem:
		if problem.category == "Programming":
			raise WebException("Please submit programming problems using the Programming interface.")
		grader = imp.load_source("grader", problem.grader)
		random = None
		if problem.autogen:
			random = autogen.get_random(pid, tid)
		correct, response = grader.grade(random, flag)

		solve = Solves(pid, _user.uid, tid, flag, correct)
		db.session.add(solve)
		db.session.commit()

		if correct:
			# Wait until after the solve has been added to the database before adding bonus
			solves = get_solves(pid)
			solve.bonus = [-1, solves][solves < 4]

			cache.invalidate_memoization(get_solves, pid)

			if _user:
				activity = UserActivity(_user.uid, 3, tid=tid, pid=pid)
				db.session.add(activity)

			db.session.commit()
			logger.log(__name__, "%s has solved %s by submitting %s" % (team.teamname, problem.title, flag), level=logger.WARNING)
			return { "success": 1, "message": response }
		else:
			logger.log(__name__, "%s has incorrectly submitted %s to %s" % (team.teamname, flag, problem.title), level=logger.WARNING)
			raise WebException(response)

	else:
		raise WebException("Problem does not exist!")
コード例 #5
0
ファイル: programming.py プロジェクト: EasyCTF/openctf-docker
def submit_program():
	params = utils.flat_multi(request.form)

	pid = params.get("pid")
	tid = session.get("tid")
	_user = user.get_user().first()

	language = params.get("language")
	submission_contents = params.get("submission")

	_problem = problem.get_problem(pid=pid).first()
	if _problem is None:
		raise WebException("Problem does not exist.")

	if _problem.category != "Programming":
		raise WebException("Can't judge this problem.")

	if language not in extensions:
		raise WebException("Language not supported.")

	solved = Solves.query.filter_by(pid=pid, tid=tid, correct=1).first()
	if solved:
		raise WebException("You already solved this problem.")

	judge_folder = os.path.join(app.config["GRADER_FOLDER"], pid)
	if not os.path.exists(judge_folder):
		os.makedirs(judge_folder)

	submission_folder = os.path.join(judge_folder, utils.generate_string())
	while os.path.exists(submission_folder):
		submission_folder = os.path.join(judge_folder, utils.generate_string())

	os.makedirs(submission_folder)

	submission_path = os.path.join(submission_folder, "program.%s" % extensions[language])

	open(submission_path, "w").write(submission_contents)
	message, log, duration = judge(submission_path, language, pid)

	number = ProgrammingSubmissions.query.filter_by(tid=tid).with_entities(ProgrammingSubmissions.number).order_by(ProgrammingSubmissions.number.desc()).first()

	if number is None:
		number = 1
	else:
		number = number[0] + 1

	submission = ProgrammingSubmissions(pid, tid, submission_path, message, log, number, duration)

	correct = message == "Correct!"

	with app.app_context():
		solve = Solves(pid, _user.uid, tid, submission_path, correct)
		db.session.add(solve)
		db.session.add(submission)
		db.session.commit()

		if correct:
			# Wait until after the solve has been added to the database before adding bonus
			solves = problem.get_solves(pid=pid)
			solve.bonus = [-1, solves][solves < 3]
			db.session.add(solve)
			cache.invalidate_memoization(problem.get_solves, pid)

			if _user:
				activity = UserActivity(_user.uid, 3, tid=tid, pid=pid)
				db.session.add(activity)

			db.session.commit()
		new = {
			"psid": submission.psid,
			"title": _problem.title,
			"message": submission.message,
			"log": submission.log,
			"date": utils.isoformat(submission.date),
			"number": submission.number
		}

	shutil.rmtree(submission_folder)

	return { "success": message == "Correct!", "message": message , "new_submission": new }
コード例 #6
0
ファイル: programming.py プロジェクト: easyctf/openctf-docker
def submit_program():
    params = utils.flat_multi(request.form)

    pid = params.get("pid")
    tid = session.get("tid")
    _user = user.get_user().first()

    language = params.get("language")
    submission_contents = params.get("submission")

    _problem = problem.get_problem(pid=pid).first()
    if _problem is None:
        raise WebException("Problem does not exist.")

    if _problem.category != "Programming":
        raise WebException("Can't judge this problem.")

    if language not in extensions:
        raise WebException("Language not supported.")

    solved = Solves.query.filter_by(pid=pid, tid=tid, correct=1).first()
    if solved:
        raise WebException("You already solved this problem.")

    judge_folder = os.path.join(app.config["GRADER_FOLDER"], pid)
    if not os.path.exists(judge_folder):
        os.makedirs(judge_folder)

    submission_folder = os.path.join(judge_folder, utils.generate_string())
    while os.path.exists(submission_folder):
        submission_folder = os.path.join(judge_folder, utils.generate_string())

    os.makedirs(submission_folder)

    submission_path = os.path.join(submission_folder,
                                   "program.%s" % extensions[language])

    open(submission_path, "w").write(submission_contents)
    message, log, duration = judge(submission_path, language, pid)

    number = ProgrammingSubmissions.query.filter_by(tid=tid).with_entities(
        ProgrammingSubmissions.number).order_by(
            ProgrammingSubmissions.number.desc()).first()

    if number is None:
        number = 1
    else:
        number = number[0] + 1

    submission = ProgrammingSubmissions(pid, tid, submission_path, message,
                                        log, number, duration)

    correct = message == "Correct!"

    with app.app_context():
        solve = Solves(pid, _user.uid, tid, submission_path, correct)
        db.session.add(solve)
        db.session.add(submission)
        db.session.commit()

        if correct:
            # Wait until after the solve has been added to the database before adding bonus
            solves = problem.get_solves(pid=pid)
            solve.bonus = [-1, solves][solves < 3]
            db.session.add(solve)
            cache.invalidate_memoization(problem.get_solves, pid)

            if _user:
                activity = UserActivity(_user.uid, 3, tid=tid, pid=pid)
                db.session.add(activity)

            db.session.commit()
        new = {
            "psid": submission.psid,
            "title": _problem.title,
            "message": submission.message,
            "log": submission.log,
            "date": utils.isoformat(submission.date),
            "number": submission.number
        }

    shutil.rmtree(submission_folder)

    return {
        "success": message == "Correct!",
        "message": message,
        "new_submission": new
    }