-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work on setting up generator challenge class
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |