Annotation of embedaddon/libnet/sample/sebek.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  *
        !             3:  *  libnet 1.1
        !             4:  *  Build a Sebek packet
        !             5:  *
        !             6:  *  Copyright (c) 2004 Frederic Raynal <pappy@security-labs.org>
        !             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: #if ((_WIN32) && !(__CYGWIN__)) 
        !            34: #include "../include/win32/config.h"
        !            35: #else
        !            36: #include "../include/config.h"
        !            37: #endif
        !            38: #endif
        !            39: #include "./libnet_test.h"
        !            40: 
        !            41: void usage(char *name)
        !            42: {
        !            43:     fprintf(stderr,
        !            44:            "usage: %s [-D eth_dst] [-s source_ip] [-d destination_ip]"
        !            45:            "[-u UDP port] [-m magic] [-v version] [-t type] [-S sec] [-U usec] [-P PID] [-I UID] [-f FD] [-c cmd]"
        !            46:            " [-i iface] [-p payload]\n",
        !            47:            name);
        !            48: 
        !            49: }
        !            50: 
        !            51: 
        !            52: int
        !            53: main(int argc, char *argv[])
        !            54: {
        !            55:     int c, port = 1101;
        !            56:     libnet_t *l;
        !            57:     char *device = NULL;
        !            58:     char *eth_dst = "11:11:11:11:11:11";
        !            59:     char *dst = "2.2.2.2", *src = "1.1.1.1";
        !            60:     u_long src_ip, dst_ip;
        !            61:     char errbuf[LIBNET_ERRBUF_SIZE];
        !            62:     libnet_ptag_t ptag = 0;
        !            63:     u_char *payload = 0;
        !            64:     char payload_flag = 0;
        !            65:     u_long payload_s = 0;
        !            66:     unsigned int magic = 0x0defaced, 
        !            67:        counter = 0x12345678,
        !            68:        sec = 0, usec = 0,
        !            69:        pid = 1,
        !            70:        uid = 666,
        !            71:        fd = 2;
        !            72:     char *cmd = "./h4ckw0r1D";
        !            73:     unsigned int length = strlen(cmd)+1;
        !            74:     unsigned short version = SEBEK_PROTO_VERSION, type = SEBEK_TYPE_READ;
        !            75: 
        !            76:     printf("libnet 1.1 packet shaping: Sebek[link]\n"); 
        !            77: 
        !            78: 
        !            79:     /*
        !            80:      * handle options
        !            81:      */ 
        !            82:     while ((c = getopt(argc, argv, "D:d:s:u:m:v:t:S:U:P:I:f:c:p:i:h")) != EOF)
        !            83:     {
        !            84:         switch (c)
        !            85:         {
        !            86:             case 'D':
        !            87:                eth_dst = optarg;
        !            88:                 break;
        !            89:             case 'd':
        !            90:                dst = optarg;
        !            91:                 break;
        !            92: 
        !            93:             case 's':
        !            94:                src = optarg;
        !            95:                 break;
        !            96: 
        !            97:            case 'i':
        !            98:                device = optarg;
        !            99:                break;
        !           100: 
        !           101:            case 'u':
        !           102:                port = atoi(optarg);
        !           103:                break;
        !           104: 
        !           105:            case 'm':
        !           106:                magic = strtoul(optarg, NULL, 10);
        !           107:                break;
        !           108: 
        !           109:            case 'v':
        !           110:                version = (unsigned short) strtoul(optarg, NULL, 10);
        !           111:                break;
        !           112: 
        !           113:            case 't':
        !           114:                type = (unsigned short) strtoul(optarg, NULL, 10);
        !           115:                break;
        !           116: 
        !           117:            case 'S':
        !           118:                sec = strtoul(optarg, NULL, 10);
        !           119:                break;
        !           120: 
        !           121:            case 'U':
        !           122:                usec = strtoul(optarg, NULL, 10);
        !           123:                break;
        !           124: 
        !           125:            case 'P':
        !           126:                pid = strtoul(optarg, NULL, 10);
        !           127:                break;
        !           128: 
        !           129:            case 'I':
        !           130:                uid = strtoul(optarg, NULL, 10);
        !           131:                break;
        !           132: 
        !           133:            case 'f':
        !           134:                fd = strtoul(optarg, NULL, 10);
        !           135:                break;
        !           136: 
        !           137:            case 'c':
        !           138:                cmd = optarg; 
        !           139:                length = strlen(cmd);
        !           140:                break;
        !           141: 
        !           142: 
        !           143:            case 'p':
        !           144:                payload_flag = 1;
        !           145:                payload = optarg; 
        !           146:                payload_s = strlen(payload);
        !           147:                break;
        !           148: 
        !           149:            case 'h':
        !           150:                usage(argv[0]);
        !           151:                exit(EXIT_SUCCESS);
        !           152: 
        !           153:             default:
        !           154:                 exit(EXIT_FAILURE);
        !           155:         }
        !           156:     }
        !           157: 
        !           158:   
        !           159:     /*
        !           160:      *  Initialize the library.  Root priviledges are required.
        !           161:      */
        !           162:     l = libnet_init(
        !           163:            LIBNET_LINK_ADV,                        /* injection type */
        !           164:            device,                                 /* network interface */
        !           165:             errbuf);                                /* error buffer */
        !           166: 
        !           167:     if (l == NULL)
        !           168:     {
        !           169:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
        !           170:         exit(EXIT_FAILURE); 
        !           171:     }
        !           172: 
        !           173:     printf("Using device %s\n", l->device);
        !           174: 
        !           175:     if (payload_flag)
        !           176:     {
        !           177:        memset(cmd, 0, sizeof(cmd));
        !           178:        memcpy(cmd, payload, (payload_s < 12 ? payload_s : 12));
        !           179:        length = payload_s;
        !           180:     }
        !           181: 
        !           182: 
        !           183:     if ((dst_ip = libnet_name2addr4(l, dst, LIBNET_RESOLVE)) == -1)
        !           184:     {
        !           185:        fprintf(stderr, "Bad destination IP address: %s\n", dst);
        !           186:        exit(EXIT_FAILURE);
        !           187:     }
        !           188:     
        !           189:     if ((src_ip = libnet_name2addr4(l, src, LIBNET_RESOLVE)) == -1)
        !           190:     {
        !           191:        fprintf(stderr, "Bad source IP address: %s\n", src);
        !           192:        exit(EXIT_FAILURE);
        !           193:     }
        !           194: 
        !           195:     if (!payload)
        !           196:     {
        !           197:        payload = cmd;
        !           198:        payload_s = length;
        !           199:     }
        !           200: 
        !           201: 
        !           202:     ptag = libnet_build_sebek(
        !           203:        magic,
        !           204:        version,
        !           205:        type,
        !           206:        counter,
        !           207:        sec,
        !           208:        usec,
        !           209:        pid,
        !           210:        uid,
        !           211:        fd,
        !           212:        cmd,
        !           213:        /* LIBNET_ETH_H + LIBNET_IPV4_H + LIBNET_UDP_H + LIBNET_SEBEK_H +*/ length,
        !           214:        payload,
        !           215:        payload_s,
        !           216:        l,
        !           217:        0
        !           218:        );
        !           219: 
        !           220:     if (ptag == -1)
        !           221:     {
        !           222:        fprintf(stderr, "Can't build Sebek header: %s\n", libnet_geterror(l));
        !           223:        goto bad;
        !           224:     }
        !           225: 
        !           226:     ptag = libnet_build_udp(
        !           227:        port,                                      /* source port */
        !           228:        port,                                      /* destination port */
        !           229:        LIBNET_UDP_H + LIBNET_SEBEK_H + payload_s, /* packet length */
        !           230:        0,                                         /* checksum */
        !           231:        NULL,                                      /* payload */
        !           232:        0,                                         /* payload size */
        !           233:        l,                                         /* libnet handle */
        !           234:        0);                                        /* libnet id */
        !           235: 
        !           236:     if (ptag == -1)
        !           237:     {
        !           238:        fprintf(stderr, "Can't build UDP header: %s\n", libnet_geterror(l));
        !           239:        goto bad;
        !           240:     }
        !           241: 
        !           242:     ptag = libnet_build_ipv4(
        !           243:        LIBNET_IPV4_H + LIBNET_UDP_H + LIBNET_SEBEK_H + payload_s,/* length */
        !           244:        0,                                          /* TOS */
        !           245:        242,                                        /* IP ID */
        !           246:        0,                                          /* IP Frag */
        !           247:        64,                                         /* TTL */
        !           248:        IPPROTO_UDP,                                /* protocol */
        !           249:        0,                                          /* checksum */
        !           250:        src_ip,                                     /* source IP */
        !           251:        dst_ip,                                     /* destination IP */
        !           252:        NULL,                                       /* payload */
        !           253:        0,                                          /* payload size */
        !           254:        l,                                          /* libnet handle */
        !           255:        0);                                         /* libnet id */
        !           256:     
        !           257:     if (ptag == -1)
        !           258:     {
        !           259:        fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
        !           260:        exit(EXIT_FAILURE);
        !           261:     }
        !           262: 
        !           263:     
        !           264:     eth_dst = libnet_hex_aton(eth_dst, &c);
        !           265:     ptag = libnet_autobuild_ethernet(
        !           266:        eth_dst,                                /* ethernet destination */
        !           267:        ETHERTYPE_IP,                           /* protocol type */
        !           268:        l);                                     /* libnet handle */
        !           269: 
        !           270:     free(eth_dst);
        !           271:     if (ptag == -1)
        !           272:     {
        !           273:         fprintf(stderr, "Can't build ethernet header: %s\n",
        !           274:                 libnet_geterror(l));
        !           275:         goto bad;
        !           276:     }
        !           277: 
        !           278: 
        !           279:     /*
        !           280:      * write to the wire
        !           281:      */
        !           282:     c = libnet_write(l);
        !           283:     if (c == -1)
        !           284:     {
        !           285:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
        !           286:         goto bad;
        !           287:     }
        !           288:     else
        !           289:     {
        !           290:         fprintf(stderr, "Wrote %d byte Sebek packet; check the wire.\n", c);
        !           291:     }
        !           292:     libnet_destroy(l);
        !           293:     return (EXIT_SUCCESS);
        !           294:   bad:
        !           295:     libnet_destroy(l);
        !           296:     return (EXIT_FAILURE);
        !           297: 
        !           298:     return 0;
        !           299: }

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