-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathConfiguration.cpp
544 lines (428 loc) · 16.7 KB
/
Configuration.cpp
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
*
* (C) 2021-24 - ntop.org
*
*
* 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, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include "include.h"
/* ******************************************************* */
Configuration::Configuration() {
nfq_queue_id = 0;
marker_unknown.set(0), marker_pass.set(1000); marker_drop.set(2000);
default_policy = marker_pass; configured = false;
all_tcp_ports = all_udp_ports = true;
host_name = Utils::execCmd("/bin/hostname", trace);
#if defined __FreeBSD__ || defined __APPLE__
host_ip = host_name; /* To be improved */
#else
host_ip = Utils::execCmd("/bin/hostname -I | cut -f 1 -d ' '", trace); /* Pick only the first IP address of the list */
#endif
/* Remove trailing \n */
Utils::zapNewline(host_name);
Utils::zapNewline(host_ip);
running = true;
telegramThread = new std::thread(&Configuration::sendTelegramMessages, this);
cmdThread = new std::thread(&Configuration::executeCommands, this);
}
/* ******************************************************* */
Configuration::~Configuration() {
running = false;
telegramThread->join();
cmdThread->join();
delete telegramThread;
delete cmdThread;
}
/* *************************2****************************** */
u_int16_t Configuration::ctry_cont2u16(char *ctry_cont_code) {
if(ctry_cont_code == NULL || strlen(ctry_cont_code) < 2) return 0;
return ((((u_int16_t) ctry_cont_code[0]) << 8) | ((u_int16_t) ctry_cont_code[1]));
}
/* ******************************************************* */
bool Configuration::readConfigFile(const char *path) {
Json::Value root;
std::ifstream ifs;
JSONCPP_STRING errs;
Json::CharReaderBuilder builder;
ifs.open(path);
builder["collectComments"] = true;
if(!parseFromStream(builder, ifs, &root, &errs)) {
std::cout << errs << std::endl;
return(false);
}
/* **************************** */
if(root["queue_id"].empty()) {
trace->traceEvent(TRACE_ERROR, "Missing %s from %s", "queue_id", path);
return(false);
} else
nfq_queue_id = root["queue_id"].asUInt();
/* **************************** */
if(!root["markers"].empty()) {
if(root["markers"]["pass"].empty()) {
trace->traceEvent(TRACE_INFO, "Missing %s from %s: using default %u", "pass", path, DEFAULT_PASS_MARKER);
marker_pass.set(DEFAULT_PASS_MARKER);
} else {
marker_pass.set(root["markers"]["pass"].asUInt());
}
if(root["markers"]["drop"].empty()) {
trace->traceEvent(TRACE_INFO, "Missing %s from %s: using default %u", "drop", path, DEFAULT_DROP_MARKER);
marker_drop.set(DEFAULT_DROP_MARKER);
} else {
marker_drop.set(root["markers"]["drop"].asUInt());
}
if(marker_drop.get() == marker_pass.get()) {
trace->traceEvent(TRACE_ERROR, "Markers values must be distinct in %s", path);
return(false);
}
if((marker_drop.get() == 0) || (marker_pass.get() == 0)) {
trace->traceEvent(TRACE_ERROR, "Markers values must be greater than 0 in %s", path);
return(false);
}
}
trace->traceEvent(TRACE_INFO, "Markers are set to: pass %d, drop %d", marker_pass, marker_drop);
/* **************************** */
if(root["default_policy"].empty()) {
trace->traceEvent(TRACE_ERROR, "Missing %s from %s", "default_policy", path);
return(false);
} else {
std::string m = root["default_policy"].asString();
trace->traceEvent(TRACE_INFO, "Default policy: %s", m.c_str());
default_policy = (m == "PASS") ? marker_pass : marker_drop;
}
all_tcp_ports = all_udp_ports = true;
/* **************************** */
if(!root["monitored_ports"].empty()) {
if(!root["monitored_ports"]["tcp"].empty()) {
all_tcp_ports = false;
for(Json::Value::ArrayIndex i = 0; i != root["monitored_ports"]["tcp"].size(); i++) {
unsigned int port = root["monitored_ports"]["tcp"][i].asUInt();
trace->traceEvent(TRACE_INFO, "Adding TCP/%u", port);
tcp_ports[port] = true;
}
}
if(!root["monitored_ports"]["udp"].empty()) {
all_udp_ports = false;
for(Json::Value::ArrayIndex i = 0; i != root["monitored_ports"]["udp"].size(); i++) {
unsigned int port = root["monitored_ports"]["udp"][i].asUInt();
trace->traceEvent(TRACE_INFO, "Adding UDP/%u", port);
udp_ports[port] = true;
}
}
if(!root["monitored_ports"]["ignored_ports"].empty()) {
for(Json::Value::ArrayIndex i = 0; i != root["monitored_ports"]["ignored_ports"].size(); i++) {
unsigned int port = root["monitored_ports"]["ignored_ports"][i].asUInt();
trace->traceEvent(TRACE_INFO, "Ignoring TCP/UDP port %u", port);
ignored_ports[port] = true;
}
}
// Doesn't distinguish between UDP and TCP (and other protocols...)
if(!root["monitored_ports"]["honeypot_ports"].empty()) {
for (Json::Value::ArrayIndex i = 0; i != root["monitored_ports"]["honeypot_ports"].size(); i++) {
Json::Value honeypot_field = root["monitored_ports"]["honeypot_ports"][i];
if(honeypot_field.isString()) { // port range [A-B] or "all ports except" !P
u_int16_t except_port;
port_range p_r;
std::string s = honeypot_field.asString();
if(s.find_first_of("!") == 0) { // Might be a !P
if(parseAllExcept(s,&except_port)) { // !P "overrides" port ranges but NOT single ports
// honeypot_ports.clear();
hp_ranges.clear(); // We don't care no more about these
hp_all_except_ports[except_port] = true;
trace->traceEvent(TRACE_INFO, "Protecting all ports except %u", except_port);
}
} else if(parsePortRange(s, &p_r)) { // Might be a port range
addPortRange(p_r);
// trace->traceEvent(TRACE_INFO, "Added range...");
}
} else { // Single port
hp_ports[honeypot_field.asUInt()] = true;
trace->traceEvent(TRACE_INFO, "Protecting port %u", honeypot_field.asUInt());
}
}
if(!hp_all_except_ports.empty())
hp_ranges.clear();
}
}
/* **************************** */
if(all_tcp_ports) trace->traceEvent(TRACE_INFO, "All TCP ports will be monitored");
if(all_udp_ports) trace->traceEvent(TRACE_INFO, "All UDP ports will be monitored");
std::string json_policy_str = (default_policy.get() == marker_drop.get()) ? "drop" : "pass";
if(!root["policy"].empty() && !root["policy"][json_policy_str].empty()) {
Json::Value json_policy_obj = root["policy"][json_policy_str];
std::string json_list_str = json_policy_str == "drop" ? "_whitelist" : "_blacklist";
int counter = 2;
do {
std::string json_value_str = counter == 2 ? "countries" : "continents";
if(json_policy_obj[json_value_str+json_list_str].empty()) {
trace->traceEvent(TRACE_INFO, "Missing %s from %s", (json_value_str+json_list_str).c_str(), path);
} else {
for(Json::Value::ArrayIndex i = 0; i != json_policy_obj[json_value_str+json_list_str].size(); i++) {
std::string ctry_cont = json_policy_obj[json_value_str+json_list_str][i].asString();
trace->traceEvent(TRACE_INFO, "Adding %s to %s", ctry_cont.c_str(), (json_value_str+json_list_str).c_str());
ctrs_conts[ctry_cont2u16((char*)ctry_cont.c_str())] = json_policy_str == "drop" ? marker_pass : marker_drop;
}
}
} while(--counter);
}
/* **************************** */
if(!root["watches"].empty()) {
size_t num_watches = root["watches"].size();
for(Json::Value::ArrayIndex i = 0; i != num_watches; i++) {
Json::Value item = root["watches"][i];
std::string name = item["name"].asString();
std::string cmd = item["cmd"].asString();
bool geo_ip = false;
if((!item["mode"].empty()) && (item["mode"].asString() == "geo-ip"))
geo_ip = true;
if(name.empty() || cmd.empty()) {
trace->traceEvent(TRACE_WARNING, "Invalid watch format");
break;
} else
watches[name] = std::make_pair(cmd, geo_ip);
}
}
/* **************************** */
if(!root["whitelists"].empty()) {
size_t n_paths = root["whitelists"].size();
for(Json::Value::ArrayIndex i = 0; i != n_paths; i++) {
std::string path (root["whitelists"][i].asString());
trace->traceEvent(TRACE_NORMAL, "Loading %s...", path.c_str());
whitelists.loadIPsetFromFile(path.c_str());
}
}
/* **************************** */
if(!root["blacklists"].empty()) {
size_t n_urls = root["blacklists"].size();
for(Json::Value::ArrayIndex i = 0; i != n_urls; i++) {
std::string url (root["blacklists"][i].asString());
blacklists.loadIPsetFromURL(url.c_str());
}
}
/* **************************** */
if(root["blacklist_dump_path"].empty()) {
const char *def = "/var/tmp/banned_addresses.txt";
trace->traceEvent(TRACE_WARNING, "Missing %s: using default %s",
"blacklist_dump_path", def);
setDumpPath(def);
} else
setDumpPath(root["blacklist_dump_path"].asCString());
/* **************************** */
if(!root["telegram"].empty()) {
if(!root["telegram"]["bot_token"].empty())
telegram_bot_token = root["telegram"]["bot_token"].asString();
if(!root["telegram"]["chat_id"].empty())
telegram_chat_id = root["telegram"]["chat_id"].asString();
}
/* **************************** */
if(!root["cmd"].empty()) {
if(!root["cmd"]["ban"].empty())
cmd_ban = root["cmd"]["ban"].asString();
if(!root["cmd"]["unban"].empty())
cmd_unban = root["cmd"]["unban"].asString();
}
/* **************************** */
if(!root["zmq"].empty()) {
if(!root["zmq"]["url"].empty())
zmq_url = root["zmq"]["url"].asString();
if(!root["zmq"]["encryption_key"].empty()) {
std::string encryption_key = root["zmq"]["encryption_key"].asString();
const char *zmq_encryption_key_hex = encryption_key.c_str();
char _zmq_encryption_key[42];
Utils::fromHex((char*)zmq_encryption_key_hex, strlen(zmq_encryption_key_hex),
_zmq_encryption_key, sizeof(_zmq_encryption_key));
zmq_encryption_key = std::string(_zmq_encryption_key);
}
}
return(configured = true);
}
/* ******************************************************* */
Marker Configuration::getMarker(char *country, char *continent) {
u_int16_t id = ctry_cont2u16(country);
std::unordered_map<u_int16_t, Marker>::iterator it = ctrs_conts.find(id);
if(it != ctrs_conts.end())
return(it->second); // country found
id = ctry_cont2u16(continent);
it = ctrs_conts.find(id);
if(it != ctrs_conts.end())
return(it->second); // continent found
return(default_policy);
}
/* ******************************************************* */
/**
* @brief Assuming r1.high > r2.low or viceversa, puts in 'ret'
* the union between the two ranges.
* e.g. mergeRanges(15-30,20-40) returns 15-40.
*
* @param r1
* @param r2
* @param ret pointer to structure which will hold the merged range
* @return true if mergeable, false otherwise
*/
bool Configuration::mergePortRanges (port_range r1, port_range r2, port_range *ret) {
if(r1.first < r1.second || r2.first < r2.second || !ret)
return false; // r1 || r2 || ret is invalid
port_range
l = r1.second <= r2.second ? r1 : r2, // "left" range
r = r1.second <= r2.second ? r2 : r1; // "right" range
if( l.first < r.second )
return false; // r1 and r2 are disjoint ranges
if( r.first < l.first )
ret->first = l.first; // r is completely included in l
else
ret->first = r.first;
ret->second = l.second;
trace->traceEvent(TRACE_INFO, "Merging ranges [%u-%u] and [%u-%u] into [%u-%u]",
r1.second,r1.first,r2.second,r2.first,ret->second,ret->first
);
return true;
}
/* **************************************************** */
/**
* @brief adds a range to hp_ranges, making sure that all
* ranges in the set are disjoint and ordered using range upper bounds
*
* @param r range to be inserted
*/
void Configuration::addPortRange(port_range r) {
port_range curr (r), merged (r);
// the set must be ordered using the upper bound
// NB: a set of pair is ordered using pair.first
if(r.first < r.second) {
curr.first = merged.first = r.second;
curr.second = merged.second = r.first;
}
std::set<port_range>::iterator it = hp_ranges.begin();
while(it != hp_ranges.end()) {
if(mergePortRanges(curr, *it, &merged)) {
if(merged!=*it) { // if merge operation has generated a new range
hp_ranges.erase(it); // remove the range now included in 'merged'
curr = merged; // update curr for next walk
it = hp_ranges.begin(); // check if new range can be merged again
} else return;
} else it++;
}
if(it == hp_ranges.end() && curr==merged) { // walked through the whole set,
hp_ranges.insert(curr);
trace->traceEvent(TRACE_INFO, "Protecting range [%u-%u]",merged.second,merged.first);
}
}
/* **************************************************** */
bool Configuration::parsePortRange(std::string s, port_range *r) {
if(!r) return false;
size_t delim;
if( (delim = s.find("-")) != std::string::npos) {
std::string s_l = s.substr(0,delim), s_r = s.substr(delim + 1, std::string::npos);
return (stringToU16(s_l, &(r->first)) && stringToU16(s_r, &(r->second)));
}
return false;
}
/* **************************************************** */
bool Configuration::parseAllExcept(std::string s, u_int16_t *port) {
if(!port) return false;
size_t delim;
if( (delim = s.find("!")) != std::string::npos) {
return (stringToU16(s.substr(delim + 1, std::string::npos), port));
}
return false;
}
/* **************************************************** */
bool Configuration::stringToU16(std::string s, u_int16_t *toRet) {
if(!toRet) return false;
char *err;
const char *_s = s.c_str();
unsigned long v = strtoul(_s, &err, 10);
if(*_s != '\0' && *err == '\0' &&
v <= USHRT_MAX) { // string is valid number
*toRet = v;
return true;
}
// there are some invalid characters
return false;
}
/* **************************************************** */
bool Configuration::isProtectedPort(u_int16_t port) {
if (hp_ports.find(port) != hp_ports.end() || // single port match
// included by a "!port"
(!hp_all_except_ports.empty() && hp_all_except_ports.find(port) == hp_all_except_ports.end()) ||
(isIncludedInRange(port))
)
return true;
return false;
}
/* **************************************************** */
bool Configuration::isIncludedInRange(u_int16_t port) {
port_range toSearch {port, 0}; // we don't care about .second
std::set<port_range>::iterator it = hp_ranges.lower_bound(toSearch);
if(it == hp_ranges.end()) // No range includes port
return false;
/* else */ if((*it).second <= port) // <= (*it).first
return true;
/* else */ return false;
}
/* **************************************************** */
int Configuration::sendTelegramMessage(std::string msg) {
if((!telegram_bot_token.empty()) && (!telegram_chat_id.empty())) {
telegram_queue_lock.lock();
telegram_queue.push(msg);
telegram_queue_lock.unlock();
return(0);
}
return(-1);
}
/* **************************************************** */
void Configuration::sendTelegramMessages() {
pthread_setname_np(pthread_self(), __FUNCTION__);
while(true) {
if(telegram_queue.size() > 0) {
std::string message;
telegram_queue_lock.lock();
message = telegram_queue.front();
telegram_queue.pop();
telegram_queue_lock.unlock();
Utils::sendTelegramMessage(telegram_bot_token, telegram_chat_id, message, trace);
} else {
if(!running)
break;
else
sleep(1);
}
}
}
/* **************************************************** */
void Configuration::execDeferredCmd(std::string cmd) {
cmd_queue_lock.lock();
cmd_queue.push(cmd);
cmd_queue_lock.unlock();
}
/* **************************************************** */
void Configuration::executeCommands() {
pthread_setname_np(pthread_self(), __FUNCTION__);
while(true) {
if(cmd_queue.size() > 0) {
std::string cmd;
cmd_queue_lock.lock();
cmd = cmd_queue.front();
cmd_queue.pop();
cmd_queue_lock.unlock();
Utils::execCmd(cmd.c_str(), trace);
} else {
if(!running)
break;
else
sleep(1);
}
}
}