Skip to content

Commit

Permalink
Work on setting up generator challenge class
Browse files Browse the repository at this point in the history
  • Loading branch information
huner2 committed Jun 5, 2018
1 parent b13dd71 commit c64d282
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
78 changes: 78 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -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/')
13 changes: 13 additions & 0 deletions generators/__init__.py
Original file line number Diff line number Diff line change
@@ -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 = {}
14 changes: 14 additions & 0 deletions generators/example.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c64d282

Please sign in to comment.