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