forked from kamushadenes/ctf-engine
/
challenges.php
332 lines (280 loc) · 12 KB
/
challenges.php
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
<?php
require('../include/ctf.inc.php');
require(CONST_PATH_THIRDPARTY . 'nbbc/nbbc.php');
enforce_authentication();
$time = time();
$bbc = new BBCode();
$bbc->SetEnableSmileys(false);
head('Challenges');
if (isset($_GET['status'])) {
if ($_GET['status']=='correct') {
message_inline_green('<h1>Correct flag, you are awesome!</h1>', false);
} else if ($_GET['status']=='incorrect') {
message_inline_red('<h1>Incorrect flag, try again.</h1>', false);
} else if ($_GET['status']=='manual') {
message_inline_blue('<h1>Your submission is awaiting manual marking.</h1>', false);
}
}
$categories = db_select_all(
'categories',
array(
'id',
'title',
'description',
'available_from',
'available_until'
),
array(
'exposed'=>1
),
'title ASC'
);
// determine which category to display
if (isset($_GET['category'])) {
validate_id($_GET['category']);
$current_category = array_search_matching_key(
$_GET['category'],
$categories,
'id'
);
if (!$current_category) {
message_error(lang_get('no_category_for_id'), false);
}
} else {
// if no category is selected, display
// the first available category
foreach ($categories as $cat) {
if ($time > $cat['available_from'] && $time < $cat['available_until']) {
$current_category = $cat;
break;
}
}
// if no category has been made available
// we'll just set it to the first one
// alphabetically and display an error
// message
if (!isset($current_category)) {
$current_category = $categories[0];
}
}
if (empty($current_category)) {
message_generic('Challenges', 'Your CTF is looking a bit empty! Start by adding a category using the management console.');
}
// write out our categories menu
echo '<div id="categories-menu">
<ul id="categories-menu">';
foreach ($categories as $cat) {
if ($time < $cat['available_from'] || $time > $cat['available_until']) {
echo '<li class="disabled">
<a data-container="body" data-toggle="tooltip" data-placement="top" class="has-tooltip" title="Available in '.time_remaining($cat['available_from']).'.">',htmlspecialchars($cat['title']),'</a>
</li>';
} else {
echo '<li ',($current_category['id'] == $cat['id'] ? ' class="active"' : ''),'><a href="',CONFIG_SITE_URL,'challenges?category=',htmlspecialchars($cat['id']),'">',htmlspecialchars($cat['title']),'</a></li>';
}
}
echo '</ul>
</div>';
// check that the category is actually available for display
if ($time < $current_category['available_from'] || $time > $current_category['available_until']) {
message_generic('Category unavailable','This category is not available. It is open from ' . date_time($current_category['available_from']) . ' ('. time_remaining($current_category['available_from']) .' from now) until ' . date_time($current_category['available_until']) . ' ('. time_remaining($current_category['available_until']) .' from now)', false);
}
// write out the category description, if one exists
if ($current_category['description']) {
echo '<div id="category-description">', $bbc->parse($current_category['description']), '</div>';
}
// get all the challenges for the selected category
$challenges = db_query_fetch_all('
SELECT
c.id,
c.title,
c.description,
c.available_from,
c.available_until,
c.points,
c.num_attempts_allowed,
c.min_seconds_between_submissions,
c.automark,
c.relies_on,
IF(c.automark = 1, 0, (SELECT ss.id FROM submissions AS ss WHERE ss.challenge = c.id AND ss.user_id = :user_id_1 AND ss.marked = 0)) AS unmarked, -- a submission is waiting to be marked
(SELECT ss.added FROM submissions AS ss WHERE ss.challenge = c.id AND ss.user_id = :user_id_2 AND ss.correct = 1) AS correct_submission_added, -- a correct submission has been made
(SELECT COUNT(*) FROM submissions AS ss WHERE ss.challenge = c.id AND ss.user_id = :user_id_3) AS num_submissions, -- number of submissions made
(SELECT max(ss.added) FROM submissions AS ss WHERE ss.challenge = c.id AND ss.user_id = :user_id_4) AS latest_submission_added
FROM challenges AS c
WHERE
c.category = :category AND
c.exposed = 1
ORDER BY c.points ASC, c.id ASC',
array(
'user_id_1'=>$_SESSION['id'],
'user_id_2'=>$_SESSION['id'],
'user_id_3'=>$_SESSION['id'],
'user_id_4'=>$_SESSION['id'],
'category'=>$current_category['id']
)
);
echo '<div id="challenges-container" class="panel-group">';
foreach($challenges as $challenge) {
// if the challenge isn't available yet, display a message and continue to next challenge
if ($time < $challenge['available_from']) {
echo '
<div class="panel panel-default challenge-container">
<div class="panel-heading">
<h4 class="challenge-head">Hidden challenge worth ', number_format($challenge['points']), 'pts</h4>
</div>
<div class="panel-body">
<div class="challenge-description">
Available in ',time_remaining($challenge['available_from']),' (from ', date_time($challenge['available_from']), ' until ', date_time($challenge['available_until']), ')
</div>
</div>
</div>';
continue;
}
$remaining_submissions = $challenge['num_attempts_allowed'] ? ($challenge['num_attempts_allowed']-$challenge['num_submissions']) : 1;
$panel_class = "panel-default";
if (!$remaining_submissions) {
$panel_class = "panel-danger";
} else if ($challenge['correct_submission_added']) {
$panel_class = "panel-success";
}
echo '
<div class="panel ', $panel_class, ' challenge-container">
<div class="panel-heading">
<h4 class="challenge-head">
<a href="challenge?id=',htmlspecialchars($challenge['id']),'">',htmlspecialchars($challenge['title']), '</a> (', number_format($challenge['points']), 'pts)';
if ($challenge['correct_submission_added']) {
$solve_position = db_query_fetch_one('
SELECT
COUNT(*)+1 AS pos
FROM
submissions AS s
WHERE
s.correct = 1 AND
s.added < :correct_submission_added AND
s.challenge = :challenge_id',
array(
'correct_submission_added'=>$challenge['correct_submission_added'],
'challenge_id'=>$challenge['id']
)
);
echo ' <span class="glyphicon glyphicon-ok"></span>';
echo get_position_medal($solve_position['pos']);
}
echo '</h4>';
if (should_print_metadata($challenge)) {
print_time_left_tooltip($challenge);
}
echo '</div>
<div class="panel-body">';
unset($relies_on);
// if this challenge relies on another being solved, get the related information
if ($challenge['relies_on']) {
$relies_on = db_query_fetch_one('
SELECT
c.id,
c.title,
cat.id AS category_id,
cat.title AS category_title,
s.correct AS has_solved_requirement
FROM
challenges AS c
LEFT JOIN categories AS cat ON cat.id = c.category
LEFT JOIN submissions AS s ON s.challenge = c.id AND s.correct = 1
WHERE
c.id = :relies_on',
array('relies_on'=>$challenge['relies_on'])
);
}
// if this challenge relies on another, and the user hasn't solved that requirement
if (isset($relies_on) && !$relies_on['has_solved_requirement']) {
echo '
<div class="challenge-description relies-on">',
lang_get(
'challenge_relies_on',
array(
'relies_on_link' => '<a href="challenge?id='.htmlspecialchars($relies_on['id']).'">'.htmlspecialchars($relies_on['title']).'</a>',
'relies_on_category_link' => '<a href="challenges?category='.htmlspecialchars($relies_on['category_id']).'">'.htmlspecialchars($relies_on['category_title']).'</a>'
)
)
,'</div>
';
}
// this challenge either does not have a requirement, or the user has solved it
else {
// write out challenge description
if ($challenge['description']) {
echo '
<div class="challenge-description">
',$bbc->parse($challenge['description']),'
</div> <!-- / challenge-description -->';
}
// write out hints
if (cache_start(CONST_CACHE_NAME_CHALLENGE_HINTS . $challenge['id'], CONFIG_CACHE_TIME_HINTS)) {
$hints = db_select_all(
'hints',
array('body'),
array(
'visible' => 1,
'challenge' => $challenge['id']
)
);
foreach ($hints as $hint) {
message_inline_yellow('<strong>Hint!</strong> ' . $bbc->parse($hint['body']), false);
}
cache_end(CONST_CACHE_NAME_CHALLENGE_HINTS . $challenge['id']);
}
// write out files
if (cache_start(CONST_CACHE_NAME_FILES . $challenge['id'], CONFIG_CACHE_TIME_FILES)) {
$files = db_select_all(
'files',
array(
'id',
'title',
'size',
'md5'
),
array('challenge' => $challenge['id'])
);
if (count($files)) {
print_attachments($files);
}
cache_end(CONST_CACHE_NAME_FILES . $challenge['id']);
}
// only show the hints and flag submission form if we're not already correct and if the challenge hasn't expired
if (!$challenge['correct_submission_added'] && $time < $challenge['available_until']) {
if ($remaining_submissions) {
if ($challenge['num_submissions'] && !$challenge['automark'] && $challenge['marked']) {
message_inline_blue('Your submission is awaiting manual marking.');
}
echo '
<div class="challenge-submit">
<form method="post" class="form-flag" action="actions/challenges">
<textarea name="flag" type="text" class="flag-input form-control" placeholder="Please enter flag for challenge: ',htmlspecialchars($challenge['title']),'"></textarea>
<input type="hidden" name="challenge" value="',htmlspecialchars($challenge['id']),'" />
<input type="hidden" name="action" value="submit_flag" />';
form_xsrf_token();
if (CONFIG_RECAPTCHA_ENABLE_PRIVATE) {
display_captcha();
}
echo '<button class="btn btn-sm btn-primary flag-submit-button" type="submit" data-countdown="',max($challenge['latest_submission_added']+$challenge['min_seconds_between_submissions'], 0),'" data-countdown-done="Submit flag">Submit flag</button>';
if (should_print_metadata($challenge)) {
echo '<div class="challenge-submit-metadata">';
print_submit_metadata($challenge);
echo '</div>';
}
echo '</form>';
echo '
</div>
';
}
// no remaining submission attempts
else {
message_inline_red("You have no remaining submission attempts. If you've made an erroneous submission, please contact the organizers.");
}
}
}
echo '
</div> <!-- / panel-body -->
</div> <!-- / challenge-container -->';
}
echo '</div> <!-- / challenges-container-->';
foot();