-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdns-spoof-detect.c
388 lines (318 loc) · 10.1 KB
/
dns-spoof-detect.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
/*
Tarefa 7 - MC942 - DNS Spoof Detect
Anderson Toshiyuki Sasaki RA058908
Davi Colli Tozoni RA060061
Guilherme Henrique Goulart Pozzato RA061240
*/
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <arpa/inet.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <resolv.h>
#define ETHER_ADDR_LEN 6
typedef unsigned short u16;
typedef unsigned long u32;
char * interface;
/* Estrutura para uma pergunta DNS */
struct question {
char *qname;
char qtype[2];
char qclass[2];
};
/* Estrutura para uma resposta DNS */
struct answer {
char *name;
char atype[2];
char aclass[2];
char ttl[4];
char RdataLen[2];
char *Rdata;
};
/*cabecalho do DNS*/
struct dnshdr {
char id[2];
char flags[2];
char qdcount[2];
char ancount[2];
char nscount[2];
char arcount[2];
};
struct lista_req {
char identificador[100];
char pergunta[100];
char IP[20][100];
int nIP;
struct lista_req * prox;
};
struct lista_req * no_cabeca;
/*função que retorna a posição da "pergunta" na lista ligada de requisições*/
struct lista_req * procuraLista(char * id, struct lista_req ** anterior){
struct lista_req * atual = no_cabeca;
*anterior = NULL;
/*procura por requisicao na lista*/
while(atual!=NULL){
if(!strcmp(atual->identificador, id)){
break;
}
*anterior = atual;
atual = atual->prox;
}
return atual;
}
/*função que remove elemento da lista*/
void removeLista(struct lista_req * elemento, struct lista_req * anterior){
anterior->prox = elemento->prox;
free(elemento);
}
/*função que adiciona IP ao elemento*/
void adicionaIPLista(struct lista_req * elemento, char * IP){
int pos = elemento->nIP;
strcpy(elemento->IP[pos], IP);
(elemento->nIP)++;
}
/*função que insere novo elemento na lista*/
struct lista_req * adicionaLista(char * novo_id, char * nova_pergunta, char * IP){
struct lista_req * novo = (struct lista_req *)malloc(sizeof(struct lista_req));
strcpy(novo->identificador, novo_id);
strcpy(novo->pergunta, nova_pergunta);
novo->prox = no_cabeca->prox;
no_cabeca->prox = novo;
novo->nIP = 0;
adicionaIPLista(novo, IP);
return novo;
}
/* Faz o parsing de uma string contendo um endereço IP e o converte
* num inteiro de 32 bits (4 bytes) */
unsigned int ipParser(char *ip){
char string[16];
char *aux;
unsigned int ret = 0;
strcpy (string, ip);
aux = strtok(string, ".");
ret += (atoi(aux) << 24);
aux = strtok(NULL, ".");
ret += (atoi(aux) << 16);
aux = strtok(NULL, ".");
ret += (atoi(aux) << 8);
aux = strtok(NULL, ".");
ret += atoi(aux);
return ret;
}
/* estrutura para o header ethernet */
struct etherhdr{
u_char ether_dhost[ETHER_ADDR_LEN]; /* endereço de destino */
u_char ether_shost[ETHER_ADDR_LEN]; /* endereço de origem */
u_short ether_type; /* protocolo da camada de rede */
};
/* Faz o parsing dos argumentos do programa */
void argParser(int argc, char *argv[]){
int i;
for(i=0; i<argc; i++){
if(!strcmp(argv[i], "--interface")){
interface = argv[i+1];
i++;
}
}
}
/* Filtra e trata os pacotes de acordo com a expressão dada como parametro */
int packetFilter(char *expr){
struct pcap_pkthdr header;
pcap_t *handle;
char errbuf[PCAP_ERRBUF_SIZE];
struct bpf_program fp;
const u_char *packet;
struct etherhdr *ether, *ether_novo;
struct udphdr *udp, *udp_novo;
struct iphdr *ip, *ip_novo;
struct dnshdr *dns, *dns_novo;
struct question query;
char *query_nova;
unsigned int size_ip_header = 0;
char pergunta[200];
char * atual;
struct answer resposta;
int i=0;
int j=0;
int k;
int tam, tamanho_query, offset;
int tamanho;
bpf_u_int32 mask; /* A máscara de rede da interface escutada */
bpf_u_int32 net; /* O ip da máquina escutada */
if (pcap_lookupnet(interface, &net, &mask, errbuf) == -1) {
fprintf(stderr, "ERRO: Não foi possível obter a máscara de rede (%s)\n", errbuf);
net = 0;
mask = 0;
}
/* Abre o arquivo de entrada para processamento */
handle = pcap_open_live(interface, BUFSIZ, 1, 100000, errbuf);
if (handle == NULL) {
printf("Erro ao ficar open! (%s)\n", errbuf);
return(2);
}
/* Compila e aplica o filtro */
if (pcap_compile(handle, &fp, expr, 0, 0) == -1) {
fprintf(stderr, "Couldn't parse filter %s: %s\n", expr, pcap_geterr(handle));
return(2);
}
if (pcap_setfilter(handle, &fp) == -1) {
fprintf(stderr, "Couldn't install filter %s: %s\n", expr, pcap_geterr(handle));
return(2);
}
/* captura e trata um pacote */
while ((packet = pcap_next(handle,&header))) {
/* acessa início de header ethernet */
ether = (struct etherhdr*)(packet);
/* acessa início de header ip */
ip = (struct iphdr*)(((char*)packet) + 14);
/* acessa início de header udp */
size_ip_header = ip->ihl*4;
udp = (struct udphdr *)(((char*) ip) + size_ip_header);
/* acessa início de header dns */
dns = (struct dnshdr*)(((char*) udp) + sizeof(struct udphdr));
/* acessa querry do protocolo DNS */
query.qname = (((char*)dns) + 12);
atual = query.qname;
tam = atual[0];
/* monta nome da pergunta */
j=0;
i=0;
while(tam!=0){
i++; /* primeiro caracter */
for(k=0; k<tam; k++){
pergunta[j]=atual[i+k];
j++;
}
pergunta[j]='.';
j++;
i+=tam; /* reposiciona indice */
tam=atual[i];
}
j--; /* retira último ponto colocado */
pergunta[j] = '\0';
tamanho = j;
int y=0;
struct lista_req * atual_el = no_cabeca->prox;
//printf("\n\nESTADO DA LISTA:\n");
/*imprime lista atual*/
/*while(atual_el!=NULL){
printf("pergunta(id = %s): %s\n", atual_el->identificador, atual_el->pergunta);
for(y=0; y<atual_el->nIP; y++){
printf("IP(%d):%s\n", y, atual_el->IP[y]);
}
atual_el=atual_el->prox;
}
printf("\n\n");
*/
/* Monta identificador da pergunta como uma string */
char identificador[100];
sprintf(identificador, "%hu", *((unsigned short *)dns->id));
printf("A pergunta é: %s (id = %s)\n", pergunta, identificador);
/* Se se tratar de um DNS request, apaga elemento da lista com pergunta igual, se existir*/
if(!(*((unsigned short*)(dns->ancount)))){
printf(" e se trata de uma requisição.\n");
struct lista_req ** anterior;
anterior = (struct lista_req **)malloc(sizeof(struct lista_req *));
struct lista_req * eliminado = procuraLista(identificador, anterior);
if(eliminado!=NULL) removeLista(eliminado, *anterior);
}
/* Se se tratar de um DNS reply, adiciona novo IP encontrado */
else {
printf(" e se trata de um reply.\n");
struct lista_req ** anterior;
anterior = (struct lista_req **)malloc(sizeof(struct lista_req *));
struct lista_req * elemento;
int q=0;
elemento = procuraLista(identificador, anterior);
int ja_existia = 0;
char * comeco = (char *)query.qname + tamanho + 6;
/*encontra os endereço IP contidos no reply*/
printf("o número de respostas é %hu\n", htons(*((unsigned short int*)(dns->ancount))));
for(q=0; q<htons(*((unsigned short int*)(dns->ancount))); q++){
unsigned short int tipo = ((unsigned short int*)(comeco+2))[0];
unsigned short int classe = ((unsigned short int*)(comeco+4))[0];
unsigned short int resp_size = ((unsigned short int*)(comeco+10))[0];
/*
* Resposta:
* bytes
* ---- DNS HEADER ----
* 0-1: ID
* 2-3: Flags: QR Opcode(4) AA TC RD RA Z AD CD Rcode(4) -> 1 0000 0 0 0 0 0 0 0 0000
* 4-5: # de perguntas -> 0
* 6-7: # de RRs de respostas -> 1
* 8-9: # de RRs com autoridade -> 0
* 10-11: # de RRs adicionais -> 0
* ---- payload ----
* <size> + perguntas -> 0
* <size> + respostas -> 1
* TYPE(2B) + CLASS(2B) -> 1 + 1
* TTL
* RdataLen + Rdata:::
* <size> + autoridade
* <size> + info adicional
*
* */
resposta.atype[0] = (char) 0;
resposta.atype[1] = (char) 1;
resposta.aclass[0] = (char) 0;
resposta.aclass[1] = (char) 1;
resposta.Rdata = "10.0.0.1";
resposta.RdataLen = sizeof("10.0.0.1");
res_send((char*)&query, tamanho, (char*)&resposta, 2 );
printf("O tipo é %d\n", htons(tipo));
printf("A classe é %d\n", htons(classe));
printf("o resp_size é %d\n", htons(resp_size));
if(htons(tipo) == 1){
char IP[100];
unsigned int IPi = ((unsigned int*)(comeco+12))[0];
sprintf(IP, "%u.%u.%u.%u", ((unsigned char *)(&IPi))[0], ((unsigned char *)(&IPi))[1], ((unsigned char *)(&IPi))[2], ((unsigned char *)(&IPi))[3]);
if(elemento !=NULL){
adicionaIPLista(elemento, IP);
ja_existia = 1;
}
else elemento = adicionaLista(identificador, pergunta, IP);
//printf("end : %s;\n", IP);
//printf("size : %d;\n", htons(resp_size));
comeco = comeco + 16;
}
else{
comeco = comeco + 12 + htons(resp_size);
}
}
/*Se já existia um reply de mesmo ID, imprime no STDOUT*/
if(ja_existia){
printf("Requisicao DNS sobre o dominio %s\n", elemento->pergunta);
printf("Respostas: ");
printf("%s", elemento->IP[0]);
for(q=1; q<elemento->nIP; q++){
printf(", %s", elemento->IP[q]);
}
printf("\n\n");
}
}
}
/* Fecha a sessão */
pcap_close(handle);
return 0;
}
/* Main do programa */
int main (int argc, char *argv[]){
/* inicializa nó cabeça */
no_cabeca = (struct lista_req *)malloc(sizeof(struct lista_req));
no_cabeca->nIP = 0;
no_cabeca->prox = NULL;
argParser(argc, argv);
char expr[1000];
/* Cria a expressão utilizada no filtro */
sprintf(expr, "udp and port 53");
packetFilter(expr);
return 0;
}