-
Notifications
You must be signed in to change notification settings - Fork 115
/
domain.c
357 lines (317 loc) · 9.25 KB
/
domain.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
/*
* domain.c
* Copyright (C) 2019, basil
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "domain.h"
#include "main.h"
#define RATE_LIMIT 8000
#define rand16() \
(rand() & 0xFF) | ((rand() & 0xFF) << 8)
// Domain blacklist:
struct blacklist
{
size_t size;
size_t len;
char **names;
};
static struct blacklist *blacklist = NULL;
// State:
static struct name *names[UINT16_MAX] = {NULL};
static uint8_t sbox[UINT8_MAX] = {0};
static uint8_t sbox1[UINT8_MAX] = {0};
static uint16_t key[4] = {0};
static LONGLONG counter = 0;
static LONGLONG counter_0 = 0;
static LONGLONG counter_1 = 0;
static LONGLONG rate = 0;
static HANDLE names_lock = NULL;
// Prototypes:
static uint16_t domain_encrypt(uint16_t count);
static void domain_ref(struct name *name);
static struct blacklist *domain_blacklist_read(void);
static bool domain_blacklist_lookup(struct blacklist *blacklist,
const char *name);
static int __cdecl domain_blacklist_compare_0(const void *x, const void *y);
static int domain_blacklist_compare(const char *name0, size_t len,
const char *name1);
// "Encrypt" the count to give the IPs a random look-and-feel. Not security
// critical.
static uint16_t domain_encrypt(uint16_t count)
{
for (size_t i = 0; i < sizeof(key) / sizeof(uint16_t); i++)
{
count = (count << 3) | (count >> 13); // Mix
count ^= key[i]; // Round-key
count = (uint16_t)sbox[count & 0xFF] | // S-Box
((uint16_t)sbox[count >> 8] << 8);
}
return count;
}
// Initialize this module:
extern void domain_init(void)
{
// Load the domain blacklist.
blacklist = domain_blacklist_read();
// Init S-BOXes:
for (size_t i = 1; i <= UINT8_MAX; i++)
{
uint8_t idx = (uint8_t)rand16();
while (sbox[idx] != 0)
idx += 23;
sbox[idx] = (uint8_t)i;
if (i % 16 == 0)
srand(random());
}
srand(random());
for (size_t i = 1; i <= UINT8_MAX; i++)
{
uint8_t idx = (uint8_t)rand16();
while (sbox1[idx] != 0)
idx += 23;
sbox1[idx] = (uint8_t)i;
}
// Init the key sched.
for (size_t i = 0; i < sizeof(key) / sizeof(uint16_t); i++)
key[i] = (uint16_t)random();
counter = rand16();
counter <<= 8;
counter |= (LONGLONG)rand16();
counter_1 = counter_0 = counter;
names_lock = create_lock();
}
// Lookup an address given a domain name. If the name does not exist then
// create one.
extern uint32_t domain_lookup_addr(const char *name0)
{
if (name0[0] == '.')
name0++;
if (domain_blacklist_lookup(blacklist, name0))
{
debug(RED, "BLOCK", "%s", name0);
return 0; // Blocked!
}
if (InterlockedIncrement64(&rate) >= RATE_LIMIT)
{
debug(RED, "BLOCK", "%s (rate limit)", name0);
return 0;
}
uint64_t idx0 = (uint64_t)InterlockedIncrement64(&counter);
uint16_t idx = domain_encrypt((uint16_t)idx0);
uint8_t msb = sbox1[(idx0 >> 16) & 0xFF];
uint32_t addr = ADDR_BASE | ((uint32_t)msb << 16) | (uint32_t)idx;
if (names[idx] != NULL)
{
// Name table is full!
debug(RED, "BLOCK", "%s (name entry is full)", name0);
return 0;
}
size_t len = strlen(name0);
size_t size = sizeof(struct name) + (len + 1) * sizeof(char);
struct name *name = (struct name *)malloc(size);
if (name == NULL)
{
warning("failed to allocate %u bytes for domain name", size);
exit(EXIT_FAILURE);
}
name->ref_count = 1;
name->msb = msb;
memcpy(name->name, name0, len+1);
names[idx] = name;
return addr;
}
// Lookup a name based on the address.
extern struct name *domain_lookup_name(uint32_t addr)
{
if (!is_fake_addr(addr))
return NULL;
uint8_t msb = (uint8_t)(addr >> 16);
uint16_t idx = (uint16_t)addr;
lock(names_lock);
struct name *name = names[idx];
if (name == NULL || name->msb != msb)
{
unlock(names_lock);
return NULL;
}
domain_ref(name);
unlock(names_lock);
return name;
}
// Reference counting:
static void domain_ref(struct name *name)
{
if (name == NULL)
return;
name->ref_count++;
}
extern void domain_deref(struct name *name)
{
if (name == NULL)
return;
LONG old = InterlockedDecrement(&name->ref_count);
if (old == 0)
free(name);
}
// Cleanup old names.
extern void domain_cleanup(size_t count)
{
InterlockedExchange64(&rate, 0);
if (count % 16 != 0)
return;
LONGLONG start = counter_1, end = counter_0;
counter_1 = counter_0;
counter_0 = counter;
for (; start < end; start++)
{
uint16_t idx = (uint16_t)start;
idx = domain_encrypt(idx);
lock(names_lock);
struct name *old_name = names[idx];
names[idx] = NULL;
unlock(names_lock);
if (old_name != NULL)
debug(YELLOW, "CLEANUP", "%s", old_name->name);
domain_deref(old_name);
}
}
// Read the blacklist file:
static struct blacklist *domain_blacklist_read(void)
{
struct blacklist *blacklist =
(struct blacklist *)malloc(sizeof(struct blacklist));
if (blacklist == NULL)
{
warning("failed to allocate %u bytes for domain blacklist",
sizeof(struct blacklist));
exit(EXIT_FAILURE);
}
blacklist->size = 0;
blacklist->len = 0;
blacklist->names = NULL;
const char *filename = "hosts.deny";
FILE *stream = fopen(filename, "r");
if (stream == NULL)
{
warning("failed to open \"%s\" for reading", filename);
return blacklist;
}
// Read blocked domains:
int c;
char buf[256];
while (true)
{
while (isspace(c = getc(stream)))
;
if (c == EOF)
break;
if (c == '#')
{
while ((c = getc(stream)) != '\n' && c != EOF)
;
continue;
}
size_t i = 0;
while (i < sizeof(buf)-1 && (c == '-' || c == '.' || isalnum(c)))
{
buf[i++] = c;
c = getc(stream);
}
if (i >= sizeof(buf)-1 || !isspace(c))
{
warning("failed to parse domain blacklist from the \"%s\" file",
filename);
exit(EXIT_FAILURE);
}
buf[i] = '\0';
if (blacklist->len >= blacklist->size)
{
blacklist->size = (blacklist->size == 0? 32: 2 * blacklist->size);
blacklist->names = (char **)realloc(blacklist->names,
blacklist->size * sizeof(char *));
if (blacklist->names == NULL)
{
warning("failed to (re)allocate %u bytes for domain blacklist",
blacklist->size * sizeof(char *));
exit(EXIT_FAILURE);
}
}
size_t size = (i+1) * sizeof(char);
char *name = (char *)malloc(size);
if (name == NULL)
{
warning("failed to allocate %u bytes for domain blacklist entry",
size);
exit(EXIT_FAILURE);
}
for (size_t j = 0; j < i; j++)
name[j] = buf[i - 1 - j];
name[i] = '\0';
blacklist->names[blacklist->len++] = name;
}
fclose(stream);
qsort(blacklist->names, blacklist->len, sizeof(char *),
domain_blacklist_compare_0);
return blacklist;
}
// Check if a domain matches the blacklist or not:
static bool domain_blacklist_lookup(struct blacklist *blacklist,
const char *name)
{
if (blacklist->len == 0)
return false;
size_t len = strlen(name);
ssize_t lo = 0, hi = blacklist->len-1;
while (lo <= hi)
{
ssize_t mid = (lo + hi) / 2;
int cmp = domain_blacklist_compare(name, len, blacklist->names[mid]);
if (cmp > 0)
hi = mid-1;
else if (cmp < 0)
lo = mid+1;
else
return true;
}
return false;
}
// Domain compare function(s):
static int __cdecl domain_blacklist_compare_0(const void *x, const void *y)
{
const char *name0 = *(const char **)x;
const char *name1 = *(const char **)y;
return strcmp(name0, name1);
}
static int domain_blacklist_compare(const char *name0, size_t len,
const char *name1)
{
size_t i = 0;
ssize_t j = (ssize_t)len - 1;
for (; j >= 0 && name1[i] != '\0'; i++, j--)
{
int cmp = (int)name1[i] - (int)name0[j];
if (cmp != 0)
return cmp;
}
if (j < 0 && name1[i] != '\0')
return 1;
return 0;
}