diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..d7f7eb5 --- /dev/null +++ b/__init__.py @@ -0,0 +1,78 @@ +from CTFd.plugins import register_plugin_assets_directory +from CTFd.plugins.challenges import BaseChallenge, CHALLENGE_CLASSES +from CTFd.plugins.keys import get_key_class +from CTFd.models import db, Solves, WrongKeys, Keys, Challenges, Files, Tags, Teams, Hints +import random +import string + +class GenFlagChallenge(BaseChallenge): + id = "genflag" + name = "genflag" + + @staticmethod + def create(request): + files = request.files.getlist('files[]') + + # Create challenge + chal = GenFlags( + name=request.form['name'], + value=request.form['value'], + category=request.form['category'], + type=request.form['chaltype'], + generator=request.form['generator'] + ) + + if 'hidden' in request.form: + chal.hidden = True + else: + chal.hidden = False + + max_attempts = request.form.get('max_attempts') + if max_attempts and max_attempts.isdigit(): + chal.max_attempts = int(max_attempts) + + gen = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(6)) + chal.key = "generated_flag_" + gen + + db.session.add(chal) + db.session.commit() + + flag = Keys(chal.id, chal.key, 'static') + db.session.add(flag) + db.session.commit() + + for f in files: + utils.upload_file(file=f, chalid=chal.id) + + db.session.commit() + + +class GenFlagCDBM(challenges): + __mapper_args__ = {'polymorphic_identity': 'genflags'} + id = db.Column(None, db.ForeignKey('challenges.id'), primary_key=True) + key = db.Column(db.Text) + generator = db.Column(db.Text) + + def __init__(self, name, value, category, type='genflag', key, generator): + self.name = name + self.value = value + self.category = category + self.type = type + self.key = key + self.generator = generator + +class GenFlags(db.model): + id = db.Column(Integer, db.ForeignKey('challenges.id')) + teamid = db.Column(None, db.ForeignKey('teams.id'), primary_key=True) + description = db.Column(db.Text) + flag = db.Column(db.Text) + + def __init__(self, teamid, description, flag): + self.teamid = teamid + self.flag = flag + self.description = description + +def load(app): + app.db.create_all() + CHALLENGE_CLASSES['genflag'] = GenFlagChallenge + register_plugin_assets_directory(app, base_path='/plugins/ctfd-generate-flags/assets/') diff --git a/generators/__init__.py b/generators/__init__.py new file mode 100644 index 0000000..0364711 --- /dev/null +++ b/generators/__init__.py @@ -0,0 +1,13 @@ +class BaseGen(object): + id = None # Unique identifier used to register generator + name = None # Name of generator + + @staticmethod + def genflag(challenge): # Override this method + pass + +""" +Global dictionary used to hold all gnerators. +Insert into this dictionary to register your generator +""" +GENERATOR_CLASSES = {} diff --git a/generators/example.py b/generators/example.py new file mode 100644 index 0000000..4e70678 --- /dev/null +++ b/generators/example.py @@ -0,0 +1,14 @@ +from generators import GENERATOR_CLASSES +import random + +class ExampleGenerator(BaseGen): + id = "random" + name = "random" + + @staticmethod + def genflag(challenge): + ran = random.randint(1,101) + challenge.description = "Your flag is " + ran "." + challenge.flag = str(ran) + +GENERATOR_CLASSES['example'] = ExampleGenerator