Annotation of embedaddon/libnet/sample/dns.c, revision 1.1.1.3
1.1 misho 1: /*
2: *
3: * libnet 1.1
4: * Build a DNSv4 packet
5: * To view: /usr/sbin/tcpdump -vvvvven -s 0 port 53
6: *
1.1.1.2 misho 7: * Copyright (c) 2003 Frédéric Raynal <pappy@security-labs.org>
1.1 misho 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: #include "./libnet_test.h"
34:
35: void
36: usage(char *prog)
37: {
38: fprintf(stderr, "Usage: %s -d dst_ip -q query_host [-s src_ip] [-t]\n", prog);
39: exit(1);
40: }
41:
42:
43: int
44: main(int argc, char *argv[])
45: {
46: char c;
47: u_long src_ip = 0, dst_ip = 0;
48: u_short type = LIBNET_UDP_DNSV4_H;
49: libnet_t *l;
50:
51: libnet_ptag_t ip;
52: libnet_ptag_t ptag4; /* TCP or UDP ptag */
53: libnet_ptag_t dns;
54:
55: char errbuf[LIBNET_ERRBUF_SIZE];
56: char *query = NULL;
57: char payload[1024];
58: u_short payload_s;
59:
60: printf("libnet 1.1 packet shaping: DNSv4[raw]\n");
61:
62: /*
1.1.1.3 ! misho 63: * Initialize the library. Root privileges are required.
1.1 misho 64: */
65: l = libnet_init(
66: LIBNET_RAW4, /* injection type */
67: NULL, /* network interface */
68: errbuf); /* error buffer */
69:
70: if (!l)
71: {
72: fprintf(stderr, "libnet_init: %s", errbuf);
73: exit(EXIT_FAILURE);
74: }
75:
76: /*
77: * parse options
78: */
79: while ((c = getopt(argc, argv, "d:s:q:t")) != EOF)
80: {
81: switch (c)
82: {
83:
84: case 'd':
85: if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
86: {
87: fprintf(stderr, "Bad destination IP address: %s\n", optarg);
88: exit(EXIT_FAILURE);
89: }
90: break;
91: case 's':
92: if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
93: {
94: fprintf(stderr, "Bad source IP address: %s\n", optarg);
95: exit(EXIT_FAILURE);
96: }
97: break;
98: case 'q':
99: query = optarg;
100: break;
101: case 't':
102: type = LIBNET_TCP_DNSV4_H;
103: break;
104: default:
105: exit(EXIT_FAILURE);
106: }
107: }
108:
109: if (!src_ip)
110: {
111: src_ip = libnet_get_ipaddr4(l);
112: }
113:
114: if (!dst_ip || !query)
115: {
116: usage(argv[0]);
117: exit(EXIT_FAILURE);
118: }
119:
120: /*
121: * build dns payload
122: */
123: payload_s = snprintf(payload, sizeof payload, "%c%s%c%c%c%c%c",
124: (char)(strlen(query)&0xff), query, 0x00, 0x00, 0x01, 0x00, 0x01);
125:
126: /*
127: * build packet
128: */
129: dns = libnet_build_dnsv4(
130: type, /* TCP or UDP */
131: 0x7777, /* id */
132: 0x0100, /* request */
133: 1, /* num_q */
134: 0, /* num_anws_rr */
135: 0, /* num_auth_rr */
136: 0, /* num_addi_rr */
1.1.1.3 ! misho 137: (uint8_t *)payload,
1.1 misho 138: payload_s,
139: l,
140: 0
141: );
142:
143: if (dns == -1)
144: {
145: fprintf(stderr, "Can't build DNS packet: %s\n", libnet_geterror(l));
146: goto bad;
147: }
148:
149: if (type == LIBNET_TCP_DNSV4_H) /* TCP DNS */
150: {
151: ptag4 = libnet_build_tcp(
152: 0x6666, /* source port */
153: 53, /* destination port */
154: 0x01010101, /* sequence number */
155: 0x02020202, /* acknowledgement num */
156: TH_PUSH|TH_ACK, /* control flags */
157: 32767, /* window size */
158: 0, /* checksum */
159: 0, /* urgent pointer */
160: LIBNET_TCP_H + LIBNET_TCP_DNSV4_H + payload_s, /* TCP packet size */
161: NULL, /* payload */
162: 0, /* payload size */
163: l, /* libnet handle */
164: 0); /* libnet id */
165:
166: if (ptag4 == -1)
167: {
168: fprintf(stderr, "Can't build UDP header: %s\n", libnet_geterror(l));
169: goto bad;
170: }
171:
172:
173: ip = libnet_build_ipv4(
174: LIBNET_IPV4_H + LIBNET_TCP_H + type + payload_s,/* length */
175: 0, /* TOS */
176: 242, /* IP ID */
177: 0, /* IP Frag */
178: 64, /* TTL */
179: IPPROTO_TCP, /* protocol */
180: 0, /* checksum */
181: src_ip, /* source IP */
182: dst_ip, /* destination IP */
183: NULL, /* payload */
184: 0, /* payload size */
185: l, /* libnet handle */
186: 0); /* libnet id */
187:
188: if (ip == -1)
189: {
190: fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
191: exit(EXIT_FAILURE);
192: }
193:
194: }
195: else /* UDP DNS */
196: {
197: ptag4 = libnet_build_udp(
198: 0x6666, /* source port */
199: 53, /* destination port */
200: LIBNET_UDP_H + LIBNET_UDP_DNSV4_H + payload_s, /* packet length */
201: 0, /* checksum */
202: NULL, /* payload */
203: 0, /* payload size */
204: l, /* libnet handle */
205: 0); /* libnet id */
206:
207: if (ptag4 == -1)
208: {
209: fprintf(stderr, "Can't build UDP header: %s\n", libnet_geterror(l));
210: goto bad;
211: }
212:
213:
214: ip = libnet_build_ipv4(
215: LIBNET_IPV4_H + LIBNET_UDP_H + type + payload_s,/* length */
216: 0, /* TOS */
217: 242, /* IP ID */
218: 0, /* IP Frag */
219: 64, /* TTL */
220: IPPROTO_UDP, /* protocol */
221: 0, /* checksum */
222: src_ip, /* source IP */
223: dst_ip, /* destination IP */
224: NULL, /* payload */
225: 0, /* payload size */
226: l, /* libnet handle */
227: 0); /* libnet id */
228:
229: if (ip == -1)
230: {
231: fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
232: exit(EXIT_FAILURE);
233: }
234: }
235:
236: /*
237: * write to the wire
238: */
239: c = libnet_write(l);
240: if (c == -1)
241: {
242: fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
243: goto bad;
244: }
245: else
246: {
247: fprintf(stderr, "Wrote %d byte DNS packet; check the wire.\n", c);
248: }
249: libnet_destroy(l);
250: return (EXIT_SUCCESS);
251: bad:
252: libnet_destroy(l);
253: return (EXIT_FAILURE);
254: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>