【问题标题】:How to remove ASP.Net MVC Default HTTP Headers?如何删除 ASP.Net MVC 默认 HTTP 标头?
【发布时间】:2011-03-26 00:24:09
【问题描述】:

我正在使用的 MVC 应用程序中的每个页面都会在响应中设置这些 HTTP 标头:

X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version: 2.0

如何防止这些显示?

【问题讨论】:

标签: asp.net-mvc security http-headers


【解决方案1】:

如Removing standard server headers on Windows Azure Web Sites页面所示,您可以使用以下内容删除标题:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <clear />
      </customHeaders>
    </httpProtocol>
    <security>
      <requestFiltering removeServerHeader="true"/>
    </security>
  </system.webServer>
  <system.web>
    <httpRuntime enableVersionHeader="false" />
  </system.web>
</configuration>

这会删除 Server 标头和 X- 标头。

这在我在 Visual Studio 2015 中的测试中本地工作。

其他参考资料:

【讨论】:

  • 添加 removeServerHeader="true" 在我的 ASP.NET 4.5.3 应用程序中出现 500 错误
  • @LachlanB 这是在 IIS 10 中添加的:IIS 10.0 添加了 removeServerHeader 属性以禁止将 HTTP 服务器标头发送到远程客户端。 来源:iis.net/configreference/system.webserver/security/…
  • 我喜欢 Azure 页面提供屏幕截图而不是代码块。他们确实尽其所能尽可能地消除这些不必要且具有潜在危险的标签。另外,我不敢相信我引用了一个三年前的 SO 问题来纠正这个问题,但没有任何被纠正的迹象。
  • 我认为这个 Web.config 不会删除 X-AspNetMvc-Version 标头。要删除那个,我们需要在 Global.asax stackoverflow.com/a/20739875/1678525 中添加一些内容
【解决方案2】:

X-Powered-By 是 IIS 中的自定义标头。从 IIS 7 开始,您可以通过将以下内容添加到您的 web.config 来删除它:

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

此标头也可以根据您的需要进行修改,更多信息请参阅http://www.iis.net/ConfigReference/system.webServer/httpProtocol/customHeaders


将此添加到web.config 以摆脱X-AspNet-Version 标头:

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

最后,要删除 X-AspNetMvc-Version,编辑 Global.asax.cs 并在 Application_Start 事件中添加以下内容:

protected void Application_Start()
{
    MvcHandler.DisableMvcResponseHeader = true;
}

您还可以通过Global.asax.cs 中的Application_PreSendRequestHeaders 事件在运行时修改标头。如果您的标头值是动态的,这很有用:

protected void Application_PreSendRequestHeaders(object source, EventArgs e)
{
      Response.Headers.Remove("foo");
      Response.Headers.Add("bar", "quux");
}

【讨论】:

  • +1 - 出于兴趣,1) 你为什么要这样做? 2) 有没有不良影响?
  • 出于安全原因,您这样做是为了混淆您用于生成网页的技术。这迫使黑客更加努力地工作。
  • @BritishDeveloper 这是来自安全审查的建议。我认为最好不要宣传您的技术堆栈,因为这有助于黑客针对该平台的特定漏洞。
  • @RedFilter 感谢您快速详细的回答!
  • 在 IIS 8 上,这不会删除 X-Powered-By 标头。在web.config 中查看有关如何实现此目的的其他答案。
【解决方案3】:

检查this blog 不要使用代码来删除标题。根据Microsoft 是不稳定的

我对此的看法:

<system.webServer>          
    <httpProtocol>
    <!-- Security Hardening of HTTP response headers -->
    <customHeaders>
        <!--Sending the new X-Content-Type-Options response header with the value 'nosniff' will prevent 
                Internet Explorer from MIME-sniffing a response away from the declared content-type. -->
        <add name="X-Content-Type-Options" value="nosniff" />

        <!-- X-Frame-Options tells the browser whether you want to allow your site to be framed or not. 
                 By preventing a browser from framing your site you can defend against attacks like clickjacking. 
                 Recommended value "x-frame-options: SAMEORIGIN" -->
        <add name="X-Frame-Options" value="SAMEORIGIN" />

        <!-- Setting X-Permitted-Cross-Domain-Policies header to “master-only” will instruct Flash and PDF files that 
                 they should only read the master crossdomain.xml file from the root of the website. 
                 https://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html -->
        <add name="X-Permitted-Cross-Domain-Policies" value="master-only" />

        <!-- X-XSS-Protection sets the configuration for the cross-site scripting filter built into most browsers. 
                 Recommended value "X-XSS-Protection: 1; mode=block". -->
        <add name="X-Xss-Protection" value="1; mode=block" />

        <!-- Referrer-Policy allows a site to control how much information the browser includes with navigations away from a document and should be set by all sites. 
                 If you have sensitive information in your URLs, you don't want to forward to other domains 
                 https://scotthelme.co.uk/a-new-security-header-referrer-policy/ -->
        <add name="Referrer-Policy" value="no-referrer-when-downgrade" />

        <!-- Remove x-powered-by in the response header, required by OWASP A5:2017 - Do not disclose web server configuration -->
        <remove name="X-Powered-By" />

        <!-- Ensure the cache-control is public, some browser won't set expiration without that  -->
        <add name="Cache-Control" value="public" />
    </customHeaders>
