Synchronizer Token Pattern
This blog post will be explaining about how Synchronizer Token Pattern can be used to prevent CSRF attacks.
What is Synchronizer Token Pattern?
Synchronizer Token Pattern (STP) is a technique that uses a token (unique secret value) for each request. This token is embedded in all HTML forms and verified on the server side. Because of this random value, an attacker will not be able to place a correct token in their forged request, thus making them unable to perform CSRF attacks.
How does it work?
To explain how Synchronizer token patterns work, let us take a look at the following example (Source code can be obtained here).
Upon running the web application, we are greeted to a login page, we login using the hardcoded credentials:
STP example web application |
Username : user
Password : csrf1
Before continuing with the flow let us take a look at the token class implemented as shown below:
Token class and its methods
As shown above, the token class has three methods implemented in it:
- GenerateToken : This method generates a random value using OpenSSL pseudo random bytes. the randomness of this value is further enhanced by base64_encode and the resulting value is set as the token.
- ObtainTokenByID : This method takes the session ID of the current session and searches for the token stored in the server side. It finds the token that is mapped to the session ID and return it.
- CompareTokens : This method compares the received token from the form with the token stored in the server side.
When we enter our login credentials, the following actions are performed:
Actions being performed upon login |
As seen in the above code section, when the user enters their credentials and logs in, a session identifier and it is set as a cookie named sessionID in the browser.
At the same time a token is generated using the generateToken() method of the token class. The generated session ID and token are mapped together and stored in the server side in a text file (tokens.txt).
Session IDs and their corresponding Tokens saved in the server side |
After the token is saved in the server side, we will be redirected to the update status page.
In the above screenshot we can see the sessionID cookie is set with its value containing the session identifier. This page contains a POST form which asks for a status input. It also contains a hidden form which will contain the value of the token corresponding to the session ID.
Update status form |
In the above screenshot we can see the sessionID cookie is set with its value containing the session identifier. This page contains a POST form which asks for a status input. It also contains a hidden form which will contain the value of the token corresponding to the session ID.
In this example we have implemented an endpoint which accepts HTTP POST requests and responds with the token. The code below shows how the token is received using an Ajax call.
Update Status page source code which contains hidden token |
Endpoint which responds with the token |
In this session our session ID is "09nik6iq9lkhl4liiu9apfef46". The figure below shows which token is mapped to this session ID in tokens.txt. This token is called in using the above shown Ajax call and inserted into the hidden token form.
Token mapped to the current session ID |
Token added as the hidden field value |
Once we write a status and submit the form, the next page will compare the submitted token value with the token stored in the server side by calling the CompareTokens() method in the token class:
Comparing tokens and displaying either a success or error message |
The two tokens are compared and if they match, the page will display a success message. If they do not match, the page will display an error message.
Success Message |
Error Message
|
My next blog post is a similar presentation of how the Double Submit Cookie Pattern can be used to protect against CSRF attacks. It can be viewed here.
Comments
Post a Comment