The number of cyber attacks increase enormously each day. Therefore security is a must. If you web application is not protected, it might be vulnerable to CSRF or Cross Site Request Forgery.
What is Cross Site Request Forgery?
When an unwanted action is forced to perform on a trusted site which the user is currently authenticated to , it's called a CSRF.
How to protect from CSRF.
To apply this protection against a CSRF scenario, we should have a web application. Suppose we have the server side and the client side of the web app as server.php and index.php in order.
First of all we sould create a session in the client side and set a cookie to store the session id. The cookie will be used for validating the token when we go along the process.
Now when we run the client side, we get a session for the user and the ID will be saved under the sessionID variable.
Now all we want is a CSRF Token. this should be saved in the server side of the web application.
First we generate a key if there is none and then the key is used to generate the CSRF Token. We use sha256 to encrypt this token.
We now have to ask the server to get the CSRF Token when the client page is being loaded. We can use a AJAX and JavaScript combination to do this transfer to the server in the background.
To do this we need to send a request to the server side and then retrieve the CSRF Token sent from the server when loading the client side page.
loadDOC function to retrieve response value from server side
This value must be sent back to the server side at the moment when the user clicks the login button.
After the button click, all the values filled in the form along with the user session id and the user CSRF value will be sent to the server.php which is our server side.
LoginValidate() function
This function is there to validate the login credentials the user enters when accessing. There are 4 arguments, one to accept the username, $password to retrieve the password, $user_CSRF to capture the CSRF Token from the user side and the $user_SessionID to get the session id from the client side. All these are cross matched with the server side session id, csrf token and the username password list available in the database.
If all arguments return true only, the server will prompt a login success alert box.
This function is there to validate the login credentials the user enters when accessing. There are 4 arguments, one to accept the username, $password to retrieve the password, $user_CSRF to capture the CSRF Token from the user side and the $user_SessionID to get the session id from the client side. All these are cross matched with the server side session id, csrf token and the username password list available in the database.
If all arguments return true only, the server will prompt a login success alert box.
This is how we will achieve in cross site request forgery (CSRF) prevention through Synchronizer Token Patterns

Comments
Post a Comment