What is cross-site request forgery (CSRF)?
Cross-Site Request Forgery (CSRF) is an attack that forces an end user to execute unwanted actions on a web application in which they're currently authenticated. CSRF attacks specifically target state-changing requests, not theft of data, since the attacker has no way to see the response to the forged request.
We can avoid these kind of attack (CSRF) by two ways
1. Synchronizer Tokens
2. Double submitted cookies
1. Synchronizer Tokens
2. Double submitted cookies
In this case, we are considering about Synchronize Tokens. When we login, there will be a random number generated and stored on both the client side and server side. So when the attacker creates the code, he wont be knowing the token value. So he can not create a code with the correct token value. Anyhow, if the victim clicks the link, that will not be harmful. because the token verification will fail.
For the first step we want to create the session in the client side (index.php). Then we want create a cookies and set session id to the cookie. It shown the below figure (figure 1).

Figure 01
After executing the code, a cookie(session_id) will be created and which will contain the 'current session ID' of the client as shown in figure 02.

Figure 02
Now we should request to the server to get the CSRF token when client page is loaded. (Using Ajax we can send data to the server in the background). So we have to create a file called 'config.js' and create a function there. This function's work is to send a request to server side and grab CSRF Token and store it in the 'hidden DOM' field on the client side when the page is loaded. The code for the function is shown as in figure 3.

Figure 03
Now the 'index.php' has to be called.

Figure 04
If the user press the login button, it will redirect to 'server.php' page. So in this page we have to do all the validations in server side such as login. Code for login validate is shown in Figure 5.
Comments
Post a Comment