ASP.NET Web.config & Http Headers 安全設定大全 (Guide to Secure your Web application by web.config configuration)


  1. 說明
    1. 伺服器資訊隱藏
    2. 避免點擊劫持 (Clickjacking Vulnerability)
    3. 關閉 Debug Mode
    4. 主動關閉目錄瀏覽
    5. 防護 XSS Attacks
    6. Content sniffing
    7. Set Referrer to Origin
    8. Enable HTTP Strict Transport Security (HSTS)
    9. 防止 Cookie 被竊取
    10. 防止 HTTP Header 惡意注入
    11. Contentt Security Policy (CSP)
  2. 結論
  3. 參考資料

不論是程式開發或是資安實務都會提到對於 Web.config 的安全設定,本筆記整理各種無論是隱藏伺服器資訊、保護 Cookie, Session 等設定 Web.config 的方式 💡

logo

說明

首先建議先使用 securityheaders 來驗證自己網站 Http Headers 的安全性,再根據不足的部分加以設定。

伺服器資訊隱藏

用途: 減少被滲透測試時偵查 (reconnaissance) 到的資訊。

設定:在 web.config 加入下列的設定以進行伺服器資訊的隱藏。

<system.web>
    <httpRuntime targetFramework="*" enableVersionHeader="false" />
</system.web>

<system.webServer>
   <httpProtocol>
     <customHeaders>
       <remove name="X-Powered-By" />
       <remove name="X-AspNetMvc-Version" />
     </customHeaders>
   </httpProtocol>
</system.webServer>

避免點擊劫持 (Clickjacking Vulnerability)

關鍵字:Clickjacking: X-Frame-Options header missing

用途:避免自己的網站被其他釣魚網站藉由 IFrame 的方式鑲嵌誘騙使用者看似點擊自己的網站,實則是被導向惡意的連結。設定後,其他的網頁在瀏覽器的渲染上會無法以 IFrame 的方式加載自己的網站。

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="X-Frame-Options" value="DENY" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

其他的選項還有 SAMEORIGINALLOW-FROM

X-Frame-Options

關閉 Debug Mode

在正式環境不應開啟 Debug Mode,以減少提供惡意使用者可以攻擊的地方。

<configuration>
	<system.web>
    <compilation debug="false" />
  </system.web>
</configuration>

可以進一步的設定 Deployment 為 Retail,設定以後會主動啟用 customErrros 並且將 compilation debug 設定為關閉,此外 trace 也會被關閉。

<system.web>
  <deployment retail="true" />
</system.web>

主動關閉目錄瀏覽

Web.config 會繼承自伺服器層級的設定檔,預設上不提供目錄瀏覽,但可以主動在 Web.config 加上縱深防禦的設定。

伺服器層級設定檔路徑 %systemroot%\system32\inetsrv\config\applicationHost.config

<configuration>
  <directoryBrowse enable="false"/>
</configuration>  

防護 XSS Attacks

用途:藉由 Http Header 的設定,藉由此 Header 當 Browser 偵測到 XSS 時會停止載入網頁。對當代瀏覽器的支援性不高,更適合的替代方式是使用 Content-Security-Policy

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="X-Xss-Protection" value="1; mode=block" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

MDN - X-XSS-Protection

MDN - Content-Security-Policy

Content sniffing

用途:要求使用者端 (Client) 要遵照伺服器端 Content-Type MIME 設定,不要由瀏覽器自行判斷 MIME 類型。

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="X-Content-Type-Options" value="nosniff" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

X-Content-Type-Options

Set Referrer to Origin

用途:Referrer Header 會記錄目前的瀏覽是源自的前一個連結,藉由調整 Referrer Policy 可以避免瀏覽紀錄被追蹤。

Tips: View Referrer by using Chrome Developer Tool document.referrer

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Referrer-Policy" value="No-referrer-when-downgrade" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

相關的嚴謹與寬鬆的傳送設定如下:

No-referrer — No referrer

No-referrer-when-downgrade — (Default) Only HTTPS to HTTPS

Origin — domain not full path

Origin-when-cross-origin — full path : same domain ; domain : another website

Same-origin — full path : same domain ; domain : another website

Strict-origin — domain for HTTPS to HTTPS and HTTP to HTTP

Strict-origin-when-cross-origin — full path : same domain, HTTPS to HTTPS ; domain : HTPPS CORS

Unsafe-url — full path

Referrer-Policy

yu-jack - Referrer-Policy

Enable HTTP Strict Transport Security (HSTS)

關鍵字:HTTP Strict Transport Security (HSTS) not implemented

用途:除了 Rewrite URL, Redirect URL 外,直接藉由 Http Header 的方式要求使用者瀏覽器採用 Https 的方式訪問網站。

需要注意的是 HSTS 必須設定在 HTTPS Header 中,不可以設定在 Http Header,因此設定必須藉由 URL Rewrite 來完成。

How to enable HTTP Strict Transport Security (HSTS) in IIS7+

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="HTTP to HTTPS redirect" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
                        redirectType="Permanent" />
                </rule>
            </rules>
            <outboundRules>
                <rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
                    <match serverVariable="RESPONSE_Strict_Transport_Security"
                        pattern=".*" />
                    <conditions>
                        <add input="{HTTPS}" pattern="on" ignoreCase="true" />
                    </conditions>
                    <action type="Rewrite" value="max-age=31536000;includeSubDomains;preload" />
                </rule>
            </outboundRules>
        </rewrite>
    </system.webServer>
</configuration>

關鍵字:Cookies without HttpOnly flag set、Cookies without Secure flag set

用途:藉由限制 Cookies 受使用者端的 JS 操作權限,減少 XSS 攻擊以及使用者資料被竊取的可能性。

<system.web>
   <httpCookies httpOnlyCookies="true" requireSSL="true"/>
</system.web>

The ultimate guide to secure cookies with web.config in .NET

防止 HTTP Header 惡意注入

用途:藉由檢查 Http Header,避免攻擊者將不當的資訊藉由 Response Header 的方式傳入使用者的瀏覽器環境當中。啟用 Checking 會有效能上的影響,使用上可以評估是否有需要。

<system.web>
  <httpRuntime targetFramework="*" enableHeaderChecking="true" />
</system.web>

.Net EnableHeaderChecking

Contentt Security Policy (CSP)

HTTP Header Content Security Policy (CSP)

MDN - CSP

結論

<system.web>
    <!-- Hide Server Information -->
    <httpRuntime targetFramework="*" enableVersionHeader="false" />
    <!-- Protect Cookie -->
    <httpCookies httpOnlyCookies="true" requireSSL="true"/>
    <!-- Disable Debug, Tarce & Enable Custom Errors -->
    <deployment retail="true" />
    <!-- Disable Debug in depth-->
    <compilation debug="false" />
</system.web>

<system.webServer>
   <httpProtocol>
     <customHeaders>
     <!-- Hide Server Information -->
      <remove name="X-Powered-By" />
      <remove name="X-AspNetMvc-Version" />
      <!-- Referrer Policy -->
      <add name="Referrer-Policy" value="No-referrer-when-downgrade" />
      <!-- Anti-Clickjacking-->
      <add name="X-Frame-Options" value="DENY" />
      <!-- HSTS -->
      <add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains" />
     </customHeaders>
   </httpProtocol>
</system.webServer>

參考資料