一家外部公司已经对我正在使用的ASP.NET MVC 5应用程序进行了一些渗透测试。
他们提出的问题描述如下
与会话管理链接的cookie称为AspNet.ApplicationCookie。手动输入时,应用程序将对用户进行身份验证。即使用户从应用程序注销,该cookie仍然有效。这意味着,旧的会话cookie可以在无限的时间范围内用于有效的身份验证。在插入旧值的瞬间,应用程序接受它并将其替换为新生成的cookie。因此,如果攻击者获得对现有Cookie之一的访问权限,则将创建有效会话,并且具有与过去相同的访问权限。
我们正在使用ASP.NEt Identity 2.2
这是我们在帐户控制器上的注销操作
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Login", "Account");
}
在startup.auth.cs中
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
ExpireTimeSpan = TimeSpan.FromHours(24.0),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator
.OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>(
validateInterval: TimeSpan.FromMinutes(1.0),
regenerateIdentityCallback: (manager, user) =>
user.GenerateUserIdentityAsync(manager),
getUserIdCallback: (id) => (Int32.Parse(id.GetUserId())))
}
});
我以为该框架将使旧的会话cookie失效,但浏览Owin.Security源代码却显得很麻烦。
我如何在注销时使会话cookie无效?
我已经添加了杰米·邓斯坦(Jamie Dunstan)的建议,AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
但是没有任何区别。我仍然仍然可以注销该应用程序,在Fiddler中克隆一个先前经过身份验证的请求,并使该请求被应用程序接受。
编辑:我更新的注销方法
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> LogOff()
{
var user = await UserManager.FindByNameAsync(User.Identity.Name);
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
await UserManager.UpdateSecurityStampAsync(user.Id);
return RedirectToAction("Login", "Account");
}
确保AuthenticationManager.Signout(DefaultAuthenticationTypes.ApplicationCookie);
按照Jamie的建议正确使用。
能够再次使用相同的cookie登录是设计使然。Identity不会创建内部会话来跟踪所有已登录的用户,并且如果OWIN得到的Cookie击中了所有复选框(即上一个会话的副本),它将允许您登录。
如果您在安全标记更新后仍然可以登录,则OWIN很可能无法保留ApplicationUserManager
。确保您在这条线的正上方app.UseCookieAuthentication
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
或者,如果您使用DI,请ApplicationUserManager
从DI中获取:
app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationUserManager>());
还要降低validateInterval: TimeSpan.FromMinutes(30)
到较低的值-我通常会安顿几分钟。这是Identity经常将auth-cookie中的值与数据库中的值进行比较的频率。比较完成后,Identity将重新生成Cookie以更新时间戳。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句