forked from doringeman/ctfd-quiz-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
132 lines (118 loc) · 4.19 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from flask import Blueprint
from CTFd.models import Challenges, Fails, Solves, db
from CTFd.plugins import register_plugin_assets_directory
from CTFd.plugins.challenges import CHALLENGE_CLASSES, BaseChallenge
from CTFd.utils.user import get_ip
__version__ = "0.1-dev"
class QuizChallengeModel(Challenges):
__mapper_args__ = {"polymorphic_identity": "quiz"}
id = db.Column(
db.Integer,
db.ForeignKey("challenges.id", ondelete="CASCADE"),
primary_key=True
)
variant_a = db.Column(db.String(100), default="")
variant_b = db.Column(db.String(100), default="")
variant_c = db.Column(db.String(100), default="")
variant_d = db.Column(db.String(100), default="")
correct_variant = db.Column(db.String(1), default="A")
def __init__(self, *args, **kwargs):
super(QuizChallengeModel, self).__init__(**kwargs)
class QuizChallenge(BaseChallenge):
id = "quiz"
name = "quiz"
templates = {
"create": "/plugins/quiz_challenges/assets/create.html",
"update": "/plugins/quiz_challenges/assets/update.html",
"view": "/plugins/quiz_challenges/assets/view.html",
}
scripts = {
"create": "/plugins/quiz_challenges/assets/create.js",
"update": "/plugins/quiz_challenges/assets/update.js",
"view": "/plugins/quiz_challenges/assets/view.js",
}
route = "/plugins/quiz_challenges/assets/"
blueprint = Blueprint(
"quiz_challenges",
__name__,
template_folder="templates",
static_folder="assets",
)
challenge_model = QuizChallengeModel
@classmethod
def read(cls, challenge):
challenge = QuizChallengeModel.query.filter_by(id=challenge.id).first()
data = {
"id": challenge.id,
"name": challenge.name,
"value": challenge.value,
"description": challenge.description,
"category": challenge.category,
"state": challenge.state,
"max_attempts": challenge.max_attempts,
"type": challenge.type,
"type_data": {
"id": cls.id,
"name": cls.name,
"templates": cls.templates,
"scripts": cls.scripts,
},
"variant_a": challenge.variant_a,
"variant_b": challenge.variant_b,
"variant_c": challenge.variant_c,
"variant_d": challenge.variant_d,
"correct_variant": challenge.correct_variant,
}
return data
@classmethod
def attempt(cls, challenge, request):
data = request.form or request.get_json()
try:
variant = next(filter(lambda x: data[x] is True, filter(lambda x: x.startswith("variant"), data.keys())))
correct = (challenge.correct_variant == variant[-1].upper())
except:
correct = False
return correct, "Correct" if correct else "Incorrect"
@classmethod
def solve(cls, user, team, challenge, request):
data = request.form or request.get_json()
provided = ",".join(
map(
lambda x: x.split("_")[-1].upper(),
filter(lambda x: data[x] is True, data)
)
)
solve = Solves(
user_id=user.id,
team_id=team.id if team else None,
challenge_id=challenge.id,
ip=get_ip(req=request),
provided=provided,
)
db.session.add(solve)
db.session.commit()
@classmethod
def fail(cls, user, team, challenge, request):
data = request.form or request.get_json()
provided = ",".join(
map(
lambda x: x.split("_")[-1].upper(),
filter(lambda x: data[x] is True, data)
)
)
fail = Fails(
user_id=user.id,
team_id=team.id if team else None,
challenge_id=challenge.id,
ip=get_ip(request),
provided=provided,
)
db.session.add(fail)
db.session.commit()
def load(app):
app.db.create_all()
CHALLENGE_CLASSES["quiz"] = QuizChallenge
register_plugin_assets_directory(
app,
base_path="/plugins/quiz_challenges/assets/"
)