【问题标题】:Header Manipulation Cookies in asp.netasp.net 中的标头操作 Cookie
【发布时间】:2017-02-23 22:17:23
【问题描述】:

Fortify 工具在以下代码中显示 Header 操作 cookie 问题。

HttpCookie cookieexit= new HttpCookie("CheckUserNameExists");
cookieexit.Value = userName;
cookieexit.Secure = true;
Response.Cookies.Add(cookieexit);
Response.Redirect(FormsAuthentication.GetRedirectUrl(userName, false));

有什么帮助吗?

【问题讨论】:

    标签: c# asp.net security cookies fortify


    【解决方案1】:

    它与原始问题没有直接关系。

    如果您使用 FormAuthentication,则创建 不是一个好习惯自定义 cookie 名称 以跟踪登录的用户名。

    相反,您希望使用 FormsAuthentication 使用的相同 cookie 名称 FormsAuthentication.FormsCookieName

    因此,您将 5 行代码替换为以下单行 -

    FormsAuthentication.RedirectFromLoginPage(userName, false);
    

    Global.asax.cs

    然后您从 cookie 中检索用户名,并将其保存在 Principal ObjectCurrent Thread 中。

    public class Global : HttpApplication
    {
        private void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpCookie decryptedCookie =
                Context.Request.Cookies[FormsAuthentication.FormsCookieName];
    
            FormsAuthenticationTicket ticket =
                FormsAuthentication.Decrypt(decryptedCookie.Value);
    
            var identity = new GenericIdentity(ticket.Name);
            var principal = new GenericPrincipal(identity, null);
    
            HttpContext.Current.User = principal;
            Thread.CurrentPrincipal = HttpContext.Current.User;
        }
    }
    

    然后,只要 HttpContext.Current 可用,您就可以在代码中的 anywhere 字面上检索用户名。例如,

    string userName = User.Identity.Name;
    

    【讨论】:

      【解决方案2】:

      Fortify 之所以抱怨,是因为它认为 userName 可能包含危险字符,例如换行符,这可能(可能)允许在社会工程/网络钓鱼攻击的情况下进行标头操纵攻击。

      我不太确定此类攻击的实用性,但我很确定如果您验证 userName 仅包含排除任何空格的白名单字符集,则可以防止此类攻击。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-17
        • 1970-01-01
        • 2017-10-24
        相关资源
        最近更新 更多