Data Integrity Failures
How web applications maintain sessions, why trusting client-side data fails, and how the JWT "none" algorithm bug lets an attacker forge a token.
Cookies and Session Tokens
When a user logs in, they're usually assigned a session token that the browser stores for as long as the session lasts. The token is repeated on each request so the application knows who we are. These tokens come in many forms but are usually set via cookies. Cookies are key-value pairs a web application stores in the user's browser and that the browser automatically repeats on each request to the site that issued them.
For example, a webmail application could assign a cookie containing the user's username after login, and the browser would send that username on every request so the application knows who is connecting. Security-wise this is a terrible idea: cookies live in the user's browser, so a user who tampers with the cookie and changes the username could impersonate someone else and read their mail. The application would suffer a data integrity failure, trusting data an attacker can tamper with.
JSON Web Tokens
One fix is an integrity mechanism that guarantees the cookie hasn't been altered. Rather than reinventing the wheel, we can use a token implementation that handles the cryptography for us. One such implementation is JSON Web Tokens (JWT).
JWTs are simple tokens that store key-value pairs while providing integrity as part of the token. You can hand users a token knowing they can't alter the key-value pairs and still pass the integrity check. A JWT is formed of 3 parts:
The header contains metadata indicating this is a JWT and the signing algorithm in use is HS256. The payload contains the key-value pairs the application wants the client to store. The signature is similar to a hash, taken to verify the payload's integrity. If you change the payload, the application can see the signature no longer matches and know you tampered with the JWT. Unlike a simple hash, the signature uses a secret key held only by the server, so without that key you can't produce a matching signature for a changed payload.
Each of the 3 parts is just plaintext encoded with base64. You can use this online tool to encode/decode base64. Try decoding the header and payload of this token:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6Imd1ZXN0IiwiZXhwIjoxNjY1MDc2ODM2fQ.C8Z3gJ7wPgVLvEUonaieJWBJBYt5xOph2CpIhlxqdUw
Note: The signature contains binary data, so decoding it won't make much sense anyway.
JWT and the None Algorithm
A data integrity failure existed in some JWT libraries a while ago. JWT uses a signature to validate the payload's integrity, but the vulnerable libraries let attackers bypass that validation by changing two things:
- Modify the header section so the
algheader holds the valuenone. - Remove the signature part.
Taking the earlier JWT as an example, to change the payload so the username becomes "admin" with no signature check, we decode the header and payload, modify them, and encode them back. Note that we remove the signature but keep the dot at the end.
Let's walk through the process in an example scenario. Navigate to http://10.10.154.77:8089/ and follow the questions below.
Try logging into the application as guest. What is guest's account password?
Correct Answer
Hint
After a successful login you should have a JWT stored as a cookie. Press F12 to open the Developer Tools. Depending on your browser, you can edit cookies from these tabs:
Firefox
Chrome
Forging the Token
Base64 for encoding the JSON and putting it into an HTTP request to pass the attack:
{ "typ":"JWT","alg":"none" }
eyAidHlwIjoiSldUIiwiYWxnIjoibm9uZSIgfQ
{ "username":"admin","exp":165076836 }
eyAidXNlcm5hbWUiOiJhZG1pbiIsImV4cCI6MTY1MDc2ODM2IH0=
Combined: eyAidHlwIjoiSldUIiwiYWxnIjoibm9uZSIgfQ.eyAidXNlcm5hbWUiOiJhZG1pbiIsImV4cCI6MTY1MDc2ODM2IH0.