Lesson 1 of 9 · 25m
Padding Oracles: When One Error Message Decrypts Everything
How a tiny padding-validity signal lets an attacker decrypt CBC ciphertext byte by byte, and why error handling is a cryptographic boundary.
The padding oracle is the attack I use to explain why cryptography is not composable by vibes. AES-CBC can be implemented correctly, with a strong key and a random IV, and still collapse because the application says 'bad padding' in one branch and 'bad credentials' in another. One bit of distinction is enough.
The shape of the bug
CBC decrypts each block as:
P[i] = D(C[i]) XOR C[i-1]Before plaintext is returned, PKCS#7 padding is checked. For a 16-byte block, valid padding ends in one 0x01, two 0x02 bytes, three 0x03 bytes, and so on. If the server reveals whether padding is valid, the attacker can vary the previous ciphertext block and use the oracle answer to infer bytes of D(C[i]).
A vulnerable service often looks innocent:
try:
plaintext = decrypt_cbc(key, iv, ciphertext)
except InvalidPadding:
return JSONResponse({"error": "bad padding"}, status_code=400)
user = parse_session(plaintext)
if user is None:
return JSONResponse({"error": "invalid session"}, status_code=403)Those two status codes are the oracle.
Why the attack is practical
The attacker does not brute-force AES. They brute-force one padding byte at a time: at most 256 guesses per byte, 16 bytes per block, usually far fewer because multiple guesses can be tested in parallel or inferred from existing plaintext. A few thousand requests can recover a token. If the same service accepts modified ciphertext, the attacker can often encrypt chosen plaintext too.
The real-world version was not a CTF trick. TLS padding-oracle variants kept returning for years because implementations differed in timing, alert behavior, or padding checks. POODLE, Lucky13, and a long line of library bugs were all variations on 'a tiny validation signal crossed the cryptographic boundary.'
What you look for in code review
- separate exceptions or HTTP responses for padding, MAC, and application parse errors
- logging that records the exact padding failure for attacker-controlled ciphertext
- compression before encryption, which makes the attack easier to shape
- retry-friendly endpoints with no rate limiting
- CBC without encrypt-then-MAC authentication
The correct fix
Do not patch the error message and keep CBC. Use authenticated encryption. AES-GCM or ChaCha20-Poly1305 verifies the ciphertext before plaintext exists, so modified ciphertext is rejected uniformly. If you are maintaining a legacy CBC system, encrypt-then-MAC with independent keys and verify the MAC in constant time before attempting decryption.
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
aesgcm = AESGCM(key) # key must be 16, 24, or 32 random bytes
ct = aesgcm.encrypt(nonce, plaintext, associated_data)
pt = aesgcm.decrypt(nonce, ct, associated_data) # raises InvalidTag uniformlyField notes
- A timing difference can be the oracle even when response bodies match.
- Do not log attacker-controlled decrypt failures at different severity levels by error type.
- Padding oracles love session tokens, encrypted cookies, and URL parameters because those are high-value and attacker-replayable.
- Rate limiting reduces exploitation speed; it does not fix the cryptographic flaw.
- If a design requires decryption before authentication, treat it as legacy and plan migration, not refinement.
Was this lesson helpful?