-
Notifications
You must be signed in to change notification settings - Fork 1
/
challenge13.py
159 lines (125 loc) · 4.98 KB
/
challenge13.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
from collections import OrderedDict
from Crypto.Cipher import AES
from Crypto import Random
import re
USER_DB = OrderedDict()
user_cnt = 0
KEY = Random.new().read(16)
class objectify:
"""class for creating and representing JSON-like objects from a cookie-like object
"""
def __init__(self, cookie):
self.cookie = cookie
self.obj = OrderedDict()
def convert(self):
"""converts a cookie-like object into a dictionary of key=value pairs
Returns:
[dict]: dictionary of key=value pairs
"""
# if already converted
if len(self.obj) > 0:
return self.obj
# get key=value pairs
kv = self.cookie.split('&')
# assign key=value pairs to dictionary: dict[key]=value
for pair in kv:
k, v = pair.split('=')
self.obj[k] = v
return self.obj
def __repr__(self):
"""converts a dictionary of key=value pairs to JSON-like format for representation
Returns:
str: a string formatted like a JSON-object
"""
self.convert()
ret_value = "{\n"
last_key = next(reversed(self.obj))
for key, value in self.obj.items():
if not key == last_key:
ret_value += f"\t{ key }: '{ value }',\n"
else:
ret_value += f"\t{ key }: '{ value }'\n"
ret_value += "}"
return ret_value
def pad(value, size):
"""applies PKCS#7 padding to `value` to make it of size `size`
Args:
value (bytes): a byte-string to pad
size (size): the required size
Returns:
bytes: a byte-string = `value` + padding, such that its size is `size`
"""
if len(value) % size == 0:
return value
padding = size - len(value) % size
padValue = bytes([padding]) * padding
return value + padValue
def profile_for(user_info):
"""generates an encrypted profile info for the given `user_info`
Args:
user_info (str): the email address of the user
Returns:
bytes: the AES-ECB encrypted profile info (email, uid, role)
"""
# get cookie from user_info
global user_cnt
user_info = re.sub("&|=", "", user_info) # sanitize the `user_info` to remove '&' and '=' signs
cookie = f"email={ user_info }&uid={ user_cnt }&role=user"
user_cnt += 1
# encrypt the encoded cookie-info
paddedCookie = pad(bytes(cookie, 'ascii'), AES.block_size) # PKCS#7 padding
ecb = AES.new(KEY, AES.MODE_ECB)
cipherCookie = ecb.encrypt(paddedCookie)
return cipherCookie
def decrypt_profile(key, cipherCookie):
"""decrypts the encoded ciphertext with the given `key` under AES_ECB
Args:
key (bytes): the enryption key of size `AES.block_size`
cipherCookie (bytes): the encrypted text
Returns:
str: the cookie-like object decrypted from the `cipherCookie`
str: the JSON-like object obtained by parsing the cookie
"""
# decrypt the `cipherCookie`
ecb = AES.new(key, AES.MODE_ECB)
plainCookie = ecb.decrypt(cipherCookie)
# remove PKCS#7 padding
last_byte = plainCookie[-1]
# padding is a numeral whose value ranges from 0 to block_size - 1
if last_byte in range(AES.block_size - 1):
padding = bytes([last_byte]) * last_byte
# check if the `last_byte` number of bytes are the padding bytes
if plainCookie[-last_byte:] == padding:
plainCookie = plainCookie[:-plainCookie[-1]]
# parse cookie to object format
cookie = plainCookie.decode('ascii')
obj = objectify(cookie)
return cookie, str(obj)
def create_admin_profile():
"""creates an `admin profile` given the key for encryption/decryption
"""
# first create a block so that `email=<x>&uid=<x>&role=` occupies one block_size
# and value of the role occupies the last block
cookie_parts = 'email=@gmail.com&uid=2&role='
username = 'A' * (AES.block_size - len(cookie_parts) % AES.block_size)
email = username + "@gmail.com"
cipherCookie1 = profile_for(email)
# second, create a block so that `admin` occupies a block for itself
# create an email that occupies one full block
cookie_param = "email="
hacker_mail = 'A' * (AES.block_size - len(cookie_param) % AES.block_size)
# now append 'admin' + 'padding' to the mail so that it occupies a block of its own
value = pad(b'admin', AES.block_size).decode('ascii')
hacker_mail += value
cipherCookie2 = profile_for(hacker_mail)
# all except the last block of `cipherCookie1` i.e., `email=***@gmail.com&uid=0&role=`
block1 = cipherCookie1[:-AES.block_size]
# just the block containing `admin`, here it is the second block
block2 = cipherCookie2[AES.block_size:AES.block_size*2]
# concatenate `block1` and `block2`
cipherBlock = block1 + block2
cookie, obj = decrypt_profile(KEY, cipherBlock)
print(f"Cookie Created: { cookie }")
print(f"Object Created: { obj }")
if __name__ == "__main__":
create_admin_profile()