CBC Mode is Malleable. Don’t trust it for Authentication — Arxum Path Security (2024)

On a recent pentest, I encountered an authentication system that used a block cipher in CBC mode, which I was able to break using a Padding Oracle. The vulnerability required access to valid ciphertext, which limited the scope of the attack, but it was possible to decrypt a full authentication token in about an hour even though the token was encrypted with AES-256.

Cryptography is difficult to implement securely due to the fact that it is complicated and requires many moving parts, and if any of these components are not handled properly, the entire system can be compromised.

The problem with CBC mode is that it is malleable. Recently, researchers broke PDF encryption using CBC Gadgets to inject content into an encrypted document (https://pdf-insecurity.org/encryption/cbc-malleability.html). The notion that simply because something is encrypted it can be trusted is false. Here are some reasons why.

Bit Flipping Attacks

In CBC mode, each block of plaintext is XOR’d against the previous block of ciphertext BEFORE encryption (the first block of plaintext is XOR’d against a block known as the Initialization Vector). This makes the value that is encrypted completely dependent on the prior ciphertext block.

During decryption the process is in reverse. A block is decrypted to an intermediate value, and then this intermediate value is XOR’d against the previous ciphertext block to return to the original plaintext.

XOR, or Exclusive OR, is a commutative operation, much like addition. For example,

This example is simple, because anything XOR’d by itself is 0, so when the plaintext decrypts to 0 here, we know that

Under such an attack, the attacker’s previous block will decrypt to garbage, so for this to work, that garbage needs to be discarded by the application (think of tokens that are split on &, for example). If an attacker can safely get the application to ignore the garbage, then the target block would decrypt to a forged authenticated token.

Note, CTR mode is also vulnerable to an attack like this, but in an even more direct fashion, as previous blocks don’t need to be tampered with.

Padding Oracles

While Padding Oracles do not recover the encryption key, a ciphertext encrypted with that key can be decrypted with 256 guesses per byte. The reason the vulnerability exists is because block ciphers must have valid padding, and encryption algorithms will handle the padding for developers during encryption. Consequently, during development and testing, valid ciphertexts are used and developers may never even be aware padding exists. This is dangerous because not handling padding errors safely can compromise the system.

So what is padding and why is it necessary?

A block cipher deals with fixed sizes of data, or blocks. In AES, the block size is 16 bytes, or 128 bits. A ciphertext block will always be 16 bytes, and so plaintext must also always be in blocks of 16 bytes. Real world scenarios don’t conform to such requirements, however. If the plaintext is 20 bytes in length, the first 16 bytes will form a block, and the remaining 4 bytes will be 12 bytes short of the 16 byte block size. Those remaining 12 bytes will get filled with padding.

The PKCS#7 standard defines how this padding is constructed, and it is quite simple. The number of padding bytes will be filled with the value of how much padding is necessary. To demonstrate, here we have 13 bytes of plaintext, and 3 bytes of padding. So we pad the 13 bytes with 3 bytes of \x03.

Padding can be anywhere from 1 to 16 bytes. The reason 16 bytes of padding would exist is if the plaintext evenly falls into 16 byte blocks. An additional block of only padding would then be required so that the algorithm knows the padding is valid.

During decryption, a ciphertext is first decrypted and then the padding is discarded. If the ciphertext was not tampered with, then the padding will be valid. But if the padding can’t be found and the application errors, then an attacker can leverage this error as an oracle.

Using techniques similar to those in the bit flipping attack, an attacker can force decryption of a block to one that has valid padding if the application provides information when the padding is invalid. This is because for each byte in the target block, there will be an 8bit value in the previous block that XORs the intermediate value into valid padding. For instance, for the last byte of the block, we are looking for a padding of \x01. So a valid equation would look like this:

Using XORs commutative property, and the original previous block, we can then decrypt the last byte of the target block

Knowing the last byte of plaintext, the attacker would then need to find a block that decrypts to intermediate values that end in \x02\x02, \x03\x03\x03, \x04\x04\x04\x04, etc. This allows for an efficient decryption of a ciphertext without ever knowing the key, simply because the application didn’t handle an error.

Recommendations

Just because something is encrypted, doesn’t make it trustworthy. To ensure the integrity of ciphertexts, sign them with a Message Authentication Code (MAC), or consider using a block cipher mode that provides authentication, such as GCM. For authentication tokens, using an HMAC with SHA-256 is advisable.

CBC Mode is Malleable. Don’t trust it for Authentication — Arxum Path Security (2024)
Top Articles
What is an earthquake? Answer at BYJU’S IAS
Why You Should Never Sit With a Wallet in Your Back Pocket — Compassion Family Chiropractic
Ffxiv Act Plugin
Week 2 Defense (DEF) Streamers, Starters & Rankings: 2024 Fantasy Tiers, Rankings
Weeminuche Smoke Signal
Sarah F. Tebbens | people.wright.edu
Craigslist Vermillion South Dakota
How do you mix essential oils with carrier oils?
Lesson 1 Homework 5.5 Answer Key
Hartford Healthcare Employee Tools
Nioh 2: Divine Gear [Hands-on Experience]
TS-Optics ToupTek Color Astro Camera 2600CP Sony IMX571 Sensor D=28.3 mm-TS2600CP
Used Drum Kits Ebay
Jenn Pellegrino Photos
ARK: Survival Evolved Valguero Map Guide: Resource Locations, Bosses, & Dinos
How Much You Should Be Tipping For Beauty Services - American Beauty Institute
Craigslist West Valley
Nordstrom Rack Glendale Photos
U Of Arizona Phonebook
8005607994
Ihub Fnma Message Board
Villano Antillano Desnuda
Truvy Back Office Login
Unreasonable Zen Riddle Crossword
Infinite Campus Asd20
Sinfuldeed Leaked
Florence Y'alls Standings
La Qua Brothers Funeral Home
Boondock Eddie's Menu
Ark Unlock All Skins Command
Final Exam Schedule Liberty University
Go Smiles Herndon Reviews
Emerge Ortho Kronos
Mcgiftcardmall.con
Temu Y2K
Fifty Shades Of Gray 123Movies
Gateway Bible Passage Lookup
2023 Fantasy Football Draft Guide: Rankings, cheat sheets and analysis
Giovanna Ewbank Nua
Natasha Tosini Bikini
60 Days From May 31
Autozone Battery Hold Down
Craigslist St Helens
Conan Exiles Colored Crystal
Ups Customer Center Locations
SF bay area cars & trucks "chevrolet 50" - craigslist
Wwba Baseball
Morbid Ash And Annie Drew
Spongebob Meme Pic
Nfsd Web Portal
Honeybee: Classification, Morphology, Types, and Lifecycle
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 6381

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.