-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_attacks.py
192 lines (143 loc) · 7.04 KB
/
test_attacks.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
#!/usr/bin/python3
import ccm
import pytest
import binascii
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
key = b'Sixteen byte key'
IV = b'Sixteen bytes IV'
cipher_ecb = AES.new(key, AES.MODE_ECB)
cipher_cbc = AES.new(key, AES.MODE_CBC, iv=IV)
# |----- 00 -----|------ 01 -----|------ 02 -----|------ 03 -----|------ 04 -----|
# |----- iv -----|timestap=1597405984&username=username-that-fills&role=guest
sample_1 = b'timestap=1597405984&username=username-that-fills&role=guest'
# |----- 00 -----|------ 01 -----|------ 02 -----|------ 03 -----|------ 04 -----|------ 05 -----|
# |----- iv -----|timestap=1597405984&username=username-that-fills&role=guest&language=en
sample_2 = b'timestap=1597405984&username=username-that-fills&role=guest&language=en'
# |----- 00 -----|------ 01 -----|------ 02 -----|------ 03 -----|------ 04 -----|------ 05 -----|
# |----- iv -----|timestap=1597405984&username=username-that-fills&isadmin=0&language=en
sample_3 = b'timestap=1597405984&username=username-that-fills&isadmin=0&language=en'
# |------ 00 -----|------ 01 -----|------ 02 -----|------ 03 -----|------ 04 -----|
# |timestap=1597405984&username=use1&ame-that-fills&rid=44&isadmin=0&language=en
sample_4 = b'timestap=1597405984&username=use1;ame-that-fills&rid=44&isadmin=0&language=en'
sample_1_cbc = binascii.hexlify(IV + cipher_cbc.encrypt(pad(sample_1, AES.block_size))).decode('utf-8')
sample_2_cbc = binascii.hexlify(IV + cipher_cbc.encrypt(pad(sample_2, AES.block_size))).decode('utf-8')
sample_3_cbc = binascii.hexlify(IV + cipher_cbc.encrypt(pad(sample_3, AES.block_size))).decode('utf-8')
sample_4_ecb = binascii.hexlify(cipher_ecb.encrypt(pad(sample_4, AES.block_size))).decode('utf-8')
#####################################################################
# Aimed Flipping #
#####################################################################
aimed_flipping_format = "crypted, current, desired, start, end, check"
aimed_flipping_samples = [
(sample_1_cbc, 'guest', 'admin', 0, -1, '&role=admin'),
(sample_2_cbc, 'guest', 'admin', 0, -1, '&role=admin&'),
(sample_3_cbc, '=0&', '=1&', 0, -1, '&isadmin=1&'),
(sample_1_cbc, 'guest', 'admin', 3, 3, '&role=admin'),
(sample_2_cbc, 'guest', 'admin', 0, 3, '&role=admin&'),
(sample_3_cbc, '=0&', '=1&', 1, 3, '&isadmin=1&'),
]
@pytest.mark.parametrize(aimed_flipping_format, aimed_flipping_samples)
def test_aimed_flipping(crypted, current, desired, start, end, check):
cookie = ccm.CryptedCookie(crypted, block_sizes=[16])
generator = cookie.aimed_flipping_generator(current, desired, 0, -1, start, end)
result = False
for item in generator:
cipher = AES.new(key, AES.MODE_CBC, iv=IV)
item = binascii.unhexlify(item)
item = cipher.decrypt(item)
if check.encode('utf-8') in item:
result = True
assert result is True
#####################################################################
# Bit Flipping #
#####################################################################
bit_flipping_format = "crypted, start, end, check"
bit_flipping_samples = [
(sample_3_cbc, 0, -1, '&isadmin=1&'),
(sample_3_cbc, 3, 3, '&isadmin=1&'),
(sample_3_cbc, 0, 3, '&isadmin=1&'),
(sample_3_cbc, 1, 3, '&isadmin=1&'),
]
@pytest.mark.parametrize(bit_flipping_format, bit_flipping_samples)
def test_bit_flipping(crypted, start, end, check):
cookie = ccm.CryptedCookie(crypted, block_sizes=[16])
generator = cookie.flipping_generator(0, -1, start, end)
result = False
for item in generator:
cipher = AES.new(key, AES.MODE_CBC, iv=IV)
item = binascii.unhexlify(item)
item = cipher.decrypt(item)
if check.encode('utf-8') in item:
result = True
assert result is True
#####################################################################
# Block Shuffeling #
#####################################################################
shuffle_format = "crypted, start, end, target_blocks, check"
shuffle_samples = [
(sample_4_ecb, 0, -1, [2], '&isadmin=1;'),
(sample_4_ecb, 3, 3, [2], '&isadmin=1;'),
(sample_4_ecb, 3, 5, [2], '&isadmin=1;'),
(sample_4_ecb, 0, -1, [3], '&isadmin=1;'),
(sample_4_ecb, 0, 2, [3], '&isadmin=1;'),
(sample_4_ecb, 2, 2, [3], '&isadmin=1;'),
]
@pytest.mark.parametrize(shuffle_format, shuffle_samples)
def test_shuffeling(crypted, start, end, target_blocks, check):
cookie = ccm.CryptedCookie(crypted, block_sizes=[16])
generator = cookie.shuffeling_generator(start=start, end=end, target_blocks=target_blocks)
result = False
for item in generator:
cipher = AES.new(key, AES.MODE_ECB)
item = binascii.unhexlify(item)
item = cipher.decrypt(item)
if check.encode('utf-8') in item:
result = True
assert result is True
#####################################################################
# Full Shuffeling #
#####################################################################
full_shuffle_format = "crypted, start, end, check"
full_shuffle_samples = [
(sample_4_ecb, 0, -1, '&isadmin=1;'),
(sample_4_ecb, 2, 3, '&isadmin=1;'),
(sample_4_ecb, 2, 4, '&isadmin=1;'),
]
@pytest.mark.parametrize(full_shuffle_format, full_shuffle_samples)
def test_full_shuffeling(crypted, start, end, check):
cookie = ccm.CryptedCookie(crypted, block_sizes=[16])
generator = cookie.full_shuffeling_generator(start=start, end=end)
result = False
for item in generator:
cipher = AES.new(key, AES.MODE_ECB)
item = binascii.unhexlify(item)
item = cipher.decrypt(item)
if check.encode('utf-8') in item:
result = True
assert result is True
#####################################################################
# Padding Oracle #
#####################################################################
padding_format = "crypted, padding"
padding_samples = [(sample_1_cbc, 0x5), (sample_2_cbc, 0x9), (sample_3_cbc, 0xa)]
@pytest.mark.parametrize(padding_format, padding_samples)
def test_padding(crypted, padding):
cookie = ccm.CryptedCookie(crypted, block_sizes=[16])
generator = cookie.padding_generator()
count = 0
for item in generator:
cipher = AES.new(key, AES.MODE_CBC, iv=IV)
item = binascii.unhexlify(item)
item = cipher.decrypt(item)
try:
item = unpad(item, 16)
except ValueError:
count += 1
# Imagine the original padding is 030303. Flipping the last byte
# of the padding, two valid paddings can occur: 03 and 01. So, if
# the original padding is != 0, there will be 256 - 2 = 254 padding
# violations. If the original padding is == 0, it should be 255.
if padding == 0x1:
assert count == 255
else:
assert count == 254