如何删除IIS / ASP.NET响应头

我有几个IIS / 6.0服务器,安全要求我删除一些响应头,根据请求发送到客户端浏览器。 他们担心通过响应头来泄露平台信息。 我已经删除了网站的IISconfiguration中的所有HTTP头(X-Powered-By或某个这样的头文件)。

(我个人知道这个信息很容易被发现,即使它是隐藏的,但这不是我的电话。)

我想删除的标题:

  • 服务器 – Microsoft-IIS / 6.0
  • X-AspNet-Version – 2.0.50727

我也知道,ASP.NET MVC也发出自己的头,如果你知道如何删除它,这将是有益的。

  • X-AspNetMvc版本 – 1.0

您的安全部门希望您这样做,使服务器types难以识别。 这可能会减less自动化黑客攻击的手段,并使人们更难以闯入服务器。

在IIS中,打开网站属性,然后转到HTTP标题选项卡。 大部分的X头可以在这里find并删除。 这可以为单个站点或整个服务器完成(修改树中Web站点对象的属性)。

对于服务器头,在IIS6上,您可以使用Microsoft的URLScan工具来远程执行此操作。 Port 80软件还生产一个名为ServerMask的产品,为您处理这个问题,还有更多。

对于IIS7,有一篇关于使用自定义模块来修改服务器标题的文章。

对于MVC头文件,在Global.asax中:

MvcHandler.DisableMvcResponseHeader = true; 

要删除所有披露了太多信息的自定义标头 – 对于IIS 7,这些方法各有不同(不幸的是):

标题名称: X-Powered-By

加:

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

<system.webServer>部分。

标题名称: 服务器

通过调用PreSendRequestHeaders事件中的Response.Headers.Remove(“Server”)实现一个httpModule。 另一个资源: 在IIS 7上隐藏ASP.NET MVC Web应用程序

标题名称: X-AspNet版本

在web.config的httpRuntime部分 – 设置:

 <httpRuntime enableVersionHeader="false" /> 

标题名称: X-AspNetMvc版本

从global.asax中的Application_Start事件 – 执行下面的代码(C#):

 MvcHandler.DisableMvcResponseHeader = true; 

把它放在一个ASP.NET应用程序的web.config文件中将摆脱X-AspNet-Version头文件:

 <system.web> <httpRuntime enableVersionHeader="false" /> </system.web> 

请注意,system.web标记应该已经存在于文件中。 不要创build一个副本,只需添加httpRuntime标记。 httpRuntime标记也可能已经存在。 如果是这样,只要添加属性或设置它的值,如果它已经在那里。

在刚刚完成我当前项目的“强化”循环之后 – 我在博客中介绍了我们所采取的方法,其中包括一个用于删除以下标题的HTTPModule :

服务器,
X-ASPNET版,
X-AspNetMvc版,
X供电,通过

相关部分转载如下:

但是没有简单的方法通过configuration删除服务器响应头。 幸运的是,IIS7有一个可pipe理的可插拔模块基础结构,可以让您轻松扩展其function。 以下是用于删除指定的HTTP响应头列表的HttpModule的源代码:

 namespace Zen.Core.Web.CloakIIS { #region Using Directives using System; using System.Collections.Generic; using System.Web; #endregion /// <summary> /// Custom HTTP Module for Cloaking IIS7 Server Settings to allow anonymity /// </summary> public class CloakHttpHeaderModule : IHttpModule { /// <summary> /// List of Headers to remove /// </summary> private List<string> headersToCloak; /// <summary> /// Initializes a new instance of the <see cref="CloakHttpHeaderModule"/> class. /// </summary> public CloakHttpHeaderModule() { this.headersToCloak = new List<string> { "Server", "X-AspNet-Version", "X-AspNetMvc-Version", "X-Powered-By", }; } /// <summary> /// Dispose the Custom HttpModule. /// </summary> public void Dispose() { } /// <summary> /// Handles the current request. /// </summary> /// <param name="context"> /// The HttpApplication context. /// </param> public void Init(HttpApplication context) { context.PreSendRequestHeaders += this.OnPreSendRequestHeaders; } /// <summary> /// Remove all headers from the HTTP Response. /// </summary> /// <param name="sender"> /// The object raising the event /// </param> /// <param name="e"> /// The event data. /// </param> private void OnPreSendRequestHeaders(object sender, EventArgs e) { this.headersToCloak.ForEach(h => HttpContext.Current.Response.Headers.Remove(h)); } } } 

确保您签署程序集,然后将其安装到Web服务器的GAC中,并简单地对应用程序的web.config(或者如果要将其全局应用到machine.config)进行以下修改:

 <configuration> <system.webServer> <modules> <add name="CloakHttpHeaderModule" type="Zen.Core.Web.CloakIIS.CloakHttpHeaderModule, Zen.Core.Web.CloakIIS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=<YOUR TOKEN HERE>" /> </modules> </system.webServer> </configuration> 

我使用下面的代码,并为我工作iis 7.5

 protected void Application_PreSendRequestHeaders() { Response.Headers.Remove("Server"); Response.Headers.Remove("X-AspNet-Version"); Response.Headers.Remove("X-AspNetMvc-Version"); }