彻底移除 IIS Response Header 版本信息
[*] 转自:http://blog.darkthread.net/post-2018-01-30-remove-iis-response-server-header.aspx
[*]
[*] 从 IIS Reponse Header 移除 Server、X-AspNet-Version、X-Powered-By 等版本信息,可降低因曝露信息被锁定***的机率,被视为提高资安防护的手段(效果高低见仁见智, 但有些资安扫瞄将此列为弱点,不做也得做)。 这已算是老话题,网络上有不少讨论与参考文章:
[*] mrkt 的程序学习笔记- ASP.NET MVC - 移除特定的 Response Headers 内容
[*] Troy Hunt: Shhh... don’t let your response headers talk too loudly
[*] KingKong Bruce记事: 提升ASP.NET MVC项目安全性与效能小技巧
综观常见的几种做法,不管是用 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 )。
有个笨方法,设定将所有静态档案也导入 ASP.NET Pipeline,虽然管用,但原本由 IIS 轻巧做掉的工作通通被导进为复杂情境设计的笨重程序,对效能很伤。
Server Header 是当中最棘手的项目,IIS Manager HTTP Response Headers 或 URL Rewrite Module 可以改写或清空 Server Header,但无法移除,而 UrlScan 可以清除 Server Header 只支持到 IIS 7。
最后我找到一个不错的解决方案 - StripHeaders。 一个 C++ 开发的开源模块,使用 WIN32 API 在 IIS 核心执行,能涵盖静态内容,核心模块的 Overhead 低,加上原生程序执行效能远比 .NET 程序快,较不用担心效能问题。
IIS 原生模块的安装程序蛮多,不过 StripHeaders 提供MSI 安装档,大大简化安装步骤。 目前最新版 iis_stripheaders_module_1.0.5.msi 于 2016-11-19 推出,支持 Server 2016。
安装程序在背后做了一堆事:
[*] Installs stripheaders.dll
[*] Registers the Native-Code module with IIS using the appcmd.exe command
[*] Extends the IIS configuration schema to allow setting of headers to remove
[*] Adds default settings to the IIS configuration to remove the common "Server", "X-Powered-By" and "X-Aspnet-Version" respon se headers
[*] 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 消失,成功!
页:
[1]