.NET

Fight Off Forgery Attacks

By Dino Esposito, April 09, 2010

Use ASP.NET MVC to fend off intruders

Modern browsers implement restrictions to fend off cross-site scripting (XSS) attacks. But there's another type of attack that is even easier to launch -- cross-site request forgery (CSRF). A CSRF attack consists of two distinct steps:

CSRF attackers set up a Web page with clickable elements that can trigger malicious operations. The fake page typically contains hidden script code and markup that collects data from a local computer and posts it to the attacker's server. As the victim triggers the malicious code, the HTML page performs a form post to the attacker's server. Because the post occurs from the victim's computer, authentication cookies on the machine are uploaded. As a result, the attacker is served cookies that may identify another user to some sort of target application. If successful, a CSRF attack lets intruders upload their own data through the victim's account and possibly gain full control over the victim's credentials.

How can you protect yourself? ASP.NET MVC offers one solution. In ASP.NET MVC, you publish server functionalities through a public method of a controller class. If the method is critical, you can add attributes to prevent CSRF attacks. To make this possible, ASP.NET MVC provides a helper method to generate some ad hoc HTML markup and a ValidateAntiForgeryToken attribute.

The HttpPost attribute requires a POST request to execute the method. This alone cuts off all possible requests made through a plain GET. Additionally, ValidateAntiForgeryToken instructs the invoker of the ASP.NET MVC method to look for some special content in the request body before executing the code.

ValidateAntiForgeryToken contains code that kicks in during the authorization phase of an action method request, ensuring the posted request contains a cookie and a form field with a common fixed name. If any of these items are missing, an exception is thrown; otherwise, the attribute ensures that the content of the cookie and the input field match.

How do you insert content into a Web page? By using this ASP.NET MVC HTML helper:

<%= Html.AntiForgeryToken() %>

The Html.AntiForgeryToken method creates a cookie on your machine and adds a hidden field like this to the form:

If the target of the form includes the ValidateAntiForgeryToken attribute, the content of the cookie and input field are matched before the action method is authorized. In this way, intruders can't create valid cookies because they don't know what content to put in them. And even if the victim's machine already contains an antiforgery cookie (because of a previous legitimate operation against the site), the content of the cookie can't be read via script to arrange a form input field on-the-fly. An antiforgery cookie, in fact, is HttpOnly and can't be accessed via script.

Not the definitive barrier, but a little thing that makes it harder for the bad guys.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!