-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.c
181 lines (138 loc) · 4.79 KB
/
dns.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <getopt.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <netinet/udp.h>
#include <time.h>
#include "dns.h"
void formatDNSName(unsigned char* dns,unsigned char* host)
{
if(strncmp((const char *) host, ".", 1) == 0)
*dns++ = '\0';
else {
char *token = strtok((char *) host, ".");
do {
*dns++ = strlen((char *) token);
for(int i = 0; i < strlen((const char *) token) ; i++)
*dns++ = token[i];
} while((token = strtok(NULL, ".")) != NULL);
}
*dns++ = '\0';
}
unsigned char *addEDNS(unsigned char *buffer, int *payloadSize) {
unsigned char *tmp = NULL;
tmp = realloc(buffer, (*payloadSize) + 11);
if(!tmp) {
perror("addEDNS :: Couldn't reallocate buffer");
}
void *edns = (void *) &tmp[*payloadSize + 1];
memset(edns, 0x00, 1);
memset(edns + 1, 0x29, 1); //set edns RR type code '41'
memset(edns + 2, 0xFF, 2); //set edns send UDP Max size
memset(edns + 4, 0x00, 7); //set remaining fields to 0
*payloadSize += 11;
return tmp;
}
unsigned char *addQuestion(unsigned char *buffer, int *payloadSize) {
unsigned char *tmp = NULL;
tmp = realloc(buffer, (*payloadSize) + sizeof(struct QUES));
if(!tmp) {
perror("addQuestion :: Couldn't reallocate buffer");
exit(EXIT_FAILURE);
}
struct QUES *question = (struct QUES *)&tmp[*payloadSize];
question -> qtype = htons(255);
question -> qclass = htons(1);
*payloadSize += sizeof(struct QUES);
return tmp;
}
unsigned char *addRecord(unsigned char *buffer, unsigned char *query_name, int *payloadSize) {
unsigned char *tmp = NULL;
tmp = realloc(buffer, (*payloadSize) + strlen((const char *) query_name) + 1);
if(!tmp) {
perror("addRecord :: Couldn't reallocate buffer");
exit(EXIT_FAILURE);
}
unsigned char *qname = &tmp[*payloadSize];
formatDNSName(qname, query_name);
*payloadSize += strlen( (const char *) qname) + 1;
return tmp;
}
unsigned char *encapsulateDNS(unsigned char *buffer, int *payloadSize) {
unsigned char *tmp = NULL;
tmp = realloc(buffer, (*payloadSize) + sizeof(struct DNS_HDR));
if(!tmp) {
perror("encapsulateDNS :: Couldn't reallocate buffer");
exit(EXIT_FAILURE);
}
memcpy(&tmp[sizeof(struct DNS_HDR)], buffer, *payloadSize);
struct DNS_HDR *dnsh = (struct DNS_HDR *) tmp;
dnsh -> id = (unsigned short) htons(rand());
dnsh -> qr = 0; //this is a query
dnsh -> op = 0;
dnsh -> aa = 0;
dnsh -> tc = 0;
dnsh -> rd = 1; //want resolver to do the resolution
dnsh -> ra = 0;
dnsh -> z = 0;
dnsh -> ad = 0;
dnsh -> cd = 0;
dnsh -> rcode = 0;
dnsh -> ques_count = htons(1);
dnsh -> ans_count = 0;
dnsh -> auth_count = 0;
dnsh -> add_count = htons(1);
*payloadSize += sizeof(struct DNS_HDR);
return tmp;
}
void *dns_listen_thread(void *args) {
char * file_name = args;
FILE *outfile = fopen(file_name, "w");
if(outfile == NULL) {
printf("Error opening outfile!\n");
exit(-1);
}
else {
printf("Starting listener to file: %s...\n", file_name);
}
unsigned char *rcv_buff = malloc(65536);
struct sockaddr_in any;
socklen_t len = sizeof(any);
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
char *host = malloc(17);
memset(host, 0 , 17);
if(sockfd < 0) {
perror("Couldn't create socket");
exit(-1);
}
while(1) {
int resp_size = recvfrom(sockfd, rcv_buff, 65536, 0, (struct sockaddr *) &any, &len);
if(resp_size < 0) {
perror("Error getting response :/");
exit(-1);
}
struct iphdr *iph = (struct iphdr*) rcv_buff;
if(iph -> protocol == 17) {
unsigned short ipheader_len = iph -> ihl * 4;
struct udphdr *udph = (struct udphdr *) (rcv_buff + ipheader_len);
if(ntohs(udph->source) == 53) {
//Get ip address of server that responded
host = inet_ntoa(any.sin_addr);
if(strncmp(host, "127", 3) == 0 || strncmp(host, "192", 3) == 0 || strncmp(host, "10.", 3) == 0) continue; //ignore local host ? ip addr = FF.FF.FF.FF, so if any.sin_addr < 0xFF000000 ?
struct DNS_HDR * dns_header = (struct DNS_HDR *) (rcv_buff + ipheader_len + 8);
if(dns_header -> ra == 1) {
//got a answer
fprintf(outfile,"%s %d\n", host, resp_size);
fflush(outfile);
}
}
}
}
}