网站安全测试显示Response.Redirect易受攻击

use*_*119 5 c# asp.net security http

使用Acunetix扫描我的Web应用程序后,结果显示9个"重定向页面中找到HTML表单"的实例.重现测试攻击的HTTP标头如下:

Request
GET /entities/add HTTP/1.1
Pragma: no-cache
Referer: https://test.mysite.com/entities/view
Acunetix-Aspect: enabled
Acunetix-Aspect-Password: 082119f75623eb7abd7bf357698ff66c
Acunetix-Aspect-Queries: filelist;aspectalerts
Cookie: __AntiXsrfToken=97c0a6bb164d4121b07327df405f9db4; mysitecookie=
Host: test.mysite.com
Connection: Keep-alive
Accept-Encoding: gzip,deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Acunetix-Product: WVS/8.0 (Acunetix Web Vulnerability Scanner - NORMAL)
Acunetix-Scanning-agreement: Third Party Scanning PROHIBITED
Acunetix-User-agreement: http://www.acunetix.com/wvs/disc.htm
Accept: */*

Response
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=utf-8
Location: /login
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Set-Cookie: mysitecookie=; expires=Mon, 11-Oct-1999 22:00:00 GMT; path=/; HttpOnly
X-Powered-By: ASP.NET
Date: Tue, 24 Sep 2013 10:38:12 GMT
Content-Length: 9447
Run Code Online (Sandbox Code Playgroud)

Location: /login响应的部分让我相信,如果我在将用户重定向到登录页面后结束了响应,则漏洞将被插入.所以我改变了这段代码的所有实例:

protected void Page_Load(object sender, EventArgs e)
{
    if (!HttpContext.Current.User.Identity.IsAuthenticated)
    {
        Response.Redirect("/login");
    }
    else
    {
        // etc
    }
}
Run Code Online (Sandbox Code Playgroud)

至:

protected void Page_Load(object sender, EventArgs e)
{
    if (!HttpContext.Current.User.Identity.IsAuthenticated)
    {
        Response.Redirect("/login", true);
    }
    else
    {
        // etc
    }
}
Run Code Online (Sandbox Code Playgroud)

可能是它仍然表现为脆弱的原因是什么?

mik*_*key 1

引发该标志是因为页面中存在 HTML 表单,并且响应标头中存在重定向。我认为这是一个漏洞,因为它可以让未经身份验证的用户了解您的应用程序的工作原理。为防止引发此标志,您可以采取的措施是在重定向之前清除您的响应。

请尝试以下操作:

protected void Page_Load(object sender, EventArgs e)
{
    if (!HttpContext.Current.User.Identity.IsAuthenticated)
    {
        Response.Clear();
        Response.Redirect("/login", true); // btw true is the default...
    }
    else
    {
        // etc
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您要设置会话变量、cookie 等,那么您需要稍微调整一下,以便确保所有内容都进入响应,例如使用 endResponse=false, Response.End(); 返回; ETC。

http://msdn.microsoft.com/en-us/library/system.web.httpresponse.clear.aspx

  • HTTP 请求和响应由标头和正文(以及方法,请参阅下面的链接)组成。在您的情况下,您的标头说“否”(重定向),但您的正文通过发送(至少部分)HTML 表单说“是”(抱歉,我无法抗拒)。如果您想要使用不同的方法,您可以清除标头,但在您的情况下,您不想清除标头(您希望它重定向),您只想清除正文。http://geekexplains.blogspot.com/2008/06/whats-http-explain-http-request-and.html (2认同)
  • @user982119 - 您应该认真考虑使用表单身份验证,而不是在所有页面上手工制作“登录检查” - 您可以将所有应限制在文件夹中的页面并按角色自动限制对该文件夹的访问使用表单验证。http://msdn.microsoft.com/en-us/library/7t6b43z4.aspx (2认同)