Cross-site Request Forgery (CSRF/XSRF), also sometimes called sea surf or session riding, refers to an attack against authenticated web applications using cookies. The attacker is able to trick the victim into making a request that the victim did not intend to make. Therefore, the attacker abuses the trust that a web application has for the victim’s browser. While Cross-site Request Forgery (CSRF) attacks do not provide an attacker with the response returned from the server, a clever attacker could cause a fair amount of damage, especially when paired with well-crafted social engineering attack on administrative users.

Cross-site Request Forgery (CSRF) is a type of confused deputy attack, which leverages the authentication and authorization of the victim when a forged request is being sent to the web server. Therefore, a CSRF vulnerability that affects highly privileged users, such as administrators, could result in a full application compromise. During a successful CSRF attack, the victim’s web browser is tricked by a malicious website into unwanted action – it sends HTTP requests to the web application as intended by the attacker. Normally, such a request would involve submitting forms present on the web application to alter some data.

Upon sending an HTTP request (legitimate or otherwise), the victim’s browser will include the cookie header. Cookies are typically used to store a user’s session identifier so that the user does not have to enter their login credentials for each request, which would obviously be impractical. If the victim’s authentication session is stored in a session cookie that is still valid (a browser window/tab does not necessarily need to be open), and if the application is vulnerable to Cross-site Request Forgery (CSRF), then the attacker can leverage CSRF to launch any desired malicious requests against the website and the server-side code is unable to distinguish whether these are legitimate requests.

CSRF attacks can, for example, be used to compromise online banking by forcing the victim to make an operation involving their bank account. CSRF can also facilitate Cross-site Scripting (XSS). Therefore, CSRF should be treated as a serious web application security issue, even though it is not currently included in the OWASP Top 10.

Cross-site Request Forgery in GET Requests

The following is a simple example of how Cross-site Request Forgery (CSRF) can be abused in GET requests through the use of the <img> tag.

<img src="https://example.com/changePassword.php/?newPassword=attackerPassword">

The above is a CSRF attack using an HTTP GET request. If the victim visits a web page controlled by the attacker with the following payload, the browser will send a request containing the cookie to the attacker-crafted URL.

Cross-site Request Forgery in POST Requests

GET requests, however, are not the only HTTP method an attacker can abuse. POST requests are equally susceptible to Cross-site Request Forgery (CSRF), however, an attacker will need to make use of a little bit of JavaScript to submit the POST request.

The following is a simple example of how CSRF can be abused using POST requests through the use of an <iframe> tag. This code would be loaded in an iframe which is made invisible to the victim.

iframe definition

<iframe src="https://attacker.com/csrfAttack" style="width:0;height:0;border:0;border:none;"></iframe>

iframe HTML contents

<body onload="document.getElementById('csrf').submit()">
  <form id="csrf" action="http://example.com/changePassword.php" method="POST">
    <input name="newPassword" value="attackerPassword" />
  </form>
</body>

CSRF Protection

There are two primary approaches to prevent Cross-site Request Forgery (CSRF) – synchronizing the cookie with an anti-CSRF token that has already been provided to the browser or preventing the browser from sending cookies to the web application in the first place.

Anti-CSRF Tokens

The recommended and the most widely used prevention technique for Cross-site Request Forgery (CSRF) attacks is known as an anti-CSRF token, sometimes referred to as a synchronizer token or just simply a CSRF token. When a user submits a form or makes some other authenticated request that requires a cookie, a random token should be included in the request. The web application will then verify the existence and correctness of this token before processing the request. If the token is missing or incorrect, the request can be rejected.

It’s highly recommended that you use an existing, well tested and reliable anti-CSRF library. Depending on your language and framework of choice, there are several high-quality open source libraries that are ready-to-use. The characteristics of a well designed anti-CSRF system involve the following attributes:

  • The anti-CSRF token should be unique for each user session
  • The session should automatically expire after a suitable amount of time
  • The anti-CSRF token should be a cryptographically random value of significant length
  • The anti-CSRF token should be cryptographically secure, that is, generated by a strong pseudo-random number generator (PRNG) algorithm
  • The anti-CSRF token can be added as a hidden field for forms or within URLs (only necessary if GET requests cause state changes, that is, GET requests are not idempotent)
  • The server should reject the requested action if the anti-CSRF token fails validation

SameSite Cookies

The SameSite cookie attribute is a new attribute that can be set on cookies to instruct the browser to disable third-party usage for specific cookies. The SameSite attribute is set by the server when setting the cookie and requests the browser to only send the cookie in a first-party context. Therefore, the request has to originate from the same origin – requests made by third-party sites will not include the SameSite cookie. This effectively eliminates Cross-site Request Forgery attacks without the use of synchronizer tokens. Unfortunately, this method of CSRF protection is not yet effective in all web browsers but .

Source: https://www.acunetix.com/blog