This repository has been archived by the owner on Apr 20, 2024. It is now read-only.
forked from CybersecNatLab/ICC2022-AD-CTF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.py
327 lines (260 loc) · 13.5 KB
/
checker.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
#!/usr/bin/env python3
import base64
import checklib
import errno
import gmpy2
import hashlib
import json
import os
import random
import requests
import string
import time
data = checklib.get_data()
action = data['action']
rd = data['round']
team_id = data['teamId']
service_addr = '10.60.' + team_id + '.1'
service_port = "5000"
service_name = 'Trademark'
SLEEP_MIN_TIME_MS = 500
SLEEP_MAX_TIME_MS = 1500
# Create directory to store round data.
data_dir = 'data'
try:
os.makedirs(data_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
# Read stored data for team-round
def read_round_data():
try:
fl = hashlib.sha256(data['flag'].encode()).hexdigest()
with open(f'{data_dir}/{team_id}-{fl}.json', 'r') as f:
raw = f.read()
return json.loads(raw)
except (FileNotFoundError, json.decoder.JSONDecodeError):
return {}
# Store data for team-round
def store_round_data(d):
raw = json.dumps(d)
fl = hashlib.sha256(data['flag'].encode()).hexdigest()
with open(f'{data_dir}/{team_id}-{fl}.json', 'w') as f:
f.write(raw)
def random_string(min, max):
letters = string.ascii_letters + string.digits
return ''.join(random.choice(letters) for i in range(random.randint(min, max)))
# Check user can register and read profile
def check_sla_user():
username = random_string(16, 24)
password = random_string(12, 24)
# Register user
r = requests.post(f'http://{service_addr}:{service_port}/api/register',
data={'username': username, 'password': password}, timeout=5)
if r.status_code != 200 and r.status_code != 201:
checklib.quit(checklib.Status.DOWN, 'Request failure',
f'check_sla_user http://{service_addr}/api/register (username={username}, password={password}): {r.status_code}\n{r.text}')
sessionData = r.json()
if 'user_id' not in sessionData or not isinstance(sessionData['user_id'], int) or 'session' not in sessionData or not isinstance(sessionData['session'], str):
checklib.quit(checklib.Status.DOWN, 'Response error',
f'check_sla_user http://{service_addr}/api/register (username={username}, password={password}): {r.status_code}\n{r.text}')
token = 'Bearer ' + \
base64.b64encode(
(str(sessionData['user_id'])+':'+sessionData['session']).encode()).decode()
time.sleep(random.randint(SLEEP_MIN_TIME_MS, SLEEP_MAX_TIME_MS) / 1000)
# Check profile
r = requests.get(f'http://{service_addr}:{service_port}/api/users',
headers={'Authorization': token}, timeout=5)
if r.status_code != 200:
checklib.quit(checklib.Status.DOWN, 'Request failure',
f'check_sla_user http://{service_addr}/api/users: {r.status_code}\n{r.text}')
userData = r.json()
if 'id' not in userData or not isinstance(userData['id'], int) or 'username' not in userData or not isinstance(userData['username'], str):
checklib.quit(checklib.Status.DOWN, 'Response error',
f'check_sla_user http://{service_addr}/api/users: {r.status_code}\n{r.text}')
# Check SLA
def check_sla():
slas = [check_sla_user]
random.shuffle(slas)
try:
for sla in slas:
sla()
except (requests.ConnectTimeout, requests.ReadTimeout, requests.Timeout) as e:
checklib.quit(checklib.Status.DOWN, 'Connection timeout', str(e))
except requests.ConnectionError as e:
checklib.quit(checklib.Status.DOWN, 'Connection error', str(e))
except json.decoder.JSONDecodeError as e:
checklib.quit(checklib.Status.DOWN, 'Response error', str(e))
checklib.quit(checklib.Status.OK, 'OK')
# Put the flag using the flag as the seed for random stuff
def put_flag():
flag = data['flag']
username = random_string(16, 24)
password = random_string(12, 24)
product = random_string(16, 24)
try:
# Register user
r = requests.post(f'http://{service_addr}:{service_port}/api/register', data={
'username': username, 'password': password}, timeout=5)
if r.status_code != 200 and r.status_code != 201:
checklib.quit(checklib.Status.DOWN, 'Request failure',
f'put_flag http://{service_addr}/api/register (username={username}, password={password}): {r.status_code}\n{r.text}')
sessionData = r.json()
if 'user_id' not in sessionData or not isinstance(sessionData['user_id'], int) or 'session' not in sessionData or not isinstance(sessionData['session'], str):
checklib.quit(checklib.Status.DOWN, 'Response error',
f'put_flag http://{service_addr}/api/register (username={username}, password={password}): {r.status_code}\n{r.text}')
token = 'Bearer ' + \
base64.b64encode(
(str(sessionData['user_id'])+':'+sessionData['session']).encode()).decode()
time.sleep(random.randint(SLEEP_MIN_TIME_MS, SLEEP_MAX_TIME_MS) / 1000)
# Create product
r = requests.post(f'http://{service_addr}:{service_port}/api/products', data={
'name': product, 'description': '', 'content': flag}, headers={'Authorization': token}, timeout=15)
if r.status_code != 200 and r.status_code != 201:
checklib.quit(checklib.Status.DOWN, 'Request failure',
f'put_flag http://{service_addr}/api/products (name={product}): {r.status_code}\n{r.text}')
productData = r.json()
if 'id' not in productData or not isinstance(productData['id'], int) or 'keys' not in productData or not isinstance(productData['keys'], list):
checklib.quit(checklib.Status.DOWN, 'Response error',
f'put_flag http://{service_addr}/api/products (name={product}): {r.status_code}\n{r.text}')
product_id = productData['id']
keys = []
for k in productData['keys']:
if not isinstance(k, str):
checklib.quit(checklib.Status.DOWN, 'Response error',
f'put_flag http://{service_addr}/api/products key not string: {r.status_code}\n{r.text}')
keys.append(k)
if len(keys) < 1:
checklib.quit(checklib.Status.DOWN, 'Response error',
f'put_flag http://{service_addr}/api/products key not string: {r.status_code}\n{r.text}')
except (requests.ConnectTimeout, requests.ReadTimeout, requests.Timeout) as e:
checklib.quit(checklib.Status.DOWN, 'Connection timeout', str(e))
except requests.ConnectionError as e:
checklib.quit(checklib.Status.DOWN, 'Connection error', str(e))
except json.decoder.JSONDecodeError as e:
checklib.quit(checklib.Status.DOWN, 'Response error', str(e))
store_round_data({
'username': username,
'password': password,
'product': product,
'product_id': product_id,
'keys': keys,
})
checklib.post_flag_id(service_name, service_addr, str(product_id))
checklib.quit(checklib.Status.OK, 'OK')
# Check if the flag still exists, use the flag as the seed for random stuff as for put flag
def get_flag():
flag = data['flag']
storedData = read_round_data()
if not storedData:
checklib.quit(checklib.Status.DOWN,
'Precondition Failed', 'storedData empty')
if 'product_id' not in storedData or 'keys' not in storedData:
checklib.quit(checklib.Status.ERROR, 'Precondition Failed',
'storedData invalid: ' + json.dumps(storedData))
username = random_string(16, 24)
password = random_string(12, 24)
product_id = storedData['product_id']
keys = storedData['keys']
try:
# Register user
r = requests.post(f'http://{service_addr}:{service_port}/api/register', data={
'username': username, 'password': password}, timeout=5)
if r.status_code != 200 and r.status_code != 201:
checklib.quit(checklib.Status.DOWN, 'Request failure',
f'put_flag http://{service_addr}/api/register (username={username}, password={password}): {r.status_code}\n{r.text}')
sessionData = r.json()
if 'user_id' not in sessionData or not isinstance(sessionData['user_id'], int) or 'session' not in sessionData or not isinstance(sessionData['session'], str):
checklib.quit(checklib.Status.DOWN, 'Response error',
f'get_flag http://{service_addr}/api/register (username={username}, password={password}): {r.status_code}\n{r.text}')
token = 'Bearer ' + \
base64.b64encode(
(str(sessionData['user_id'])+':'+sessionData['session']).encode()).decode()
time.sleep(random.randint(SLEEP_MIN_TIME_MS, SLEEP_MAX_TIME_MS) / 1000)
# Show product
r = requests.get(f'http://{service_addr}:{service_port}/api/products/{product_id}',
headers={'Authorization': token}, timeout=5)
if r.status_code != 200:
checklib.quit(checklib.Status.DOWN, 'Request failure',
f'get_flag http://{service_addr}/api/products/{product_id}: {r.status_code}\n{r.text}')
productData = r.json()
if 'id' not in productData or not isinstance(productData['id'], int) or 'license' not in productData or not isinstance(productData['license'], dict) or 'mod' not in productData['license'] or not isinstance(productData['license']['mod'], str) or 'poly' not in productData['license'] or not isinstance(productData['license']['poly'], list):
checklib.quit(checklib.Status.DOWN, 'Response error',
f'get_flag http://{service_addr}/api/products/{product_id}: {r.status_code}\n{r.text}')
mod = gmpy2.mpz(productData['license']['mod'])
poly = []
for p in productData['license']['poly']:
poly.append(gmpy2.mpz(p))
# Validate license
lic = random.choice(keys)
if not verify_licence(lic, poly, mod):
checklib.quit(checklib.Status.DOWN, 'Response error',
f'License does not validate: lic={lic}, mod={mod}, poly={poly}')
# Download product
r = requests.post(f'http://{service_addr}:{service_port}/api/products/{product_id}/download', data={
'license': lic}, headers={'Authorization': token}, timeout=10)
if r.status_code != 200:
checklib.quit(checklib.Status.DOWN, 'Request failure',
f'get_flag http://{service_addr}/api/products/{product_id}/download (license={lic}): {r.status_code}\n{r.text}')
if flag not in r.text:
checklib.quit(checklib.Status.DOWN, 'Response error',
f'put_flag http://{service_addr}/api/products/{product_id}/download (license={lic}): {r.status_code}\n{r.text}')
except (requests.ConnectTimeout, requests.ReadTimeout, requests.Timeout) as e:
checklib.quit(checklib.Status.DOWN, 'Connection timeout', str(e))
except requests.ConnectionError as e:
checklib.quit(checklib.Status.DOWN, 'Connection error', str(e))
except (json.decoder.JSONDecodeError, ValueError) as e:
checklib.quit(checklib.Status.DOWN, 'Response error', str(e))
checklib.quit(checklib.Status.OK, 'OK')
# VULNS:
# - In create_licences: it is wrong to start from [0,1]; this allows licences like AAAAAA-AAAAAA-AAAAAAA-AAAAAA to always pass
# Fix: start from [1]
# - In create_licences: it is wrong to get mod as a prime number, since you can easily solve for the licences knowing the polynomial
# Fix: mod = next_prime(random.getrandbits(1024))*next_prime(random.getrandbits(1024))
# - In create_licences: if n is small enough, since licences << mod you can find small roots with LLL and find licences (for reference: like n=5 allows to retrieve licences)
# Fix: bigger licences/smaller modulus in order to make LLL unfeasible
# - In verify_licence: checks on the number of characters of the licences are missing, so you can craft infinite valid licences from a known one. Probably useless.
def multiply_poly(a, b, mod):
res = [0] * (len(a) + len(b) - 1)
for i in range(len(a)):
for j in range(len(b)):
res[i+j] += a[i]*b[j]
res[i+j] %= mod
return res
def eval_poly(coefs, x, mod):
res = 0
for i, c in enumerate(coefs):
res += c*pow(x, i, mod)
res %= mod
return res
def num_to_list(x):
if x < 26:
return [x]
return [x % 26] + num_to_list(x//26)
def licence_to_string(x):
alph = string.ascii_uppercase
lic = ''.join([alph[i] for i in num_to_list(x)])
while len(lic) % 7 != 0:
lic += "A"
return '-'.join([lic[i:i+7] for i in range(0, len(lic), 7)])
def create_licences(n, app_secret, flag_id):
mod = gmpy2.next_prime(random.getrandbits(1024))
licences = [random.getrandbits(128) for _ in range(n)]
poly = multiply_poly(
[0, 1], [int(hashlib.sha512(app_secret + flag_id).hexdigest(), 16), 1], mod)
for lic in licences:
poly = multiply_poly(poly, [-lic, 1], mod)
return (poly, mod, [licence_to_string(x) for x in licences])
def verify_licence(x, poly, mod):
alph = string.ascii_uppercase
real_lic = x.replace("-", "")
real_lic = [alph.index(c) for c in real_lic]
real_lic = sum(c*26**i for i, c in enumerate(real_lic))
return eval_poly(poly, real_lic, mod) == 0
if __name__ == "__main__":
if action == checklib.Action.CHECK_SLA.name:
check_sla()
elif action == checklib.Action.PUT_FLAG.name:
put_flag()
elif action == checklib.Action.GET_FLAG.name:
get_flag()