</httpProtocol>

<!-- Prerequisite for the <rewrite> section
            Install the URL Rewrite Module on the Web Server https://www.iis.net/downloads/microsoft/url-rewrite -->
<rewrite>
    <!-- Remove Server response headers (OWASP Security Measure) -->
    <outboundRules rewriteBeforeCache="true">
        <rule name="Remove Server header">
            <match serverVariable="RESPONSE_Server" pattern=".+" />

            <!-- Use custom value for the Server info -->
            <action type="Rewrite" value="Your Custom Value Here." />
        </rule>
    </outboundRules>
</rewrite>
</system.webServer>

【讨论】:

    【解决方案4】:

    .NET 核心

    要删除 Server 标头,请在 Program.cs 文件中添加以下选项:

    .UseKestrel(opt => opt.AddServerHeader = false)
    

    对于 dot net core 1,在 .UseKestrel() 调用中添加选项。对于 dot net core 2,在 UseStartup() 之后添加该行。

    要删除 X-Powered-By 标头,如果部署到 IIS,请编辑您的 web.config 并在 system.webServer 标记内添加以下部分:

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

    .NET 4.5.2

    要删除 Server 标头,请在 global.asax 文件中添加以下内容:

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            string[] headers = { "Server", "X-AspNet-Version" };
    
            if (!Response.HeadersWritten)
            {
                Response.AddOnSendingHeaders((c) =>
                {
                    if (c != null && c.Response != null && c.Response.Headers != null)
                    {
                        foreach (string header in headers)
                        {
                            if (c.Response.Headers[header] != null)
                            {
                                c.Response.Headers.Remove(header);
                            }
                        }
                    }
                });
            }
    
        }
    

    .NET 4.5.2 之前

    将以下 c# 类添加到您的项目中:

    public class RemoveServerHeaderModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PreSendRequestHeaders += OnPreSendRequestHeaders;
        }
    
        public void Dispose() { }
    
        void OnPreSendRequestHeaders(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Headers.Remove("Server");
        }
    }
    

    然后在您的 web.config 中添加以下 部分:

    <system.webServer>
        ....
     <modules>
        <add name="RemoveServerHeaderModule" type="MyNamespace.RemoveServerHeaderModule" />
     </modules>
    

    但是我遇到了一个问题,即子项目找不到这个模块。不好玩。

    删除 X-AspNetMvc-Version 标头

    要删除“X-AspNetMvc-Version”标签,对于任何版本的 .NET,修改“web.config”文件以包含:

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

    感谢 Microsoft 让这变得难以置信的困难。或者也许这是您的意图,以便您可以跟踪世界各地的 IIS 和 MVC 安装...

    【讨论】:

    • 在当今时代,这被认为是“最糟糕的做法”,很难相信微软仍然将“不安全”作为默认设置,并且选择“安全”非常棘手。它让我想起了 Windows 在默认情况下是如何隐藏常见文件扩展名的,因此毫无戒心的用户会点击病毒。我似乎记得比尔盖茨在 2003 年宣布“默认安全”——这个想法发生了什么?
    • @mikenelson 如果它让你感觉好些,尝试删除 nginx 中的服务器标签同样困难——我最终不得不破解实际的源代码本身。
    • 关于RemoveServerHeaderModule 在WebApi项目中是行不通的。
    【解决方案5】:

    IIS 将 X-Powered-By 标头添加到 HTTP 响应中,因此您甚至可以通过 IIS 管理器在服务器级别将其删除:

    你可以直接使用web.config:

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

    【讨论】:

      【解决方案6】:

      为了完整起见,还有另一种方法可以删除 Server 标头,使用 regedit。

      See this MSDN blog.

      在以下注册表项中创建一个名为 DisableServerHeader 的 DWORD 条目并将值设置为 1。

      HKLM\SYSTEM\CurrentControlSet\Services\HTTP\Parameters

      我宁愿使用Web.config找到一个合适的解决方案,但是使用&lt;rewrite&gt;并不好,因为它需要安装重写模块,即使这样它也不会真正删除标题,只需清空它.

      【讨论】:

      • 如果这可行,这对我的情况来说是一个很好的解决方案。我有 30 个不同版本的 .net 网站,因此需要 3 种不同的方法来删除所有这些网站中的标题和更新代码。我宁愿有一个配置设置或注册表而不是修改代码。
      • 我前两天申请成功,效果很好。
      • 这对我不起作用。添加密钥后,我重新启动了服务器。我错过了什么吗?
      【解决方案7】:

      在 Asp.Net Core 中,您可以像这样编辑 web.config 文件:

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

      您可以在 Kestrel 选项中删除服务器标头:

                  .UseKestrel(c =>
                  {
                      // removes the server header
                      c.AddServerHeader = false;
                  }) 
      

      【讨论】:

        【解决方案8】:

        您可以更改Application_EndRequest() 中的任何标题或任何内容,试试这个

        protected void Application_EndRequest()
        {
            // removing excessive headers. They don't need to see this.
            Response.Headers.Remove("header_name");
        }
        

        【讨论】:

          【解决方案9】:

          如Cloaking your ASP.NET MVC Web Application on IIS 7 中所述,您可以通过将以下配置部分应用于您的 web.config 来关闭 X-AspNet-Version 标头:

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

          并通过如下更改 Global.asax.cs 来删除 X-AspNetMvc-Version 标头:

          protected void Application_Start() 
          { 
              MvcHandler.DisableMvcResponseHeader = true; 
          }
          

          如Custom Headers 中所述,您可以通过将以下配置部分应用于您的 web.config 来删除“X-Powered-By”标头:

          <system.webServer>
             <httpProtocol>
                <customHeaders>
                   <clear />
                </customHeaders>
             </httpProtocol>
          </system.webServer>
          

          没有简单的方法可以通过配置删除“服务器”响应标头,但您可以实现 HttpModule 以删除特定的 HTTP 标头,如 Cloaking your ASP.NET MVC Web Application on IIS 7 和 how-to-remove-server-x-aspnet-version-x-aspnetmvc-version-and-x-powered-by-from-the-response-header-in-iis7 中所述。

          【讨论】:

          • 使用 bkaid 答案我可以删除“服务器”标头。 IIS 8。
          • bkaid 的答案很好,但它需要编码,所以我发现我描述的解决方案更方便,因为它是基于配置的。
          【解决方案10】:

          我在我的 web.config 中找到了这个配置,该配置用于在 Visual Studio 中创建的 New Web Site...(而不是 New Project...)。由于问题说明了一个 ASP.NET MVC 应用程序,因此相关性不高,但仍然是一个选项。

          <system.webServer>
            <httpProtocol>
              <customHeaders>
                <clear />
                <remove name="X-Powered-By" />
              </customHeaders>
             </httpProtocol>
          </system.webServer>
          

          更新:此外,Troy Hunt 有一篇标题为 Shhh… don’t let your response headers talk too loudly 的文章,其中详细介绍了删除这些标头的步骤,以及指向他的 ASafaWeb 工具的链接,用于扫描它们和其他安全配置。

          【讨论】:

          • 最好的选择,但需要 iis7+ 你不需要 他们......删除就足够了......你也可能想将它添加到 system.webserver 以删除另一个漏洞:code code
          • 我认为 元素会清除所有标题,包括“X-Powererd-By”,因此 元素是多余的。
          【解决方案11】:

          您也可以通过将代码添加到 global.asax 文件来删除它们:

           protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
           {
             HttpContext.Current.Response.Headers.Remove("X-Powered-By");
             HttpContext.Current.Response.Headers.Remove("X-AspNet-Version");
             HttpContext.Current.Response.Headers.Remove("X-AspNetMvc-Version");
             HttpContext.Current.Response.Headers.Remove("Server");
           }
          

          【讨论】:

          • 就我而言,只有最后三个有效,对于“X-Powered-By”,我仍然需要&lt;system.webServer&gt; &lt;httpProtocol&gt; &lt;customHeaders&gt; &lt;remove name="X-Powered-By" /&gt; &lt;/customHeaders&gt; &lt;redirectHeaders&gt; &lt;clear /&gt; &lt;/redirectHeaders&gt; &lt;/httpProtocol&gt; &lt;/system.webServer&gt;
          • 在我的情况下,以上标题都没有被删除。我正在使用 .net 4.0 和 IIS 7。感谢此线程中的其他 cmets。我已经设法删除了所有不需要的标头,但“服务器”除外,这是最坏的情况。
          • 它是否适用于未通过代码路径的内容文件/图像/等?
          • 你在“服务器”里放了什么?应该是这样吗? Response.Headers.Remove("服务器:Microsoft-IIS/7.0"); ?还是应该是“服务器”?请帮忙
          • 奇怪“PreSendRequestHeaders”实际上是预发送响应标头?
          猜你喜欢
          • 2023-01-31
          • 1970-01-01
          • 2015-09-26
          • 2018-07-16
          • 1970-01-01
          • 2021-07-10
          • 2011-12-15
          • 2015-04-25
          相关资源
          最近更新 更多