Replay attacks are very popular against public APIs.
What is a replay attack ?
When an attacker intercepts a valid HTTP request to your API and then replay the same request again and again tricking your API into thinking that it is a valid HTTP request coming from your user.
Protection
Here is a very simple method that I use to protect my APIs from Replay Attacks.
Client side steps
1. Generate a unique token on server for the device when app is used for the first time or if app requires login then generate token on successful login.
2. Whenever app makes a call to server it adds the following in HTTP headers
- Token
- 128 characters long random value (call it Random1)
- Current Unix Timestamp of device
- SHA256 of "Token+|||+Random1+|||+Timestamp" (call this result Random2)
Server side steps
When a HTTP request is received by any API it takes the following steps to verify the validity of request
- Check if Token is a valid token that exists in database (you can implement expiry mechanism to make it more secure)
- Check if Timestamp is valid
- Calculate SHA256 of "Token+|||+Random1+|||+Timestamp" and compare it with Random2 present in request headers.
- Check if this SHA256 result does not already exists in your database
- Save SHA256 result in database
You can make it more secure by adding more random numbers. In my example you can see I have 3 pipes hardcoded twice (Token+|||+Random1+|||+Timestamp). You can make number of pipes random and then pass those random numbers in request headers.
Comments
Post a Comment
Share your wisdom