def oracle(pt): unknown = b'Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg\ aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq\ dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK' pt = pad(pt + decode(unknown, 'base64'), 16) cipher = AES.new(key, AES.MODE_ECB) return cipher.encrypt(pt)
def encrypt(plaintext: str): """Enterprise Grade Super secure encryption function""" plaintext = plaintext.replace(';', '%3B').replace('=', '%3D') plaintext = b"comment1=cooking%20MCs;userdata=" + bytes( plaintext, 'ascii') + b";comment2=%20like%20a%20pound%20of%20bacon" print(plaintext) return cbc_encrypt(pad(plaintext), STATIC_KEY, STATIC_IV)
def encrypt(profile): cipher = AES.new(key, AES.MODE_ECB) pt = b'' for k,v in profile.items(): pt += bytes(k, 'utf-8') + b'=' + bytes(v, 'utf-8') + b'&' pt = pt[:-1] pt = pad(pt, (len(pt) // 16 + 1) * 16) return cipher.encrypt(pt)
def ecb_encrypt(plaintext, key): """ :param plaintext: plain text to decrypt :param key: key used for encryption :return: cipher text as bytes """ plaintext = challenge9.pad(plaintext, BLOCK_SIZE) return AES.new(key, AES.MODE_ECB).encrypt(plaintext)
def encryption_oracle(pt): pt = urandom(randint(5,10)) + pt + urandom(randint(5, 10)) pt = pad(pt, (len(pt) // 16 + 1) * 16) if len(pt) % 16 else pt key = urandom(16) if randint(0, 1): print('encrypted with ECB. ', end='') cipher = AES.new(key, AES.MODE_ECB) else: print('encrypted with CBC. ', end='') cipher = AES.new(key, AES.MODE_CBC, urandom(16)) return cipher.encrypt(pt)
def encryption_oracle(plaintext: bytearray): """Encrypt plaintext with CBC or ECB half the time whilst appending bytes before and after""" key = os.urandom(16) if bool(random.getrandbits(1)): cipher = AES.new(key, AES.MODE_ECB) else: iv = os.urandom(16) cipher = AES.new(key, AES.MODE_CBC, iv) plaintext = os.urandom(random.randint(5, 10)) + plaintext + os.urandom( random.randint(5, 10)) return cipher, cipher.encrypt(pad(plaintext))
def cbc_encrypt(plaintext, key, iv): """ :param plaintext: plain text to decrypt :param key: key used for encryption :param iv: initialization vector :return: cipher text as bytes """ plaintext = challenge9.pad(plaintext, BLOCK_SIZE) ciphertext = bytearray() for i in range(0, len(plaintext), BLOCK_SIZE): block = plaintext[i:i + BLOCK_SIZE] _bytes = ecb_encrypt(xor(block, iv), key) iv = _bytes ciphertext += _bytes return bytes(ciphertext)
def enterprise_encryption_function(plaintext: bytearray): """Enterprise Grade Super secure encryption function""" cipher = AES.new(STATIC_KEY, AES.MODE_ECB) plaintext = PREPENDED_STRING + plaintext + UNKNOWN_STRING return cipher.encrypt(pad(plaintext))
"""Generate profile for email""" return OrderedDict([('email', html.escape(email)), ('uid', 10), ('role', 'user')]) assert profile_for("*****@*****.**") == OrderedDict([('email', '*****@*****.**'), ('uid', 10), ('role', 'user')]) # Now, two more easy functions. Generate a random AES key, then: key = os.urandom(16) cipher = AES.new(key, AES.MODE_ECB) encrypted_profile = cipher.encrypt( pad(bytes(dict_to_form(profile_for("*****@*****.**")), 'ascii'))) # Tamper the block to make the ciphertext read role=admin plaintext_profile = pad( bytes(dict_to_form(profile_for("*****@*****.**")), 'ascii')) encrypted_profile = cipher.encrypt(plaintext_profile) print("Profile: {0}, Encrypted Profile: {1}".format(plaintext_profile, encrypted_profile)) #[email protected] maaaaaa&uid=10& role=user 7 padding bytes #[email protected] maaaaaa&uid=10& role=admin 6 padding bytes replacement_block = b"role=admin" + b"\x06" * 6 encrypted_replacement_block = cipher.encrypt(replacement_block) blocks = group(encrypted_profile, AES.block_size)