Cross-Site Request Forgery (CSRF) occurs when an attacker makes a victim perform malicious activity by making a user of a web application visit another website while browsing the application. This makes it cross-site, and it happens because the browser sends the cookies of that particular domain along with the request. This behaviour of the browser will enable the attacker to take advantage, and perform illegitimate requests using the Session Id of the user's session which is valid, making it impossible for the application to recognize whether it is a legitimate request or not.
For example, attackers can make users visit a gossip site by sending a link while the user is browsing a site, and this gossip link could lead to a page which has an image tag which does not really show any image, but contains a link to another site as in the tag mentioned below.
<img src="https://..." width="0" height="0">
The image will not be visible as the width and height is zero, but the link will be called.
The attackers can also shorten the correct URL of a website to make the link look legitimate and trustable.
Making server shutdown requests, or unfriending people on a social website are some other examples of the malicious activities that can be performed.
This is possible only because the request made is a GET request. Therefore, any state-changing operation in the system which either can change data or the behaviour of the system must not be implemented using the HTTP GET method. Instead, methods like POST and PUT must be used.
Although some attack scenarios can be prevented by following this security best practice, it is still possible for attackers to attack through POST and PUT methods in certain circumstances.
For instance, an attacker can make an Administrator create a user account for the attacker by sending a link to the admin which will redirect the admin to a page with a form with hidden fields. This form will contain user information which will not be visible to the admin, and this form may contain a misleading button which will visible to the user and make the user submit the form, or a button click event may be triggered through JavaScript when the form loads, making the user submit the form with a valid Session Id sent along with the request. Since session cookie goes along with the request, as mentioned before, the server will not be able to distinguish whether the request is legitimate or not.
The picture included below illustrates the basic flow of this process I have explained above:
There are several ways that can be used to protect web applications from CSRF. Synchronized Token Pattern and Double Submit Cookies Pattern are two of these methods I will discuss in the future.