AES CBC Bit-Flipping
- •
What it is
- •
An attack on CBC decryption where flipping bits in one ciphertext block predictably flips the same bits in the next decrypted plaintext block, because . Controlling the IV (or a preceding block) lets an attacker rewrite arbitrary bytes of the following plaintext block without knowing the key.
- •
- •
When to apply
- •
An application decrypts attacker-supplied ciphertext and IV under CBC, then trusts fields parsed out of the resulting plaintext (e.g.
admin=Falsein a cookie) with no integrity check (no MAC/signature).
- •
- •
Math
- •
; (applied at the byte offsets you want to change).
- •
- •
Worked example
- •
Cookie
user=guest_perm0, encrypted under an all-zero IV. Computingdelta = xor("user=guest_perm0", "user=admin_perm0")and XORing it into the IV, then decrypting the original ciphertext with the new IV, yields the plaintextuser=admin_perm0exactly — the key was never touched or needed.
- •
- •
Python
- •
delta = xor(b"admin=False", b"admin=True;") new_iv = xor(iv[:len(delta)], delta) + iv[len(delta):]
- •
- •
- •
Cards
- •
Why does flipping a bit in a CBC ciphertext block affect the next plaintext block, not the same one?
- •
Because — the previous ciphertext block (or IV) is XORed in after decryption, so changes to it propagate directly into that XOR.
- •
- •
What defense would have prevented this attack entirely?
- •
Verifying ciphertext/IV integrity (a MAC) before decrypting and trusting the plaintext.
- •
- •