轉自:http://blog.darkthread.net/post-2018-01-30-remove-iis-response-server-header.aspxcss
從 IIS Reponse Header 移除 Server、X-AspNet-Version、X-Powered-By 等版本信息,可下降因曝露信息被鎖定***的機率,被視爲提升資安防禦的手段(效果高低見仁見智, 但有些資安掃瞄將此列爲弱點,不作也得作)。 這已算是老話題,網絡上有很多討論與參考文章:
html
Troy Hunt: Shhh... don’t let your response headers talk too loudlygithub
綜觀常見的幾種作法,無論是用 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。
安裝程序在背後作了一堆事:
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 消失,成功!