修复 ASP.Net 中的 Session Fixation 缺陷

Fix Session Fixation flaw in ASP.Net

使用 .Net Framework 4.7 的混合 webforms/mvc asp.net 应用程序在 Veracode 动态扫描中被标记为 "Session Fixation" 漏洞。这意味着 Veracode 获取登录页面,更改 SessionId cookie (ASP.NET_SessionId),然后使用有效的用户 ID 和密码发布以进行登录。 ASP.Net 登录用户,但获取这个更改后的 SessionId cookie 并继续使用它;使用注入的 SessionId 值的行为是缺陷。

换句话说,当 Veracode 获取页面时,SessionId cookie 可能是 "abc123"。 Veracode 将该 cookie 更改为 "def456" 并回发。 ASP.Net 登录用户并使用 "def456" 作为 SessionId。

根据 Veracode,我必须使在成功登录之前创建的 ASP.Net_SessionID cookie 无效。这当然很容易做到,我可以在用户成功登录时简单地重置 ASP.NET_SessionId cookie。问题是,这会导致用户被重定向回登录页面。那么会发生什么:

  1. 用户提交登录页面。
  2. 服务器端,如果登录成功,我将 ASP.NET_SessionId 重置为某个新值(通过调用 SessionIDManager.SaveSessionID(),这又会简单地重置 ASP.Net_SessionID饼干)。
  3. 用户被重定向到应用程序主页,然后立即被重定向回登录页面

该应用程序使用表单身份验证,带有一个网络表单登录页面。登录页面使用 asp.net 登录控件。在这个控件的 "OnAuthenticate" 事件中,我有这样的代码:

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        bool b = Membership.Validateuser(Login1.UserName, Login1.Password);
        if(b)
        {
            e.Authenticated = true;
            SessionIDManager mgr = new SessionIDManager();
            string newId = mgr.CreateSessionID(Context);
            mgr.SaveSessionID(Context, newId, out bool redirected, out bool cookieAdded);
        }
}

这运行没有错误。 ASP.net 将用户重定向到应用程序主页。但是 asp.net 立即将用户从应用程序主页重定向回登录页面。

有什么方法可以改变 SessionId cookie 以便

  1. Veracode 注入的 SessionId cookie 值被放弃。
  2. 用户保持身份验证状态,而不是简单地重定向回登录页面。

我已经尝试 运行 在各种页面事件(PreInit、Load 等)中更改 SessionId 的代码,所有这些都具有相同的结果——用户被重定向回登录页面.

请不要将此问题标记为已回答。 SO上这个问题有好几个答案,都建议像我上面那样重新设置SessionId cookie,而且都有评论指出这实际上行不通。

经过反复考虑,Veracode 顾问的最终回复如下,基本上是说 "don't worry about it." 这是 Veracode 的回复:

I also looked further into the dynamic findings from your application. The session ID that we are finding session fixation on, is not the ID you are using for authentication, and so the risk for your application is low. An attacker would not be able to gain access to an authenticated session of a user by controlling just the ASP.NET_SessionId cookie. By protecting the .ssoIIMAuth cookie your application is already preventing this sort of attack.