QA Interview Hub · Knowledge base · Quizzes · Українська

Penetration Testing › Authentication: passwords, MFA, sessions, JWT

Sessions and JWT: fixation, signature, expiry

Session fixation and JWT mistakes give access without a password. In the first the session id stays the same after login, in the second the server trusts an unchecked token. After login or a privilege change a new id is issued. A typical JWT (JWS) is signed but not encrypted. The server verifies the signature with a fixed algorithm and exp; defects: alg none, RS256 to HS256 confusion, a weak secret.

Pentest · Authentication

The token is signed, so it cannot be forged. Why is that confidence sometimes wrong?

A user has logged in and from then on the server recognizes them by a session identifier or a token. If an attacker made a victim use an id the attacker already knows, or if the server trusts the data in a token without a proper signature check, the password is no longer needed. This lesson covers two related weaknesses: session fixation and typical JWT mistakes.

Session fixation. An attacker gets a not yet authenticated session id from the site (say A1), slips it to the victim (via a link with a parameter or a cookie from a subdomain) and waits for the victim to log in. If after login the server does not change the id, A1 becomes authenticated and the attacker already holds the key. The fix is simple: after any change of privilege level (login, MFA, privilege elevation) issue a new id and destroy the old one.

GET /login HTTP/1.1
HTTP/1.1 200 OK
Set-Cookie: sid=A1; HttpOnly

POST /login   Cookie: sid=A1
HTTP/1.1 302 Found
Set-Cookie: sid=A1

The test is simple: compare the id before and after login. If it is the same, it is a defect (ideally Set-Cookie after login returns a different value).

JWT. A JSON Web Token has three parts in base64url encoding separated by dots: a header (the algorithm), a payload (claims: sub, exp, a role) and a signature. The signature guarantees the data was not changed but does not encrypt it: anyone decodes the header and payload without a key. So secrets and excess personal data are not put in the payload.

The three parts of a token

Headeralg, typPayloadsub, exp, roleSignaturesealthe signature covers these twobase64url: readable without a key
Figure 1. The signature protects against changes but does not hide the content.

Typical implementation and configuration defects:

  • alg: none — the library accepts a token without a signature and the attacker writes any payload;
  • Algorithm confusion (RS256 to HS256) — the server expects an RSA signature but accepts HMAC and uses the public key as the HMAC secret, and everyone knows the public key;
  • A weak HMAC secret — it is guessed offline from a single captured token;
  • No exp or it is not checked — a stolen token works forever;
  • No revocation — a self-contained token works until it expires, even after logout or a password change;
  • Trusting token data without checking — the role from the payload is taken as is although the signature was not verified.

Example: a token that was changed. A genuine user’s token split into parts (shortened, without the signature):

eyJhbGciOiJIUzI1NiJ9
.eyJzdWIiOiI3Iiwicm9sZSI6InVzZXIifQ
.(signature)

After decoding these are the header {"alg":"HS256"} and the payload {"sub":"7","role":"user"}. A tester on a test system assembles a token with the role and the algorithm changed:

eyJhbGciOiJub25lIn0
.eyJzdWIiOiI3Iiwicm9sZSI6ImFkbWluIn0
.
GET /api/admin/users HTTP/1.1
Authorization: Bearer eyJhbGciOiJub25lIn0.eyJzdWIiOiI3Iiwicm9sZSI6ImFkbWluIn0.

The right answer is 401: the server verifies the signature with the expected algorithm and rejects any other. An answer of 200 means the token was forged without a key. The recommendations are collected in RFC 8725 (JWT Best Current Practices): set allowed algorithms explicitly, check iss and aud, do not trust data from the token header; exp is checked per the base RFC 7519. To protect tokens they are stored in an HttpOnly cookie instead of localStorage, and a short lifetime is combined with a refresh token that can be revoked.

A real case. In March 2015 researcher Tim McLean described critical vulnerabilities in JWT libraries on the Auth0 blog. Some of them treated a token with the algorithm none as having a verified signature, so an attacker could compose a token with any payload and access other people’s accounts. Another problem was RS256 to HS256 confusion: a server that expected RSA accepted an HMAC token and used the public key as the secret, so a forged token passed verification. Popular libraries for Node.js, Python and PHP were at risk; the fix was updates and an explicit algorithm parameter during verification. A lesson for QA: every authorization test suite has a “token with another alg” case and an “expired exp” case.

Limits and alternatives. A JWT is not better than a server-side session: a session is easy to revoke and small, while a JWT is convenient when different services verify the token without a shared store. But it is harder to revoke (a short lifetime, a revocation list, key rotation). A cookie token still needs CSRF protection and a token in localStorage is readable by any XSS script. The choice depends on architecture, and both options can be made safe.

Common mistakes.

  • Not changing the session id after login.
  • Treating the JWT payload as secret: everyone can read it and it has no place for passwords and sensitive data.
  • Not checking exp, aud and iss.
  • Taking the verification algorithm from the token’s own header.
  • A short HMAC secret like secret or the project name.

Check yourself

The login page issues the cookie sid=A1 and after login the server keeps sid=A1. How can an attacker use it and what to change?

Answer

The attacker slips the victim a known A1; the victim logs in and A1 becomes authenticated. A new id has to be issued after login (and after MFA or privilege elevation) and the old one destroyed.

A JWT is signed with HMAC and the secret secret. Why does the signature not protect here?

Answer

A weak secret is guessed offline from one captured token and then any payload is signed. The secret must be long and random and keys rotated.

In an interview. Junior: “A JWT is signed but not encrypted: everyone can read its content. The server must verify the signature, the algorithm and the expiry. After login a new session id must be issued to avoid fixation”. Middle/Senior: I add: alg none and RS256/HS256 confusion (the 2015 library example), a weak HMAC secret, revocation and refresh tokens, the choice between a session and a JWT, and RFC 8725.

More articles in this chapter