-
Notifications
You must be signed in to change notification settings - Fork 0
/
set2.py
executable file
·740 lines (571 loc) · 22.7 KB
/
set2.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
#!/usr/bin/env python3
import collections
import doctest
import math
import os
import random
import sys
from set1 import b2b64, b642b, xorvec
import util
"""
// ------------------------------------------------------------
9. Implement PKCS#7 padding
Pad any block to a specific block length, by appending the number of
bytes of padding to the end of the block. For instance,
"YELLOW SUBMARINE"
padded to 20 bytes would be:
"YELLOW SUBMARINE\x04\x04\x04\x04"
The particulars of this algorithm are easy to find online.
"""
INPUT_9 = b'YELLOW SUBMARINE'
OUTPUT_9 = b'YELLOW SUBMARINE\x04\x04\x04\x04'
def pkcs7pad(data, blocksize):
"""
>>> pkcs7pad(b'YELLOW SUBMARINE', 20) == b'YELLOW SUBMARINE\x04\x04\x04\x04'
True
>>> pkcs7pad(b'YELLOW SUBMARINE', 16) == b'YELLOW SUBMARINE\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10'
True
"""
blocks = len(data) // blocksize
remainder = len(data) % blocksize
padlen = blocksize - remainder
padchar = bytes([padlen])
data = data + (padchar * padlen)
return data
def pkcs7unpad(data):
"""
>>> pkcs7unpad(b'YELLOW SUBMARINE\x04\x04\x04\x04')
b'YELLOW SUBMARINE'
>>> pkcs7unpad(b'YELLOW SUBMARINE\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10')
b'YELLOW SUBMARINE'
>>> pkcs7unpad(b'ICE ICE BABY\x04\x04\x04\x04')
b'ICE ICE BABY'
>>> pkcs7unpad(b'ICE ICE BABY\x05\x05\x05\x05')
Traceback (most recent call last):
...
Exception: Bad padding!
>>> pkcs7unpad(b'ICE ICE BABY\x01\x02\x03\x04')
Traceback (most recent call last):
...
Exception: Bad padding!
"""
unpadded = pkcs7unpad_core(data)
if unpadded is None:
raise Exception('Bad padding!')
return unpadded
def pkcs7unpad_core(data):
padlen = data[-1]
for c in data[-padlen:]:
if c != padlen:
return None
return data[:-padlen]
def run_p9():
print('Problem 9')
print('Expected output:', OUTPUT_9)
output = pkcs7pad(INPUT_9, 20)
assert(output == OUTPUT_9)
print('Actual output: ', output)
print()
"""
// ------------------------------------------------------------
10. Implement CBC Mode
In CBC mode, each ciphertext block is added to the next plaintext
block before the next call to the cipher core.
The first plaintext block, which has no associated previous ciphertext
block, is added to a "fake 0th ciphertext block" called the IV.
Implement CBC mode by hand by taking the ECB function you just wrote,
making it encrypt instead of decrypt (verify this by decrypting
whatever you encrypt to test), and using your XOR function from
previous exercise.
DO NOT CHEAT AND USE OPENSSL TO DO CBC MODE, EVEN TO VERIFY YOUR
RESULTS. What's the point of even doing this stuff if you aren't going
to learn from it?
The buffer at:
https://gist.github.com/3132976
is intelligible (somewhat) when CBC decrypted against "YELLOW
SUBMARINE" with an IV of all ASCII 0 (\x00\x00\x00 &c)
"""
import AES128
def run_p10():
print('Problem 10')
INPUT = b642b(open('set2p10.txt').read())
KEY = b'YELLOW SUBMARINE'
IV = bytes(AES128.BLOCKSIZE)
output = AES128.CBC_decrypt(IV + INPUT, KEY)
print('First 3 lines of output:')
for line in output.splitlines()[:3]:
print(line.decode())
print()
"""
// ------------------------------------------------------------
11. Write an oracle function and use it to detect ECB.
Now that you have ECB and CBC working:
Write a function to generate a random AES key; that's just 16 random
bytes.
Write a function that encrypts data under an unknown key --- that is,
a function that generates a random key and encrypts under it.
The function should look like:
encryption_oracle(your-input)
=> [MEANINGLESS JIBBER JABBER]
Under the hood, have the function APPEND 5-10 bytes (count chosen
randomly) BEFORE the plaintext and 5-10 bytes AFTER the plaintext.
Now, have the function choose to encrypt under ECB 1/2 the time, and
under CBC the other half (just use random IVs each time for CBC). Use
rand(2) to decide which to use.
Now detect the block cipher mode the function is using each time.
"""
def p11_oracle(data):
# Generate a random key
key = os.urandom(AES128.KEYSIZE)
# Munge the data
data = (os.urandom(random.randint(5, 10)) +
data +
os.urandom(random.randint(5, 10)))
encryption = random.choice([AES128.encrypt, AES128.CBC_encrypt])
return encryption(pkcs7pad(data, AES128.BLOCKSIZE), key), encryption
def is_ecb(ciphertext):
# It is overwhelmingly likely that CBC will scramble things enough
# that there will be no identical blocks. Meanwhile, ECB will have
# many duplicate blocks because I'm passing in all zeros (see
# below).
blocks = list(util.grouper(AES128.BLOCKSIZE, ciphertext))
return (len(set(blocks)) != len(blocks))
def run_p11():
print('Problem 11')
# Just throw in a very long zero vector
data = bytes(AES128.BLOCKSIZE * 100)
runs = 0
correct = 0
for i in range(1000):
runs += 1
ciphertext, encryption = p11_oracle(data)
if is_ecb(ciphertext):
guess = AES128.encrypt
else:
guess = AES128.CBC_encrypt
if encryption is guess:
correct += 1
print('Out of', runs, 'tries, the classifier got', correct, 'correct.')
if runs == correct:
print('NOT BAD!')
print()
"""
// ------------------------------------------------------------
12. Byte-at-a-time ECB decryption, Full control version
Copy your oracle function to a new function that encrypts buffers
under ECB mode using a consistent but unknown key (for instance,
assign a single random key, once, to a global variable).
Now take that same function and have it append to the plaintext,
BEFORE ENCRYPTING, the following string:
Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg
aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq
dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUg
YnkK
SPOILER ALERT: DO NOT DECODE THIS STRING NOW. DON'T DO IT.
Base64 decode the string before appending it. DO NOT BASE64 DECODE THE
STRING BY HAND; MAKE YOUR CODE DO IT. The point is that you don't know
its contents.
What you have now is a function that produces:
AES-128-ECB(your-string || unknown-string, random-key)
You can decrypt "unknown-string" with repeated calls to the oracle
function!
Here's roughly how:
a. Feed identical bytes of your-string to the function 1 at a time ---
start with 1 byte ("A"), then "AA", then "AAA" and so on. Discover the
block size of the cipher. You know it, but do this step anyway.
b. Detect that the function is using ECB. You already know, but do
this step anyways.
c. Knowing the block size, craft an input block that is exactly 1 byte
short (for instance, if the block size is 8 bytes, make
"AAAAAAA"). Think about what the oracle function is going to put in
that last byte position.
d. Make a dictionary of every possible last byte by feeding different
strings to the oracle; for instance, "AAAAAAAA", "AAAAAAAB",
"AAAAAAAC", remembering the first block of each invocation.
e. Match the output of the one-byte-short input to one of the entries
in your dictionary. You've now discovered the first byte of
unknown-string.
f. Repeat for the next byte.
"""
SECRET_SUFFIX_12 = b642b("""
Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg
aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq
dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUg
YnkK
""")
KEY_12 = os.urandom(AES128.KEYSIZE)
def secret_suffix_oracle(secret_suffix, data):
return AES128.encrypt(pkcs7pad(data + secret_suffix, AES128.BLOCKSIZE),
KEY_12)
def p12_oracle(data):
return secret_suffix_oracle(SECRET_SUFFIX_12, data)
def find_block_size(oracle):
def oracle_len(length):
return len(oracle(bytes(length)))
start_size = len(oracle(bytes(1)))
# Run the oracle with successively inputs until the output
# suddenly gets longer.
cur_bytes = 1
while oracle_len(cur_bytes) == start_size:
cur_bytes += 1
# OK, the output just jumped in length at cur_size bytes. Now add
# bytes until it jumps again.
jump_bytes = cur_bytes
jump_size = oracle_len(cur_bytes)
while oracle_len(cur_bytes) == jump_size:
cur_bytes += 1
# The output jumped in length again. We now know the block size.
return (cur_bytes - jump_bytes)
def find_secret_suffix_length(oracle):
# Work out how many data bytes you need to pass in before the
# oracle output grows by a block. That's how many bytes short of
# an even block the oracle suffix is.
bytes_short = 0
oracle_null_output_length = len(oracle(b''))
# Due to the PKCS7 padding, we expect the number of blocks to jump
# as soon as we reach an even block length.
while (len(oracle(bytes(bytes_short))) ==
oracle_null_output_length):
bytes_short += 1
return oracle_null_output_length - bytes_short
def find_next_byte(oracle, blocksize, known_prefix):
# Generate a vector of NULs that, when prepended to the known
# prefix, bring it to one byte short of a full block.
chosen_part_len = (-(len(known_prefix) + 1) % blocksize)
chosen_part = bytes(chosen_part_len)
# Work out how many blocks of the oracle's output we should know
# (accounting for the us-controlled varying byte at the end).
num_known_blocks = math.ceil(
(len(chosen_part) + len(known_prefix) + 1) /
blocksize)
# Generate the oracle output such that its hidden prefix is
# aligned with the start of our known_prefix. We'll be looking at
# the first num_known_blocks blocks below.
oracle_output = oracle(chosen_part)
oracle_output_start = oracle_output[:(num_known_blocks*blocksize)]
# Test each possible next-byte of the known prefix in turn. The
# first num_known_blocks of one of them should match the first
# num_known_blocks of oracle_output.
for next_byte in range(256):
stimulus = chosen_part + known_prefix + bytes([next_byte])
if oracle(stimulus).startswith(oracle_output_start):
return next_byte
# Hmm. That didn't work. PANIC.
assert(False)
def find_secret_suffix(oracle, blocksize):
secret_suffix_length = find_secret_suffix_length(oracle)
print('The secret suffix is', secret_suffix_length, 'bytes long.')
known_prefix = bytearray()
for i in range(secret_suffix_length):
#print(known_prefix)
next_byte = find_next_byte(oracle, blocksize, known_prefix)
known_prefix.append(next_byte)
secret_suffix = known_prefix
#print(len(secret_suffix), secret_suffix)
return secret_suffix
def run_p12():
print('Problem 12')
oracle = p12_oracle
blocksize = find_block_size(oracle)
print('Block size is', blocksize, 'bytes.')
ecb_check_data = list(util.grouper(blocksize, oracle(bytes(1000))))
if len(set(ecb_check_data)) == len(ecb_check_data):
print('...not ECB, apparently. Huh?')
return
else:
print('ECB detected, as expected.')
secret_suffix = find_secret_suffix(oracle, blocksize)
if not secret_suffix == SECRET_SUFFIX_12:
print("WE'RE WRONNNNNNGGG!")
return
print('We got it!')
test_data = b'Hi there!'
assert(secret_suffix_oracle(secret_suffix, test_data) ==
oracle(test_data))
print('The secret suffix was:')
print(secret_suffix.decode())
print()
"""
// ------------------------------------------------------------
13. ECB cut-and-paste
Write a k=v parsing routine, as if for a structured cookie. The
routine should take:
foo=bar&baz=qux&zap=zazzle
and produce:
{
foo: 'bar',
baz: 'qux',
zap: 'zazzle'
}
(you know, the object; I don't care if you convert it to JSON).
Now write a function that encodes a user profile in that format, given
an email address. You should have something like:
profile_for("foo@bar.com")
and it should produce:
{
email: 'foo@bar.com',
uid: 10,
role: 'user'
}
encoded as:
email=foo@bar.com&uid=10&role=user
Your "profile_for" function should NOT allow encoding metacharacters
(& and =). Eat them, quote them, whatever you want to do, but don't
let people set their email address to "foo@bar.com&role=admin".
Now, two more easy functions. Generate a random AES key, then:
(a) Encrypt the encoded user profile under the key; "provide" that
to the "attacker".
(b) Decrypt the encoded user profile and parse it.
Using only the user input to profile_for() (as an oracle to generate
"valid" ciphertexts) and the ciphertexts themselves, make a role=admin
profile.
"""
def profile_decode(s):
"""
>>> list(sorted(profile_decode('uid=bar&email=qux&role=zazzle').items()))
[('email', 'qux'), ('role', 'zazzle'), ('uid', 'bar')]
"""
d = {} # Dictionaries are like objects, right?
for kv in s.split('&'):
k, v = kv.split('=', 1)
d[k] = v
return d
def profile_encode(d):
# Helpfully put the keys in the order that makes it easy to apply
# cut-and-paste.
keys = ['email', 'uid', 'role']
out = '&'.join((k + '=' + d[k]) for k in keys)
return out
def profile_for(email):
"""
>>> profile_for('bob@g.c')
'email=bob@g.c&uid=10&role=user'
"""
# Strip out the metacharacters.
for c in '&=':
email = email.replace(c, '')
d = {'email': email,
'role' : 'user',
'uid' : '10'}
return profile_encode(d)
P13_KEY = os.urandom(AES128.KEYSIZE)
def profile_cookie(email):
return AES128.encrypt(pkcs7pad(profile_for(email).encode(), AES128.BLOCKSIZE), P13_KEY)
def profile_cookie_decode(cookie):
return profile_decode(pkcs7unpad(AES128.decrypt(cookie, P13_KEY)).decode())
def gen_admin_cookie():
# 1. Create a plaintext using (e.g.) XXXXXXXXXadmin\0b... as the
# email to get the ciphertext for the following ('|' is the
# block boundary):
#
# email=XXXXXXXXXXXXXX| \
# admin\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b| \
# &uid=10&role=user
#
# The idea here is to get that middle "admin" block, which, if
# it were to appear as the last block in a PKCS7 padded
# plaintext, would decode to the simple "admin"
admin_plaintext = pkcs7pad(b'admin', AES128.BLOCKSIZE)
right_email = ((AES128.BLOCKSIZE - len('email=')) * b'X') + admin_plaintext
right_ciphertext = profile_cookie(right_email.decode())
right_block = right_ciphertext[AES128.BLOCKSIZE:(2*AES128.BLOCKSIZE)]
# 2. Create a plaintext using XXXXXX email inputs to get the
# ciphertext for the following:
#
# email=XXXXX&uid=10&role=|user
#
# Note that the left chunk will need to be two blocks, because
# the prefilled data is longer than a block (19 characters).
left_email = (2*AES128.BLOCKSIZE - len('email=') - len('&uid=10&role=')) * 'X'
left_ciphertext = profile_cookie(left_email)
left_block = left_ciphertext[:(2*AES128.BLOCKSIZE)]
# 3. Paste the first block (2) and the second block of (1)
# together to produce a profile ciphertext. It will represent
# the following plaintext:
#
# email=X&uid=10&role=| \
# admin\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b
#
# which PKCS7-unpads to
# email=X&uid=10&role=|admin
admin_cookie = left_block + right_block
return admin_cookie
def run_p13():
print('Problem 13')
cookie = gen_admin_cookie()
print('Created a cookie')
decode = profile_cookie_decode(cookie)
print('It decodes to this object:', decode)
if decode['role'] == 'admin':
print('We made an admin cookie!')
else:
print('Sadly, we failed at making an admin cookie :-(')
print()
"""
// ------------------------------------------------------------
14. Byte-at-a-time ECB decryption, Partial control version
Take your oracle function from #12. Now generate a random count of
random bytes and prepend this string to every plaintext. You are now
doing:
AES-128-ECB(random-prefix || attacker-controlled || target-bytes, random-key)
Same goal: decrypt the target-bytes.
What's harder about doing this?
How would you overcome that obstacle? The hint is: you're using
all the tools you already have; no crazy math is required.
Think about the words "STIMULUS" and "RESPONSE".
// ------------------------------------------------------------
"""
P14_PREFIX = os.urandom(random.randint(1,50))
def p14_oracle(data):
return p12_oracle(P14_PREFIX + data)
def find_data_injection_block(oracle):
# Figure out what block the beginning byte is in.
nul = list(util.grouper(AES128.BLOCKSIZE, oracle(b'')))
single = list(util.grouper(AES128.BLOCKSIZE, oracle(b'x')))
# The first block that differs between the two contains the first
# byte.
for i in range(min(len(single), len(nul))):
if nul[i] != single[i]:
return i
return None
def find_distance_to_block_edge(oracle, injection_block):
# Figure out how many bytes of data we need to add to fill up the
# block where data is injected.
def test_ct(i):
# Generate a candidate injection block. Used later.
data = b'x' * i
blocks = list(util.grouper(AES128.BLOCKSIZE, oracle(data)))
return blocks[injection_block]
last_try = test_ct(0)
ct = 1
while last_try != test_ct(ct):
last_try = test_ct(ct)
ct += 1
return ct - 1
def run_p14():
print('Problem 14')
# Where is our data getting injected in the oracle's output?
injection_block_index = find_data_injection_block(p14_oracle)
# How many bytes of data do we need to prefix our input with in
# order to guarantee that it lines up at a block boundary?
pad_bytes = find_distance_to_block_edge(p14_oracle,
injection_block_index)
# OK, now define a variant on the oracle that pads the data to
# fill the injection_block and returns the p14_oracle output
# starting right after injection_block. Then we can apply the same
# technique we did in p12.
def new_oracle(data):
pad_vector = bytes(pad_bytes)
injection_block_bytes = (injection_block_index + 1) * AES128.BLOCKSIZE
return p14_oracle(pad_vector + data)[injection_block_bytes:]
secret_suffix = find_secret_suffix(new_oracle, AES128.BLOCKSIZE)
if secret_suffix != SECRET_SUFFIX_12:
print("We ... didn't get it?!")
else:
print("Found it! The secret suffix was:")
print(secret_suffix.decode())
"""
15. PKCS#7 padding validation
Write a function that takes a plaintext, determines if it has valid
PKCS#7 padding, and strips the padding off.
The string:
"ICE ICE BABY\x04\x04\x04\x04"
has valid padding, and produces the result "ICE ICE BABY".
The string:
"ICE ICE BABY\x05\x05\x05\x05"
does not have valid padding, nor does:
"ICE ICE BABY\x01\x02\x03\x04"
If you are writing in a language with exceptions, like Python or Ruby,
make your function throw an exception on bad padding.
// ------------------------------------------------------------
"""
############################################################
# See pkcs7unpad and its doctests at the top of this file. #
############################################################
def run_p15():
print('Problem 15 has no output. See the source code.')
print()
"""
16. CBC bit flipping
Generate a random AES key.
Combine your padding code and CBC code to write two functions.
The first function should take an arbitrary input string, prepend the
string:
"comment1=cooking%20MCs;userdata="
and append the string:
";comment2=%20like%20a%20pound%20of%20bacon"
The function should quote out the ";" and "=" characters.
The function should then pad out the input to the 16-byte AES block
length and encrypt it under the random AES key.
The second function should decrypt the string and look for the
characters ";admin=true;" (or, equivalently, decrypt, split the string
on ;, convert each resulting string into 2-tuples, and look for the
"admin" tuple. Return true or false based on whether the string exists.
If you've written the first function properly, it should not be
possible to provide user input to it that will generate the string the
second function is looking for.
Instead, modify the ciphertext (without knowledge of the AES key) to
accomplish this.
You're relying on the fact that in CBC mode, a 1-bit error in a
ciphertext block:
* Completely scrambles the block the error occurs in
* Produces the identical 1-bit error (/edit) in the next ciphertext
block.
Before you implement this attack, answer this question: why does CBC
mode have this property?
// ------------------------------------------------------------
"""
P16_KEY = os.urandom(AES128.KEYSIZE)
P16_PREFIX = b'comment1=cooking%20MCs;userdata='
P16_SUFFIX = b';comment2=%20like%20a%20pound%20of%20bacon'
def p16_cookie(userdata):
"""
>>> print(p16_cookie_decode(p16_cookie(b'hello')).decode())
comment1=cooking%20MCs;userdata=hello;comment2=%20like%20a%20pound%20of%20bacon
>>> print(p16_cookie_decode(p16_cookie(b';admin=true;')).decode())
comment1=cooking%20MCs;userdata=\;admin\=true\;;comment2=%20like%20a%20pound%20of%20bacon
"""
# Sanitize
userdata = userdata.replace(b';', b'\\;')
userdata = userdata.replace(b'=', b'\\=')
# And go!
data = (P16_PREFIX + userdata + P16_SUFFIX)
return AES128.CBC_encrypt(pkcs7pad(data, AES128.BLOCKSIZE), P16_KEY)
def p16_cookie_decode(cookie):
return pkcs7unpad(AES128.CBC_decrypt(cookie, P16_KEY))
def p16_cookie_is_admin(cookie):
return b'admin=true' in p16_cookie_decode(cookie)
def run_p16():
print('Problem 16')
# Get a cookie using some random data.
cookie = p16_cookie(b'ohai')
assert(not p16_cookie_is_admin(cookie))
# The second block of the cookie encodes the first block of the
# prefix. Since we know that, we can modify the IV (the first
# block of the cookie) such that the second block will decode to
# what we want.
new_first_block_plaintext = b';admin=true;'
new_first_block_plaintext += bytes((-len(new_first_block_plaintext))
% AES128.BLOCKSIZE)
old_iv = cookie[:AES128.BLOCKSIZE]
new_iv = xorvec(old_iv,
P16_PREFIX[:AES128.BLOCKSIZE],
new_first_block_plaintext)
new_cookie = new_iv + cookie[AES128.BLOCKSIZE:]
if p16_cookie_is_admin(new_cookie):
print('We made an admin cookie!')
else:
print('Our best attempts at forgery were insufficient. We made this cookie:')
print(p16_cookie_decode(new_cookie))
assert(0)
if __name__ == '__main__':
if (doctest.testmod()[0]) > 0:
sys.exit(1)
run_p9()
run_p10()
run_p11()
run_p12()
run_p13()
run_p14()
run_p15()
run_p16()