<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://keepitlocked.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>KeepItLocked.net : ASP.NET, CSRF</title><link>http://keepitlocked.net/archive/tags/ASP.NET/CSRF/default.aspx</link><description>Tags: ASP.NET, CSRF</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP2 (Build: 20611.960)</generator><item><title>More ASP.NET CSRF Protection Options</title><link>http://keepitlocked.net/archive/2008/12/16/more-asp-net-csrf-protection-options.aspx</link><pubDate>Tue, 16 Dec 2008 21:39:00 GMT</pubDate><guid isPermaLink="false">a3f75fb5-0505-4d35-9795-aaa2ed659a71:63</guid><dc:creator>Alex</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://keepitlocked.net/rsscomments.aspx?PostID=63</wfw:commentRss><comments>http://keepitlocked.net/archive/2008/12/16/more-asp-net-csrf-protection-options.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://idunno.org/" mce_href="http://idunno.org/"&gt;Barry Dorrans&lt;/a&gt; created a filter for &lt;a href="http://en.wikipedia.org/wiki/Cross-site_request_forgery" mce_href="http://en.wikipedia.org/wiki/Cross-site_request_forgery"&gt;CSRF&lt;/a&gt; protection in ASP.NET. It's inspired by the &lt;a href="http://blog.codeville.net/2008/09/01/prevent-cross-site-request-forgery-csrf-using-aspnet-mvcs-antiforgerytoken-helper/" mce_href="http://blog.codeville.net/2008/09/01/prevent-cross-site-request-forgery-csrf-using-aspnet-mvcs-antiforgerytoken-helper/"&gt;ASP.NET MVC CSRF token&lt;/a&gt; approach. It's a simple and effective protection mechanism when you can't use the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.ui.page.viewstateuserkey.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.web.ui.page.viewstateuserkey.aspx"&gt;ViewStateUserKey&lt;/a&gt; because you've disabled ViewState. It doesn't rely on sessions either. Now if I could only get him to support GET requests on an opt-in basis! Check out &lt;a href="http://idunno.org/archive/2008/12/14/announcing-anticsrf-for-asp.net.aspx" mce_href="http://idunno.org/archive/2008/12/14/announcing-anticsrf-for-asp.net.aspx"&gt;his blog post&lt;/a&gt; and the code on &lt;a href="http://www.codeplex.com/AntiCSRF/Release/ProjectReleases.aspx" mce_href="http://www.codeplex.com/AntiCSRF/Release/ProjectReleases.aspx"&gt;Codeplex&lt;/a&gt;.&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://keepitlocked.net/archive/2008/12/16/more-asp-net-csrf-protection-options.aspx&amp;amp;;subject=More+ASP.NET+CSRF+Protection+Options" target="_blank" title = "Post http://keepitlocked.net/archive/2008/12/16/more-asp-net-csrf-protection-options.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://keepitlocked.net/archive/2008/12/16/more-asp-net-csrf-protection-options.aspx&amp;amp;;title=More+ASP.NET+CSRF+Protection+Options" target="_blank" title = "Post http://keepitlocked.net/archive/2008/12/16/more-asp-net-csrf-protection-options.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://keepitlocked.net/archive/2008/12/16/more-asp-net-csrf-protection-options.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://keepitlocked.net/archive/2008/12/16/more-asp-net-csrf-protection-options.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://keepitlocked.net/archive/2008/12/16/more-asp-net-csrf-protection-options.aspx&amp;amp;title=More+ASP.NET+CSRF+Protection+Options" target="_blank" title = "Post http://keepitlocked.net/archive/2008/12/16/more-asp-net-csrf-protection-options.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://keepitlocked.net/archive/2008/12/16/more-asp-net-csrf-protection-options.aspx&amp;amp;;title=More+ASP.NET+CSRF+Protection+Options" target="_blank" title = "Post http://keepitlocked.net/archive/2008/12/16/more-asp-net-csrf-protection-options.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://keepitlocked.net/archive/2008/12/16/more-asp-net-csrf-protection-options.aspx&amp;amp;;title=More+ASP.NET+CSRF+Protection+Options&amp;amp;;top=1" target="_blank" title = "Post http://keepitlocked.net/archive/2008/12/16/more-asp-net-csrf-protection-options.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://keepitlocked.net/aggbug.aspx?PostID=63" width="1" height="1"&gt;</description><category domain="http://keepitlocked.net/archive/tags/Software+Security/default.aspx">Software Security</category><category domain="http://keepitlocked.net/archive/tags/CSRF/default.aspx">CSRF</category><category domain="http://keepitlocked.net/archive/tags/ASP.NET/default.aspx">ASP.NET</category></item><item><title>ViewStateUserKey Doesn’t Prevent Cross-Site Request Forgery</title><link>http://keepitlocked.net/archive/2008/05/29/viewstateuserkey-doesn-t-prevent-cross-site-request-forgery.aspx</link><pubDate>Thu, 29 May 2008 21:30:00 GMT</pubDate><guid isPermaLink="false">a3f75fb5-0505-4d35-9795-aaa2ed659a71:26</guid><dc:creator>Alex</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://keepitlocked.net/rsscomments.aspx?PostID=26</wfw:commentRss><comments>http://keepitlocked.net/archive/2008/05/29/viewstateuserkey-doesn-t-prevent-cross-site-request-forgery.aspx#comments</comments><description>&lt;p&gt;ViewStateUserKey is not a completely effective mitigation against Cross-Site Request Forgery. It doesn't work for non post-backs (I.e. GET requests), and it doesn't work if the ViewState MAC is turned off.
&lt;/p&gt;&lt;p&gt;In &lt;a href="http://msdn.microsoft.com/en-us/library/ms972969.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms972969.aspx"&gt;several&lt;/a&gt;
		&lt;a href="http://www.guidanceshare.com/wiki/ASP.NET_2.0_Security_Guidelines_-_Parameter_Manipulation" mce_href="http://www.guidanceshare.com/wiki/ASP.NET_2.0_Security_Guidelines_-_Parameter_Manipulation"&gt;different&lt;/a&gt;
		&lt;a href="http://channel9.msdn.com/wiki/default.aspx/Channel9.ASPNETSecurityGuidelines" mce_href="http://channel9.msdn.com/wiki/default.aspx/Channel9.ASPNETSecurityGuidelines"&gt;places&lt;/a&gt;, we see a piece of advice repeated - use the ViewStateUserKey property to prevent One-Click Attacks. Often, this piece of advice is accompanied by the following code:
