-
Notifications
You must be signed in to change notification settings - Fork 15
/
NwInterface.cpp
1050 lines (810 loc) · 30.2 KB
/
NwInterface.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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* (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"
#define TOO_MANY_INVALID_ATTEMPTS 3
#define NUM_PURGE_LOOP 300
/* #define DEBUG */
#ifdef __linux__
/* Forward */
int netfilter_callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg,
struct nfq_data *nfa, void *data);
#endif
/* **************************************************** */
NwInterface::NwInterface(u_int nf_device_id,
Configuration *_c,
GeoIP *_g, std::string c_path) {
conf = _c, geoip = _g, confPath = c_path;
reloaderThread = NULL;
ifaceRunning = false;
fw = NULL;
#ifdef __linux__
fw = new LinuxFirewall();
#else
#if defined __FreeBSD__
fw = new FreeBSDFirewall();
#endif
#endif
#ifdef __linux__
queueId = nf_device_id, nfHandle = nfq_open();
if(nfHandle == NULL) {
trace->traceEvent(TRACE_ERROR, "Unable to get netfilter handle [queueId=%d]", queueId);
throw 1;
}
if(nfq_unbind_pf(nfHandle, AF_INET) < 0) {
trace->traceEvent(TRACE_ERROR, "Unable to unbind [queueId=%d]: are you root ?", queueId);
throw 1;
}
if(nfq_bind_pf(nfHandle, AF_INET) < 0) {
trace->traceEvent(TRACE_ERROR, "Unable to bind [queueId=%d]", queueId);
throw 1;
}
if((queueHandle = nfq_create_queue(nfHandle, queueId, &netfilter_callback, this)) == NULL) {
trace->traceEvent(TRACE_ERROR, "Unable to attach to NF_QUEUE %d: is it already in use?", queueId);
throw 1;
} else
trace->traceEvent(TRACE_NORMAL, "Successfully connected to NF_QUEUE %d", queueId);
#if !defined(__mips__)
nfnl_rcvbufsiz(nfq_nfnlh(nfHandle), NF_BUFFER_SIZE);
#endif
if(nfq_set_mode(queueHandle, NFQNL_COPY_PACKET, 0XFFFF) < 0) {
trace->traceEvent(TRACE_ERROR, "Unable to set packet_copy mode");
throw 1;
}
if(nfq_set_queue_maxlen(queueHandle, NF_MAX_QUEUE_LEN) < 0) {
trace->traceEvent(TRACE_ERROR, "Unable to set queue len");
throw 1;
}
nf_fd = nfq_fd(nfHandle);
#endif
if(!conf->getZMQUrl().empty()) {
std::string url = conf->getZMQUrl();
std::string enc = conf->getZMQEncryptionKey();
zmq = new ZMQ(url.c_str(), enc.c_str());
} else
zmq = NULL;
flush_ban();
}
/* **************************************************** */
NwInterface::~NwInterface() {
#ifndef __linux__
if(pcap_handle != NULL)
pcap_close(pcap_handle);
#endif
for(u_int i=0, s = pipes.size(); i < s; i++)
pclose(pipes[i]);
for(std::unordered_map<std::string, WatchMatches*>::iterator it = watches_blacklist.begin();
it != watches_blacklist.end(); it++)
delete it->second;
/* Wait until the reload thread ends */
if(reloaderThread) {
reloaderThread->join();
delete reloaderThread;
}
#ifdef __linux__
nf_fd = 0;
#endif
flush_ban();
#ifdef __linux__
if(queueHandle) nfq_destroy_queue(queueHandle);
if(nfHandle) nfq_close(nfHandle);
#endif
logStartStop(false /* stop */);
if(zmq) delete zmq;
if(fw) delete fw;
}
/* **************************************************** */
#ifdef __linux__
int netfilter_callback(struct nfq_q_handle *qh,
struct nfgenmsg *nfmsg,
struct nfq_data *nfa,
void *data) {
const u_char *payload;
struct nfqnl_msg_packet_hdr *ph = nfq_get_msg_packet_hdr(nfa);
/* The HW address is readeable only at certain hook points and it is the source address */
struct nfqnl_msg_packet_hw *hdr = nfq_get_packet_hw(nfa);
NwInterface *iface = (NwInterface *)data;
u_int payload_len;
u_int32_t id = ph ? ntohl(ph->packet_id) : 0;
u_int16_t marker;
u_int8_t null_mac[6] = {0};
if(!ph) return(-1);
#ifdef HAVE_NFQ_SET_VERDICT2
payload_len = nfq_get_payload(nfa, (unsigned char **)&payload);
#else
payload_len = nfq_get_payload(nfa, (char **)&payload);
#endif
marker = iface->dissectPacket(hdr ? true : false, payload, payload_len).get();
return(nfq_set_verdict2(qh, id, NF_ACCEPT, marker, 0, NULL));
}
#endif
/* **************************************************** */
void NwInterface::packetPollLoop() {
struct nfq_handle *h;
int fd;
std::vector<bool> geo_ip_pipes;
u_int num_loops = 0;
loadTemporarelyBannedHosts(conf->getDumpPath());
watches = conf->get_watches();
/* Spawn reload config thread in background */
reloaderThread = new std::thread(&NwInterface::reloadConfLoop, this);
/* Start watches */
for(std::unordered_map<std::string, std::pair<std::string, bool>>::iterator it = watches->begin(); it != watches->end(); it++) {
std::pair<std::string, bool> item = it->second;
FILE *watcher = popen(item.first.c_str(), "r");
if(watcher == NULL) {
trace->traceEvent(TRACE_ERROR, "Unable to run watch %s", item.first.c_str());
} else {
fd = fileno(watcher);
pipes.push_back(watcher);
fcntl(fd, F_SETFL, O_NONBLOCK);
pipes_fileno.push_back(std::make_pair(fd, it->first));
geo_ip_pipes.push_back(item.second); /* true = geo-ip, false = block-ip */
trace->traceEvent(TRACE_NORMAL, "Added watch %s [%s]", item.first.c_str(), item.first.c_str());
}
}
ifaceRunning = true;
logStartStop(true /* start */);
#ifdef __linux__
h = get_nfHandle();
fd = get_fd();
#else
char errbuf[PCAP_ERRBUF_SIZE];
pcap_handle = pcap_open_live(conf->getInterfaceName(), 1600, 0 /* promisc */, 1000, errbuf);
if(pcap_handle == NULL) {
trace->traceEvent(TRACE_ERROR, "Unable to open %s: %s",
conf->getInterfaceName(), errbuf);
exit(1);
} else
pcap_handle_fileno = pcap_fileno(pcap_handle);
#endif
while(isRunning()) {
fd_set mask;
struct timeval wait_time;
int max_fd = fd, num, id;
FD_ZERO(&mask);
FD_SET(fd, &mask);
for(u_int i=0, s = pipes_fileno.size(); i < s; i++) {
int fd = pipes_fileno[i].first;
FD_SET(fd, &mask);
if(fd > max_fd)
max_fd = fd;
}
#ifndef __linux__
if(pcap_handle_fileno != -1) {
FD_SET(pcap_handle_fileno, &mask);
if(pcap_handle_fileno > max_fd)
max_fd = pcap_handle_fileno;
}
#endif
wait_time.tv_sec = 1, wait_time.tv_usec = 0;
id = num = select(max_fd+1, &mask, 0, 0, &wait_time);
num_loops++;
if(num > 0) {
#ifdef __linux__
if(FD_ISSET(fd, &mask)) {
/* Socket data */
char pktBuf[8192] __attribute__ ((aligned));
int len = recv(fd, pktBuf, sizeof(pktBuf), 0);
// trace->traceEvent(TRACE_INFO, "Pkt len %d", len);
if(len >= 0) {
int rc = nfq_handle_packet(h, pktBuf, len);
if(rc < 0)
trace->traceEvent(TRACE_ERROR, "nfq_handle_packet() failed: [len: %d][rc: %d][errno: %d]", len, rc, errno);
} else {
trace->traceEvent(TRACE_ERROR, "NF_QUEUE receive error: [len: %d][errno: %d]", len, errno);
break;
}
num--;
}
#endif
/* Watches */
for(u_int i=0, s = pipes_fileno.size(); i < s; i++) {
if(FD_ISSET(pipes_fileno[i].first, &mask)) {
char ip[64];
while(fgets(ip, sizeof(ip), pipes[i]) != NULL) {
char ip_country[3]={}, ip_continent[3]={};
#ifdef DEBUG
trace->traceEvent(TRACE_ERROR, "Watch %s received %s", pipes_fileno[i].second, ip);
#endif
ip[strlen(ip)-1] = '\0'; /* Zap trailing /n */
geoip->lookup(ip, ip_country, sizeof(ip_country), ip_continent, sizeof(ip_continent));
if(geo_ip_pipes[i] == true /* geo-ip */) {
/*
In this case we need to check if the returned IP
has to be geographically banned or not
*/
if(strchr(ip, ':') == NULL) {
/* IPv4 */
struct in_addr ip_addr;
ip_addr.s_addr = inet_addr(ip);
if(conf->isBlacklistedIPv4(&ip_addr))
ban(ip, true, true, "ban-" + pipes_fileno[i].second, ip_country);
} else {
/* IPv6 */
struct in6_addr ip_addr;
inet_pton(AF_INET6, ip, &ip_addr);
if(conf->isBlacklistedIPv6(&ip_addr))
ban(ip, true, true, "ban-" + pipes_fileno[i].second, ip_country);
}
if(ip_country[0] != '\0') {
if(conf->getMarker(ip_country, ip_continent).get() != conf->getMarkerPass().get())
ban(ip, true, true, "ban-" + pipes_fileno[i].second, ip_country);
}
} else {
/* In this case the IP has to be banned immediately */
ban(ip, true, true, "ban-" + pipes_fileno[i].second, ip_country);
}
}
}
} /* for */
#ifndef __linux__
if(FD_ISSET(pcap_handle_fileno, &mask)) {
struct pcap_pkthdr hdr;
const u_char *packet = pcap_next(pcap_handle, &hdr);
if(packet != NULL) {
struct ndpi_ethhdr *e = (struct ndpi_ethhdr*)packet;
u_int16_t l3_proto = ntohs(e->h_proto);
if((l3_proto == ETHERTYPE_IP) || (l3_proto == ETHERTYPE_IPV6)) {
/* IPv4 or IPv6 */
u_int16_t marker = dissectPacket(true, &packet[sizeof(ndpi_ethhdr)],
hdr.caplen - sizeof(ndpi_ethhdr)).get();
if(marker == conf->getMarkerDrop().get()) {
/* Eventually do an action */
}
}
}
}
#endif
}
if((id == 0) || (num_loops > NUM_PURGE_LOOP)) {
harvestWatches();
num_loops = 0;
}
if(shadowConf != NULL) {
/* Swap configurations */
delete conf;
conf = shadowConf;
shadowConf = NULL;
trace->traceEvent(TRACE_NORMAL, "New configuration has been updated");
}
} /* while */
saveTemporarelyBannedHosts(conf->getDumpPath());
trace->traceEvent(TRACE_NORMAL, "Leaving packet poll loop");
ifaceRunning = false;
}
/* **************************************************** */
Marker NwInterface::dissectPacket(bool is_ingress_packet,
const u_char *payload, u_int payload_len) {
/* We can see only IP addresses */
u_int16_t ip_offset = 0, vlan_id = 0 /* FIX */;
if(payload_len >= ip_offset) {
struct ndpi_iphdr *iph = (struct ndpi_iphdr *)&payload[ip_offset];
bool ipv4 = false, ipv6 = false;
struct ndpi_tcphdr *tcph = NULL;
struct ndpi_udphdr *udph = NULL;
u_int16_t src_port, dst_port;
u_int8_t proto, ip_payload_offset = 40 /* ipv6 is 40B long */;
char src[INET6_ADDRSTRLEN] = {}, dst[INET6_ADDRSTRLEN] = {};
if(iph->version == 6) {
struct ndpi_ipv6hdr *ip6h = (struct ndpi_ipv6hdr *)&payload[ip_offset];
ipv6 = true;
proto = ip6h->ip6_hdr.ip6_un1_nxt;
// ipv6 address stringification
inet_ntop(AF_INET6, &(ip6h->ip6_src), src, sizeof(src));
inet_ntop(AF_INET6, &(ip6h->ip6_dst), dst, sizeof(dst));
} else if(iph->version == 4) {
ipv4 = true;
u_int8_t frag_off = ntohs(iph->frag_off);
struct in_addr a;
if((iph->protocol == IPPROTO_UDP) && ((frag_off & 0x3FFF /* IP_MF | IP_OFFSET */) != 0))
return(conf->getMarkerUnknown()); /* Don't block it */
// get protocol and offset
proto = iph->protocol;
ip_payload_offset = iph->ihl * 4;
// ipv4 address stringification
a.s_addr = iph->saddr, inet_ntop(AF_INET, &a, src, sizeof(src));
a.s_addr = iph->daddr, inet_ntop(AF_INET, &a, dst, sizeof(dst));
} else { // Neither ipv4 or ipv6...unlikely to be evaluated
return(conf->getMarkerPass());
}
u_int8_t *nxt = ((u_int8_t *)iph + ip_payload_offset);
switch (proto) {
case IPPROTO_TCP:
tcph = (struct ndpi_tcphdr *)(nxt);
src_port = tcph->source, dst_port = tcph->dest;
break;
case IPPROTO_UDP:
udph = (struct ndpi_udphdr *)(nxt);
src_port = udph->source, dst_port = udph->dest;
break;
default:
// we do not care about ports in other protocols
src_port = dst_port = 0;
}
return(makeVerdict(is_ingress_packet,
proto, vlan_id,
src_port, dst_port,
src, dst, ipv4, ipv6));
}
return(conf->getMarkerPass());
}
/* **************************************************** */
const char* NwInterface::getProtoName(u_int8_t proto) {
switch(proto) {
case IPPROTO_TCP: return("TCP");
case IPPROTO_UDP: return("UDP");
case IPPROTO_ICMP: return("ICMP");
default: return("???");
}
}
/* **************************************************** */
bool NwInterface::isPrivateIPv4(u_int32_t addr /* network byte order */) {
addr = ntohl(addr);
if(((addr & 0xFF000000) == 0x0A000000 /* 10.0.0.0/8 */)
|| ((addr & 0xFFF00000) == 0xAC100000 /* 172.16.0.0/12 */)
|| ((addr & 0xFFFF0000) == 0xC0A80000 /* 192.168.0.0/16 */)
|| ((addr & 0xFF000000) == 0x7F000000 /* 127.0.0.0/8 */)
|| ((addr & 0xFFFF0000) == 0xA9FE0000 /* 169.254.0.0/16 Link-Local communication rfc3927 */)
)
return(true);
else
return(false);
}
/* **************************************************** */
bool NwInterface::isBroadMulticastIPv4(u_int32_t addr /* network byte order */) {
addr = ntohl(addr);
if((addr == 0xFFFFFFFF /* 255.255.255.255 */)
|| (addr == 0x0 /* 0.0.0.0 */)
|| ((addr & 0xF0000000) == 0xE0000000 /* 224.0.0.0/4 */))
return(true);
else
return(false);
}
/* **************************************************** */
bool NwInterface::isPrivateIPv6(struct ndpi_in6_addr addr) {
bool is_link_local, is_unique_local;
u_int32_t first_byte = ntohl(addr.u6_addr.u6_addr32[0]);
is_link_local = (first_byte & (0xffc00000)) == (0xfe800000); // check the first 10 bits
is_unique_local = (first_byte & (0xfe000000)) == (0xfc000000); // check the first 7 bits
return(is_unique_local || is_link_local);
}
/* **************************************************** */
void NwInterface::addCommonJSON(Json::Value *root) {
(*root)["source"]["ip"] = conf->getHostIP();
(*root)["source"]["name"] = conf->getHostName();
(*root)["source"]["epoch"] = (unsigned int)time(NULL);
(*root)["source"]["version"] = std::string(PACKAGE_NAME) + std::string(" ") + std::string(IPT_RELEASE);
}
/* **************************************************** */
void NwInterface::logHostBan(char *host_ip,
bool ban_ip, bool ban_traffic,
std::string reason,
std::string country) {
Json::Value root;
std::string json_txt;
Json::FastWriter writer;
addCommonJSON(&root);
root["reason"] = (reason.size() > 0) ? reason.c_str() : "watch-host-ban";
root["host"] = host_ip;
if(!country.empty()) root["country"] = country.c_str();
root["action"] = ban_ip ? "ban" : "unban";
json_txt = writer.write(root);
trace->traceEvent(TRACE_INFO, "%s", json_txt.c_str());
if(zmq)
zmq->sendMessage(ZMQ_TOPIC_NAME, json_txt.c_str());
sendTelegramMessage(json_txt);
if(!conf->getCmd(ban_ip).empty()) {
std::string cmd = conf->getCmd(ban_ip) + " " + host_ip;
conf->execDeferredCmd(cmd);
}
#ifdef HAVE_NTOP_CLOUD
if(cloud && ban_ip) {
cloud->ban(host_ip,
ban_traffic ? bl_geofence_monitored_port : bl_geofence_watch,
(char*)reason.c_str(),
(char*)(ban_ip ? "ban" : "unban"),
(char*)country.c_str(),
(char*)conf->getHostIP(),
(char*)conf->getHostName(),
(char*)(std::string(PACKAGE_NAME) + std::string(" ") + std::string(IPT_RELEASE)).c_str());
}
#endif
}
/* **************************************************** */
void NwInterface::logStartStop(bool start) {
Json::Value root;
std::string json_txt;
Json::FastWriter writer;
addCommonJSON(&root);
root["action"] = start ? "start" : "stop";
json_txt = writer.write(root);
trace->traceEvent(TRACE_NORMAL, "%s", json_txt.c_str());
if(zmq)
zmq->sendMessage(ZMQ_TOPIC_NAME, json_txt.c_str());
sendTelegramMessage(json_txt);
}
/* **************************************************** */
void NwInterface::logFlow(const char *proto_name,
char *src_host, u_int16_t sport, char *src_country, char *src_continent, bool src_blacklisted,
char *dst_host, u_int16_t dport, char *dst_country, char *dst_continent, bool dst_blacklisted,
bool pass_verdict) {
Json::Value root;
std::string json_txt;
Json::FastWriter writer;
addCommonJSON(&root);
root["reason"] = "flow-ban";
root["proto"] = proto_name;
root["src"]["host"] = src_host;
root["src"]["port"] = sport;
if(src_country && (src_country[0] != '\0')) root["src"]["country"] = src_country;
if(src_continent && (src_continent[0] != '\0')) root["src"]["continent"] = src_continent;
if(src_blacklisted) root["src"]["blacklisted"] = src_blacklisted;
root["dst"]["host"] = dst_host;
root["dst"]["port"] = dport;
if(dst_country && (dst_country[0] != '\0')) root["dst"]["country"] = dst_country;
if(dst_continent && (dst_continent[0] != '\0')) root["dst"]["continent"] = dst_continent;
if(dst_blacklisted) root["dst"]["blacklisted"] = dst_blacklisted;
root["verdict"] = pass_verdict ? "pass" : "drop";
json_txt = writer.write(root);
if(pass_verdict)
trace->traceEvent(TRACE_INFO, "%s", json_txt.c_str());
else {
trace->traceEvent(TRACE_INFO, "%s", json_txt.c_str());
if(zmq)
zmq->sendMessage(ZMQ_TOPIC_NAME, json_txt.c_str());
// sendTelegramMessage(json_txt);
}
}
/* **************************************************** */
Marker NwInterface::makeVerdict(bool is_ingress_packet,
u_int8_t proto, u_int16_t vlanId,
u_int16_t sport /* network byte order */,
u_int16_t dport /* network byte order */,
char *src_host, char *dst_host,
bool ipv4, bool ipv6) {
// Step 0 - Check ip protocol
if(!(ipv4 || ipv6)) return(conf->getDefaultPolicy());
struct in_addr in;
char src_country[3]={}, dst_country[3]={}, src_continent[3]={}, dst_continent[3]={} ;
const char *proto_name = getProtoName(proto);
Marker m, src_marker, dst_marker;
u_int16_t _dport = dport;
bool drop = false;
sport = ntohs(sport), dport = ntohs(dport);
geoip->lookup(src_host, src_country, sizeof(src_country), src_continent, sizeof(src_continent));
geoip->lookup(dst_host, dst_country, sizeof(dst_country), dst_continent, sizeof(dst_continent));
/* ******************************************************* */
/* Check if sender/recipient are blacklisted */
if(ipv4) {
u_int32_t saddr = inet_addr(src_host), daddr = inet_addr(dst_host);
/* Broadcast source (e.g. for DHCP) traffic shoud paas */
if(isBroadMulticastIPv4(daddr))
return(conf->getMarkerPass());
/* For all ports/protocols, check if sender/recipient are white/black-listed and if so, pass/block this flow */
in.s_addr = saddr;
if(conf->isWhitelistedIPv4(&in)) {
return(conf->getMarkerPass()); /* Whitelisted IP (src) */
}
if(conf->isBlacklistedIPv4(&in)) {
logFlow(proto_name,
src_host, sport, src_country, src_continent, true,
dst_host, dport, dst_country, dst_continent, false,
false /* drop */);
// trace->traceEvent(TRACE_ERROR, "(1) %s <-> %s", src_host, dst_host);
ban(src_host, true /* ban */, true, "blacklisted-client", src_country);
return(conf->getMarkerDrop());
}
in.s_addr = daddr;
if(conf->isWhitelistedIPv4(&in)) {
return(conf->getMarkerPass()); /* Whitelisted IP (dst) */
}
if(conf->isBlacklistedIPv4(&in)) {
logFlow(proto_name,
src_host, sport, src_country, src_continent, false,
dst_host, dport, dst_country, dst_continent, true,
false /* drop */);
// trace->traceEvent(TRACE_ERROR, "(2) %s <-> %s", src_host, dst_host);
ban(dst_host, true /* ban */, true, "blacklisted-server", dst_country);
return(conf->getMarkerDrop());
}
// trace->traceEvent(TRACE_ERROR, "(3) %s <-> %s", src_host, dst_host);
} else if(ipv6) {
struct in6_addr a;
inet_pton(AF_INET6, src_host, &a);
if(conf->isBlacklistedIPv6(&a)) {
logFlow(proto_name,
src_host, sport, src_country, src_continent, true,
dst_host, dport, dst_country, dst_continent, false,
false /* drop */);
ban(src_host, true /* ban */, true, "blacklisted-client", src_country);
return(conf->getMarkerDrop());
}
inet_pton(AF_INET6, dst_host, &a);
if(conf->isBlacklistedIPv6(&a)) {
logFlow(proto_name,
src_host, sport, src_country, src_continent, false,
dst_host, dport, dst_country, dst_continent, true,
false /* drop */);
ban(dst_host, true /* ban */, true, "blacklisted-server", dst_country);
return(conf->getMarkerDrop());
}
}
/* ******************************************************* */
/* Pass flows on ignored ports */
if(conf->isIgnoredPort(sport) || conf->isIgnoredPort(dport)) {
logFlow(proto_name,
src_host, sport, src_country, src_continent, false,
dst_host, dport, dst_country, dst_continent, false,
true /* pass */);
return(conf->getMarkerPass());
}
/* ******************************************************* */
/* Check honeypot ports and (eventually) ban host */
if(is_ingress_packet && conf->isProtectedPort(dport)) {
char str[128];
logFlow(proto_name,
src_host, sport, src_country, src_continent, false,
dst_host, dport, dst_country, dst_continent, false,
false /* drop */);
snprintf(str, sizeof(str), "ban-honeypot-%d", dport);
ban(src_host, true /* ban */, true, str, src_country);
return(conf->getMarkerDrop());
}
/* ******************************************************* */
/* On TCP/UDP ignore traffic for non-monitored ports (country blacklists won't apply here) */
switch(proto) {
case IPPROTO_TCP:
if((conf->isMonitoredTCPPort(sport)) || conf->isMonitoredTCPPort(dport))
;
else {
trace->traceEvent(TRACE_INFO, "Ignoring TCP ports %u/%u", sport, dport);
return(conf->getMarkerPass());
}
break;
case IPPROTO_UDP:
if((conf->isMonitoredUDPPort(sport)) || conf->isMonitoredUDPPort(dport))
;
else {
trace->traceEvent(TRACE_INFO, "Ignoring UDP ports %u/%u", sport, dport);
return(conf->getMarkerPass());
}
break;
}
/* ******************************************************* */
m = src_marker = dst_marker = conf->getDefaultPolicy();
/* For monitored TCP/UDP ports (and ICMP) check the country blacklist */
if(src_country[0] != '\0') {
src_marker = conf->getMarker(src_country, src_continent);
} else {
/* Unknown or private IP address */
src_marker = conf->getMarkerPass();
}
if(dst_country[0] != '\0') {
dst_marker = conf->getMarker(dst_country, dst_continent);
} else {
/* Unknown or private IP address */
dst_marker = conf->getMarkerPass();
}
/* Final step: compute the flow verdict */
if((src_marker.get() == conf->getMarkerPass().get())
&& (dst_marker.get() == conf->getMarkerPass().get())) {
m = conf->getMarkerPass();
logFlow(proto_name,
src_host, sport, src_country, src_continent, false,
dst_host, dport, dst_country, dst_continent, false,
true /* pass */);
} else {
std::string msg = "ban-monitored-port-" + std::to_string(dport);
m = conf->getMarkerDrop();
logFlow(proto_name,
src_host, sport, src_country, src_continent, false,
dst_host, dport, dst_country, dst_continent, false,
false /* drop */);
if((proto == IPPROTO_TCP) || (proto == IPPROTO_UDP)) /* Ignore non TCP/UDP */
ban(src_host, true /* ban */, true, msg, src_country);
}
return(m);
}
/* **************************************************** */
u_int32_t NwInterface::computeNextReloadTime() {
u_int32_t confReloadTimeout = 86400 /* once a day */;
u_int32_t now = time(NULL);
u_int32_t next_reload = now + confReloadTimeout;
/* Align to the midnight */
next_reload -= (next_reload % confReloadTimeout);
return(next_reload);
}
/* **************************************************** */
void NwInterface::reloadConfLoop() {
u_int32_t next_reload = computeNextReloadTime();
shadowConf = NULL;
trace->traceEvent(TRACE_NORMAL, "Starting reload configuration loop");
while(isRunning()) {
u_int32_t now = time(NULL);
if(now > next_reload) {
trace->traceEvent(TRACE_NORMAL, "Reloading config file");
if(shadowConf != NULL) {
/* Too early */
trace->traceEvent(TRACE_WARNING, "An existing configuration is already available: trying again");
next_reload = now + 300; /* 5 mins */
} else {
Configuration *newConf = new Configuration();
newConf->readConfigFile(this->confPath.c_str());
if(newConf->isConfigured()) {
shadowConf = newConf;
}
else
trace->traceEvent(TRACE_ERROR, "Something went wrong: please check the JSON config file");
next_reload = computeNextReloadTime();
}
} else {
// trace->traceEvent(TRACE_INFO, "Will reload in %u sec", next_reload-now);
/* Important: make a short nap as we need to exit this thread during shutdown */
sleep(1); /* Too early */
}
} /* while */
trace->traceEvent(TRACE_NORMAL, "Reload configuration loop is over");
}
/* **************************************************** */
void NwInterface::harvestWatches() {
u_int32_t when = time(NULL) - MAX_IDLENESS;
#ifdef DEBUG
trace->traceEvent(TRACE_NORMAL, "NwInterface::harvestWatches()");
#endif
for(std::unordered_map<std::string, WatchMatches*>::iterator it = watches_blacklist.begin();
it != watches_blacklist.end();) {
WatchMatches *match = it->second;
#ifdef DEBUG
trace->traceEvent(TRACE_NORMAL, "last_match=%u / now=%u [to go: %d]",
match->get_last_match(), when, (match->get_last_match() - when));
#endif
if(match->ready_to_harvest(when)) {
#ifdef DEBUG
trace->traceEvent(TRACE_NORMAL, "Harvesting");
#endif
ban((char*)it->first.c_str(), false /* unban */, false, "unban", "");
delete it->second;
watches_blacklist.erase(it++); // or "it = m.erase(it)" since C++11
} else
++it;
}
}
/* **************************************************** */
void NwInterface::ban(char *host, bool ban_ip,
bool ban_traffic, std::string reason, std::string country) {
char cmdbuf[128];
bool is_ipv4 = (strchr(host, ':') == NULL) ? true /* IPv4 */ : false /* IPv6 */;
std::unordered_map<std::string, WatchMatches*>::iterator it = watches_blacklist.find(std::string(host));
// trace->traceEvent(TRACE_NORMAL, "*** %s ***", host);
if(ban_ip) {
/* Ban */
if(it == watches_blacklist.end()) {
/* New ban */
try {
u_int32_t until_time = time(NULL) + DEFAULT_BAN_TIME;
WatchMatches *watch = new WatchMatches(1, until_time);
trace->traceEvent(TRACE_INFO, "First time (until: %u) banning %s [%s]",
until_time, host, reason.c_str());
watches_blacklist[host] = watch;
if(fw) fw->ban(host, is_ipv4);
logHostBan(host, ban_ip, ban_traffic, reason, country);
} catch(std::bad_alloc& ex) {
trace->traceEvent(TRACE_WARNING, "Not enough memory");
}
} else {
WatchMatches *watch = it->second;
u_int32_t until_time;
u_int32_t num_matches;
/* Host already banned */
watch->inc_matches();
num_matches = watch->get_num_matches();
until_time = time(NULL) + (DEFAULT_BAN_TIME * num_matches * num_matches);
trace->traceEvent(TRACE_INFO, "Recurrent (times: %d/until: %u) time banning %s [%s]",
watch->get_num_matches(), until_time,
host, reason.c_str());
watch->ban_until(until_time);
}
} else {
/* Unban */
if(fw) fw->unban(host, is_ipv4);
logHostBan(host, ban_ip, ban_traffic, reason, country);
}
}
/* **************************************************** */
void NwInterface::ban_ipv4(u_int32_t ip4 /* network byte order */, bool ban_ip,
std::string reason, std::string country) {
char ipbuf[32], *host = Utils::intoaV4(ntohl(ip4), ipbuf, sizeof(ipbuf));
ban(host, ban_ip, true, reason, country);
}
/* **************************************************** */
void NwInterface::ban_ipv6(struct ndpi_in6_addr ip6, bool ban_ip,
std::string reason, std::string country) {
char ipbuf[64], *host = Utils::intoaV6(ip6, 128, ipbuf, sizeof(ipbuf));
ban(host, ban_ip, true, reason, country);
}
/* **************************************************** */
void NwInterface::flush_ban() {
try {
#ifdef __linux__
Utils::execCmd("/usr/sbin/iptables -F IPT_GEOFENCE_BLACKLIST");
Utils::execCmd("/usr/sbin/ip6tables -F IPT_GEOFENCE_BLACKLIST");
#endif
} catch (...) {
trace->traceEvent(TRACE_ERROR, "Error while flushing blacklists");
}
}
/* **************************************************** */
int NwInterface::sendTelegramMessage(std::string message) {
return(conf->sendTelegramMessage(message));
}
/* ****************************************** */
bool NwInterface::loadTemporarelyBannedHosts(const char* path) {
std::ifstream infile(path);
std::string line;
u_int32_t num_entries = 0;
if(!infile.is_open()) {
trace->traceEvent(TRACE_WARNING, "Unable to open file %s", path);
return(false);
}
while(getline(infile, line)) {
char item[256], *ip, *num_bans;
if((line[0] == '#') || (line[0] == '\0'))
continue;
// trace->traceEvent(TRACE_INFO, "Adding %s", line.c_str());
/* Format: IP\tnum_bans */
snprintf(item, sizeof(item), "%s", line.c_str());
ip = strtok(item, "\t");