Lakshmi is a backend developer at a Coimbatore startup that runs a prepaid wallet for canteens in engineering colleges. Students top up from an app. The app calls an API with a request that says, in effect, “add ₹500 to wallet 4471”, and the request carries a signature so the server knows it came from the real app and was not altered on the way.
The design looks careful. The signature is solid, the connection uses TLS, and the test suite passes. Then a security-minded intern asks a question that makes the room go quiet: what happens if the same request, byte for byte, arrives a second time?
The answer, in Lakshmi’s system, is that the server checks the signature, finds it valid, and adds another ₹500. Nothing in the request says “this is the first time”. The signature proves who wrote the message and that it was not changed. It says nothing about whether the message is fresh. That gap is what a replay attack uses.
Where would an attacker even get the request?
Lakshmi’s first reaction is that TLS makes capture impossible. Mostly true on the wire. But a request can be copied from other places: a debugging proxy on a test phone, a log file that stored full request bodies, a corporate TLS-inspection box, a malicious browser extension, or a rooted device. The attacker needs no key and no ability to alter anything. They only need to resend. Conceptually it is like photocopying a signed cheque. The signature on the copy is genuine.
Not every replay makes money for the attacker. Sometimes the harm is nuisance, such as repeated “send OTP” calls that run up SMS costs, or repeated “submit order” calls. Sometimes it is serious: a replayed login token, a replayed command to a door controller, a replayed money movement. Even a harmless-looking duplicate becomes a headache when it corrupts inventory or accounting.
Making each request usable once
The team works out three building blocks, which are the standard answers.
A timestamp inside the signed content. The server rejects any request whose time is more than a few minutes away from its own clock. A captured request becomes useless once it ages past that window. The catch is clock drift, so the window must be generous enough for phones with wrong clocks, but not so generous that it leaves a long replay gap. Five minutes is a common starting point, though the right value depends on your users.
A nonce, meaning a number used once. Each request carries a unique random value, also inside the signed content. The server stores the nonces it has seen for as long as the timestamp window lasts, and rejects any repeat. Combined with the timestamp, storage stays bounded: after the window passes, old nonces can be forgotten, since the timestamp check already rejects those requests.
An idempotency key, which solves a related but different problem. Suppose a student’s phone loses signal after sending the top-up and the app retries. That duplicate is not an attacker; it is an honest retry. If each top-up carries a client-generated unique ID, and the server remembers the outcome for that ID, a retry returns the earlier result instead of creating a second credit. Payment APIs use this pattern widely, and it covers both accidents and replays of money-moving actions.
The parts that bite later
Lakshmi’s team hits two snags. The first: the nonce store. With several API servers behind a load balancer, each one remembering nonces only in its own memory means an attacker can replay against a different server. The store has to be shared, a fast key-value store with expiry, for example. The second: the timestamp and nonce must be part of what is signed. If they travel outside the signature, an attacker simply swaps in fresh values and the old signature still verifies.
Related situations follow the same logic. Login sessions have this issue when a stolen session token is replayed, which is why tokens expire and why sensitive actions ask for re-authentication. One-time passwords are, as the name says, defences against replay: each code is valid once or for a short slice of time. Challenge-response login works the same way. The server sends a fresh random challenge, and the client proves knowledge of a secret by answering that specific challenge, so recording an old answer helps nobody. The wireless key fob on a car faces a similar problem and uses rolling codes for the same reason.
Replay is also a good example of why people say “signing is not the same as authorising this action now”. Freshness is a separate property, and cryptography will not give it to you unless you build it in.
What Lakshmi shipped
The final design added a timestamp, a nonce and a top-up reference ID to the signed body, a five-minute window, and a shared expiring store. They also changed their logging so full request bodies and signatures were never written to disk. A week later the intern ran the old duplicate-request test again and got a clean rejection on the second call. The same test, run with a deliberately retried request carrying the same reference ID, returned the original success without crediting twice, which is the behaviour an honest retry deserves.
Figures and names in the examples are illustrative.
Leave a Reply