徹底移除 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 輕巧做掉的工作通通被導進爲複雜情境設計的笨重程序,對效能很傷。

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。

安裝程序在背後做了一堆事:

  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 消失,成功!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章