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

Bry*_*ein 48 iis asp.net http-headers

我有几台 IIS/6.0 服务器,安全部门要求我删除一些根据请求发送到客户端浏览器的响应标头。他们担心通过响应头泄露平台信息。我已从网站的 IIS 配置(X-Powered-By 或某些此类标头)中删除了所有 HTTP-HEADERS。

(我个人确实知道这些信息很容易被发现,即使它是隐藏的,但这不是我的要求。)

我想删除的标题:

  • 服务器- Microsoft-IIS/6.0
  • X-AspNet 版本- 2.0.50727

我也知道 ASP.NET MVC 也发出自己的标头,如果您也知道如何删除它,那会很有帮助。

  • X-AspNetMvc-版本- 1.0

Ada*_*dam 60

要删除所有披露过多信息的自定义标头 - IIS 7 的方法(不幸的是)有所不同:

标题名称: X-Powered-By

添加:

<httpProtocol>
  <customHeaders>
    <remove name="X-Powered-By" />
  </customHeaders>
</httpProtocol>
Run Code Online (Sandbox Code Playgroud)

在该<system.webServer>部分。

标头名称:服务器

实现一个 httpModule,通过从 PreSendRequestHeaders 事件调用 Response.Headers.Remove("Server") 来去除这个标头。另一个资源:Cloaking your ASP.NET MVC Web Application on IIS 7

标头名称:X-AspNet-Version

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

<httpRuntime enableVersionHeader="false" />
Run Code Online (Sandbox Code Playgroud)

标头名称:X-AspNetMvc-Version

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

MvcHandler.DisableMvcResponseHeader = true;
Run Code Online (Sandbox Code Playgroud)


Jus*_*ott 32

您的安全部门希望您这样做以使服务器类型更难识别。这可能会减少自动黑客工具的攻击,并使人们更难闯入服务器。

在 IIS 中,打开网站属性,然后转到 HTTP 标头选项卡。大多数 X- 标头都可以在此处找到并删除。这可以为单个站点或整个服务器完成(修改树中网站对象的属性)。

对于 Server 标头,在 IIS6 上,您可以使用 Microsoft 的URLScan工具对其进行远程处理。Port 80 Software 还制作了一个名为ServerMask的产品,它将为您解决这个问题,以及更多。

对于 IIS7(及更高版本),您可以使用URL 重写模块来重写服务器标头或将其值设为空白。在 web.config(在一个站点或整个服务器)中,在安装 URL Rewrite Module 后添加以下内容:

<rewrite>    
  <outboundRules rewriteBeforeCache="true">
    <rule name="Remove Server header">
      <match serverVariable="RESPONSE_Server" pattern=".+" />
      <action type="Rewrite" value="" />
    </rule>
  </outboundRules>
</rewrite>
Run Code Online (Sandbox Code Playgroud)

如果您愿意,您可以将自定义值放入重写操作中。此示例来自本文,其中还包含其他重要信息。

对于 MVC 标头,在 Global.asax 中:

MvcHandler.DisableMvcResponseHeader = true;
Run Code Online (Sandbox Code Playgroud)

编辑 11-12-2019 以更新 IIS7 信息,因为 TechNet 博客链接不再有效。

  • 接受的答案,希望我能与@squillman 分享答案。Web.config 修复 X-AspNet-Version: &lt;system.web&gt; &lt;httpRuntime enableVersionHeader="false" /&gt; &lt;/system.web&gt; (3认同)

squ*_*man 15

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

<system.web>
<httpRuntime enableVersionHeader="false" />
</system.web>
Run Code Online (Sandbox Code Playgroud)

请注意,system.web 标记应该已经存在于文件中。不要创建重复项,只需添加 httpRuntime 标记即可。httpRuntime 标记也可能已经存在。如果是这样,只需添加属性或设置它的值(如果它已经存在)。


小智 5

刚刚经历了我当前项目的“强化”周期 -我在博客中介绍了我们采用的方法,其中包括一个用于删除以下标头的 HTTPModule

服务器,
X-AspNet-版本,
X-AspNetMvc-版本,
X-Powered-By

相关作品转载如下:

但是没有简单的方法可以通过配置删除服务器响应头。幸运的是 IIS7 有一个托管的可插拔模块基础结构,它允许您轻松扩展其功能。下面是用于删除指定的 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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

确保对程序集进行签名,然后可以将其安装到 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>
Run Code Online (Sandbox Code Playgroud)

  • 通过配置抑制头的生成似乎比生成头然后删除它更有意义。 (2认同)