ASPXAUTH Cookie的安全标志

17

我们有一个外部应用程序,由外部安全公司进行了渗透测试。该应用程序是在ASP.NET MVC4上开发,运行在IIS8 / Windows 2012 Server上。

报告中报告的漏洞之一是ASPXAUTH不安全。当我在cookie检查器上检查时,有一些带有安全标志的cookie。但是ASPXAUTH不是其中之一。

我进行了一些研究,并在web.config上设置了以下这些标志。

<forms loginUrl="~/Account/Login" timeout="2880"  requireSSL=""  name="AppName" />

<httpCookies httpOnlyCookies="true" requireSSL="true" />

尽管设置了这些选项,但身份验证Cookie未标记为安全。我认为这些标志足以将应用程序Cookie标记为安全,但还有一些其他Cookie也未标记为安全。我对它们不太担心,因为它们不包含任何敏感信息。但我希望将ASPXAUTH标记为安全。

我的问题是:

  1. 在web.config上设置了这些标志,如果ASPXAUTH没有安全标志,会存在安全问题吗?
  2. 如果存在安全问题,您能告诉我标记为安全的正确方法吗?

谢谢。

4个回答

17

我发现了一段代码,它使我的身份验证 cookie 更加安全。我记不清这段代码的来源,但是如果您将其添加到 global.asax 中,它可以解决问题。我不知道为什么在 <authentication> 标签中使用 requireSSL=true 不足以使其安全。

  protected void Application_EndRequest(Object sender, EventArgs e)
    {
        string authCookie = FormsAuthentication.FormsCookieName;

        foreach (string sCookie in Request.Cookies)
        {
            if (sCookie.Equals(authCookie))
            {
                // Set the cookie to be secure. Browsers will send the cookie
                // only to pages requested with https
                var httpCookie = Response.Cookies[sCookie];
                if (httpCookie != null) httpCookie.Secure = true;
            }
        }
    }

2
@AnarchistGreek,对抗IBM的AppScan没有奏效。它仍然声称cookie不安全。 - Roy Oliver
1
我明白了,你的代码需要进行编辑。foreach语句应该访问Request.Cookies而不是Response.Cookies。 - Roy Oliver

12

你的问题似乎是由于表单配置不正确。你有:

<forms ... requireSSL="" ... />

你应该有

<forms ... requireSSL="true" ... />

根据Microsoft的说法,httpCookies标签中的requireSSL属性会被forms标签中的requireSSL属性覆盖。您没有设置该值,但指定可能会导致IIS使用默认值false。您应将其设置为true

0
修改了AnarchistGeek的回答:你不想直接遍历Request.Cookies,因为使用响应集合添加一个cookie会使cookie立即在请求集合中可用(请参阅HttpRequest.Cookies文档这里中的注释)。当你去设置/更改响应的.ASPXAUTH cookie时,这将导致"Collection was modified after the enumerator was instantiated"错误,因为它还修改了请求集合。
protected void Application_EndRequest(Object sender, EventArgs e)
{
    string authCookie = FormsAuthentication.FormsCookieName;
    string[] cookieNames = Request.Cookies.AllKeys;

    foreach (string sCookie in cookieNames)
    {
        if (sCookie.Equals(authCookie))
        {
            var httpCookie = Response.Cookies[sCookie];
            if (httpCookie != null) httpCookie.Secure = true;
        }
    }
}

请注意,此特定解决方案将清除 .ASPXAUTH cookie 的现有值(请参见此post)。

0

回答您的第二个问题

可能是如何保护.ASPXAUTH令牌的重复

根据xelco的答案

To prevent forms authentication cookies from being captured and tampered with while crossing the network, ensure that you use SSL with all pages that require authenticated access and restrict forms authentication tickets to SSL channels by setting requireSSL="true" on the <forms> element.

To restrict forms authentication cookies to SSL channels set requireSSL="true" on the <forms> element, as shown in the following code:

<forms loginUrl="Secure\Login.aspx" requireSSL="true" ... />

By setting requireSSL="true", you set the secure cookie property that determines whether browsers should send the cookie back to the server. With the secure property set, the cookie is sent by the browser only to a secure page that is requested using an HTTPS URL.

谢谢。我已经阅读了那个答案,这就是我在问题中所描述的所做的。我完全按照所描述的去做了,但是 ASPXAUTH 令牌 cookie 仍然没有标记为安全。 - AnarchistGeek
我不喜欢重复显而易见的事情,但是requireSSL是设置安全cookie标志的方法。https://www.owasp.org/index.php/SecureFlag。需要检查的一件事是配置文件的层次结构。它可能在顶部的web.config中设置,但在子目录中被覆盖。您也可以在代码中进行检查。http://msdn.microsoft.com/en-us/library/system.web.httpcookie.secure%28v=vs.110%29.aspx - 0leg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接