[研究]Fortify SCA報告 HTML5: MINE Sniffing (HTML5:MINE 嗅探) 之解決測試
2025-02-04
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19
Fortify SCA => OpenText Fortify Static Code Analyzer
********************************************************************************
![]() |
Dir1\Web.config
<?xml version="1.0"?> <configuration> <system.web> <authorization> <allow roles="Administrator"/> <deny users="*"/> </authorization> </system.web> </configuration> |
Dir2\Web.config
<?xml version="1.0"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="X-Content-Type-Options" value="nosniff"/>
</customHeaders>
</httpProtocol>
</system.webServer>
<system.web>
<authorization>
<allow roles="Administrator"/>
<deny users="*"/>
</authorization>
</system.web>
</configuration>
|
********************************************************************************
掃描結果
點選那2筆 Web.config 檢視,也就是 Dir2 目錄的 Web.Config 添加內容後,可以解決。
********************************************************************************
補充:如果發行(Deploy)時出錯 ( Compile 可能會通過),出現錯誤訊息:
每個組態檔只允許一個 <configSections> 元素,而且 (如果有) 它必須是根 <configuration> 元素的第一個子系。
這是指<configSections>區域必須優先放在<configuration>之下。
********************************************************************************
補充:在唯一金鑰屬性 'name' 設為 'X-Content-Type-Options' 的情況下,無法新增類型 'add' 的重複集合項目
Compile 和 Deploy 都成功,但執行失敗
HTTP 錯誤 500.19 - Internal Server Error
無法存取要求的網頁,因為與該網頁相關的設定資料不正確。
詳細錯誤資訊:
模組 ProtocolSupportModule
通知 SendResponse
處理常式 PageHandlerFactory-Integrated-4.0
錯誤碼 0x8007000d
設定錯誤 在唯一金鑰屬性 'name' 設為 'X-Content-Type-Options' 的情況下,無法新增類型 'add' 的重複集合項目
設定檔案 \\?\D:\Production\Web\web.config
要求的 URL https://localhost:1071/default.aspx
實體路徑 D:\Production\Web\default.aspx
登入方法 匿名
登入使用者 匿名 |
設定來源:
342: <add name="X-XSS-Protection" value="1; mode=block" />
343: <add name="X-Content-Type-Options" value="nosniff" />
344: <add name="Cache-Control" value="no-store" />
|
發現同一個 Web.config 中有相同重複的 <add name="X-Content-Type-Options" value="nosniff" /> 存在,移除一個。
****************************************
補充:Compile 和 Deploy 都成功,但執行依然失敗
若根目錄中Web.Config 已經設定,子目錄中多加一行 <remove name="X-Content-Type-Options" />
<?xml version="1.0"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="X-Content-Type-Options" />
<add name="X-Content-Type-Options" value="nosniff"/>
</customHeaders>
</httpProtocol>
</system.webServer>
<system.web>
<authorization>
<allow roles="Administrator"/>
<deny users="*"/>
</authorization>
</system.web>
</configuration>
|
****************************************
補充:再次失敗,移除子目錄 Web.Config 中
<remove name="X-Content-Type-Options" />
<add name="X-Content-Type-Options" value="nosniff"/>
整個專案僅有根目錄 Web.Config 中有此設定,結果執行時期,依然有此訊息,可能 IIS 層級的 ApplicationHost.config 或 Machine.config 預設已經有此設定或防護,根本不需要在 Web.Config 中添加,在 Web.Config 中添加了反而執行失敗,不添加 Fortify SCA以為沒有防護而誤判。
********************************************************************************
甚麼是 HTML5: MIME Sniffing
ChatGPT 說:
MIME Sniffing(MIME 嗅探)是一種技術,當瀏覽器收到網頁伺服器回應時,如果 HTTP Content-Type 標頭與實際內容不符,瀏覽器會嘗試分析(嗅探)內容,並根據其判斷來解析檔案的 MIME 類型。
MIME Sniffing 的運作方式
1.伺服器回應檔案時,會在 Content-Type 標頭中指定 MIME 類型,例如:
Content-Type: text/plain
2.瀏覽器接收後,會根據 Content-Type 標頭處理內容。
3.如果 Content-Type 與內容不匹配,某些瀏覽器(如 Chrome、Edge)可能會檢查內容的開頭(例如前 256 個位元組),嘗試推測實際類型。
4.如果瀏覽器認為檔案應該是其他類型(如 text/html 或 image/jpeg),它可能會忽略 Content-Type 標頭並使用自己的推測結果來解析。
MIME Sniffing 的安全風險
MIME Sniffing 可能會導致 MIME Confusion Attack(MIME 混淆攻擊),讓惡意網站繞過安全防護,例如:
XSS(跨站腳本攻擊):如果伺服器將惡意 HTML 檔案標示為 text/plain,但瀏覽器透過 MIME Sniffing 解析為 text/html,則可能執行惡意 JavaScript。
跨站請求偽造(CSRF):如果攻擊者能夠欺騙瀏覽器執行惡意請求,可能會影響使用者的帳戶或資料安全。
如何防範 MIME Sniffing 攻擊
1.強制瀏覽器遵守 Content-Type,在 HTTP 標頭加入:
X-Content-Type-Options: nosniff
這會禁止瀏覽器進行 MIME Sniffing,確保它嚴格按照 Content-Type 解析內容。
2.適當設定 Content-Type
確保伺服器正確回應 MIME 類型,例如:
Content-Type: application/json
確保 text/html、application/javascript 等敏感類型不被誤標記為其他格式。
3.Content Security Policy (CSP) 使用 CSP 限制資源載入方式,例如:
Content-Security-Policy: default-src 'self'
總結
- MIME Sniffing 是瀏覽器根據內容推測 MIME 類型的機制。
- 可能導致安全風險,例如 XSS 和 CSRF。
- 建議使用 X-Content-Type-Options: nosniff 防止 MIME Sniffing 攻擊。
相關
沒有留言:
張貼留言