-
Notifications
You must be signed in to change notification settings - Fork 17
/
resolver.c
186 lines (138 loc) · 4.52 KB
/
resolver.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
/* cSploit - a simple penetration testing suite
* Copyright (C) 2014 Massimo Dragano aka tux_mind <[email protected]>
*
* cSploit is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cSploit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with cSploit. If not, see <http://www.gnu.org/licenses/>.
*/
#include <pthread.h>
#include <stdint.h>
#include <ares.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <netdb.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <csploit/logger.h>
#include <csploit/control.h>
#include "event.h"
#include "host.h"
#include "resolver.h"
struct resolver_data resolver_info;
void on_query_end(void *arg, int status, int timeouts, struct hostent *ent) {
uint32_t ip;
struct host *h;
struct event *e;
if(status != ARES_SUCCESS)
return;
// avoid unuseful names
if(strstr(ent->h_name, ".in-addr.arpa"))
return;
e = NULL;
ip = *((uint32_t *) &arg);
pthread_mutex_lock(&(hosts.control.mutex));
h = get_host(ip);
if(h && !(h->name)) {
e = (struct event *) malloc(sizeof(struct event));
if(!e) {
print( ERROR, "malloc: %s\n", strerror(errno));
} else {
e->type = NEW_NAME;
e->ip = ip;
}
h->name = strdup(ent->h_name);
}
pthread_mutex_unlock(&(hosts.control.mutex));
if(e) add_event(e);
}
void begin_dns_lookup(uint32_t ip) {
pthread_mutex_lock(&(resolver_info.control.mutex));
ares_gethostbyaddr(resolver_info.channel, &ip, 4, AF_INET, on_query_end, ((void *)0) + ip);
pthread_mutex_unlock(&(resolver_info.control.mutex));
pthread_cond_broadcast(&(resolver_info.control.cond));
}
void *resolver(void *arg) {
int nfds;
fd_set readers, writers;
struct timeval tv, *tvp;
pthread_mutex_lock(&(resolver_info.control.mutex));
while (resolver_info.control.active) {
pthread_mutex_unlock(&(resolver_info.control.mutex));
FD_ZERO(&readers);
FD_ZERO(&writers);
nfds = ares_fds(resolver_info.channel, &readers, &writers);
if(!nfds) {
// c-ares docs say that we should break and exit here,
// but have 0 queries is an accaptable state, we have
// just to wait for new ones.
pthread_cond_wait(&(resolver_info.control.cond), &(resolver_info.control.mutex));
continue;
}
tvp = ares_timeout(resolver_info.channel, NULL, &tv);
select(nfds, &readers, &writers, NULL, tvp);
ares_process(resolver_info.channel, &readers, &writers);
pthread_mutex_lock(&(resolver_info.control.mutex));
}
pthread_mutex_unlock(&(resolver_info.control.mutex));
ares_library_cleanup();
return NULL;
}
static int can_use_hosts_file() {
struct stat st;
return !stat(PATH_HOSTS, &st) && st.st_size < HOSTS_MAXSIZE;
}
int start_resolver() {
int ret, optmask;
struct ares_options options;
ret = ares_library_init(ARES_LIB_INIT_ALL);
if(ret) {
print( ERROR, "ares_library_init: %s\n", ares_strerror(ret));
return -1;
}
memset(&options, 0, sizeof(struct ares_options));
options.flags = ARES_FLAG_STAYOPEN;
optmask = ARES_OPT_FLAGS;
if(!can_use_hosts_file()) {
optmask |= ARES_OPT_LOOKUPS;
options.lookups = "b";
}
ret = ares_init_options(&(resolver_info.channel), &options, optmask);
if(ret) {
print( ERROR, "ares_init_options: %s\n", ares_strerror(ret));
ares_library_cleanup();
return -1;
}
if(pthread_create(&(resolver_info.tid), NULL, resolver, NULL)) {
print( ERROR, "pthread_create: %s\n", strerror(errno));
resolver_info.tid = 0;
ares_destroy(resolver_info.channel);
ares_library_cleanup();
return -1;
}
return 0;
}
void stop_resolver() {
pthread_t tid;
pthread_mutex_lock(&(resolver_info.control.mutex));
tid = resolver_info.tid;
resolver_info.tid = 0;
resolver_info.control.active = 0;
pthread_mutex_unlock(&(resolver_info.control.mutex));
if(!tid) return;
pthread_cond_broadcast(&(resolver_info.control.cond));
ares_destroy(resolver_info.channel);
pthread_join(tid, NULL);
}