コード例 #1
0
def encryption_oracle(plaintext: bytes) -> bytes:
    encoded_to_append = "Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK"  # noqa
    to_append = base64.b64decode(encoded_to_append)
    garbage_padded_text = b"".join([prepend, plaintext, to_append])

    padded_text = pkcs7_pad(garbage_padded_text)
    ciphertext = encrypt_aes_ecb(padded_text, consistent_key)
    return ciphertext
コード例 #2
0
def encryption_oracle(plaintext: bytes) -> Tuple[str, bytes]:
    key = generate_16_random_bytes()

    prepadding = bytes(
        [random.randrange(0, 0xff) for i in range(0, random.randrange(5, 11))])
    postpadding = bytes(
        [random.randrange(0, 0xff) for i in range(0, random.randrange(5, 11))])

    garbage_padded_text = b"".join([prepadding, plaintext, postpadding])
    padded_text = pkcs7_pad(garbage_padded_text)
    if random.random() > 0.5:
        # ECB
        mode = "ECB"
        ciphertext = encrypt_aes_ecb(padded_text, key)
    else:
        # CBC
        mode = "CBC"
        iv = generate_16_random_bytes()
        ciphertext = encrypt_aes_cbc(padded_text, key, iv)
    return mode, ciphertext
コード例 #3
0
def encrypt_aes_cbc(plaintext: bytes, key: bytes, iv: bytes) -> bytes:
    if len(plaintext) % 16 == 0:
        num_chunks = len(plaintext) // 16
    else:
        num_chunks = len(plaintext) // 16 + 1

    encrypted = []
    prev_cipher_chunk = None
    for i in range(0, num_chunks):
        curr_chunk = plaintext[i * 16:(i + 1) * 16]
        if len(curr_chunk) < 16:
            curr_chunk = pkcs7_pad(curr_chunk)

        if not prev_cipher_chunk:
            xored_chunk = xor_bytes(curr_chunk, iv)
        else:
            xored_chunk = xor_bytes(curr_chunk, prev_cipher_chunk)

        encrypted_chunk = encrypt_aes_ecb(xored_chunk, key)
        encrypted.append(encrypted_chunk)
        prev_cipher_chunk = encrypted_chunk
    return b"".join(encrypted)
コード例 #4
0
def profile_oracle(email: bytes) -> bytes:
    return encrypt_aes_ecb(pkcs7_pad(create_querystring(profile_for(email))), consistent_key)
コード例 #5
0
def cbc_oracle(plaintext: bytes) -> bytes:
    sanitized_plaintext = plaintext.replace(b";",
                                            b"\";\"").replace(b"=", b"\"=\"")
    total = b"".join([prepend, sanitized_plaintext, append])
    padded_total = pkcs7_pad(total)
    return encrypt_aes_cbc(padded_total, consistent_key, consistent_iv)