def solve(): ciphertext = encrypter('A' * 16 * 3) blocks = crypty.get_blocks(ciphertext) blocks = [blocks[0], '00' * 16, blocks[0]] plaintext = decrypter("".join(blocks)) blocks = crypty.get_blocks(plaintext) foundkey = crypty.xor_hex_strings(blocks[0], blocks[2]) print "Key : %s" % (foundkey)
def get_suffix(block_size): suffix_len = get_suffix_size() guessed_suffix = "" for idx in xrange(0,suffix_len): prefix_len = block_size - len(guessed_suffix)%block_size - 1 plaintext = 'A'*prefix_len + guessed_suffix ciphertext_1 = encryption_oracle(crypty.a2h('A'*prefix_len)) last_blk_idx = len(plaintext)/block_size for c in xrange(0,256): ciphertext_2 = encryption_oracle(crypty.a2h(plaintext[last_blk_idx*block_size:]+chr(c))) if crypty.get_blocks(ciphertext_2)[0] == crypty.get_blocks(ciphertext_1)[last_blk_idx]: guessed_suffix+=chr(c) break return guessed_suffix
def get_prefix_size(): for i in xrange(32, 32 + 16): ciphertext = encryption_oracle(crypty.a2h('A' * i)) idx = get_rep_block_idx(crypty.get_blocks(ciphertext)) if idx is not None: return (16 * idx) - i + 32 return None
def MDFunction1(plaintext, IV, pad=True): hash = simplePadIVFn(IV) if pad: plaintext = simplePadMessageFn(plaintext) for block in get_blocks(a2h(plaintext), block_size=8): block = h2a(block) hash = simpleHashFn(block, hash) return hash
def solve(): iv, ciphertext = encrypter() blocks = crypty.get_blocks(ciphertext) plaintext = "" for i in xrange(len(blocks)): x = decipher_block(iv, blocks[i]) iv = blocks[i] plaintext += x print plaintext.decode("hex")
def solve(): block_size = get_block_size() plaintext = crypty.a2h("A" * block_size) ciphertext = encryption_oracle(plaintext) ciphertext_blks = crypty.get_blocks(ciphertext, block_size=block_size) if ciphertext_blks[0] == ciphertext_blks[1]: print "[+] Detected ECB Encryption." print get_suffix(block_size) return
def solve(): ciphertext = encrypter('A' * 16) blocks = crypty.get_blocks(ciphertext) old_cipher_block = blocks[1] new_cipher_block = crypty.xor_hex_strings( crypty.xor_hex_strings(old_cipher_block, crypty.a2h('A' * 16)), crypty.a2h(";admin=true;XXXX")) blocks[1] = new_cipher_block if decrypter("".join(blocks)): print "New Ciphertext : %s" % ("".join(blocks))
def get_suffix(block_size, suffix_len, prefix_size): prefix_pad_len = (block_size - (prefix_size) % block_size) % block_size guessed_suffix = "" for idx in xrange(0, suffix_len): plaintext_len = prefix_pad_len + block_size - len( guessed_suffix) % block_size - 1 actual_plaintext = 'X' * prefix_size + 'A' * plaintext_len + guessed_suffix ciphertext_1 = encryption_oracle(crypty.a2h('A' * plaintext_len)) last_blk_idx = len(actual_plaintext) / block_size for c in xrange(0, 256): ciphertext_2 = encryption_oracle( crypty.a2h('A' * prefix_pad_len + actual_plaintext[last_blk_idx * block_size:] + chr(c))) if crypty.get_blocks( ciphertext_2)[(prefix_pad_len + prefix_size) / block_size] == crypty.get_blocks( ciphertext_1)[last_blk_idx]: guessed_suffix += chr(c) break return guessed_suffix
def encrypt_manual(hex_string, key, iv): """ Manually AES-128-CBC Encrypts the input hex string using key, iv and AES-128-ECB :param hex_string: Hex string :param key: Hex string :param iv: Hex string :return: hex string """ assert len(iv) == len(key) blocks = crypty.get_blocks(hex_string, block_size=16) ciphertext = "" for block in blocks: if len(crypty.h2a(block)) % 16 is not 0: block = crypty.pad_pkcs_7(block, block_size=16) xor_block = crypty.xor_hex_strings(block, iv) iv = ecb_cipher.infra.encrypt(xor_block, key) ciphertext += iv return ciphertext
def decrypt_manual(hex_string, key, iv, raiser=True): """ Manually AES-128-CBC decrypts the input hex string using key, iv and AES-128-ECB :param hex_string: Hex string :param key: Hex string :param iv: Hex string :return: hex string """ assert len(iv) == len(key) blocks = crypty.get_blocks(hex_string, block_size=16) plaintext = "" for block in blocks: xor_block = ecb_cipher.infra.decrypt(block, key) plaintext += crypty.xor_hex_strings(xor_block, iv) iv = block if crypty.is_pcks7_padded(plaintext): plaintext = crypty.unpad_pkcs_7(plaintext) elif raiser: raise Exception("Wrongly padded hex string") return plaintext
def solve(): ciphertexts = get_ciphertexts() for idx,ciphertext in enumerate(ciphertexts): blocks = crypty.get_blocks(ciphertext, block_size=16) if len(blocks)>len(set(blocks)): print "[+] Found ECB Encrypted line : %d "%(idx)