This repository has been archived by the owner on Nov 30, 2020. It is now read-only.
/
task_gen.py
272 lines (215 loc) · 9.34 KB
/
task_gen.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
import os
import sys
import hashlib
import traceback
import json
import time
from loguru import logger
import tasks
import util
import team
from configuration import configuration
from api import api, ADMIN, USER, GUEST, ApiArgumentError
class PresetNotFoundError(Exception):
pass
def get_token(team_name, task_id):
task = tasks.read_task(task_id)
team_seed = team.read_team(team_name).seed
task_seed = task.get_seed()
ctfhost_seed = util.get_ctfhost_seed()
return hashlib.sha224(
'team:{},task:{},glob:{};'.format(
team_seed,
task_seed,
ctfhost_seed
).encode()
).hexdigest()
def read_preset(preset_name):
presets_dir = configuration['gen_config_presets_path']
os.makedirs(presets_dir, exist_ok=True)
preset_filename = os.path.join(presets_dir, preset_name + '.py')
if not os.access(preset_filename, os.R_OK):
raise PresetNotFoundError()
with open(preset_filename) as f:
return f.read()
def make_task_generation_config_from_preset(task_id, preset_name):
if type(task_id) is not int:
raise tasks.TaskNotFoundError()
task_dir = os.path.join(configuration['tasks_path'], str(task_id))
if not os.access(task_dir, os.R_OK | os.X_OK):
raise tasks.TaskNotFoundError()
preset = read_preset(preset_name)
logger.info('Making task ({}) generation config from preset "{}"', task_id, preset_name)
task_gen_file = os.path.join(task_dir, 'generate.py')
with open(task_gen_file, 'w') as f:
f.write(preset)
def read_task_generation_config(task_id):
if type(task_id) is not int:
raise tasks.TaskNotFoundError()
task_dir = os.path.join(configuration['tasks_path'], str(task_id))
if not os.access(task_dir, os.R_OK | os.X_OK):
raise tasks.TaskNotFoundError()
task_gen_file = os.path.join(task_dir, 'generate.py')
if not os.access(task_gen_file, os.R_OK):
make_default_task_generation_config(task_id)
with open(task_gen_file) as f:
return f.read()
def make_default_task_generation_config(task_id):
task = tasks.read_task(task_id)
if task.group == 0:
make_task_generation_config_from_preset(task_id, 'noop')
else:
# Guarantees that config will be available at the moment of exit
write_task_generation_config(task_id, read_group_generation_config(task.group))
logger.info('Inheriting task ({}) generation config from group ({})', task_id, task.group)
def make_group_generation_config_from_preset(group_id, preset_name):
if type(group_id) is not int:
raise groups.GroupNotFoundError()
group_dir = os.path.join(configuration['groups_path'], str(group_id))
if not os.access(group_dir, os.R_OK | os.X_OK):
raise groups.GroupNotFoundError()
preset = read_preset(preset_name)
logger.info('Making group ({}) generation config from preset "{}"', group_id, preset_name)
group_gen_file = os.path.join(group_dir, 'generate.py')
with open(group_gen_file, 'w') as f:
f.write(preset)
def read_group_generation_config(group_id):
if type(group_id) is not int:
raise groups.GroupNotFoundError()
group_dir = os.path.join(configuration['groups_path'], str(group_id))
if not os.access(group_dir, os.R_OK | os.X_OK):
raise groups.GroupNotFoundError()
group_gen_file = os.path.join(group_dir, 'generate.py')
if not os.access(group_gen_file, os.R_OK):
make_default_group_generation_config(group_id)
with open(group_gen_file) as f:
return f.read()
def make_default_group_generation_config(group_id):
group = tasks.read_group(group_id)
if group['parent'] == 0:
make_group_generation_config_from_preset(group_id, 'noop')
else:
# Guarantees that config will be available at the moment of exit
write_group_generation_config(group_id, read_group_generation_config(group['parent']))
logger.info('Inheriting group ({}) generation config from group ({})', group_id, group['parent'])
def write_task_generation_config(task_id, config):
if type(task_id) is not int:
raise tasks.TaskNotFoundError()
task_dir = os.path.join(configuration['tasks_path'], str(task_id))
if not os.access(task_dir, os.R_OK | os.X_OK):
raise tasks.TaskNotFoundError()
task_gen_file = os.path.join(task_dir, 'generate.py')
with open(task_gen_file, 'w') as f:
f.write(config)
def write_group_generation_config(group_id, config):
if type(group_id) is not int:
raise groups.TaskNotFoundError()
group_dir = os.path.join(configuration['groups_path'], str(group_id))
if not os.access(group_dir, os.R_OK | os.X_OK):
raise groups.TaskNotFoundError()
group_gen_file = os.path.join(group_dir, 'generate.py')
with open(group_gen_file, 'w') as f:
f.write(config)
def get_generated_task(task_id, token, team):
if not tasks.task_exists(task_id):
raise tasks.TaskNotFoundError(task_id)
maybe_generate(task_id, token, team)
return read_generated_task(task_id, token)
def read_generated_task(task_id, token):
if type(task_id) is not int:
raise tasks.TaskNotFoundError(task_id)
task_dir = os.path.join(configuration['tasks_path'], str(task_id), 'generated', str(token))
task_file = os.path.join(task_dir, 'task.json')
try:
with open(task_file) as f:
task_str = f.read()
task = json.loads(task_str)
return tasks.Task(task_id, task)
except FileNotFoundError:
raise tasks.TaskNotFoundError(task_id)
def write_generated_task(task, token):
task_id = task.task_id
if type(task_id) is not int:
raise TaskNotFoundError(task_id)
task_dir = os.path.join(configuration['tasks_path'], str(task_id), 'generated', str(token))
os.makedirs(task_dir, exist_ok=True)
task_file = os.path.join(task_dir, 'task.json')
obj = task.to_dict(False)
with open(task_file, 'w') as f:
f.write(json.dumps(obj))
def maybe_generate(task_id, token, team):
read_task_generation_config(task_id) # Make sure that the config is available
if should_generate(task_id, token):
generate(task_id, token, team)
def should_generate(task_id, token):
task_dir = os.path.join(configuration['tasks_path'], str(task_id), 'generated', str(token))
if not os.access(task_dir, os.F_OK):
return True
try:
gen_ts = get_generation_timestamp(task_id, token)
except BaseException:
return True
conf_ts = get_config_modification_timestamp(task_id)
task_ts = get_task_modification_timestamp(task_id)
return gen_ts < conf_ts or gen_ts < task_ts
def get_generation_timestamp(task_id, token):
task_dir = os.path.join(configuration['tasks_path'], str(task_id), 'generated', str(token))
with open(os.path.join(task_dir, 'gen_ts')) as f:
return float(f.read())
def get_config_modification_timestamp(task_id):
task_dir = os.path.join(configuration['tasks_path'], str(task_id))
task_gen_file = os.path.join(task_dir, 'generate.py')
return os.stat(task_gen_file).st_mtime
def get_task_modification_timestamp(task_id):
task_dir = os.path.join(configuration['tasks_path'], str(task_id))
task_json_file = os.path.join(task_dir, 'task.json')
return os.stat(task_json_file).st_mtime
def generate(task_id, token, team):
read_task_generation_config(task_id) # Make sure that the config is available
logger.info('Generating task {} with token {}', task_id, token)
task_dir = os.path.join(configuration['tasks_path'], str(task_id))
os.makedirs(task_dir, exist_ok=True)
mod = util.import_file(os.path.join(task_dir, 'generate.py'))
raw_task = tasks.read_task(task_id)
gen_task = mod.generate(task=raw_task, token=token, team=team)
write_generated_task(gen_task, token)
with open(os.path.join(task_dir, 'generated', token, 'gen_ts'), 'w') as f:
f.write(str(time.time()))
def get_generated_task_list(team_name):
for task in tasks.get_task_list():
token = get_token(team_name=team_name, task_id=task.task_id)
yield get_generated_task(task_id=task.task_id, token=token, team=team.read_team(team_name))
def api_update_gen_config(api, sess, args):
http = args['http_handler']
request = json.loads(http.request.body)
what = request['what']
new_config = request['new_config']
if what == 'task':
task_id = request['task_id']
try:
task_id = int(task_id)
except (ValueError, TypeError) as e:
raise Exception(
lc.get('api_invalid_data_type').format(
expected=lc.get('int'),
param='task_id',
)
)
write_task_generation_config(task_id, new_config)
http.write(json.dumps({'success': True}))
elif what == 'group':
group_id = request['group_id']
try:
group_id = int(group_id)
except (ValueError, TypeError) as e:
raise Exception(
lc.get('api_invalid_data_type').format(
expected=lc.get('int'),
param='group_id',
)
)
write_group_generation_config(group_id, new_config)
http.write(json.dumps({'success': True}))
else:
raise ApiArgumentError('what')
api.add('update_gen_config', api_update_gen_config, access_level=ADMIN)