简体   繁体   English

为什么CSRF从OWASP前10名中删除,如何防止ASP.NET mvc上的CSRF?

[英]why CSRF is removed from OWASP top 10, how to prevent CSRF on ASP.NET mvc?

Currently HP fortify scans our ASP.NET MVC code and show me some CSRF problems 目前HP强化扫描我们的ASP.NET MVC代码并向我展示一些CSRF问题

I try to search some information on OWASP, and find CSRF is already removed from OWASP TOP 10 with the reason of "More frameworks offering secure-by-default settings and some form of protections" 我尝试搜索有关OWASP的一些信息,并发现CSRF已经从OWASP TOP 10中删除,其原因是"More frameworks offering secure-by-default settings and some form of protections"

So, what is the protection on CSRF on ASP.NET MVC ? 那么,ASP.NET MVC对CSRF的保护是什么?

if we don't add [ValidateAntiForgeryToken] attribute on the Controller, Does that mean, the controller still vulnerable to CSRF attack ? 如果我们不在Controller上添加[ValidateAntiForgeryToken]属性,这是否意味着,控制器仍然容易受到CSRF攻击?

Using ASP.Net MVC as an example, CSRF protection on the platform has in the past needed two things: an anti-forgery token that must be included with every form (ie: @Html.AntiForgeryToken() ), and a [ValidateAntiForgeryToken] attribute decorating controller actions for those forms. 以ASP.Net MVC为例,平台上的CSRF保护过去需要两件事:一个必须包含在每个表单中的防伪标记(即: @Html.AntiForgeryToken() )和一个[ValidateAntiForgeryToken]属性装饰控制器对这些表单的操作。 These two items, in combination with a same-site cookie policy now active in all major browsers, generally mitigate against CSRF. 这两项与现在在所有主流浏览器中都处于活动状态的同站点cookie策略相结合,通常可以缓解CSRF。 However, these were postive actions the developer needed to take. 但是,这些是开发人员需要采取的积极行动。 Failing to do them generally resulted in vulnerable forms, and a new developer who hasn't yet encountered CSRF might not know to look for them. 如果不这样做,通常会导致易受攻击的形式,而尚未遇到CSRF的新开发人员可能不知道要查找它们。

I don't have the docs or announcement handy, but IIRC they changed the @Html.Form() method in a recent version of MVC so it will now automatically include the token. 我没有方便的文档或公告,但IIRC他们在最新版本的MVC中更改了@Html.Form()方法,因此它现在将自动包含令牌。 They also added a new [AutoValidateAntiForgeryToken] attribute which skips safe http actions (GET,HEAD,etc) but checks for the token on unsafe http actions (POST!). 他们还添加了一个新的[AutoValidateAntiForgeryToken]属性,它跳过安全的http动作(GET,HEAD等),但检查不安全的http动作(POST!)上的令牌。 Then they updated the scaffolding Visual Studio generates for new projects to include a filter that adds the new attribute on your actions. 然后,他们更新了Visual Studio为新项目生成的脚手架,以包含一个过滤器,用于为您的操作添加新属性。 If for some reason you have an action that should NOT check the token, there is also a new [IgnoreAntiforgeryToken] attribute you can use to override the behavior created by the filter. 如果由于某种原因您有一个不应该检查令牌的操作,那么您还可以使用新的[IgnoreAntiforgeryToken]属性来覆盖过滤器创建的行为。

Existing projects adding new actions still need to remember to use the old attributes, but new projects — even by developers who don't know any better — will already have all of the right things in all of the right places. 添加新操作的现有项目仍然需要记住使用旧属性,但是新项目 - 即使是那些不了解更好的开发人员 - 也将在所有正确的位置拥有所有正确的东西。 If you really want to shoot yourself in the foot, you can write manual <form> elements, remove the filter after the scaffolding is generated, or add bad "ignore" attributes. 如果你真的想用脚射击自己,可以编写手动<form>元素,在生成脚手架后删除过滤器,或者添加不好的“忽略”属性。 But the typical new developer shouldn't even need to think about this anymore for new projects... assuming, of course, you're using a sufficiently recent MVC release. 但是,典型的新开发人员甚至不需要再考虑这个新项目了......当然,假设您使用的是最新的MVC版本。

Keeping in mind that other platforms have added or are adding similar or equivalent protections to out-of-the-box behavior, and it's clear why this is no longer in the "Top 10": the default behavior of the major platforms is already safe from this attack. 请记住,其他平台已添加或正在为开箱即用的行为添加类似或等效的保护,并且很清楚为什么这不再是“前10名”:主要平台的默认行为已经安全从这次袭击。 It's still documented at OWASP, and the actual threat has not diminished... but now you're no longer vulnerable by default. 它仍然记录在OWASP中,并且实际的威胁并没有减少......但是现在你不再容易受到攻击。 You have to go out of your way for this to be a problem. 你必须尽力避免这个问题。 It's not that this is no longer an issue, but that other items like external xml entities or insufficient logging are now perceived as larger threats, and so have replaced it in the top 10 list. 并不是这不再是一个问题,而是外部xml实体或日志记录不足等其他项目现在被视为更大的威胁,因此已将其替换为前10名列表。

This also highlights one reason you really should use the @Html helpers... they make it possible for your html code to easily stay up to date with the latest best practices, as long as the app continues to receive even minimal maintenance. 这也突出了你真正应该使用@Html助手的一个原因......它们使你的html代码可以轻松地与最新的最佳实践保持@Html ,只要应用程序继续获得最小的维护。

ASP.NET contains the Antiforgery package that can be used to secure your application against CSRF attack. ASP.NET包含Antiforgery包,可用于保护您的应用程序免受CSRF攻击。 See MS Docs 请参阅MS Docs

Like OWASP says lots of frameworks offer the preventions besides ASP.NET, we just need to apply these security libraryies. 与OWASP一样,除了ASP.NET之外,许多框架都提供了预防,我们只需要应用这些安全库。 For example, OAuth 2.0 provides the state parameter to prevent CSRF. 例如, OAuth 2.0提供状态参数以防止CSRF。

If a state parameter is included in the client's request, the same value should appear in the response. 如果客户端请求中包含状态参数,则响应中应显示相同的值。 It's a good practice for the application to verify that the state values in the request and response are identical before using the response. 对于应用程序来说,在使用响应之前验证请求和响应中的状态值是否相同是一种很好的做法。

As developers, we just need to use these Authentication libraries and middlewares which implement OAuth 2.0 such as Azure Active Directory Authentication JS Library ( ADAL ) and .NET Middleware directly. 作为开发人员,我们只需要使用这些身份验证库和中间件来直接实现OAuth 2.0,例如Azure Active Directory身份验证JS库( ADAL )和.NET中间件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM