-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
66 lines (45 loc) · 1.12 KB
/
server.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
from flask import Flask
from Crypto.Cipher import AES
from os import urandom as rand
from binascii import unhexlify
app = Flask(__name__)
flag = 'flag{' + rand(32).hex() + '}'
key = rand(32) # AES key
# Creates an AES cipher object
def aes(iv):
return AES.new(key, AES.MODE_CBC, iv)
# apply PKCS#7 padding to m
def pad(m):
ln = 16 - len(m) % 16
return m + (chr(ln) * ln)
# remove PKCS#7 padding. Exposes a padding oracle
def unpad(m):
ln = m[-1]
for c in m[-ln:]:
if c != ln:
return
if 0 < ln <= 16:
return m[:-ln]
# returns the flag, encrypted under the key and a random IV
@app.route('/flag')
def get_flag():
iv = rand(16)
c = aes(iv).encrypt(pad(flag))
return f'{iv.hex()}{c.hex()}'
def decrypt(c):
c = unhexlify(c)
iv = c[:16]
c = c[16:]
if len(c) % 16:
return
return unpad(aes(iv).decrypt(c))
# attempts to decrypt
@app.route('/decrypt/<c>')
def decrypt_silent(c):
p = decrypt(c)
if p is None:
return 'bad padding'
return 'ok'
@app.route('/decrypt_reveal/<c>')
def decrypt_loud(c):
return decrypt(c)