完全移除 IIS Response Header 版本信息

综观常见的几种作法,无论是用 IHttpModule 或 Global.asax.cs 在 PreSendRequestHeader() 将 Server Header 移除,都只对 ASP.NET WebForm 或 ASP.NET MVC 有效,***者只要改下载 HTML/JS/CSS/JPG/PNG 等静态档案,甚至随便想个不存在的 html,HTTP 404 Reponse 冒出 Server: Microsoft-IIS/10.0 当场破功,白忙半天。
安全


这是由于静态内容由 IIS 直接处理,不会通过咱们设计的机制(延伸阅读:system.web 与 system.webServer )。网络


有个笨方法,设定 <modules runAllManagedModulesForAllRequests="true"> 将全部静态档案也导入 ASP.NET Pipeline,虽然管用,但本来由 IIS 轻巧作掉的工做统统被导进为复杂情境设计的笨重程序,对效能很伤。
mvc

Server Header 是当中最棘手的项目,IIS Manager HTTP Response Headers 或 URL Rewrite Module 能够改写或清空 Server Header,但没法移除,而 UrlScan 能够清除 Server Header 只支持到 IIS 7。
app

最后我找到一个不错的解决方案 - StripHeaders。 一个 C++ 开发的开源模块,使用 WIN32 API 在 IIS 核心执行,能涵盖静态内容,核心模块的 Overhead 低,加上原生程序执行效能远比 .NET 程序快,较不用担忧效能问题。
ide

IIS 原生模块的安装程序蛮多,不过 StripHeaders 提供MSI 安装档,大大简化安装步骤。 目前最新版 iis_stripheaders_module_1.0.5.msi 于 2016-11-19 推出,支持 Server 2016。

安装程序在背后作了一堆事:

  1. Installs stripheaders.dll

  2. Registers the Native-Code module with IIS using the appcmd.exe command

  3. Extends the IIS configuration schema to allow setting of headers to remove

  4. Adds default settings to the IIS configuration to remove the common "Server", "X-Powered-By" and "X-Aspnet-Version" respon se headers

  5. Adds a registry setting to remove the "Server: Microsoft-HTTPAPI/2.0" response header.

理论上重开机后就会生效,若是你不想重开机,可使用net stop http 重启底层 HTTP 服务再手动启动 IIS 及其余相依服务。 不过我实测时停用 HTTP 失败(处于停用中的状态,一直关不掉),最后只能重开机。 但我遇的情况是重开完也没生效,最后参考 Github 的安装程序原始码(Open Source 万岁!),手动注册 StripHeadersModule 才解决问题:

appcmd install module /name:StripHeadersModule /image:%windir%\system32\inetsrv\stripheaders.dll /add:true /lock:true


安装稳当后,以下图应该要在 IIS 模块列表看到 StripHeadersModule:



StripHeaders 默认会移除 Server、X-Powered-By、X-AspNet-Version 等 Response Header,不需修改 web.config 就会生效。 如需移除额外 Header,则可在 web.config system.webServer/stripHeaders 中设定。


以 css 实测,未启用 StripHeaders 前:


启用后,Server、X-Powered-By 消失,成功!

相关文章
相关标签/搜索