将 [
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..)
我已经研究这个问题有一段时间了,并得出结论,将 EnableHeaderChecking 设置为
true
实际上足以防止 http 标头注入攻击。
查看“反射”的 ASP.NET 代码,我发现:
HttpResponseHeader
(内部)的实例HttpResponseHeader.MaybeEncodeHeader
(对于IIS7WorkerRequests
)HttpResponseHeader
实例是在发送 RedirectLocation 或 ContentType 等已知标头之前创建的 (HttpResponse.GenerateResponseHeaders
)HttpResponseHeader
构造函数检查EnableheaderChecking设置,并在设置为HttpResponseHeader.MaybeEncodeHeader
时调用
true
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 报告此类问题。
AFAIK 这已经足够了,应该默认打开。
我认为 Fortify 只是考虑深度防御,就像更改部署中的配置等一样。
我假设你没有在你的配置中严格设置它,如果你有,也许 Fortify 没有那么聪明地认为我们的。
确认的最佳方法是利用它:)
EnableHeaderChecking 仅适用于不可信数据。如果您将数据直接从 cookie 传递到重定向,则生成的标头可能被认为是可信的并且 值不会被转义。
Josef,HttpResponse.AppendHeader() 并不是不受信任的数据可以进入 HTTP 响应标头的唯一地方。
如果数据包含回车符(或任何被解释为回车符的内容),则最终出现在 Cookie 或 HTTP 重定向中的攻击者的任何数据都可以写入新标头。
一般来说,利用时间来验证数据比坐下来尝试找出漏洞要好得多。黑客很可能比你我更擅长。
考虑到该线程的年龄,我需要一个现代的解决方案。我在相关帖子中发布了我喜欢的一个。