Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insecure Cookie Encryption due to usage of insecure mode of operation when doing AES cipher #759

Open
JAckLosingHeart opened this issue May 27, 2024 · 0 comments

Comments

@JAckLosingHeart
Copy link

Hi team,

There might be a potential security issue in ninja-core/src/main/java/ninja/utils/CookieEncryption.java (ninja-core 7.0.0 the latest version) that I want to report and check with you guys.

Security risk:

In the function encrypt(), when AES is specified as the cipher algorithm without any more settings, AES/ECB/PKCS5Padding is used by default
image

image

However, ECB as a block cipher mode is not secure, encrypting each block independently without any IV.
Patterns in the plaintext can be easily observed in the ciphertext if similar blocks are present, which is a significant security weakness
In Ninja case the weakness could lead to leakage of sensitive information in session data when encryption mode is used

Proof Of Concept:

I will use the unit test case under src/test/java/ninja/utils/CookieEncryptionTest.java here as an example
We got 16 'a's, 16 'b's, 16 'c's, and another 16 'b's in the end, which's 64 characters in total as a string to encrypt
image

As said before we'll see the pattern in the ciphertext as well.
Encrypt it and check the cipher text before Base64 encoding:
image

image

As we can see, same plaintext block generates identical 16 bytes cipher text block

Recommendation:

Could specify cipher mode explicitly and consider using more secure cipher modes. Only for example which might not apply to this case:

SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/GCM/PKCS5Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, key);

That's pretty much the security issue I found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant