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