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
|
/*
* Copyright (c) 2018-2022, OARC, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "dnscap_common.h"
#if defined(HAVE_LIBCRYPTO) && defined(HAVE_OPENSSL_CONF_H) && defined(HAVE_OPENSSL_ERR_H) && defined(HAVE_OPENSSL_EVP_H)
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#define USE_OPENSSL 1
#endif
static set_iaddr_t anonaes128_set_iaddr = 0;
static logerr_t* logerr;
static int only_clients = 0, only_servers = 0, dns_port = 53, encrypt_v4 = 0, decrypt = 0;
static unsigned char key[16];
static unsigned char iv[16];
#ifdef USE_OPENSSL
static EVP_CIPHER_CTX* ctx = 0;
#endif
enum plugin_type anonaes128_type()
{
return plugin_filter;
}
void usage(const char* msg)
{
fprintf(stderr, "anonaes128.so usage error: %s\n", msg);
exit(1);
}
void anonaes128_usage()
{
fprintf(stderr,
"\nanonaes128.so options:\n"
"\t-? print these instructions and exit\n"
"\t-k <key> A 16 character long key\n"
"\t-K <file> Read the 16 first bytes from file and use as key\n"
"\t-i <key> A 16 character long Initialisation Vector (IV)\n"
"\t-I <file> Read the 16 first bytes from file and use as IV\n"
"\t-D Decrypt IPv6 addresses\n"
"\t-c Only en/de-crypt clients (port != 53)\n"
"\t-s Only en/de-crypt servers (port == 53)\n"
"\t-p <port> Set port for -c/-s, default 53\n"
"\t-4 Encrypt IPv4 addresses, not default or recommended\n");
}
void anonaes128_extension(int ext, void* arg)
{
switch (ext) {
case DNSCAP_EXT_SET_IADDR:
anonaes128_set_iaddr = (set_iaddr_t)arg;
break;
}
}
void anonaes128_getopt(int* argc, char** argv[])
{
int c, got_key = 0, got_iv = 0;
unsigned long ul;
char* p;
while ((c = getopt(*argc, *argv, "?k:K:i:I:Dcsp:4")) != EOF) {
switch (c) {
case 'k':
if (strlen(optarg) != 16) {
usage("key must be 16 characters long");
}
memcpy(key, optarg, 16);
got_key = 1;
break;
case 'K': {
int fd;
ssize_t r;
if ((fd = open(optarg, O_RDONLY)) < 0) {
perror("open()");
usage("unable to open key file");
}
if ((r = read(fd, key, 16)) < 0) {
perror("read()");
usage("unable to read from key file");
}
if (r != 16) {
usage("unable to read 16 bytes from key file");
}
close(fd);
got_key = 1;
break;
}
case 'i':
if (strlen(optarg) != 16) {
usage("IV must be 16 characters long");
}
memcpy(iv, optarg, 16);
got_iv = 1;
break;
case 'I': {
int fd;
ssize_t r;
if ((fd = open(optarg, O_RDONLY)) < 0) {
perror("open()");
usage("unable to open IV file");
}
if ((r = read(fd, iv, 16)) < 0) {
perror("read()");
usage("unable to read from IV file");
}
if (r != 16) {
usage("unable to read 16 bytes from IV file");
}
close(fd);
got_iv = 1;
break;
}
case 'D':
decrypt = 1;
break;
case 'c':
only_clients = 1;
break;
case 's':
only_servers = 1;
break;
case 'p':
ul = strtoul(optarg, &p, 0);
if (*p != '\0' || ul < 1U || ul > 65535U)
usage("port must be an integer 1..65535");
dns_port = (unsigned)ul;
break;
case '4':
encrypt_v4 = 1;
break;
case '?':
anonaes128_usage();
if (!optopt || optopt == '?') {
exit(0);
}
// fallthrough
default:
exit(1);
}
}
if (!got_key || !got_iv) {
usage("must have key (-k/-K) and IV (-i/-I)");
}
if (decrypt && encrypt_v4) {
usage("decryption (-D) can not be done for IPv4 addresses (-4)");
}
#ifdef USE_OPENSSL
if (!(ctx = EVP_CIPHER_CTX_new())) {
usage("unable to create openssl cipher context");
}
if (!EVP_CipherInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv, decrypt ? 0 : 1)) {
unsigned long e = ERR_get_error();
fprintf(stderr, "%s:%s:%s", ERR_lib_error_string(e), ERR_func_error_string(e), ERR_reason_error_string(e));
usage("unable to initialize AES128 cipher");
}
EVP_CIPHER_CTX_set_padding(ctx, 0);
#else
usage("no openssl support built in, can't encrypt IP addresses");
#endif
if (only_clients && only_servers) {
usage("-c and -s options are mutually exclusive");
}
}
int anonaes128_start(logerr_t* a_logerr)
{
logerr = a_logerr;
return 0;
}
void anonaes128_stop()
{
#ifdef USE_OPENSSL
EVP_CIPHER_CTX_free(ctx);
ctx = 0;
#endif
}
int anonaes128_open(my_bpftimeval ts)
{
return 0;
}
int anonaes128_close(my_bpftimeval ts)
{
return 0;
}
int anonaes128_filter(const char* descr, iaddr* from, iaddr* to, uint8_t proto, unsigned flags,
unsigned sport, unsigned dport, my_bpftimeval ts,
const u_char* pkt_copy, const unsigned olen,
const u_char* payload, const unsigned payloadlen)
{
#ifdef USE_OPENSSL
unsigned char outbuf[16 + EVP_MAX_BLOCK_LENGTH];
int outlen = 0;
for (;;) {
if (only_clients && sport == dns_port) {
from = 0;
break;
}
if (only_servers && sport != dns_port) {
from = 0;
break;
}
switch (from->af) {
case AF_INET6:
if (!EVP_CipherUpdate(ctx, outbuf, &outlen, (unsigned char*)&from->u.a6, 16)) {
logerr("anonaes128.so: error en/de-crypting IP address: %s", ERR_reason_error_string(ERR_get_error()));
exit(1);
}
if (outlen != 16) {
logerr("anonaes128.so: error en/de-crypted output is not 16 bytes");
exit(1);
}
memcpy(&from->u.a6, outbuf, 16);
break;
case AF_INET:
if (encrypt_v4) {
memcpy(((uint8_t*)&from->u.a6) + 4, &from->u.a4, 4);
memcpy(((uint8_t*)&from->u.a6) + 8, &from->u.a4, 4);
memcpy(((uint8_t*)&from->u.a6) + 12, &from->u.a4, 4);
if (!EVP_CipherUpdate(ctx, outbuf, &outlen, (unsigned char*)&from->u.a6, 16)) {
logerr("anonaes128.so: error en/de-crypting IP address: %s", ERR_reason_error_string(ERR_get_error()));
exit(1);
}
if (outlen != 16) {
logerr("anonaes128.so: error en/de-crypted output is not 16 bytes");
exit(1);
}
memcpy(&from->u.a4, outbuf, 4);
break;
}
default:
from = 0;
break;
}
break;
}
for (;;) {
if (only_clients && dport == dns_port) {
to = 0;
break;
}
if (only_servers && dport != dns_port) {
to = 0;
break;
}
switch (to->af) {
case AF_INET6:
if (!EVP_CipherUpdate(ctx, outbuf, &outlen, (unsigned char*)&to->u.a6, 16)) {
logerr("anonaes128.so: error en/de-crypting IP address: %s", ERR_reason_error_string(ERR_get_error()));
exit(1);
}
if (outlen != 16) {
logerr("anonaes128.so: error en/de-crypted output is not 16 bytes");
exit(1);
}
memcpy(&to->u.a6, outbuf, 16);
break;
case AF_INET:
if (encrypt_v4) {
memcpy(((uint8_t*)&to->u.a6) + 4, &to->u.a4, 4);
memcpy(((uint8_t*)&to->u.a6) + 8, &to->u.a4, 4);
memcpy(((uint8_t*)&to->u.a6) + 12, &to->u.a4, 4);
if (!EVP_CipherUpdate(ctx, outbuf, &outlen, (unsigned char*)&to->u.a6, 16)) {
logerr("anonaes128.so: error en/de-crypting IP address: %s", ERR_reason_error_string(ERR_get_error()));
exit(1);
}
if (outlen != 16) {
logerr("anonaes128.so: error en/de-crypted output is not 16 bytes");
exit(1);
}
memcpy(&to->u.a4, outbuf, 4);
break;
}
default:
to = 0;
break;
}
break;
}
if (anonaes128_set_iaddr && (from || to)) {
anonaes128_set_iaddr(from, to);
}
#endif
return 0;
}
|