Skip to content

Lesson 2 of 9 · 22m

CBC Bit-Flipping: Malleability Is Not a Theoretical Problem

CBC ciphertext can be edited without the key; learn exactly which plaintext bits move, what corruption costs, and how attackers exploit predictable token layouts.

CBC confidentiality does not mean integrity. That sentence has burned more applications than weak key sizes ever did. If plaintext has a predictable field — admin=0, role=user, amount=100 — an attacker can flip ciphertext bits and often turn it into the plaintext they want.

The mechanics in one diagram

P[i] = D(C[i]) XOR C[i-1]

Changing a bit in C[i-1] flips the same bit in P[i]. The cost is that C[i-1] itself decrypts to garbage because the previous plaintext block is corrupted. Attackers either target the first block, where the IV is controllable, or place the target in a later block and tolerate one damaged adjacent block.

# target plaintext byte is "0" (0x30); desired byte is "1" (0x31)
mask = 0x30 ^ 0x31
forged = bytearray(ciphertext)
forged[target_block_offset - 16] ^= mask

No key is involved. The attacker is algebraically editing the XOR input.

Why real systems make this easy

The exploit needs a known or guessable plaintext layout. Applications helpfully provide one:

session=u=314159;role=user;exp=1893456000;sig=none

The attacker does not need to know the whole token. They need the byte position of user or 0 and enough control over neighboring content to absorb corruption. Long attacker-controlled usernames are perfect because they let the attacker align fields and overwrite sacrificial bytes.

This is why CBC bit-flipping appears in encrypted-cookie bugs, firmware update formats, payment callbacks, and 'temporary custom encryption' that someone wrote because JWT felt heavy.

The common wrong defenses

  • Encode the ciphertext, check only format, or hide the IV. Base64 is reversible; parseable forgeries still pass format validation; the IV is not secret.
  • Add a CRC32. CRCs are linear and attacker-adjustable. They are error detection, not authentication.
  • Use a longer key. AES-256-CBC is just as malleable as AES-128-CBC.

What correct looks like

Authenticated encryption makes ciphertext modification fail closed:

from cryptography.hazmat.primitives.ciphers.aead import AESGCM

sealed = AESGCM(key).encrypt(nonce, token_bytes, b"session-v1")
# Any bit flip causes decrypt() to raise InvalidTag before plaintext is used.

If legacy compatibility forces CBC, use encrypt-then-MAC over iv || ciphertext with a separate MAC key, verify first, and only then decrypt. Include a version byte or purpose string in the authenticated data so tokens cannot be replayed across features.

Field notes

  • CBC decryption oracles and bit-flipping often coexist in the same endpoint; fixing one without the other is not defense.
  • Random padding or prefixing plaintext does not reliably prevent targeted edits.
  • Never concatenate secrets into plaintext and call that integrity protection.
  • Test malleability by flipping one byte and asserting the application rejects the entire token.
  • Treat every unauthenticated ciphertext as attacker-controlled plaintext waiting for alignment.

Was this lesson helpful?

Sign in to rate