Example #1
0
def chall_four(unknown_string):
    unknown_hex = base64.b64decode(unknown_string).hex()

    found_block_sizes = find_cipher_block_size(unknown_hex)
    block_size = found_block_sizes[1] - found_block_sizes[0]
    my_string = ("A" * block_size * 3).encode().hex(
    )  # want to pad >2 block lengths to detect repetition (i.e. force message to have 2 equivalent blocks)

    padded_msg = pkcs7_pad(my_string + unknown_hex, block_size)
    (ciphertext,
     CipherObj) = aes_ecb_encrypt_with_key(bytes.fromhex(padded_msg),
                                           bytes.fromhex(key))
    ciphertext = ciphertext.hex()
    ecb_detected = detect_ecb(ciphertext)
    if not ecb_detected:
        print("ECB not detected; not exploitable")
        return

    # now we know the block size, and it's in ECB
    decrypted_blocks = ''.join([
        padding_oracle(unknown_hex[i:i + block_size * 2], block_size, '')
        for i in range(0, len(unknown_hex), block_size * 2)
    ])

    #unpad string
    padding = ord(decrypted_blocks[-1])
    decrypted_blocks = decrypted_blocks[:-padding]

    return decrypted_blocks
Example #2
0
def find_cipher_block_size(unknown_string):
    padded_lengths = []
    for i in range(0, 64):
        rand_str = ("A" * i).encode().hex()
        try:
            (ciphertext, CipherObj) = aes_ecb_encrypt_with_key(
                bytes.fromhex(rand_str + unknown_string), bytes.fromhex(key))
            padded_lengths.append(i)
        except ValueError:
            pass  #print(f"Invalid block length {i}; trying next length")
    return padded_lengths
Example #3
0
def aes_ctr_operation(key, data, nonce):
    ctr_output = ''
    ctr = 0
    block = data[ctr*32:(ctr+1)*32]

    while block:
        ctr_data = pack('<QQ', int(nonce, 16), ctr)      # see struct library for packing formatting
        aes_out = aes_ecb_encrypt_with_key(ctr_data, bytes.fromhex(key))

        temp_output = xor_hex_strings(aes_out[0].hex()[:len(block)], block)
        if len(temp_output) % 2: temp_output = '0' + temp_output

        ctr_output += temp_output

        ctr += 1
        block = data[ctr*32:(ctr+1)*32]

    return ctr_output
Example #4
0
def encryption_oracle(msg):
    rand_count = random.randint(5, 10)
    prepad_msg = get_random_int(
        rand_count * 8) + msg.encode().hex() + get_random_int(rand_count * 8)
    padded_msg = pkcs7_pad(prepad_msg.encode().hex(), 16)
    #print(padded_msg)

    ciphertext = ''
    key = get_random_int(128)
    iv = get_random_int(128)

    if random.getrandbits(1):
        ciphertext = cbc_encrypt(padded_msg, key, iv)
    else:
        (ciphertext,
         CipherObj) = aes_ecb_encrypt_with_key(bytes.fromhex(padded_msg),
                                               bytes.fromhex(key))
        ciphertext = ciphertext.hex()

    return ciphertext
Example #5
0
def generic_encrypt_ecb(msg, key, block_size):
    (ciphertext, CipherObj) = aes_ecb_encrypt_with_key(
        bytes.fromhex(pkcs7_pad(msg, block_size)), bytes.fromhex(key))
    return ciphertext.hex()
Example #6
0
def encrypt_profile(input_string):
    padded_string = pkcs7_pad(input_string.encode().hex(), 16)
    return aes_ecb_encrypt_with_key(bytes.fromhex(padded_string), bytes.fromhex(key))