electronic-code-book-ecb
Electronic Code Book (ECB)
ECB (Electronic Code Book) encryption can be abused to tamper with authentication cookies and gain access to another user’s account without knowing the encryption key.
If the target application encrypts authentication data using ECB and stores it directly in a cookie. Because ECB encrypts each block independently, it introduces critical weaknesses that attackers can exploit.
What Is ECB
ECB splits plaintext into fixed-size blocks and encrypts each block independently using the same key.
Key Weaknesses of ECB
- Identical plaintext blocks → identical ciphertext blocks
- Encrypted blocks can be:
- Removed
- Reordered
- Reused
- No integrity or authenticity protection
Detecting the Vulnerability
Static Authentication Cookie
- Logging in multiple times returns the same cookie
- Indicates:
- No randomness
- No session invalidation
- Cookie is likely reusable forever
Cookie Encoding
The cookie is:
- URL-encoded
- Base64-encoded
Example indicator:
%3D%3D → ==
After decoding, the output is binary, meaning the data is encrypted—not signed.
Identifying ECB in Use
Comparing Similar Users
Create two users:
test1 / passwordtest2 / password
Decoded cookies show:
- Large portions of ciphertext are identical
- Only blocks corresponding to username differ
This confirms block-based encryption.
Finding the Block Size
Create a user with long repetitive input:
- Username:
aaaaaaaaaaaaaaaaaaaa - Password:
aaaaaaaaaaaaaaaaaaaa
Decoded cookie shows repeated 8-byte patterns.
So, our ECB block size = 8 bytes
Understanding the Cookie Structure
By testing different username/password lengths, we infer:
[ username ][ delimiter ][ password ]
<strong># OR
</strong>[ password ][ delimiter ][ username ]
Key findings to notice using different length too:
- Delimiter size: 1 byte
- Password is not used when validating the cookie
- Only the username portion matters for authentication
Exploitation Techniques
Exploitation by Removing Blocks
We can forge a cookie that decrypts to:
admin[delimiter]
Steps:
-
Create a user:
aaaaaaaaadmin -
Decode the cookie
-
Remove the first 8 bytes (ECB block boundary)
-
Re-encode the remaining ciphertext
-
Replace the cookie in the browser
It allows you to login as admin.