Introduction

This article explains the Cross Request Forgery Attacks in ASP.NET Web API. Basically, it is a type of attack in which the attacker exploits the trust of a website on the user.

How does it Work

The CSRF attacks are based on the site's trust of the user's input. It is a malicious exploit type for the website in which the unauthorized commands are transmitted from a user that the website trusts. The Attacker attempts to get the authenticated users which click on the link for submitting the data without the user actually realizing it.

The sample code of CFRS attacks is:

When the website uses the Authentication cookie there is an additional possibility for CSRF attacks against the website. It is done because the browser sends all the cookies to the destination website. CSRF attacks are not limited to the exploitation of cookies. Digest and basic authentication are vulnerable. When the client logs in using basic and digest authentication the browser sends the reference until the end of the session.

Limitations

The attacker cannot see what is sent back by the target website to the victim as a response to the request and they are less vulnerable to exploitation by cross-site scripting.

Anti-forgery Tokens

Anti-forgery tokens can protect the websites from CSRF attacks. These tokens are also called "verification tokens".

Example of hidden form field token:

  1. <form action="/Home/Check" method="post">
  2. <input name="RVToken" type="hidden"
  3. value="6fGBtLZmVBZ59oUad1Fr33BuPxANKY9q3Srr5y[...]" />
  4. <input type="submit" value="Click Me" />
  5. </form>

To protect against a CSRF attack, use the anti-forgery token with any authentication protocol. The browser then sends the references after logging in the user. It includes the form authentication and cookie-based authentication protocol.

We have a need for the Anti-forgery token for the unsafe methods. These methods may be GET, POST, DELETE and PUT. We use the method for confirmation that the safe method does not have any side-effects. If enabled the cross-domain support like CORS and JSONP, then even safe methods are potentially vulnerable to CSRF attacks. And it allows the attacker to read the potentially sensitive data.

Anti-forgery Tokens in ASP.NET MVC.

When we add the Anti-forgery token in the Razor page we use the "HtmlHelper.AntiForgeryToken" method.

  1. @using(Html.BeginForm("Arrange" , "Money"))
  2. {
  3. @Html.AntiForgeryToken()
  4. }