Give challenge plugins the ability to specify the response message (#351)

This commit is contained in:
Kevin Chung
2017-08-14 20:27:54 -04:00
committed by GitHub
parent c392748688
commit 7e6d56694e
2 changed files with 23 additions and 20 deletions

View File

@@ -287,30 +287,33 @@ def chal(chalid):
}) })
chal_class = get_chal_class(chal.type) chal_class = get_chal_class(chal.type)
if chal_class.solve(chal, provided_key): status, message = chal_class.solve(chal, provided_key)
if status: # The challenge plugin says the input is right
if utils.ctftime(): if utils.ctftime():
solve = Solves(teamid=session['id'], chalid=chalid, ip=utils.get_ip(), flag=provided_key) solve = Solves(teamid=session['id'], chalid=chalid, ip=utils.get_ip(), flag=provided_key)
db.session.add(solve) db.session.add(solve)
db.session.commit() db.session.commit()
db.session.close() db.session.close()
logger.info("[{0}] {1} submitted {2} with kpm {3} [CORRECT]".format(*data)) logger.info("[{0}] {1} submitted {2} with kpm {3} [CORRECT]".format(*data))
return jsonify({'status': 1, 'message': 'Correct'}) return jsonify({'status': 1, 'message': message})
else: # The challenge plugin says the input is wrong
if utils.ctftime(): if utils.ctftime():
wrong = WrongKeys(teamid=session['id'], chalid=chalid, ip=utils.get_ip(), flag=provided_key) wrong = WrongKeys(teamid=session['id'], chalid=chalid, ip=utils.get_ip(), flag=provided_key)
db.session.add(wrong) db.session.add(wrong)
db.session.commit() db.session.commit()
db.session.close() db.session.close()
logger.info("[{0}] {1} submitted {2} with kpm {3} [WRONG]".format(*data)) logger.info("[{0}] {1} submitted {2} with kpm {3} [WRONG]".format(*data))
# return '0' # key was wrong # return '0' # key was wrong
if max_tries: if max_tries:
attempts_left = max_tries - fails - 1 # Off by one since fails has changed since it was gotten attempts_left = max_tries - fails - 1 # Off by one since fails has changed since it was gotten
tries_str = 'tries' tries_str = 'tries'
if attempts_left == 1: if attempts_left == 1:
tries_str = 'try' tries_str = 'try'
return jsonify({'status': 0, 'message': 'Incorrect. You have {} {} remaining.'.format(attempts_left, tries_str)}) if message[-1] not in '!().;?[]\{\}': # Add a punctuation mark if there isn't one
else: message = message + '.'
return jsonify({'status': 0, 'message': 'Incorrect'}) return jsonify({'status': 0, 'message': '{} You have {} {} remaining.'.format(message, attempts_left, tries_str)})
else:
return jsonify({'status': 0, 'message': message})
# Challenge already solved # Challenge already solved
else: else:

View File

@@ -16,8 +16,8 @@ class CTFdStandardChallenge(BaseChallenge):
chal_keys = Keys.query.filter_by(chal=chal.id).all() chal_keys = Keys.query.filter_by(chal=chal.id).all()
for chal_key in chal_keys: for chal_key in chal_keys:
if get_key_class(chal_key.key_type).compare(chal_key.flag, provided_key): if get_key_class(chal_key.key_type).compare(chal_key.flag, provided_key):
return True return True, 'Correct'
return False return False, 'Incorrect'
CHALLENGE_CLASSES = { CHALLENGE_CLASSES = {