c# - content security policy iis
從IIS 7/8中的靜態內容中刪除服務器標頭 (4)
作為努力使我們的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
Web.config文件:
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
</customHeaders>
</httpProtocol>
的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");
}
之後,對網站和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
到現在為止還挺好。 但是,我在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
我假設這是由IIS以某種方式處理,但無法找到任何地方刪除該標頭。 我試過添加:
<remove name="Server" />
如上所述,到Web.config中的httpProtocol / customHeaders部分。 我還嘗試進入IIS管理器的HTTP響應標頭部分,並為服務器標頭添加假名稱/值對。 在這兩種情況下,它仍然會返回
Server: Microsoft-IIS/8.0
加載任何圖像,CSS或JS時。 在哪裡/我需要設置什麼來解決這個問題?
不幸的是,託管代碼模塊僅適用於通過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
使用IIS UrlRewrite 2.0來消隱Server響應頭。 在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>
https://.com/a/12615970/5810078
沒有一個簡單列出的解決方案的唯一一個是“服務器”標題。 通過在web.config中添加它,我能夠在IIS和Azure網站中將其刪除
<system.webServer>
<security>
<requestFiltering removeServerHeader="true" />
</security>
</system.webServer>
與此答案以及本網站中的方式相同: ,您應該使用以下步驟:
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");
}
}
}
Web.config文件:
<system.webServer>
<modules>
<add name="CustomHeaderModule" type="MvcExtensions.Infrastructure.CustomServerName" />
</modules>
</system.webServer>