Added ability to define an unlimited number of custom rules IP:port for the same IP (it used tobe limited to 2)

This commit is contained in:
Luca Deri
2023-03-13 21:57:14 +01:00
parent ebfc1fe827
commit 3585e2d201
4 changed files with 154 additions and 108 deletions
+76 -76
View File
@@ -5089,116 +5089,116 @@ void zscoreUnitTest() {
/** /**
@brief MAIN FUNCTION @brief MAIN FUNCTION
**/ **/
int main(int argc, char **argv) { int main(int argc, char **argv) {
int i, skip_unit_tests = 0; int i, skip_unit_tests = 0;
#ifdef DEBUG_TRACE #ifdef DEBUG_TRACE
trace = fopen("/tmp/ndpiReader.log", "a"); trace = fopen("/tmp/ndpiReader.log", "a");
if(trace) { if(trace) {
int i; int i;
fprintf(trace, " #### %s #### \n", __FUNCTION__); fprintf(trace, " #### %s #### \n", __FUNCTION__);
fprintf(trace, " #### [argc: %u] #### \n", argc); fprintf(trace, " #### [argc: %u] #### \n", argc);
for(i=0; i<argc; i++) for(i=0; i<argc; i++)
fprintf(trace, " #### [%d] [%s]\n", i, argv[i]); fprintf(trace, " #### [%d] [%s]\n", i, argv[i]);
} }
#endif #endif
if(ndpi_get_api_version() != NDPI_API_VERSION) { if(ndpi_get_api_version() != NDPI_API_VERSION) {
printf("nDPI Library version mismatch: please make sure this code and the nDPI library are in sync\n"); printf("nDPI Library version mismatch: please make sure this code and the nDPI library are in sync\n");
return(-1); return(-1);
} }
if(!skip_unit_tests) { if(!skip_unit_tests) {
#ifndef DEBUG_TRACE #ifndef DEBUG_TRACE
/* Skip tests when debugging */ /* Skip tests when debugging */
#ifdef HW_TEST #ifdef HW_TEST
hwUnitTest2(); hwUnitTest2();
#endif #endif
#ifdef STRESS_TEST #ifdef STRESS_TEST
desUnitStressTest(); desUnitStressTest();
exit(0); exit(0);
#endif #endif
zscoreUnitTest(); zscoreUnitTest();
sesUnitTest(); sesUnitTest();
desUnitTest(); desUnitTest();
/* Internal checks */ /* Internal checks */
// binUnitTest(); // binUnitTest();
//hwUnitTest(); //hwUnitTest();
jitterUnitTest(); jitterUnitTest();
rsiUnitTest(); rsiUnitTest();
hashUnitTest(); hashUnitTest();
dgaUnitTest(); dgaUnitTest();
hllUnitTest(); hllUnitTest();
bitmapUnitTest(); bitmapUnitTest();
automataUnitTest(); automataUnitTest();
analyzeUnitTest(); analyzeUnitTest();
ndpi_self_check_host_match(); ndpi_self_check_host_match();
analysisUnitTest(); analysisUnitTest();
compressedBitmapUnitTest(); compressedBitmapUnitTest();
#endif #endif
} }
gettimeofday(&startup_time, NULL); gettimeofday(&startup_time, NULL);
memset(ndpi_thread_info, 0, sizeof(ndpi_thread_info)); memset(ndpi_thread_info, 0, sizeof(ndpi_thread_info));
if(getenv("AHO_DEBUG")) if(getenv("AHO_DEBUG"))
ac_automata_enable_debug(1); ac_automata_enable_debug(1);
parseOptions(argc, argv); parseOptions(argc, argv);
ndpi_info_mod = ndpi_init_detection_module(init_prefs); ndpi_info_mod = ndpi_init_detection_module(init_prefs);
if(ndpi_info_mod == NULL) return -1; if(ndpi_info_mod == NULL) return -1;
if(domain_to_check) { if(domain_to_check) {
ndpiCheckHostStringMatch(domain_to_check); ndpiCheckHostStringMatch(domain_to_check);
exit(0); exit(0);
} }
if(!quiet_mode) { if(!quiet_mode) {
printf("\n-----------------------------------------------------------\n" printf("\n-----------------------------------------------------------\n"
"* NOTE: This is demo app to show *some* nDPI features.\n" "* NOTE: This is demo app to show *some* nDPI features.\n"
"* In this demo we have implemented only some basic features\n" "* In this demo we have implemented only some basic features\n"
"* just to show you what you can do with the library. Feel \n" "* just to show you what you can do with the library. Feel \n"
"* free to extend it and send us the patches for inclusion\n" "* free to extend it and send us the patches for inclusion\n"
"------------------------------------------------------------\n\n"); "------------------------------------------------------------\n\n");
printf("Using nDPI (%s) [%d thread(s)]\n", ndpi_revision(), num_threads); printf("Using nDPI (%s) [%d thread(s)]\n", ndpi_revision(), num_threads);
const char *gcrypt_ver = ndpi_get_gcrypt_version(); const char *gcrypt_ver = ndpi_get_gcrypt_version();
if(gcrypt_ver) if(gcrypt_ver)
printf("Using libgcrypt version %s\n", gcrypt_ver); printf("Using libgcrypt version %s\n", gcrypt_ver);
} }
signal(SIGINT, sigproc); signal(SIGINT, sigproc);
for(i=0; i<num_loops; i++) for(i=0; i<num_loops; i++)
test_lib(); test_lib();
if(results_path) ndpi_free(results_path); if(results_path) ndpi_free(results_path);
if(results_file) fclose(results_file); if(results_file) fclose(results_file);
if(extcap_dumper) pcap_dump_close(extcap_dumper); if(extcap_dumper) pcap_dump_close(extcap_dumper);
if(extcap_fifo_h) pcap_close(extcap_fifo_h); if(extcap_fifo_h) pcap_close(extcap_fifo_h);
if(ndpi_info_mod) ndpi_exit_detection_module(ndpi_info_mod); if(ndpi_info_mod) ndpi_exit_detection_module(ndpi_info_mod);
if(enable_malloc_bins) if(enable_malloc_bins)
ndpi_free_bin(&malloc_bins); ndpi_free_bin(&malloc_bins);
if(csv_fp) fclose(csv_fp); if(csv_fp) fclose(csv_fp);
ndpi_free(_debug_protocols); ndpi_free(_debug_protocols);
ndpi_free(_disabled_protocols); ndpi_free(_disabled_protocols);
#ifdef DEBUG_TRACE #ifdef DEBUG_TRACE
if(trace) fclose(trace); if(trace) fclose(trace);
#endif #endif
return 0; return 0;
} }
#ifdef _MSC_BUILD #ifdef _MSC_BUILD
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
+15 -9
View File
@@ -66,20 +66,26 @@
#define UV16_MAX_USER_VALUES 2 #define UV16_MAX_USER_VALUES 2
struct patricia_uv16 {
u_int16_t user_value, additional_user_value;
};
struct patricia_uv16_list {
struct patricia_uv16 value;
struct patricia_uv16_list *next;
};
struct patricia_uv32 {
u_int32_t user_value, additional_user_value;
};
/* pointer to user data (ex. route flap info) */ /* pointer to user data (ex. route flap info) */
union ndpi_patricia_node_value_t { union ndpi_patricia_node_value_t {
/* User-defined values */ /* User-defined values */
union { union {
struct { struct patricia_uv16 uv16[UV16_MAX_USER_VALUES];
u_int16_t user_value, additional_user_value; struct patricia_uv32 uv32;
} uv16[UV16_MAX_USER_VALUES];
struct {
u_int32_t user_value, additional_user_value;
} uv32;
u_int64_t uv64; u_int64_t uv64;
void *user_data; void *user_data;
} u; } u;
}; };
+8 -8
View File
@@ -1,7 +1,7 @@
/* /*
* ndpi_typedefs.h * ndpi_typedefs.h
* *
* Copyright (C) 2011-22 - ntop.org * Copyright (C) 2011-23 - ntop.org
* *
* This file is part of nDPI, an open source deep packet inspection * This file is part of nDPI, an open source deep packet inspection
* library based on the OpenDPI and PACE technology by ipoque GmbH * library based on the OpenDPI and PACE technology by ipoque GmbH
@@ -1156,18 +1156,18 @@ struct ndpi_detection_module_struct {
risky_domain_automa, tls_cert_subject_automa, risky_domain_automa, tls_cert_subject_automa,
host_risk_mask_automa, common_alpns_automa; host_risk_mask_automa, common_alpns_automa;
/* IMPORTANT: please, whenever you add a new automa: /* IMPORTANT: please, whenever you add a new automa:
* update ndpi_finalize_initialization() * update ndpi_finalize_initialization()
* update automa_type above * update automa_type above
*/ */
ndpi_str_hash *malicious_ja3_hashmap, *malicious_sha1_hashmap; ndpi_str_hash *malicious_ja3_hashmap, *malicious_sha1_hashmap;
ndpi_list *trusted_issuer_dn; ndpi_list *trusted_issuer_dn;
void *ip_risk_mask_ptree; /* Patricia trees */
void *ip_risk_ptree; ndpi_patricia_tree_t *ip_risk_mask_ptree;
/* IP-based protocol detection */ ndpi_patricia_tree_t *ip_risk_ptree;
void *protocols_ptree; ndpi_patricia_tree_t *protocols_ptree; /* IP-based protocol detection */
/* *** If you add a new Patricia tree, please update ptree_type above! *** */ /* *** If you add a new Patricia tree, please update ptree_type above! *** */
+48 -8
View File
@@ -2164,22 +2164,22 @@ void ndpi_patricia_get_stats(ndpi_patricia_tree_t *tree, struct ndpi_patricia_tr
/* ******************************************************************** */ /* ******************************************************************** */
int ndpi_get_patricia_stats(struct ndpi_detection_module_struct *ndpi_struct, int ndpi_get_patricia_stats(struct ndpi_detection_module_struct *ndpi_struct,
ptree_type ptree_type, ptree_type ptree_type,
struct ndpi_patricia_tree_stats *stats) { struct ndpi_patricia_tree_stats *stats) {
if(!ndpi_struct || !stats) if(!ndpi_struct || !stats)
return -1; return -1;
switch(ptree_type) { switch(ptree_type) {
case NDPI_PTREE_RISK_MASK: case NDPI_PTREE_RISK_MASK:
ndpi_patricia_get_stats((ndpi_patricia_tree_t *)ndpi_struct->ip_risk_mask_ptree, stats); ndpi_patricia_get_stats(ndpi_struct->ip_risk_mask_ptree, stats);
return 0; return 0;
case NDPI_PTREE_RISK: case NDPI_PTREE_RISK:
ndpi_patricia_get_stats((ndpi_patricia_tree_t *)ndpi_struct->ip_risk_ptree, stats); ndpi_patricia_get_stats(ndpi_struct->ip_risk_ptree, stats);
return 0; return 0;
case NDPI_PTREE_PROTOCOLS: case NDPI_PTREE_PROTOCOLS:
ndpi_patricia_get_stats((ndpi_patricia_tree_t *)ndpi_struct->protocols_ptree, stats); ndpi_patricia_get_stats(ndpi_struct->protocols_ptree, stats);
return 0; return 0;
default: default:
@@ -2336,12 +2336,27 @@ u_int16_t ndpi_network_port_ptree_match(struct ndpi_detection_module_struct *ndp
if(node) { if(node) {
int i; int i;
struct patricia_uv16_list *item;
for(i=0; i<UV16_MAX_USER_VALUES; i++) { for(i=0; i<UV16_MAX_USER_VALUES; i++) {
if((node->value.u.uv16[i].additional_user_value == 0) if((node->value.u.uv16[i].additional_user_value == 0)
|| (node->value.u.uv16[i].additional_user_value == port)) || (node->value.u.uv16[i].additional_user_value == port))
return(node->value.u.uv16[i].user_value); return(node->value.u.uv16[i].user_value);
} }
/*
If we're here it means that we don't have
enough room for our custom value so we need
to check the custom_user_data pointer.
*/
item = (struct patricia_uv16_list*)node->data;
while(item != NULL) {
if(item->value.additional_user_value == port)
return(item->value.user_value);
else
item = item->next;
}
} }
return(NDPI_PROTOCOL_UNKNOWN); return(NDPI_PROTOCOL_UNKNOWN);
@@ -2505,12 +2520,30 @@ static int ndpi_add_host_ip_subprotocol(struct ndpi_detection_module_struct *ndp
if((node = add_to_ptree(ndpi_str->protocols_ptree, AF_INET, &pin, bits)) != NULL) { if((node = add_to_ptree(ndpi_str->protocols_ptree, AF_INET, &pin, bits)) != NULL) {
int i; int i;
struct patricia_uv16_list *item;
for(i=0; i<UV16_MAX_USER_VALUES; i++) { for(i=0; i<UV16_MAX_USER_VALUES; i++) {
if(node->value.u.uv16[i].user_value == 0) { if(node->value.u.uv16[i].user_value == 0) {
node->value.u.uv16[i].user_value = protocol_id, node->value.u.uv16[i].additional_user_value = htons(port); node->value.u.uv16[i].user_value = protocol_id, node->value.u.uv16[i].additional_user_value = htons(port);
return(0); return(0);
} }
} /* for */
/*
If we're here it means that we don't have
enough room for our custom value
*/
item = (struct patricia_uv16_list*)ndpi_malloc(sizeof(struct patricia_uv16_list));
if(item != NULL) {
item->value.user_value = protocol_id,
item->value.additional_user_value = htons(port),
item->next = (struct patricia_uv16_list*)node->data;
node->data = item;
return(0);
} }
return(-1); /* All slots are full */ return(-1); /* All slots are full */
@@ -3295,7 +3328,14 @@ int ndpi_get_custom_category_match(struct ndpi_detection_module_struct *ndpi_str
/* *********************************************** */ /* *********************************************** */
static void free_ptree_data(void *data) { static void free_ptree_data(void *data) {
; struct patricia_uv16_list *item = (struct patricia_uv16_list *)data;
while(item != NULL) {
struct patricia_uv16_list *next = item->next;
free(item);
item = next;
}
} }
/* ****************************************************** */ /* ****************************************************** */
@@ -3348,10 +3388,10 @@ void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_str) {
ndpi_patricia_destroy((ndpi_patricia_tree_t *) ndpi_str->protocols_ptree, free_ptree_data); ndpi_patricia_destroy((ndpi_patricia_tree_t *) ndpi_str->protocols_ptree, free_ptree_data);
if(ndpi_str->ip_risk_mask_ptree) if(ndpi_str->ip_risk_mask_ptree)
ndpi_patricia_destroy((ndpi_patricia_tree_t *) ndpi_str->ip_risk_mask_ptree, free_ptree_data); ndpi_patricia_destroy((ndpi_patricia_tree_t *) ndpi_str->ip_risk_mask_ptree, NULL);
if(ndpi_str->ip_risk_ptree) if(ndpi_str->ip_risk_ptree)
ndpi_patricia_destroy((ndpi_patricia_tree_t *) ndpi_str->ip_risk_ptree, free_ptree_data); ndpi_patricia_destroy((ndpi_patricia_tree_t *) ndpi_str->ip_risk_ptree, NULL);
if(ndpi_str->udpRoot != NULL) ndpi_tdestroy(ndpi_str->udpRoot, ndpi_free); if(ndpi_str->udpRoot != NULL) ndpi_tdestroy(ndpi_str->udpRoot, ndpi_free);
if(ndpi_str->tcpRoot != NULL) ndpi_tdestroy(ndpi_str->tcpRoot, ndpi_free); if(ndpi_str->tcpRoot != NULL) ndpi_tdestroy(ndpi_str->tcpRoot, ndpi_free);