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
/
Copy pathchecker.py
389 lines (313 loc) · 10.8 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/usr/bin/env python3
# Do not make modification to checklib.py (except for debug), it can be replaced at any time
import checklib
import random
import string
from hashlib import sha256
import zlib
import base64
import json
from Crypto.Cipher import AES
import os
os.environ["PWNLIB_NOTERM"] = "1"
from pwn import *
from service2_client import Client as AuthClient
context.timeout = 5
context.log_level = "error"
data = checklib.get_data()
action = data['action']
rd = data['round']
team_id = data['teamId']
team_addr = '10.60.' + team_id + '.1'
port = 1236
auth_port = 1234
service_name = "EncryptedNotes"
def random_string(min, max):
letters = string.ascii_letters + string.digits
return ''.join(random.choice(letters) for i in range(random.randint(min, max)))
# Auth stuff
def get_random_creds():
username = random_string(8, 24)
password = random_string(8, 24)
return username, password
def register_random_user():
try:
username, password = get_random_creds()
c = AuthClient(team_addr, auth_port, username, password, "2")
c.register()
return username, password
except Exception as e:
checklib.quit(checklib.Status.DOWN,
'Can\'t register user on auth service', str(e))
def get_token(username, password):
try:
c = AuthClient(team_addr, auth_port, username, password, "2")
c.login()
token = c.get_token()
return token
except Exception as e:
checklib.quit(checklib.Status.DOWN,
'Can\'t get a token from auth service', str(e))
# OT stuff
class Receiver:
def __init__(self, b):
self.b = b
def round2(self, pk, x):
N, e = pk
n = len(x)
assert n == len(self.b)
v = []
k = []
for i in range(n):
kk = random.randrange(1 << 2048)
k.append(kk)
cur = x[i][self.b[i]] + pow(kk, e, N)
v.append(cur % N)
self.k = k
self.N = N
return v
def decode(self, c):
n = len(c)
assert n == len(self.b)
m = []
for i in range(n):
mm = (c[i][self.b[i]]-self.k[i]) % self.N
m.append(mm)
return m
# GC evaluation
VAL_LENGTH = 5
PAD_LENGTH = 3
def xor(a, b):
return bytes(x ^ y for x, y in zip(a, b))
def H(k):
return sha256(k).digest()
def dec(k, x):
k = H(k)
val = xor(k, x)
if val[:PAD_LENGTH] == b"\0"*PAD_LENGTH:
return val[PAD_LENGTH:]
def decode_gate(opv, g, ins, to_hex):
if to_hex:
ins = [bytes.fromhex(x) for x in ins]
g = [bytes.fromhex(r) for r in g]
if opv == "INV":
res = ins[0]
elif opv == "XOR":
res = xor(ins[0], ins[1])
else:
for x in g:
k = b"".join(ins)
val = dec(k, x)
if val is not None:
res = val
break
if to_hex:
return res.hex()
return res
def evaluate(garbled_circuit, inputs, wires_out, to_hex=True):
enc_A, enc_B = inputs
vals = enc_A+enc_B+[None]*len(garbled_circuit)
for g in garbled_circuit:
idx, opv, ins, gate = g
k = [vals[i] for i in ins]
cur = decode_gate(opv, gate, k, to_hex)
assert cur is not None
vals[idx] = cur
n_outs = len(wires_out)
vals_out = vals[-n_outs:]
out = [w.index(v) for v, w in zip(vals_out, wires_out)]
return out
# Client
def bytes2bits(b):
bits = ''.join(f'{x:08b}' for x in b)
return list(map(int, bits))
def bits2bytes(arr):
n = len(arr)
assert n % 8 == 0
nbytes = n // 8
s = "".join(map(str, arr))
return int.to_bytes(int(s, 2), nbytes, "big")
class Client:
def __init__(self, host, token):
self.r = remote(host, port)
self.r.recvlines(1)
self.r.sendlineafter(b"token: ", token.encode())
def set_keyword(self, keyword):
self.r.recvlines(6)
self.r.sendlineafter(b"> ", b"1")
self.r.sendlineafter(b"secret: ", keyword.encode())
def set_public(self, data):
self.r.recvlines(6)
self.r.sendlineafter(b"> ", b"2")
self.r.sendlineafter(b"text: ", data.encode())
def get_public(self, user):
self.r.recvlines(6)
self.r.sendlineafter(b"> ", b"4")
self.r.sendlineafter(b"user: ", user.encode())
return self.r.recvline(False).decode()
def run_function(self, choice, user, my_in):
self.r.recvlines(6)
self.r.sendlineafter(b"> ", b"3")
self.r.sendlineafter(b"user: ", user.encode())
self.r.recvlines(3)
self.r.sendlineafter(b"> ", str(choice).encode())
data = self.r.recvline(False)
tmp = zlib.decompress(base64.b64decode(data))
obj = json.loads(tmp.decode())
circ = obj["circuit"]
N, e, x = obj["ot"]["N"], obj["ot"]["e"], obj["ot"]["x"]
in_B = bytes2bits(my_in)
assert len(in_B) == 128
if choice == 2:
in_B = in_B[::-1]
receiver = Receiver(in_B)
v = receiver.round2((N, e), x)
vdict = json.dumps({"v": v})
self.r.sendline(vdict.encode())
data = self.r.recvline(False)
obj = json.loads(data.decode())
c = obj["c"]
m = receiver.decode(c)
enc_B = [f'{x:010x}' for x in m]
res = evaluate(circ["gates"], (circ["enc_A"],
enc_B), circ["wires_out"])
if choice == 2:
res = res[::-1]
return bits2bytes(res)
# SLA functions
def sla_user_setkeyword():
try:
username, password = register_random_user()
token = get_token(username, password)
c = Client(team_addr, token)
except Exception as e:
checklib.quit(checklib.Status.DOWN, 'Can\'t login', str(e))
try:
key = random_string(16, 16)
rdata = random_string(32, 32)
c.set_keyword(key)
c.set_public(rdata)
return username, key
except Exception as e:
checklib.quit(checklib.Status.DOWN, 'Can\'t put data', str(e))
def sla_user_echo(target_user, key):
try:
username, password = register_random_user()
token = get_token(username, password)
c = Client(team_addr, token)
except Exception as e:
checklib.quit(checklib.Status.DOWN, 'Can\'t login', str(e))
try:
rdata = os.urandom(16)
res = c.run_function(1, target_user, rdata)
if res != rdata:
checklib.quit(checklib.Status.DOWN, 'Echo function not working',
f"sla_user_echo input={rdata.hex()} output={res.hex()}")
except Exception as e:
checklib.quit(checklib.Status.DOWN, 'Can\'t run echo function', str(e))
def sla_user_encrypt(target_user, key):
try:
username, password = register_random_user()
token = get_token(username, password)
c = Client(team_addr, token)
except Exception as e:
checklib.quit(checklib.Status.DOWN, 'Can\'t login', str(e))
try:
rdata = os.urandom(16)
#print("running GC")
res = c.run_function(2, target_user, rdata)
cipher = AES.new(key.encode(), AES.MODE_ECB)
pt = cipher.decrypt(res)
if pt != rdata:
checklib.quit(checklib.Status.DOWN, 'Encrypt function not working',
f"sla_user_encrypt input={rdata.hex()} key={key} output={res.hex()}")
except Exception as e:
checklib.quit(checklib.Status.DOWN,
'Can\'t run encrypt function', str(e))
def sla_user_readpublic(target_user, key):
try:
username, password = register_random_user()
token = get_token(username, password)
c = Client(team_addr, token)
except Exception as e:
checklib.quit(checklib.Status.DOWN, 'Can\'t login', str(e))
try:
res = c.get_public(target_user)
except Exception as e:
checklib.quit(checklib.Status.DOWN, 'Can\'t read public data', str(e))
# Check SLA
def check_sla():
user, key = sla_user_setkeyword()
slas = [sla_user_echo, sla_user_encrypt, sla_user_readpublic]
f = random.choice(slas)
f(user, key)
# If OK
checklib.quit(checklib.Status.OK, 'OK')
# Put the flag using the flag as the seed for random stuff
def put_flag():
flag = data["flag"]
try:
random.seed("123"+flag+"789")
key = random_string(16, 16)
username, password = register_random_user()
token = get_token(username, password)
c = Client(team_addr, token)
except Exception as e:
checklib.quit(checklib.Status.DOWN, 'Can\'t login', str(e))
try:
#print("putting flag", username, key)
c.set_keyword(key)
c.set_public(flag)
except Exception as e:
checklib.quit(checklib.Status.DOWN, 'Can\'t put data', str(e))
checklib.post_flag_id(service_name, team_addr, username)
# If OK
checklib.quit(checklib.Status.OK, 'OK')
# Get flag functions
def get_flag_pubdata(c, target_user, key):
try:
pubdata = c.get_public(target_user)
ct = bytes.fromhex(pubdata)
cipher = AES.new(key.encode(), AES.MODE_ECB)
pt = cipher.decrypt(ct)
if pt.decode() != data["flag"]:
checklib.quit(checklib.Status.DOWN, 'Incorrect public data for flag',
f"get_flag_pubdata pubdata={pubdata.hex()} key={key} pt={pt.hex()}")
except Exception as e:
checklib.quit(checklib.Status.DOWN, 'Can\'t read public data', str(e))
def get_flag_AES(c, target_user, key):
flag = data["flag"].encode()
try:
ct = c.run_function(2, target_user, flag[:16])
#print("run GC")
pubdata = c.get_public(target_user)
if ct.hex() != pubdata[:32]:
checklib.quit(checklib.Status.DOWN, 'Incorrect encryption function for flag',
f"get_flag_AES pubdata={pubdata.hex()} ct={ct.hex()}")
except Exception as e:
checklib.quit(checklib.Status.DOWN,
'Can\'t run encryption function', str(e))
# 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"]
try:
username, password = register_random_user()
token = get_token(username, password)
c = Client(team_addr, token)
except Exception as e:
checklib.quit(checklib.Status.DOWN, 'Can\'t login', str(e))
checks = [get_flag_pubdata, get_flag_AES]
check = random.choice(checks)
random.seed("123"+flag+"789")
key = random_string(16, 16)
username, password = get_random_creds()
check(c, username, key)
# If OK
checklib.quit(checklib.Status.OK, 'OK')
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()