-
Notifications
You must be signed in to change notification settings - Fork 4
/
p26.py
46 lines (32 loc) · 1.1 KB
/
p26.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
from os import urandom
from typing import Dict
from main import Solution
from p02 import xor
from p18 import aes_ctr
from Crypto.Cipher import AES
def _bitflip(ctxt, user_data):
inject = b'dataz;admin=true'
targetblock = ctxt[(2 * AES.block_size):(3 * AES.block_size)]
keybytes = xor(user_data, targetblock)
badblock = xor(inject, keybytes)
start = ctxt[:(2 * AES.block_size)]
end = ctxt[(3 * AES.block_size):]
return start + badblock + end
def _parse(plaintext: bytes) -> Dict[str, str]:
data = {}
for pairs in plaintext.split(b';'):
key, value = pairs.split(b'=')
data[key] = value
return data
def p26():
user_data = b'A' * 16
comment1 = b'comment1=cooking%20MCs;userdata='
comment2 = b';comment2=%20like%20a%20pound%20of%20bacon'
ptxt = comment1 + user_data.replace(b';', b'%3B').replace(b'=', b'%3D') + comment2
key = urandom(16)
ctxt = aes_ctr(ptxt, key)
ctxtmod = _bitflip(ctxt, user_data)
ptxtmod = aes_ctr(ctxtmod, key)
return _parse(ptxtmod)
def main() -> Solution:
return Solution('26: CTR bitflipping', p26)