keepcalculator

06 Numbers and Powers

Token Forgery & Replay Prevention Guide

Card No. 06·71660 · Updated · Standard formula, shown below

This guide explains how agencies and cloud providers can stop token forgery and replay attacks, which are two of the most common ways attackers bypass authentication and steal data. You will learn concrete steps to secure your token lifecycle—from generation to validation—and how to detect and respond to replay attempts. By the end, you will have a practical checklist you can implement in your own systems.

What You’ll Need

  • Access to your authentication/authorization server configuration (e.g., OAuth 2.0, OIDC, or custom JWT issuer)
  • Ability to modify application code or API gateway settings
  • A basic understanding of JSON Web Tokens (JWT) and how they are signed/verified
  • Logging and monitoring tools (e.g., SIEM, cloud watch logs) to track token usage

Step-by-Step

  1. Use strong, asymmetric signing algorithms. Replace symmetric algorithms like HS256 with RS256 or ES256 for JWTs. This ensures only the private key holder can sign tokens, and the public key is used for verification, preventing forgery if a single server is compromised.
  2. Set short token lifetimes and enforce expiry. Configure access tokens to expire in 5–15 minutes. Use refresh tokens with longer lifetimes (hours/days) but rotate them on every use. Validate the exp claim on every request and reject expired tokens with a 401 response.
  3. Include and verify the jti (JWT ID) claim. Generate a unique identifier for every token. Store it (hashed) in a denylist or allowlist until expiry. On each request, check that the jti has not been used before. This directly blocks replay attacks.
  4. Bind tokens to the client and session. Add claims like azp (authorized party) and sid (session ID). Verify these match the current client and session on every request. For APIs, require the same client certificate or API key that was used during token issuance.
  5. Use audience and issuer validation. Validate the aud claim matches your API’s identifier, and the iss claim matches your trusted token issuer. Reject tokens with mismatched values to prevent tokens issued for one service from being used on another.
  6. Implement token revocation. Maintain a revocation list (or use a distributed cache) for tokens that are compromised or logged out. Check this list before accepting a token. For stateless JWTs, this requires a database lookup, but it is essential for security.
  7. Add replay detection at the network layer. Use TLS 1.3 with 0-RTT disabled, and implement a nonce or timestamp in the token request. For OAuth, use PKCE (Proof Key for Code Exchange) to prevent authorization code interception and replay.
  8. Monitor and log token usage. Log every token validation attempt, including the jti, IP address, user agent, and timestamp. Set up alerts for unusual patterns, such as the same token used from multiple IPs in a short time or after expiry.
  9. Rotate signing keys regularly. Generate new key pairs periodically (e.g., every 30–90 days) and keep a grace period where old keys are still accepted for verification but not for signing. This limits the impact of a key compromise.
  10. Use a well-tested library or service. Do not implement crypto yourself. Use established libraries like jose, PyJWT, or a cloud provider’s managed identity service (e.g., AWS Cognito, Azure AD B2C) that already handles many of these protections.

Common Mistakes

  • Using HS256 with a shared secret. If any server leaks the secret, all tokens can be forged. Use asymmetric algorithms instead.
  • Setting token lifetimes too long. A 24-hour access token gives attackers a wide window to replay it. Keep access tokens short.
  • Not checking the jti. Without a unique ID check, a stolen token can be replayed indefinitely until expiry.
  • Ignoring the aud claim. If you don’t validate audience, a token meant for a different API could be accepted.
  • Storing tokens in plaintext logs. Never log the full token; log only a hash or the jti.
  • Relying on client-side checks. Always validate tokens server-side; never trust the client to enforce expiry or revocation.

Tips

  • Use a centralized authorization server (e.g., Keycloak, Auth0) to manage token issuance and revocation consistently across all services.
  • Implement a sliding session expiration for refresh tokens, but always rotate them—issue a new refresh token and invalidate the old one on each refresh.
  • For high-security environments, consider using mutual TLS (mTLS) to bind tokens to a client certificate.
  • Leverage cloud provider features: AWS WAF can help block replay by inspecting token claims, and Azure Front Door offers built-in token protection.
  • Regularly audit your token validation code with automated security scans and penetration testing.

Troubleshooting

Tokens are being rejected unexpectedly. Check the system clock on your servers—if it is out of sync, the exp and nbf claims will fail. Use NTP to synchronize.

Replay attacks still succeed. Verify that the jti check is actually being enforced. Many libraries allow you to disable it. Ensure your denylist is global and has low latency.

Performance issues with revocation list. Use a fast in-memory cache (Redis) with a TTL equal to token lifetime. If the list grows too large, consider using a bloom filter for quick checks.

Key rotation breaks existing tokens. Keep old public keys available for verification during a grace period. Do not delete them immediately.

Token forgery is suspected. Immediately rotate all signing keys and revoke all tokens. Investigate how the key was compromised—check server logs, access controls, and any third-party dependencies.

Frequently Asked Questions

  1. What is the difference between token forgery and replay? Forgery is creating a valid token without authorization (e.g., by stealing the signing key). Replay is using an already-issued valid token again after it has been used or expired.
  2. Can I prevent replay without a database? For stateless JWTs, you can use a short expiry and a nonce, but true replay prevention requires storing used jti values. A database or cache is necessary.
  3. Is it safe to use JWTs for sessions? Yes, but only if you implement all the protections: short expiry, jti check, audience validation, and revocation. Otherwise, JWTs are riskier than opaque session tokens.
  4. What is PKCE and why does it help? PKCE is a protocol for OAuth that adds a secret to the authorization code exchange, preventing an attacker who intercepts the code from exchanging it for a token.
  5. How often should I rotate signing keys? At least every 90 days, or immediately after any suspected compromise. Use a key management service to automate rotation.
  6. Can a replay attack be detected after the fact? Yes, by logging token usage and comparing timestamps, IPs, and jti values. If you see the same jti twice with different IPs, that is a replay.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →