如何删除ASP.Net MVC默认HTTP标头?

Pau*_*yer 172 security asp.net-mvc http-headers

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

X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version: 2.0
Run Code Online (Sandbox Code Playgroud)

如何防止这些显示?

Red*_*ter 269

"powered by"是IIS中的自定义标头.更改它取决于您使用的IIS版本.有关如何修改或删除的一些信息,请参阅此处:

http://www.iis.net/ConfigReference/system.webServer/httpProtocol/customHeaders

要删除MVC标头,

在Global.asax中,在Application Start事件中:

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

把它放在web.config中去掉X-AspNet-Version标题:

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

  • 出于安全原因,您这样做是为了混淆用于生成网页的技术.这迫使黑客更加努力地工作. (66认同)
  • @BritishDeveloper这是一项来自安全审核的建议.我认为最佳做法是不要宣传您的技术堆栈,因为这有助于黑客利用该平台瞄准特定漏洞. (18认同)
  • 在IIS 8上,这不会删除`X-Powered-By`标头.有关如何在`web.config`中实现此目的,请参阅其他答案. (6认同)
  • +1 - 为了感兴趣,1)你为什么要这样做?2)是否有任何不良影响? (4认同)

bka*_*aid 102

您还可以通过向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");
 }
Run Code Online (Sandbox Code Playgroud)

  • 在我的情况下,只有最后三个工作,对于"X-Powered-By"我仍然需要`<system.webServer> <httpProtocol> <customHeaders> <remove name ="X-Powered-By"/> </ customHeaders> < redirectHeaders> <clear /> </ redirectHeaders> </ httpProtocol> </system.webServer> (28认同)
  • 它是否适用于未通过代码路径的内容文件/图像/等? (3认同)
  • 在我的情况下,没有删除上述标题.我正在使用.net 4.0和IIS 7.感谢此主题中的其他评论.我已设法删除所有不需要的标头,但"服务器"除外,这是最糟糕的情况. (2认同)

Kev*_*son 50

我发现这个配置web.configNew Web Site...在Visual Studio中创建的(而不是a New Project...).由于问题陈述了一个ASP.NET MVC应用程序,不是相关的,但仍然是一个选项.

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

更新:另外,Troy Hunt有一篇题为Shhh的文章...不要让你的响应标题过于响亮,有关删除这些标题的详细步骤以及用于扫描它们和其他安全配置的ASafaWeb工具的链接.

  • 最好的选择,但需要iis7 +您不需要<clear />他们...删除就足够了..您也可以将其添加到system.webserver以删除另一个漏洞:`code` <security> <requestFiltering> <verbs> < add verb ="OPTIONS"allowed ="false"/> </ verbs> </ requestFiltering> </ security>`code` (5认同)

Ron*_*nyK 30

在IIS 7上隐藏ASP.NET MVC Web应用程序中所述,您可以通过将以下配置部分应用于web.config来关闭X-AspNet-Version标头:

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

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

protected void Application_Start() 
{ 
    MvcHandler.DisableMvcResponseHeader = true; 
}
Run Code Online (Sandbox Code Playgroud)

自定义标题中所述您可以通过将以下配置部分应用于web.config来删除"X-Powered-By"标题:

<system.webServer>
   <httpProtocol>
      <customHeaders>
         <clear />
      </customHeaders>
   </httpProtocol>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

没有简单的方法可以通过配置删除"服务器"响应标头,但是您可以实现HttpModule删除特定的HTTP标头,如在IIS 7上隐藏ASP.NET MVC Web应用程序如何删除服务器中所述 - x-aspnet-version-x-aspnetmvc-version-and-x-powered-from-the-response-header-in-iis7.


Roc*_*lan 27

.NET核心

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

.UseKestrel(opt => opt.AddServerHeader = false)
Run Code Online (Sandbox Code Playgroud)

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

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

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

.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);
                        }
                    }
                }
            });
        }

    }
Run Code Online (Sandbox Code Playgroud)

Pre .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");
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在你的web.config中添加以下<modules>部分:

<system.webServer>
    ....
 <modules>
    <add name="RemoveServerHeaderModule" type="MyNamespace.RemoveServerHeaderModule" />
 </modules>
Run Code Online (Sandbox Code Playgroud)

但是我遇到了子项目找不到这个模块的问题.不好玩.

删除X-AspNetMvc-Version标头

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

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

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

  • 在这个时代,这被认为是"最糟糕的做法",并且很难相信微软仍然将"不安全"作为默认设置并且选择"安全"非常棘手.它让我想起Windows默认情况下隐藏了常见的文件扩展名,因此不知情的用户会点击病毒.我似乎记得比尔盖茨在2003年宣布"默认安全" - 不管这个想法发生了什么? (3认同)
  • @mikenelson如果让您感觉更好,尝试在nginx中删除Server标记同样困难-我最终不得不破解实际的源代码本身。 (2认同)

Eri*_*way 8

如" 在Windows Azure网站删除标准服务器标头"页面上所示,您可以使用以下内容删除标头:

<?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>
Run Code Online (Sandbox Code Playgroud)

这将删除Server标头和X-header.

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

  • 添加removeServerHeader ="true"在我的ASP.NET 4.5.3应用程序上给出了500错误 (6认同)
  • @LachlanB这是在IIS 10中添加的:_IIS 10.0添加了removeServerHeader属性以禁止将HTTP服务器头发送到远程客户端.来源:https://www.iis.net/configreference/system.webserver/security/requestfiltering (4认同)
  • 我喜欢 Azure 页面提供 _screenshots_ 而不是代码块。他们确实竭尽所能,尽可能地消除这些不必要的和潜在危险的标签。另外,我不敢相信我引用了一个三年前的 SO 问题来纠正这个问题,但没有显示出被纠正的迹象。 (2认同)
  • 我认为此 Web.config 不会删除 X-AspNetMvc-Version 标头。要删除该文件,我们需要在 Global.asax 中添加一些内容 /sf/answers/1451791281/ (2认同)

Dar*_*tar 7

在Asp.Net Core中,您可以编辑web.config文件,如下所示:

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

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

            .UseKestrel(c =>
            {
                // removes the server header
                c.AddServerHeader = false;
            }) 
Run Code Online (Sandbox Code Playgroud)


mit*_*aka 5

检查此博客 不要使用代码删除标题。根据微软的说法它是不稳定的

我对此的看法:

<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>
Run Code Online (Sandbox Code Playgroud)