EnableHeaderChecking=true 是否足以防止 Http 标头注入攻击?

问题描述 投票:0回答:5

将 [

System.Web.Configuration.HttpRuntimeSection.EnableHeaderChecking
](http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.enableheaderchecking(VS.85).aspx) 设置为
 是否足够true
(默认)完全防止 Http 标头注入攻击,例如响应拆分等?

我问这个问题是因为白盒渗透测试工具(fortify)报告了

HttpResponse.Redirect
和 cookie 的可利用 http 标头注入问题,但我还没有找到成功执行攻击的方法。 (编辑:..并且我们打开了EnableHeaderChecking..)

asp.net iis security fortify
5个回答
7
投票

我已经研究这个问题有一段时间了,并得出结论,将 EnableHeaderChecking 设置为

true
实际上足以防止 http 标头注入攻击。

查看“反射”的 ASP.NET 代码,我发现:

  1. 只有一种方法可以将自定义 HTTP 标头添加到 HTTP 响应,即使用 HttpResponse.AppendHeader 方法
  2. HttpResponse.AppendHeader 要么
    • 创建
      HttpResponseHeader
      (内部)的实例
    • 或致电
      HttpResponseHeader.MaybeEncodeHeader
      (对于
      IIS7WorkerRequests
    • 或分配其各自的属性(对于已知标头,如RedirectLocationContentType
  3. HttpResponseHeader
    实例是在发送 RedirectLocationContentType 等已知标头之前创建的 (
    HttpResponse.GenerateResponseHeaders
    )
  4. HttpResponseHeader
    构造函数检查EnableheaderChecking设置,并在设置为
    HttpResponseHeader.MaybeEncodeHeader
    时调用
    true
  5. 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 没有效果),因此 always 报告此类问题。


1
投票

AFAIK 这已经足够了,应该默认打开。

我认为 Fortify 只是考虑深度防御,就像更改部署中的配置等一样。

我假设你没有在你的配置中严格设置它,如果你有,也许 Fortify 没有那么聪明地认为我们的。

确认的最佳方法是利用它:)

  • 只需获取一份 fiddler 的副本即可
  • 拦截请求
  • 尝试注入新行
  • 查看您刚刚注入的新行是否在 HTTP 响应中。

0
投票

EnableHeaderChecking 仅适用于不可信数据。如果您将数据直接从 cookie 传递到重定向,则生成的标头可能被认为是可信的并且 值不会被转义。


0
投票

Josef,HttpResponse.AppendHeader() 并不是不受信任的数据可以进入 HTTP 响应标头的唯一地方。

如果数据包含回车符(或任何被解释为回车符的内容),则最终出现在 Cookie 或 HTTP 重定向中的攻击者的任何数据都可以写入新标头。

一般来说,利用时间来验证数据比坐下来尝试找出漏洞要好得多。黑客很可能比你我更擅长。


0
投票

考虑到该线程的年龄,我需要一个现代的解决方案。我在相关帖子中发布了我喜欢的一个。

https://stackoverflow.com/a/77483123/1766253

© www.soinside.com 2019 - 2024. All rights reserved.