LOADING

Oracle Padding Attack simplified

Aug. 11, 2024 | Categories: Cybersecurity

First, let me admit something: writing a blog about cryptographic attacks isn’t the most glamorous topic out there. It’s not like picking out a new Netflix show or debating whether pineapple belongs on pizza (it doesn’t, by the way). Either way, this is all I have this week, so take it or leave it.

So, What Is an Oracle Padding Attack?

Let’s start with the basics. Imagine you’re sending a super-secret message to your friend using encryption. But there’s a catch: the message doesn’t neatly fit into encryption's blocks. It’s like trying to fit a square peg into a round hole—something’s got to give. Enter padding, and the extra data is added to fill those pesky gaps.

Here’s where things get tricky. If the padding isn’t handled correctly, it can create a sneaky little backdoor for hackers. This is where the Oracle Padding Attack comes into play. When pulled just right, it’s like a hacker finding a hidden lever that spills all your secrets.

Breaking It Down with a Real-Life Example

Let’s make this a bit more relatable. Say you use CBC (Cipher Block Chaining) mode to encrypt your data. Everything seems fine and dandy until a hacker comes along, intercepts your encrypted message, and starts fiddling with it. They don’t need the key to decrypt it; they just need to know how to manipulate the message to get the system to spill some beans.

Imagine the hacker is trying to break into your super-secret club, but they don’t have the password. Instead of guessing the password, they start tampering with the lock itself. They jiggle the lock in the right way and listen for the clicks. Each click tells them a little more about what’s inside. After enough jiggling and listening, they can figure out the entire password without ever having to know it from the start. That’s pretty much what happens in an Oracle Padding Attack—they mess with the encrypted data and listen to how the system responds until they can piece together the original message.

The Math (don’t you dare skip this part 😠)

Most commonly, padding is done using a scheme called PKCS7. It works by filling the empty space with the number of padding bytes. So, if you need to add 13 bytes, each of those 13 bytes will be the number 0x0D (which is 13 in hexadecimal).

Mathematically, if your message is ‘m’ bytes long and your block size is ‘n’ bytes, the number of padding bytes ‘p’ added is given by:

p = n − (m mod n)

Each padding byte will be:

padding_byte = p

So, for our 19-byte message with a 16-byte block size:

p = 16 − (19 mod 16) = 13

Each padding byte will be 0x0D.

Here’s where the math gets devious. Let’s say a hacker intercepts your encrypted message and starts tampering with the last block. They alter the last byte of the ciphertext, Cn, and send it to the decryption oracle.

When the oracle decrypts this altered ciphertext, the last block will decrypt to something that looks like this:

Pn = Dk(Cn) ⊕ Cn-1

Where:

Pn: the plaintext of the last block

Dk(Cn): the decryption of the last ciphertext block using the key ‘k’

Cn-1: the previous ciphertext block



​The attacker’s goal is to manipulate Cn so that the padding in Pn is valid, meaning it follows the PKCS7 scheme. If the padding is valid, the oracle will confirm it, and the hacker can infer something about the plaintext.

Let’s say the attacker guesses the last byte correctly so that the padding is valid (e.g., 0x01). The oracle says, “Yes, the padding is correct.” This means the attacker now knows the last byte of the plaintext!

Using the equation:

last_byte_of_plaintext = last_byte_of_ciphertext ⊕ padding_byte

The attacker can plug in their known values and solve for the plaintext byte.

For example, if the original last byte of ciphertext was 0xAB, the padding byte was 0x01, and the attacker modified C_n to 0xAA, they know:

last_byte_of_plaintext = 0xAA ⊕ 0x01 = 0xAB

This process is repeated for each byte in the block, slowly revealing the entire plaintext one byte at a time.

How Do You Stop This Sneaky Attack?

Now that I’ve probably made you paranoid about your encrypted messages let’s talk about how to defend against this kind of attack. Don’t worry; it’s not as hopeless as it sounds.


  1. Use Authenticated Encryption: This is like upgrading your lock to a state-of-the-art, hacker-proof version. Encryption modes like Galois/Counter Mode (GCM) or Counter with CBC-MAC (CCM) include extra security checks that ensure everything’s legit before anything gets decrypted. It’s like adding a fingerprint scanner to your secret club door.

  2. Don’t Give Away Too Much Info: Be vague, mysterious even. If your system encounters an error during decryption, don’t spill the beans on what went wrong. A generic “Something went wrong” message can be your best friend here—no need to give the hacker clues.

  3. Keep Things Consistent: Hackers love timing their attacks, so don’t make it easy for them. Make sure your system takes the same amount of time to respond, no matter what the input is. It’s like making sure your lock clicks the same way every time, no matter how much the hacker jiggles it.

  4. Rethink Padding Schemes: If padding is causing you grief, consider using encryption methods that don’t need it or at least using a tougher-to-crack padding scheme. It’s like finding a better way to fill in those gaps without leaving any weak spots.

Wrapping Up: Why Should You Care?

You might think, “Okay, cool story, but why does this matter to me?” Well, if you’re handling sensitive data—or just don’t want your encrypted texts getting exposed—then understanding and defending against Oracle Padding Attacks is crucial. Think of it as putting an extra deadbolt on your door—sure, you might not need it every day, but when you do, you’ll be glad it’s there.

So, there you have it—a not-so-glamorous but super-important dive into the world of cryptography and Oracle Padding Attacks. If you’ve made it this far, congrats! You’re now a little more prepared to keep your data safe from prying eyes. And hey, maybe next time you’re at a party, you can casually drop “Oracle Padding Attack” into the conversation. It’s sure to be a hit (or at least a good way to confuse your friends). 

I'm currently writing a demo to demonstrate this attack in action on a famous Java framework, Jetty, and encryption library, Apache Shirou, so stay tuned.