class ECB_RANDKEY_ORACLE: cipher = ECB() key = Random.new().read(block_size) thispkcs7 = PKCS7() def encrypt_ECB(self, plaintext): encrypted = self.cipher.encryptECB(self.thispkcs7.PKCS7_padding(plaintext, len(self.key)), self.key) return encrypted
def make_profile_from_encrypted(cipher, encrypted_profile, key): pkcs = PKCS7() decrypted_profile = pkcs.unpadPKCS7( cipher.decryptECB(encrypted_profile, key)) for i in range(0, len(decrypted_profile)): partial_plaintext = decrypted_profile[:len(decrypted_profile) - i] if chr(partial_plaintext[-1]) == "=": return cipher.encryptECB( pkcs.PKCS7_padding(partial_plaintext + b"admin", block_size), key)
def decrypt(self, ciphertext, key, IV): mypkcs7 = PKCS7() ecb_cipher = ECB() plaintext = b"" for i in range(0, len(ciphertext), block_size): pre_plaintext_block = mypkcs7.unpadPKCS7( ecb_cipher.decryptECB(ciphertext[i:i + block_size], key)) plaintext_block = bytes( [x ^ y for x, y in zip(IV, pre_plaintext_block)]) plaintext += plaintext_block IV = ciphertext[i:i + block_size] return mypkcs7.unpadPKCS7(plaintext).decode('utf-8')
def encrypt(self, plaintext, key, IV): mypkcs7 = PKCS7() ecb_cipher = ECB() answer = b"" for i in range(0, len(plaintext), block_size): pre_block = mypkcs7.PKCS7_padding( bytes([x ^ y for x, y in zip(IV, plaintext[i:i + block_size])]), block_size) ciphertext_block = ecb_cipher.encryptECB(pre_block, key) answer += ciphertext_block IV = ciphertext_block return answer
class HARDER_ECB_BREAKING_ORACLE: target = base64.b64decode( "Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK" ) cipher = ECB() rand_padding = Random.new().read( Random.random.randrange(block_size, block_size * 4)) key = Random.new().read(block_size) thispkcs7 = PKCS7() def encrypt_ECB(self, plaintext): encrypted = self.cipher.encryptECB( self.thispkcs7.PKCS7_padding( self.rand_padding + plaintext + self.target, len(self.key)), self.key) return encrypted
def prep_plaintext(self, plaintext): mypkcs7 = PKCS7() key = self.random_key(block_size) return (mypkcs7.PKCS7_padding( Random.new().read(random.randrange(5, 11)) + plaintext + Random.new().read(random.randrange(5, 11)), len(key)))
def check_padding(text): my_pkcs7 = PKCS7() return my_pkcs7.is_it_padded(text)
def make_profile_from_encrypted(cipher, encrypted_profile, key): pkcs = PKCS7() decrypted_profile = pkcs.unpadPKCS7( cipher.decryptECB(encrypted_profile, key)) for i in range(0, len(decrypted_profile)): partial_plaintext = decrypted_profile[:len(decrypted_profile) - i] if chr(partial_plaintext[-1]) == "=": return cipher.encryptECB( pkcs.PKCS7_padding(partial_plaintext + b"admin", block_size), key) if __name__ == "__main__": cipher = ECB() pkcs = PKCS7() key = Random.new().read(block_size) encoded_profile = profile_for("*****@*****.**", 100) profile_json_object = parse_profile(profile_for("*****@*****.**", 100)) encrypted_profile = cipher.encryptECB( pkcs.PKCS7_padding(str.encode(encoded_profile), block_size), key) decrypted_profile = pkcs.unpadPKCS7( cipher.decryptECB(encrypted_profile, key)) pasted_and_encrypted = make_profile_from_encrypted(cipher, encrypted_profile, key) decrypted_pasted_profile = pkcs.unpadPKCS7( cipher.decryptECB(pasted_and_encrypted, key)) parsed_admin = parse_profile(decrypted_pasted_profile.decode()) print(encrypted_profile) print(parse_profile(decrypted_profile.decode()))