Padding oracle
The padding oracle is one of the most elegant yet most practical crypto attacks: it lets you decrypt (and often encrypt) data protected with CBC without knowing the key, by abusing the fact that the system reveals whether the padding is valid. It shows up in cookies, tokens, encrypted parameters, and APIs.
What it is
Section titled “What it is”A padding oracle is any observable behavior that distinguishes “the padding is valid” from “the padding is invalid” after decryption. That distinction —a different HTTP status, an error message, or even a timing difference— turns the server into a machine that answers yes/no questions about the plaintext, which is enough to recover it byte by byte.
Threat model
Section titled “Threat model”The attacker needs neither the key nor to break AES. They only need:
- A manipulable CBC ciphertext (a cookie or parameter that is a multiple of the block size).
- An oracle that, on resubmitting corrupted ciphertexts, responds distinguishably depending on the padding.
With that they decrypt the whole message and, applying the property in reverse, can forge valid ciphertexts at will.
Theory: CBC and PKCS#7
Section titled “Theory: CBC and PKCS#7”In CBC, P_i = D(C_i) XOR C_{i-1} (with C_0 = IV). PKCS#7 padding fills the last block with N bytes of value N (for example 04 04 04 04). On decryption, the padding is checked to have that form; if not, a padding error occurs.
How the attack decrypts
Section titled “How the attack decrypts”sequenceDiagram
participant A as Attacker
participant O as Server (oracle)
A->>A: Picks C1' with the last byte modified
A->>O: Sends [C1' | C2]
O-->>A: "invalid padding"
A->>A: Iterates the byte until...
A->>O: Sends [C1' | C2]
O-->>A: "VALID padding" (0x01)
A->>A: D(C2)[last] = C1'[last] XOR 0x01
Note over A,O: Repeats forcing 0x02 0x02... across the block
Goal: recover P2 = D(C2) XOR C1 without the key. You manipulate C1 byte by byte (C1') and observe the oracle over [C1' | C2]:
- Adjust the last byte of
C1'until the padding is valid (0x01). ThenD(C2)[last] = C1'[last] XOR 0x01. - Knowing
D(C2)[last], force padding0x02 0x02and repeat for the second-to-last byte. - Continue until you recover all 16 bytes of
D(C2); thenP2 = D(C2) XOR C1(the realC1).
At most 256 tries per byte, and the attack recovers the whole message block by block. To encrypt arbitrary text you use the same property in reverse, choosing the C_{i-1} values that produce the desired plaintext.
Variant: RSA padding oracle (Bleichenbacher / ROBOT)
Section titled “Variant: RSA padding oracle (Bleichenbacher / ROBOT)”The same principle applies to RSA with PKCS#1 v1.5 padding: a “valid padding” oracle allows decryption or signing. Formulated by Bleichenbacher in 1998 and revived as ROBOT in 2017 at large sites. See PKI & certificates and TLS.
Red Team
Section titled “Red Team”# padbuster automates the attack against a web endpoint# syntax: padbuster URL EncryptedSample BlockSize [options]padbuster "http://host/api?data=BASE64CIPHER" BASE64CIPHER 16 \ -encoding 0 -cookies "sess=abc123"# -encoding 0 = Base64 ; BlockSize 16 = AESSigns there’s an oracle: a parameter or cookie that is clearly a ciphertext (a multiple of 16 bytes in base64/hex) and distinguishable responses when you corrupt it. When the oracle is non-standard (timing differences, subtle responses), it’s best to build the attack by hand in Python with PyCryptodome, carefully defining the oracle(ciphertext) -> bool function.
Blue Team
Section titled “Blue Team”- Authenticated encryption (AEAD): AES-GCM or ChaCha20-Poly1305 — there’s no padding to validate. See Symmetric vs asymmetric.
- If using CBC, encrypt-then-MAC: verify the MAC before decrypting or validating padding.
- Don’t distinguish errors: the same response and timing for invalid padding and invalid data.
- Don’t expose manipulable ciphertexts to the client without integrity.
CVEs and real-world cases
Section titled “CVEs and real-world cases”- Vaudenay (2002): original formulation of the CBC padding oracle.
- Lucky Thirteen (CVE-2013-0169): timing-based padding oracle in TLS CBC.
- POODLE (CVE-2014-3566): padding oracle over SSLv3 CBC.
- ROBOT (2017): Bleichenbacher (RSA padding oracle) at Facebook, PayPal, Cisco, and F5.
Checklist
Section titled “Checklist”- Locate parameters/cookies that are CBC ciphertexts (a multiple of 16)
- Does the system distinguish “invalid padding” from “invalid data”? (error/timing)
- Run padbuster or a script to decrypt block by block
- Try ciphertext forgery (controlled encryption)
- RSA PKCS#1 v1.5: look for a Bleichenbacher/ROBOT-style oracle
- Verify the defense: AEAD or encrypt-then-MAC + uniform responses