liveBook · Manning

Padding oracle attack

As explained in Hacking Cryptography

A padding oracle attack is a chosen-ciphertext technique that recovers plaintext by observing whether a decryption service accepts manipulated padding.

A padding oracle attack exploits a decryption service that reveals whether manipulated ciphertext produces valid padding. In CBC mode, changing one byte of a preceding ciphertext block changes the corresponding decrypted plaintext byte, allowing plaintext recovery without the encryption key. section 5.6 section 5.4.2

Equation 5.1
\[\begin{aligned} \begin{aligned} I_n &= \text{Decrypt}_{\text{Key}} (C_n) \\ P_n &= C_{n-1} \oplus I_n \end{aligned} \label{equation:padding-oracle-two-blocks} \end{aligned}\]

CBC decryption makes the attack possible: the plaintext block equals the XOR of the preceding ciphertext block and the decrypted target block. section 5.4.2

The exploit targets PKCS#7 padding one byte at a time. It brute-forces the final byte of the preceding ciphertext block until the service accepts padding 0x01, then adjusts known trailing bytes to 0x02 0x02, continuing leftward. section 5.4.2

A typical recovery loop:

  • Start with the final ciphertext block and work backward through the message. section 5.4.2
  • Copy the preceding ciphertext block so trial modifications do not destroy the original. section 5.4.2
  • For each padding length, preserve already recovered bytes while brute-forcing the next byte. section 5.4.2
  • Use the accepted padding result and XOR equations to derive the target plaintext byte. section 5.4.2

Go exploit test

A test creates a padding-oracle server, requests an encrypted timestamp, calls RecoverPlaintextFromPaddingOracle, and logs the recovered value. The demonstrated run recovers “Fri Jun 9 14:39:01 PDT 2023.” section 5.4.2

The failure is not a direct break of the block cipher; it is information leaked by error behavior. Treat padding validity as secret, and generate a new IV for every CBC-encrypted message. section 5.6

How Hacking Cryptography covers it

How deep it goes
Traces bytewise recovery through XOR equations, ciphertext mutation, Go code, and a passing test, stopping short of cipher internals.
Examples
Demonstrates AES-block recovery against an encrypted timestamp using Go code, equations, figures, and a unit test.
What it assumes
Assumes familiarity with block-cipher blocks, CBC decryption, XOR, and PKCS#7 padding while explaining the attack equations.
What you can do after
Build it from scratch by implementing backward block traversal, padding-byte brute force, XOR recovery, and an oracle-driven test.
How it connects
Connects block ciphers with CBC mode, PKCS#7 padding, initialization vectors, and error-message information leakage.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hacking Cryptography. The section links point to the parts of the book it draws on.