您的位置:首页 > 其它

Web应用安全之Response Header里的敏感信息

2014-11-11 17:19 597 查看
                             目录

Web应用安全之Response Header

前言

1.1  那些敏感的header

1.2 删除敏感的header

1.2.1 删除server字段

1.2.2 删除X-Powered-By字段

1.2.3 删除 X-AspNet-Version字段

1.2.4 删除X-AspNetMvc-Version

前言

在Kali Linux(http://www.xuanhun521.com/Blog/Tag/kali%20linux)系列文章中,我提到过对目标站点的信息搜集技巧中最基本的就是Banner抓取。

通过对Web服务器的Banner抓取(分析response header),我们能得到关于Web服务器、应用框架、编程语言等信息。

下图是某网站的http 响应头。



 

1.1  那些敏感的HEADER

在上图中圈出的部分,我们关注以下几个字段(针对asp.net应用常见的,并非全部):

Server:web服务器的版本。通常我们会看到 “Microsoft-IIS/7.5”, “nginx/1.0.11” 和 “Apache”这样的字段。

X-Powered-By:web应用框架信息。常见例子,“ASP.NET”, “PHP/5.2.17” 和“UrlRewriter.NET 2.0.0”。

X-AspNet-Version: asp.net版本,只有asp.net站点有这样的header。

X-AspNetMvc-Version:asp.net mvc 版本使用asp.net mvc框架会有此字段。

通常情况下这些信息并不会直接带来危险,但是如果某一天IIS的某个版本爆了一个0day漏洞,那么攻击者会根据响应头在很短的时间内找到大批的IIS站点进行攻击。另外攻击者会根据搜集到的信息结合已有漏洞进行推论和尝试,正确的信息会加快攻击者找到漏洞的步伐。

1.2 删除敏感的HEADER

接下来以我本地的asp.net mvcz站点为例,讲解如何删除响应头中的敏感字段。

1.2.1 删除SERVER字段

这里需要用到IIS扩展工具Url Scan,关于Url Scan的安装和配置项说明见之前的博文《URL Scan简介》。

打开URL Scan的配置文件( C:\Windows\System32\inetsrv\urlscan\UrlScan.ini),找到“RemoveServerHeader”,将值设置为1。配置之前:



配置之后:



注意了在II8以上的版本 UrlScan 好像不能使用了,但是移除这个server去更简单了,code 如下:以下code我在win10 上成功测试过

 public override void Init()

        {

            base.Init();

            PreSendRequestHeaders += (sender, args) => HttpContext.Current.Response.Headers.Remove("Server");

        }

1.2.2 删除X-POWERED-BY字段

打开IIS管理器,切换到站点视图,打开“HTTP响应标头”。





在这里删除X-Powered-By字段。



1.2.3 删除 X-ASPNET-VERSION字段

打开站点下的web.config,做如下配置:

<system.web>

  <httpRuntime enableVersionHeader="false" />

</system.web>

1.2.4 删除X-ASPNETMVC-VERSION

打开Global.asax文件,在Application_Start函数中添加如下代码:

MvcHandler.DisableMvcResponseHeader = true;

最后的结果:



Net中我们为了安全或其他原因起见 可能需要修改我们的标头报文等

以下方法我们通过使用HTTP Module来使用编程的方式来去除或修改它

首先我们自定义一个类CustomServerHeaderModule继承自IHttpModule 并为PreSendRequestHeaders事件创建事件处理程序

代码如下:

<pre name="code" class="csharp">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespaceCloud.ApiWeb.Models
{
publicclassCustomServerHeaderModule : IHttpModule
{
publicvoidInit(HttpApplication context)
{
context.PreSendRequestHeaders += OnPreSendRequestHeaders;
}
publicvoidDispose()
{
}
voidOnPreSendRequestHeaders(objectsender, EventArgs e)
{
//移除Server标头
//HttpContext.Current.Response.Headers.Remove("Server");
//重新设置Server标头
HttpContext.Current.Response.Headers.Set("Server","Windows Server 2012");
}
}
}

是今天發生的一起靈異現象,原本寫好的 HTTP Handler 在 IIS 6.0 和 IIS 7.0 都相安無事,但是今天部署到 IIS 6.0 時,卻發生了 "This operation requires IIS integrated pipeline mode" 的錯誤,但查遍了 Web.config 的設定,也沒有什麼問題。

後來想到之前有在 Handler 中做了一個寫入 HTTP Header 的動作,當時我用的是 context.Response.Headers.Add(),後來改用了 context.Response.AddHeader() 後,問題就消失了。

我很納悶這個問題的原因,用了 Reflector 去 trace 了一下 System.Web.dll 中的程式碼,在 HttpResponse 類別找到了這段:
找到了這段:

public NameValueCollection Headers
{
get
{
if (!(this._wr is IIS7WorkerRequest))
{
throw new PlatformNotSupportedException(SR.GetString("Requires_Iis_Integrated_Mode"));
}
if (this._headers == null)
{
this._headers = new HttpHeaderCollection(this._wr, this, 0x10);
}
return this._headers;
}
}


  
17
喵的咧
... 會檢查目前的 HttpWorkerRequest 是不是 IIS 7 的,如果不是,就會擲出這個例外,
18
但在
MSDN Library 中卻也沒看到這方面的說明,所以 ...
<modules runAllManagedModulesForAllRequests="true"></modules>

总结以下以ii8以后的版本应如下

Server和X-AspNetMvc-Version 需要code 来移除

  public override void Init()

        {

            base.Init();

            PreSendRequestHeaders += (sender, args) => HttpContext.Current.Response.Headers.Remove("Server");

        }

        protected void Application_Start()

        {

            MvcHandler.DisableMvcResponseHeader = true;

        }

X-Powered-By 和 X-AspNet-Version 需要跑配置文件来移除

<system.web>

    <compilation debug="true" targetFramework="4.6.1" />

    <httpRuntime targetFramework="4.6.1" maxQueryStringLength="2097151" enableVersionHeader="false" />

  </system.web>

  <system.webServer>

        <httpProtocol>

            <customHeaders>

                <remove name="X-Powered-By" />

            </customHeaders>

        </httpProtocol>

  </system.webServer>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