Asp.net

EnableHeaderChecking=true 是否足以防止 Http Header 注入攻擊?

  • July 22, 2010

將 [ System.Web.Configuration.HttpRuntimeSection.EnableHeaderChecking]( http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.enableheaderchecking(VS.85).aspx)設置為true(預設)是否足以完全阻止 Http響應拆分等標頭注入攻擊?

我之所以問,是因為一個白盒滲透測試工具(fortify)報告了可利用的 http 標頭注入問題HttpResponse.Redirect和 cookie,但我還沒有找到成功執行攻擊的方法。(編輯:..我們啟用了EnableHeaderChecking ..)

我已經研究了一段時間,並得出結論,將EnableHeaderChecking設置為true實際上足以防止 http 標頭注入攻擊。

查看“反映”的 ASP.NET 程式碼,我發現:

  1. 只有一種方法可以將自定義 HTTP 標頭添加到 HTTP 響應,即使用HttpResponse.AppendHeader方法
  2. HttpResponse.AppendHeader要麼
  • 創建HttpResponseHeader(內部)的實例
  • 或呼叫HttpResponseHeader.MaybeEncodeHeader(for IIS7WorkerRequests)
  • 或分配其各自的屬性(對於已知的標頭,如RedirectLocationContentType
  1. HttpResponseHeader在發送RedirectLocationContentType等已知標頭之前創建實例(HttpResponse.GenerateResponseHeaders
  2. 構造HttpResponseHeader函式檢查EnableheaderChecking設置並HttpResponseHeader.MaybeEncodeHeader在設置為時呼叫true
  3. HttpResponseHeader.MaybeEncodeHeader正確編碼換行符,這使得 HTTP 標頭注入攻擊不可能

這是一個大致展示我如何測試的片段:

// simple http response splitting attack
Response.AddHeader("foo", "bar\n" + 
   // injected http response, bad if user provided
   "HTTP/1.1 200 OK\n" + 
   "Content-Length: 19\n" +
   "Content-Type: text/html\n\n" +
   "<html>danger</html>"
);

以上僅在您明確關閉EnableHeaderChecking時才有效:

<httpRuntime enableHeaderChecking="false"/>

Fortify 根本不考慮配置(顯式設置EnableHeaderChecking無效),因此總是報告這些類型的問題。

引用自:https://stackoverflow.com/questions/869361