-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path__init__.py
350 lines (311 loc) · 13.7 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
from flask import (
abort,
request,
render_template,
redirect,
jsonify,
Blueprint,
url_for,
Response,
session,
send_file
)
import datafreeze
import dataset
import datetime
import six
import zipfile
from CTFd import utils, challenges
from CTFd.challenges import challenges_view
from CTFd.models import db, Challenges, Teams, Solves, WrongKeys
from CTFd.utils import is_admin, get_app_config
from CTFd.utils.decorators import (
authed_only,
admins_only,
during_ctf_time_only,
require_verified_emails,
viewable_without_authentication
)
from sqlalchemy.sql import and_, expression
class ChallengeFeedbackQuestions(db.Model):
id = db.Column(db.Integer, primary_key=True)
chalid = db.Column(db.Integer, db.ForeignKey('challenges.id'))
question = db.Column(db.String(100), nullable=False)
inputtype = db.Column(db.Integer)
extraarg1 = db.Column(db.String(100))
extraarg2 = db.Column(db.String(100))
def __init__(self, chalid, question, inputtype, extraarg1, extraarg2):
self.chalid = chalid
self.question = question
self.inputtype = inputtype
self.extraarg1 = extraarg1
self.extraarg2 = extraarg2
class ChallengeFeedbackAnswers(db.Model):
id = db.Column(db.Integer, primary_key=True)
questionid = db.Column(db.Integer, db.ForeignKey('challenge_feedback_questions.id'))
teamid = db.Column(db.Integer, db.ForeignKey('teams.id'))
answer = db.Column(db.String(100), nullable=False)
timestamp = db.Column(db.DateTime, default=datetime.datetime.utcnow)
def __init__(self, questionid, teamid, answer):
self.questionid = questionid
self.teamid = teamid
self.answer = answer
def load(app):
app.db.create_all()
challenge_feedback = Blueprint('challenge_feedback', __name__, template_folder='templates')
challenge_feedback_static = Blueprint('challenge_feedback_static', __name__, static_folder='static')
app.register_blueprint(challenge_feedback)
app.register_blueprint(challenge_feedback_static, url_prefix='/challenge-feedback')
utils.register_plugin_script("/challenge-feedback/static/challenge-feedback-chal-window.js")
@app.route('/admin/plugins/challenge-feedback', methods=['GET'])
@admins_only
def challenge_feedback_config_view():
challenges = Challenges.query.all()
return render_template('challenge-feedback-config.html', challenges=challenges)
@app.route('/admin/chal/<int:chalid>/feedbacks', methods=['GET'])
@admins_only
def admin_chal_feedbacks(chalid):
feedbacks = []
for feedback in ChallengeFeedbackQuestions.query.filter_by(chalid=chalid).all():
feedbacks.append({
'id': feedback.id,
'question': feedback.question,
'type': feedback.inputtype,
'extraarg1' : feedback.extraarg1,
'extraarg2' : feedback.extraarg2,
})
data = {}
data['feedbacks'] = feedbacks
return jsonify(data)
@app.route('/chal/<int:chalid>/feedbacks', methods=['GET'])
@require_verified_emails
@viewable_without_authentication(status_code=403)
def chal_feedbacks(chalid):
teamid = session.get('id')
# Get solved challenge ids
solves = []
if utils.user_can_view_challenges():
if utils.authed():
solves = Solves.query\
.join(Teams, Solves.teamid == Teams.id)\
.filter(Solves.teamid == session['id'])\
.all()
solve_ids = []
for solve in solves:
solve_ids.append(solve.chalid)
# Return nothing if challenge is not solved
if chalid not in solve_ids:
return jsonify([])
# Otherwise, return the feedback questions
feedbacks = []
for feedback in ChallengeFeedbackQuestions.query.filter_by(chalid=chalid).all():
answer_entry = ChallengeFeedbackAnswers.query.filter(and_(
ChallengeFeedbackAnswers.questionid==feedback.id,
ChallengeFeedbackAnswers.teamid==teamid
)).first()
answer = ""
if answer_entry is not None:
answer = answer_entry.answer
feedbacks.append({
'id': feedback.id,
'question': feedback.question,
'type': feedback.inputtype,
'extraarg1' : feedback.extraarg1,
'extraarg2' : feedback.extraarg2,
'answer': answer,
})
data = {}
data['feedbacks'] = feedbacks
return jsonify(data)
@app.route('/chal/<int:chalid>/feedbacks/answer', methods=['POST'])
@require_verified_emails
@viewable_without_authentication(status_code=403)
def chal_feedback_answer(chalid):
teamid = session.get('id')
success_msg = "Thank you for your feedback"
# Get solved challenge ids
solves = []
if utils.user_can_view_challenges():
if utils.authed():
solves = Solves.query\
.join(Teams, Solves.teamid == Teams.id)\
.filter(Solves.teamid == session['id'])\
.all()
solve_ids = []
for solve in solves:
solve_ids.append(solve.chalid)
# Get feedback ids for this challenge
feedback_ids = []
for feedback in ChallengeFeedbackQuestions.query.filter_by(chalid=chalid).all():
feedback_ids.append(feedback.id)
if (utils.authed() and utils.is_verified() and chalid in solve_ids):
for name, value in request.form.iteritems():
name_tokens = name.split("-")
if name_tokens[0] == "feedback":
feedbackid = int(name_tokens[1])
if feedbackid not in feedback_ids:
return jsonify({
'status': 1,
'message': "Error: Invalid feedback ID"
})
existing_feedback = ChallengeFeedbackAnswers.query.filter(and_(
ChallengeFeedbackAnswers.questionid==feedbackid,
ChallengeFeedbackAnswers.teamid==teamid
)).first()
if existing_feedback is not None:
db.session.delete(existing_feedback)
success_msg = "Your feedback has been updated"
feedback_answer = ChallengeFeedbackAnswers(feedbackid, teamid, value)
db.session.add(feedback_answer)
db.session.commit()
else:
return jsonify({
'status': 1,
'message': "Error: Authentication failed"
})
return jsonify({
'status': 0,
'message': success_msg
})
@app.route('/admin/feedbacks/<int:feedbackid>/answers', methods=['GET'])
@admins_only
def admin_feedback_answers(feedbackid):
teams = db.session.query(
Teams.id,
Teams.name
)
teamnames = {}
for team in teams:
teamnames[team.id] = team.name
answers = []
for answer in ChallengeFeedbackAnswers.query.filter_by(questionid=feedbackid).all():
answers.append({'id': answer.id,
'team': teamnames[answer.teamid],
'answer': answer.answer,
'timestamp': answer.timestamp})
data = {}
data['answers'] = answers
return jsonify(data)
@app.route('/admin/feedbacks', defaults={'feedbackid': None}, methods=['POST', 'GET'])
@app.route('/admin/feedbacks/<int:feedbackid>', methods=['GET', 'DELETE'])
@admins_only
def admin_feedbacks(feedbackid):
if feedbackid:
feedback = ChallengeFeedbackQuestions.query.filter_by(id=feedbackid).first_or_404()
if request.method == 'DELETE':
ChallengeFeedbackAnswers.query.filter_by(questionid=feedbackid).delete()
db.session.delete(feedback)
db.session.commit()
db.session.close()
return ('', 204)
json_data = {
'id': feedback.id,
'chalid': feedback.chalid,
'question': feedback.question,
'type': feedback.inputtype
}
db.session.close()
return jsonify(json_data)
else:
if request.method == 'GET':
feedbacks = ChallengeFeedbackQuestions.query.all()
json_data = []
for feedback in feedbacks:
json_data.append({
'id': feedback.id,
'chalid': feedback.chalid,
'question': feedback.question,
'type': feedback.inputtype,
'extraarg1' : feedback.extraarg1,
'extraarg2' : feedback.extraarg2,
})
return jsonify({'results': json_data})
elif request.method == 'POST':
question = request.form.get('question')
chalid = int(request.form.get('chal'))
inputtype = int(request.form.get('type') or -1)
extraarg1 = ""
extraarg2 = ""
if inputtype == 0:
extraarg1 = request.form.get('ratinglowlabel')
extraarg2 = request.form.get('ratinghighlabel')
feedback = ChallengeFeedbackQuestions(chalid=chalid, question=question, inputtype=inputtype, extraarg1=extraarg1, extraarg2=extraarg2)
db.session.add(feedback)
db.session.commit()
json_data = {
'id': feedback.id,
'chalid': feedback.chalid,
'question': feedback.question,
'type': feedback.inputtype,
'extraarg1' : feedback.extraarg1,
'extraarg2' : feedback.extraarg2,
}
db.session.close()
return jsonify(json_data)
@app.route('/admin/feedbacks/export', methods=['GET', 'POST'])
@admins_only
def admin_export_feedbacks():
backup = export_feedbacks()
ctf_name = utils.ctf_name()
day = datetime.datetime.now().strftime("%Y-%m-%d")
full_name = "{}.{}_feedbacks.zip".format(ctf_name, day)
return send_file(backup, as_attachment=True, attachment_filename=full_name)
@app.route('/admin/feedbacks/export_csv', methods=['GET', 'POST'])
@admins_only
def admin_export_feedbacks_csv():
backup = export_feedbacks_csv()
ctf_name = utils.ctf_name()
day = datetime.datetime.now().strftime("%Y-%m-%d")
full_name = "{}.{}_feedbacks.csv".format(ctf_name, day)
return send_file(backup, as_attachment=True, attachment_filename=full_name, cache_timeout=5)
def export_feedbacks():
db = dataset.connect(get_app_config('SQLALCHEMY_DATABASE_URI'))
segments = ['feedbacks']
groups = {
'feedbacks': [
'challenges',
'challenge_feedback_questions',
'challenge_feedback_answers',
]
}
# Backup database
backup = six.BytesIO()
backup_zip = zipfile.ZipFile(backup, 'w')
for segment in segments:
group = groups[segment]
for item in group:
result = db[item].all()
result_file = six.BytesIO()
datafreeze.freeze(result, format='ctfd', fileobj=result_file)
result_file.seek(0)
backup_zip.writestr('db/{}.json'.format(item), result_file.read())
backup_zip.close()
backup.seek(0)
return backup
def export_feedbacks_csv():
output_lines = []
output_lines.append("challenge_id,challenge,challenge_desc,challenge_category,challenge_maxattempts,challenge_value,team_id,team,team_email,is_solved,solve_timestamp,num_attempts,feedback_question_id,feedback_question,feedback_question_type,feedback_question_arg1,feedback_question_arg2,feedback_answer,feedback_answer_timestamp")
challenges = Challenges.query.all()
for challenge in challenges:
questions = ChallengeFeedbackQuestions.query.filter_by(chalid=challenge.id).all()
for question in questions:
teams = Teams.query.all()
for team in teams:
solve = Solves.query.filter(and_(Solves.chalid==challenge.id, Solves.teamid==team.id)).first()
wrongkeys = WrongKeys.query.filter(and_(WrongKeys.chalid==challenge.id, WrongKeys.teamid==team.id)).all()
answer = ChallengeFeedbackAnswers.query.filter(and_(ChallengeFeedbackAnswers.questionid==question.id, ChallengeFeedbackAnswers.teamid==team.id)).first()
fields = [challenge.id, challenge.name, challenge.description, challenge.category, challenge.max_attempts, challenge.value]
fields.extend([team.id, team.name, team.email])
if solve is None:
fields.extend([0, ''])
fields.append(len(wrongkeys))
else:
fields.extend([1, solve.date])
fields.append(len(wrongkeys) + 1)
fields.extend([question.id, question.question, ('Rating' if question.inputtype==0 else 'Text'), question.extraarg1, question.extraarg2])
if answer is None:
fields.extend(['', ''])
else:
fields.extend([answer.answer, answer.timestamp])
output_lines.append(','.join(map(str, fields)))
return six.StringIO('\n'.join(output_lines))