Annotation of embedaddon/libnet/sample/ip_raw.c, revision 1.1
1.1 ! misho 1: /*
! 2: *
! 3: * libnet 1.1
! 4: * Build a IPv4 packet with what you want as payload
! 5: *
! 6: * Copyright (c) 2003 Frédéric Raynal <pappy@security-labs.org>
! 7: * All rights reserved.
! 8: *
! 9: * Ex:
! 10: * - send an UDP packet from port 4369 to port 8738
! 11: * ./ip -s 1.1.1.1 -d 2.2.2.2
! 12: *
! 13: * - send a TCP SYN from port 4369 to port 8738
! 14: * ./ip -s 1.1.1.1 -d 2.2.2.2 -t -p `printf "\x04\x57\x08\xae\x01\x01\x01\x01\x02\x02\x02\x02\x50\x02\x7f\xff\xd5\x91\x41\x41"`
! 15: *
! 16: *
! 17: * Redistribution and use in source and binary forms, with or without
! 18: * modification, are permitted provided that the following conditions
! 19: * are met:
! 20: * 1. Redistributions of source code must retain the above copyright
! 21: * notice, this list of conditions and the following disclaimer.
! 22: * 2. Redistributions in binary form must reproduce the above copyright
! 23: * notice, this list of conditions and the following disclaimer in the
! 24: * documentation and/or other materials provided with the distribution.
! 25: *
! 26: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
! 27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 29: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
! 30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 36: * SUCH DAMAGE.
! 37: *
! 38: */
! 39: #if (HAVE_CONFIG_H)
! 40: #include "../include/config.h"
! 41: #endif
! 42: #include "./libnet_test.h"
! 43:
! 44: int
! 45: main(int argc, char *argv[])
! 46: {
! 47: int c;
! 48: libnet_t *l;
! 49: char *device = NULL;
! 50: char *dst = "2.2.2.2", *src = "1.1.1.1";
! 51: u_long src_ip, dst_ip;
! 52: char errbuf[LIBNET_ERRBUF_SIZE];
! 53: libnet_ptag_t ip_ptag = 0;
! 54: u_short proto = IPPROTO_UDP;
! 55: u_char payload[255] = {0x11, 0x11, 0x22, 0x22, 0x00, 0x08, 0xc6, 0xa5};
! 56: u_long payload_s = 8;
! 57:
! 58: printf("libnet 1.1 packet shaping: IP + payload[raw]\n");
! 59:
! 60: /*
! 61: * handle options
! 62: */
! 63: while ((c = getopt(argc, argv, "d:s:tp:i:h")) != EOF)
! 64: {
! 65: switch (c)
! 66: {
! 67: case 'd':
! 68: dst = optarg;
! 69: break;
! 70:
! 71: case 's':
! 72: src = optarg;
! 73: break;
! 74:
! 75: case 'i':
! 76: device = optarg;
! 77: break;
! 78:
! 79: case 't':
! 80: proto = IPPROTO_TCP;
! 81: break;
! 82:
! 83: case 'p':
! 84: strncpy(payload, optarg, sizeof(payload)-1);
! 85: payload_s = strlen(payload);
! 86: break;
! 87:
! 88: case 'h':
! 89: usage(argv[0]);
! 90: exit(EXIT_SUCCESS);
! 91:
! 92: default:
! 93: exit(EXIT_FAILURE);
! 94: }
! 95: }
! 96:
! 97:
! 98: /*
! 99: * Initialize the library. Root priviledges are required.
! 100: */
! 101: l = libnet_init(
! 102: LIBNET_RAW4, /* injection type */
! 103: device, /* network interface */
! 104: errbuf); /* error buffer */
! 105:
! 106: printf("Using device %s\n", l->device);
! 107:
! 108: if (l == NULL)
! 109: {
! 110: fprintf(stderr, "libnet_init() failed: %s", errbuf);
! 111: exit(EXIT_FAILURE);
! 112: }
! 113:
! 114: if ((dst_ip = libnet_name2addr4(l, dst, LIBNET_RESOLVE)) == -1)
! 115: {
! 116: fprintf(stderr, "Bad destination IP address: %s\n", dst);
! 117: exit(EXIT_FAILURE);
! 118: }
! 119:
! 120: if ((src_ip = libnet_name2addr4(l, src, LIBNET_RESOLVE)) == -1)
! 121: {
! 122: fprintf(stderr, "Bad source IP address: %s\n", src);
! 123: exit(EXIT_FAILURE);
! 124: }
! 125:
! 126:
! 127: /*
! 128: * Build the packet
! 129: */
! 130: ip_ptag = libnet_build_ipv4(
! 131: LIBNET_IPV4_H + payload_s, /* length */
! 132: 0, /* TOS */
! 133: 242, /* IP ID */
! 134: 0, /* IP Frag */
! 135: 64, /* TTL */
! 136: proto, /* protocol */
! 137: 0, /* checksum */
! 138: src_ip, /* source IP */
! 139: dst_ip, /* destination IP */
! 140: payload, /* payload */
! 141: payload_s, /* payload size */
! 142: l, /* libnet handle */
! 143: ip_ptag); /* libnet id */
! 144: if (ip_ptag == -1)
! 145: {
! 146: fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
! 147: goto bad;
! 148: }
! 149:
! 150: /*
! 151: * Write it to the wire.
! 152: */
! 153: c = libnet_write(l);
! 154: if (c == -1)
! 155: {
! 156: fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
! 157: goto bad;
! 158: }
! 159: else
! 160: {
! 161: fprintf(stderr, "Wrote %d byte IP packet; check the wire.\n", c);
! 162: }
! 163:
! 164: libnet_destroy(l);
! 165: return (EXIT_SUCCESS);
! 166: bad:
! 167: libnet_destroy(l);
! 168: return (EXIT_FAILURE);
! 169: }
! 170:
! 171: void
! 172: usage(char *name)
! 173: {
! 174: fprintf(stderr,
! 175: "usage: %s [-s source_ip] [-d destination_ip]"
! 176: " [-i iface] [-p payload] [-t]\n",
! 177: name);
! 178: }
! 179:
! 180: /* EOF */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>