如何保护内置于表单身份验证中的 OWASP A10?

How do I secure the OWASP A10 built into forms authentication?

如果您使用 ASP.NET 表单身份验证,您可能会熟悉像这样的 URL:

https://Example.com/Banking/login.aspx?ReturnUrl=%2fBanking%2fBalances.aspx

只要您在没有表单身份验证 cookie 的情况下访问 "Balances.aspx",就会收到此信息。 ASP.NET 会将您跳转到登录页面,但在查询字符串中提供 ReturnUrl,以便它可以将您重定向回来。

不幸的是,这引入了一个未经验证的转发,这是对 OWASP A10 的违规行为。

跨域漏洞

想象一下,如果我制作了一封电子邮件并将其发送出去......该电子邮件看起来就像一封来自您银行的电子邮件,并且有一个 URL 像这样:

https://Example.com/Banking/login.aspx?ReturnUrl=https://Example.org/Banking/login.aspx

请注意,我巧妙地将 Example.com 切换为 Example.org,您可能不会注意到。如果我很聪明,我什至可以 URL 使用十六进制代码对整个 ReturnUrl 进行编码,这样您就看不到它了。顺便说一句,我有一个 Example.org 的 EV 证书,这意味着你不会收到网络钓鱼错误,甚至会得到一个漂亮的绿色地址栏。不幸的是,您不会进入您认为自己所在的网站!在我的登录页面版本(看起来就像真实的),我会告诉你你的密码是错误的,你可以重新输入,瞧,我刚刚得到你的密码。

同域漏洞

同样的情况...我发送了一封看起来像是来自您的银行的电子邮件。但是您的银行不允许跨域重定向。不过,我可以用这个 URL:

发起攻击
https://Example.com/Banking/login.aspx?ReturnUrl=https://Example.com/Banking/Transfer.aspx?FromAcct=Checking&ToAcct=6273826&ToCountry=Nigeria&Amount=5000.00

上面看起来很糟糕,但请记住我可以使用 URL 编码来掩盖整个事情,所以它看起来就像十六进制垃圾。

当您单击 link 时,系统会提示您登录您的网站,然后它会立即向我的尼日利亚帐户发出转帐。

在我看来,ASP.NET 表单身份验证非常普遍。开发人员如何缓解此漏洞?

如何使用它来将用户重定向到登录页面:

FormsAuthentication.RedirectFromLoginPage

框架中似乎已经内置了保护措施:

By default, the ReturnUrl variable must refer to a page within the current application. If ReturnUrl refers to a page in a different application or on a different server, the RedirectFromLoginPage methods redirects to the URL in the DefaultUrl property. If you want to allow redirects to a page outside the current application, you must set the EnableCrossAppRedirects property to true using the enableCrossAppRedirects attribute of the configuration element.

示例代码:

FormsAuthentication.SetAuthCookie(user.UserName, false);
FormsAuthentication.RedirectFromLoginPage(user.UserName, false);