从IIS 7/8中的静态内容中删除服务器标头

Chr*_*ett 29 c# asp.net security iis http-headers

作为努力使我们的API和网站更安全的一部分,我正在删除泄漏有关网站运行信息的标题.

剥离标题之前的示例:

HTTP/1.1 500 Internal Server Error
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 05 Jun 2013 00:27:54 GMT
Content-Length: 3687
Run Code Online (Sandbox Code Playgroud)

Web.config文件:

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

的Global.asax.cs:

protected void Application_PreSendRequestHeaders() {
    Response.Headers.Remove("Server");
    Response.Headers.Remove("X-AspNet-Version");
    Response.Headers.Remove("X-AspNetMvc-Version");
    Response.AddHeader("Strict-Transport-Security", "max-age=300");
    Response.AddHeader("X-Frame-Options", "SAMEORIGIN");
}
Run Code Online (Sandbox Code Playgroud)

之后,对网站和API的所有调用都会返回更安全的标头,如下所示:

HTTP/1.1 500 Internal Server Error
Cache-Control: private
Content-Type: text/html; charset=utf-8
Date: Wed, 05 Jun 2013 00:27:54 GMT
Content-Length: 3687
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.但是,我在Firebug中注意到,如果你查看静态内容(例如,loading.gif),它仍然包含服务器头.

HTTP/1.1 304 Not Modified
Cache-Control: no-cache
Accept-Ranges: bytes
Etag: "a3f2a35bdf45ce1:0"
Server: Microsoft-IIS/8.0
Date: Tue, 25 Jun 2013 18:33:16 GMT
Run Code Online (Sandbox Code Playgroud)

我假设这是由IIS以某种方式处理,但无法找到任何地方删除该标头.我试过添加:

<remove name="Server" /> 
Run Code Online (Sandbox Code Playgroud)

如上所述,到Web.config中的httpProtocol/customHeaders部分.我还尝试进入IIS管理器的HTTP响应标头部分,并为服务器标头添加假名称/值对.在这两种情况下,它仍然会返回

Server: Microsoft-IIS/8.0
Run Code Online (Sandbox Code Playgroud)

加载任何图像,C​​SS或JS时.在哪里/我需要设置什么来解决这个问题?

Gab*_*iel 11

此答案以及本网站中的方式相同:,您应该使用以下步骤:

C#:

namespace MvcExtensions.Infrastructure
{
    public class CustomServerName : 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文件:

<system.webServer>
   <modules>
      <add name="CustomHeaderModule" type="MvcExtensions.Infrastructure.CustomServerName" />
   </modules>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)


ph1*_*1ll 8

不幸的是,托管代码模块仅适用于通过ASP.NET管道传递的代码,而其他人正确地建议可以通过托管代码强制所有请求,我个人觉得这不太理想.

为了从所有请求中删除标头,包括默认直接提供而不是通过托管代码提供的静态内容,可以使用Native-Code模块.不幸的是,Native-Code模块编写起来要困难得多,因为它们使用win32 API而不是ASP.NET,但根据我的经验,它们更适合删除标题.

以下链接包含可用于删除标头的Native-Code模块的二进制文件和源代码.它不需要额外的配置来删除"服务器"标头,但可以在IIS配置中添加要删除的其他标头.

http://www.dionach.com/blog/easily-remove-unwanted-http-headers-in-iis-70-to-85


小智 7

没有一个简单列出的解决方案的唯一一个是"服务器"标题.通过在web.config中添加它,我能够在IIS和Azure网站中将其删除

<system.webServer>
  <security>
    <requestFiltering removeServerHeader="true" />
  </security>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,这仅适用于 IIS 10.0+,并且 OP 在问题标题中指出他的情况涉及 IIS 7/8。 (2认同)

Bil*_*egg 6

通过将其添加到webconfig中,您应该能够强制所有请求通过托管代码:

<modules runAllManagedModulesForAllRequests="true">
Run Code Online (Sandbox Code Playgroud)

然后,即使静态文件也应遵守您的标头规则。

  • 您不应该使用它。这很浪费资源:http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html (3认同)

Aat*_*ira 6

使用 IIS UrlRewrite 2.0 来清空服务器响应标头。在Web.config文件中添加以下代码

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

/sf/answers/883117931/