&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;
			&lt;span style="color: blue;"&gt;void&lt;/span&gt; Page_Init(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: teal;"&gt;EventArgs&lt;/span&gt; e)
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;    {
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;        ViewStateUserKey = Session.SessionID;
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;    }
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;What exactly does this code do? To understand it, we first need to look at the ViewState mechanism itself. The ViewState is an ASP.NET mechanism used to persist the value of web controls between post-backs. This allows a lot of the drag and drop, UI-driven ASP.NET architecture to function "auto-magically" by serializing and de-serializing data automatically on the fly.
&lt;/p&gt;&lt;p&gt;The ViewState is encoded and stored as a hidden field. This introduces security issues, because the value is under the control of the client. There may be a value stored in a field that you do not want someone to see and modify, like an admin-only control with the visible property set to false.
&lt;/p&gt;&lt;p&gt;ASP.NET helps us out by introducing two mechanisms to help protect the ViewState. ViewState MAC prevents tampering with the ViewState by introducing a separate Message Authentication Code that is verified when the ViewState is submitted. ViewState Encryption protects the ViewState confidentially by encrypting the ViewState value. By default, the ViewState MAC is enabled, and ViewState Encryption is not.
&lt;/p&gt;&lt;p&gt;The ViewStateUserKey property is an optional addition to the data used in ViewState MAC calculation. If that value changes between post-backs, the ViewState MAC calculation will fail and the page will cause an error.
&lt;/p&gt;&lt;p&gt;When we set the value of ViewStateUserKey to something associated with a particular user (like a Session ID or a Username), we are making sure that the ViewState is valid &lt;i&gt;only for that user.&lt;/i&gt;
	&lt;/p&gt;&lt;p&gt;Back to One-Click Attacks. One-Click Attack is sometimes incorrectly referred to as Microsoft's name for Cross-Site Request Forgery. However, this is not entirely correct.
&lt;/p&gt;&lt;p&gt;One-Click Attacks refer to CSRF attacks that use a malicious ViewState to perform the request. Because web forms developed with ASP.NET use ViewState for post-backs, the attacker can perform the post-back they want the user to perform unknowingly, and record the ViewState. Due to the way that ASP.NET ignores HTTP verbs when using &lt;span style="font-family: Courier New;"&gt;Request.Params&lt;/span&gt; versus &lt;span style="font-family: Courier New;"&gt;Request.Form&lt;/span&gt;, and in web controls, this request can often be made via GET.
&lt;/p&gt;&lt;p&gt;Example:
&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: Courier New;"&gt;http://site/Default.aspx?__VIEWSTATE=%2FwEPDwULLTExOTcyMDExODNkZAShpi32DKvqCd4uvHuQ%2FmmnBcdY&amp;amp;TextBox1=&lt;span style="color: red;"&gt;&lt;b&gt;&amp;lt;MALICIOUS_CONTROL_DATA_GOES_HERE&amp;gt;&lt;/b&gt;&lt;/span&gt;&amp;amp;Button1=Button&amp;amp;__EVENTVALIDATION=%2FwEWAwKEyYGZBwLs0bLrBgKM54rG3sCHijug9ibUUfHX808cCvcppg1i
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;This link can be used in a CSRF attack. It is then known as a one-click attack, because it uses the ViewState. This, however, is not:
&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: Courier New;"&gt;http://site/DeleteUser.aspx?user_id=123456789
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;If the page is not a post-back (as in the case of a direct link), the ViewState MAC is never checked. Several ASP.NET applications allow you to modify data without submitting a form. Consider &lt;a href="http://www.asp.net/mvc/" mce_href="http://www.asp.net/mvc/"&gt;ASP.NET MVC&lt;/a&gt; - it doesn't even use post-backs.
&lt;/p&gt;&lt;p&gt;Furthermore, the ViewState MAC can be disabled at the page level:
&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="background-color: yellow;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue;"&gt;@&lt;/span&gt;
			&lt;span style="color: maroon;"&gt;Page&lt;/span&gt;
			&lt;span style="color: red;"&gt;Language&lt;/span&gt;&lt;span style="color: blue;"&gt;="C#"&lt;/span&gt;
			&lt;span style="color: red;"&gt;EnableViewStateMac&lt;/span&gt;&lt;span style="color: blue;"&gt;="false"&lt;/span&gt;&lt;span style="background-color: yellow;"&gt;%&amp;gt;&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;or in &lt;span style="font-family: Courier New;"&gt;web.config&lt;/span&gt;.:
&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;pages&lt;/span&gt;&lt;span style="color: blue;"&gt;
			&lt;/span&gt;&lt;span style="color: red;"&gt;enableViewStateMac&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;false&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;gt;      
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;pages&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;ViewState MAC is often disabled for (perceived) performance reason, or (more likely) if there is some functionality in the application that causes the ViewState MAC to cause error.
&lt;/p&gt;&lt;p&gt;This reminds of &lt;a href="http://keepitlocked.net/archive/2007/10/30/asp-net-validaterequest-and-the-html-attribute-based-cross-site-scripting.aspx" mce_href="http://keepitlocked.net/archive/2007/10/30/asp-net-validaterequest-and-the-html-attribute-based-cross-site-scripting.aspx"&gt;problems with Request Validation is ASP.NET&lt;/a&gt; - the mechanism works, sometimes. This can be dangerous, because people rely on it and can get burned. The solution is to write something similar to&lt;a href="http://www.owasp.org/index.php/Category:OWASP_CSRFGuard_Project" mce_href="http://www.owasp.org/index.php/Category:OWASP_CSRFGuard_Project"&gt; CSRFGuard&lt;/a&gt; from &lt;a href="http://www.owasp.org/index.php/Main_Page" mce_href="http://www.owasp.org/index.php/Main_Page"&gt;OWASP&lt;/a&gt; in .NET. I have a feeling that the newly resurgent &lt;a href="http://www.owasp.org/index.php/Category:OWASP_.NET_Project" mce_href="http://www.owasp.org/index.php/Category:OWASP_.NET_Project"&gt;OWASP .NET project&lt;/a&gt; will add this to their to-do list.&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://keepitlocked.net/archive/2008/05/29/viewstateuserkey-doesn-t-prevent-cross-site-request-forgery.aspx&amp;amp;;subject=ViewStateUserKey+Doesn%e2%80%99t+Prevent+Cross-Site+Request+Forgery" target="_blank" title = "Post http://keepitlocked.net/archive/2008/05/29/viewstateuserkey-doesn-t-prevent-cross-site-request-forgery.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://keepitlocked.net/archive/2008/05/29/viewstateuserkey-doesn-t-prevent-cross-site-request-forgery.aspx&amp;amp;;title=ViewStateUserKey+Doesn%e2%80%99t+Prevent+Cross-Site+Request+Forgery" target="_blank" title = "Post http://keepitlocked.net/archive/2008/05/29/viewstateuserkey-doesn-t-prevent-cross-site-request-forgery.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://keepitlocked.net/archive/2008/05/29/viewstateuserkey-doesn-t-prevent-cross-site-request-forgery.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://keepitlocked.net/archive/2008/05/29/viewstateuserkey-doesn-t-prevent-cross-site-request-forgery.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://keepitlocked.net/archive/2008/05/29/viewstateuserkey-doesn-t-prevent-cross-site-request-forgery.aspx&amp;amp;title=ViewStateUserKey+Doesn%e2%80%99t+Prevent+Cross-Site+Request+Forgery" target="_blank" title = "Post http://keepitlocked.net/archive/2008/05/29/viewstateuserkey-doesn-t-prevent-cross-site-request-forgery.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://keepitlocked.net/archive/2008/05/29/viewstateuserkey-doesn-t-prevent-cross-site-request-forgery.aspx&amp;amp;;title=ViewStateUserKey+Doesn%e2%80%99t+Prevent+Cross-Site+Request+Forgery" target="_blank" title = "Post http://keepitlocked.net/archive/2008/05/29/viewstateuserkey-doesn-t-prevent-cross-site-request-forgery.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://keepitlocked.net/archive/2008/05/29/viewstateuserkey-doesn-t-prevent-cross-site-request-forgery.aspx&amp;amp;;title=ViewStateUserKey+Doesn%e2%80%99t+Prevent+Cross-Site+Request+Forgery&amp;amp;;top=1" target="_blank" title = "Post http://keepitlocked.net/archive/2008/05/29/viewstateuserkey-doesn-t-prevent-cross-site-request-forgery.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://keepitlocked.net/aggbug.aspx?PostID=26" width="1" height="1"&gt;</description><category domain="http://keepitlocked.net/archive/tags/Software+Security/default.aspx">Software Security</category><category domain="http://keepitlocked.net/archive/tags/CSRF/default.aspx">CSRF</category><category domain="http://keepitlocked.net/archive/tags/ASP.NET/default.aspx">ASP.NET</category></item></channel></rss>