Fixed loading of non-ICANN domains that caused false positives with ndpi_load_domain_suffixes

Minor hash optimization
This commit is contained in:
Luca Deri
2024-01-27 20:40:27 +01:00
parent bcca89b78c
commit 65b9c68d7d
4 changed files with 37 additions and 21 deletions
+5 -2
View File
@@ -595,7 +595,8 @@ static void help(u_int long_help) {
" | 0 - List known protocols\n" " | 0 - List known protocols\n"
" | 1 - List known categories\n" " | 1 - List known categories\n"
" | 2 - List known risks\n" " | 2 - List known risks\n"
" -d | Disable protocol guess (by ip and by port) and use only DPI. It is a shortcut to --cfg=dpi.guess_on_giveup,0\n" " -d | Disable protocol guess (by ip and by port) and use only DPI.\n"
" | It is a shortcut to --cfg=dpi.guess_on_giveup,0\n"
" -e <len> | Min human readeable string match len. Default %u\n" " -e <len> | Min human readeable string match len. Default %u\n"
" -q | Quiet mode\n" " -q | Quiet mode\n"
" -F | Enable flow stats\n" " -F | Enable flow stats\n"
@@ -636,7 +637,8 @@ static void help(u_int long_help) {
" -x <domain> | Check domain name [Test only]\n" " -x <domain> | Check domain name [Test only]\n"
" -I | Ignore VLAN id for flow hash calculation\n" " -I | Ignore VLAN id for flow hash calculation\n"
" -A | Dump internal statistics (LRU caches / Patricia trees / Ahocarasick automas / ...\n" " -A | Dump internal statistics (LRU caches / Patricia trees / Ahocarasick automas / ...\n"
" -M | Memory allocation stats on data-path (only by the library). It works only on single-thread configuration\n" " -M | Memory allocation stats on data-path (only by the library).\n"
" | It works only on single-thread configuration\n"
" --cfg=proto,param,value | Configure the specific attribute of this protocol\n" " --cfg=proto,param,value | Configure the specific attribute of this protocol\n"
, ,
human_readeable_string_len, human_readeable_string_len,
@@ -5713,6 +5715,7 @@ void domainsUnitTest() {
assert(strcmp(ndpi_get_host_domain(ndpi_info_mod, "www.unipi.it"), "unipi.it") == 0); assert(strcmp(ndpi_get_host_domain(ndpi_info_mod, "www.unipi.it"), "unipi.it") == 0);
assert(strcmp(ndpi_get_host_domain(ndpi_info_mod, "mail.apple.com"), "apple.com") == 0); assert(strcmp(ndpi_get_host_domain(ndpi_info_mod, "mail.apple.com"), "apple.com") == 0);
assert(strcmp(ndpi_get_host_domain(ndpi_info_mod, "www.bbc.co.uk"), "bbc.co.uk") == 0); assert(strcmp(ndpi_get_host_domain(ndpi_info_mod, "www.bbc.co.uk"), "bbc.co.uk") == 0);
assert(strcmp(ndpi_get_host_domain(ndpi_info_mod, "zy1ssnfwwl.execute-api.eu-north-1.amazonaws.com"), "amazonaws.com") == 0);
} }
ndpi_exit_detection_module(ndpi_info_mod); ndpi_exit_detection_module(ndpi_info_mod);
+13 -4
View File
@@ -32,6 +32,8 @@
#define DEBUG_CONTAINS #define DEBUG_CONTAINS
#endif #endif
/* ********************************************************** */
ndpi_domain_classify* ndpi_domain_classify_alloc() { ndpi_domain_classify* ndpi_domain_classify_alloc() {
int i; int i;
ndpi_domain_classify *cat = (ndpi_domain_classify*)ndpi_malloc(sizeof(ndpi_domain_classify)); ndpi_domain_classify *cat = (ndpi_domain_classify*)ndpi_malloc(sizeof(ndpi_domain_classify));
@@ -88,6 +90,7 @@ bool ndpi_domain_classify_add(ndpi_domain_classify *s,
const char *domain) { const char *domain) {
u_int32_t i; u_int32_t i;
char *dot; char *dot;
u_int64_t hash;
if((!s) || (!domain)) if((!s) || (!domain))
return(false); return(false);
@@ -119,8 +122,14 @@ bool ndpi_domain_classify_add(ndpi_domain_classify *s,
if(i == MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS) if(i == MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS)
return(false); return(false);
return(ndpi_bitmap64_set(s->classes[i].domains, hash = ndpi_quick_hash64(domain, strlen(domain));
ndpi_quick_hash64(domain, strlen(domain))));
#ifdef DEBUG_ADD
if(strcmp(domain, "execute-api.eu-north-1.amazonaws.com") == 0)
printf("[add] %s = %d [%llu]\n", domain, s->classes[i].class_id, hash);
#endif
return(ndpi_bitmap64_set(s->classes[i].domains, hash));
} }
/* ********************************************************** */ /* ********************************************************** */
@@ -255,7 +264,8 @@ const char* ndpi_domain_classify_longest_prefix(ndpi_domain_classify *s,
if(s->classes[i].class_id != 0) { if(s->classes[i].class_id != 0) {
if(ndpi_bitmap64_isset(s->classes[i].domains, hash)) { if(ndpi_bitmap64_isset(s->classes[i].domains, hash)) {
#ifdef DEBUG_CONTAINS #ifdef DEBUG_CONTAINS
printf("[contains] %s = %d\n", hostname, s->classes[i].class_id); printf("[contains] %s = %d [%llu]\n",
hostname, s->classes[i].class_id, hash);
#endif #endif
*class_id = s->classes[i].class_id; *class_id = s->classes[i].class_id;
return(return_subprefix ? prev_elem : elem); return(return_subprefix ? prev_elem : elem);
@@ -286,4 +296,3 @@ bool ndpi_domain_classify_contains(ndpi_domain_classify *s,
return((*class_id == 0) ? false : true); return((*class_id == 0) ? false : true);
} }
+4
View File
@@ -48,6 +48,10 @@ int ndpi_load_domain_suffixes(struct ndpi_detection_module_struct *ndpi_str,
while((line = fgets(buf, sizeof(buf), fd)) != NULL) { while((line = fgets(buf, sizeof(buf), fd)) != NULL) {
u_int offset, len; u_int offset, len;
/* Skip private domains */
if(strstr(line, "// ===END ICANN DOMAINS==="))
break;
/* Skip empty lines or comments */ /* Skip empty lines or comments */
if((line[0] == '\0') || (line[0] == '/') || (line[0] == '\n') || (line[0] == '\r')) if((line[0] == '\0') || (line[0] == '/') || (line[0] == '\n') || (line[0] == '\r'))
continue; continue;
+1 -1
View File
@@ -56,7 +56,7 @@ u_int64_t ndpi_quick_hash64(const char *str, u_int str_len) {
for(i=0; i<str_len; i++) for(i=0; i<str_len; i++)
h = (h * 177) + str[i]; h = (h * 177) + str[i];
h ^= strlen(str); h ^= str_len;
return h; return h;
} }