Fixed partial string matches

This commit is contained in:
Luca Deri
2019-09-27 13:42:41 +02:00
parent 5099646938
commit 7c788f0779
3 changed files with 55 additions and 29 deletions
+1 -2
View File
@@ -3462,8 +3462,7 @@ void automataUnitTest() {
assert(ndpi_add_string_to_automa(automa, "hello") == 0); assert(ndpi_add_string_to_automa(automa, "hello") == 0);
assert(ndpi_add_string_to_automa(automa, "world") == 0); assert(ndpi_add_string_to_automa(automa, "world") == 0);
ndpi_finalize_automa(automa); ndpi_finalize_automa(automa);
assert(ndpi_match_string(automa, "This is the wonderful world of nDPI") == 0); assert(ndpi_match_string(automa, "This is the wonderful world of nDPI") == 1);
ndpi_free_automa(automa); ndpi_free_automa(automa);
} }
+28 -3
View File
@@ -1794,10 +1794,14 @@ static int ac_match_handler(AC_MATCH_t *m, AC_TEXT_t *txt, AC_REP_t *match) {
if(whatfound if(whatfound
&& (whatfound != buf) && (whatfound != buf)
&& (m->patterns->astring[0] != '.') /* The searched pattern does not start with . */ && (m->patterns->astring[0] != '.') /* The searched pattern does not start with . */
&& strchr(m->patterns->astring, '.') /* The matched pattern has a . (e.g. numeric or sym IPs) */ && strchr(m->patterns->astring, '.') /* The matched pattern has a . (e.g. numeric or sym IPs) */) {
&& (whatfound[-1] != '.') if(whatfound[-1] != '.') {
)
return(0); return(0);
} else {
memcpy(match, &m->patterns[0].rep, sizeof(AC_REP_t)); /* Partial match? */
return(0); /* Keep searching as probably there is a better match */
}
}
} }
/* /*
@@ -2215,6 +2219,13 @@ int ndpi_match_string(void *_automa, char *string_to_match) {
rc = ac_automata_search(automa, &ac_input_text, &match); rc = ac_automata_search(automa, &ac_input_text, &match);
ac_automata_reset(automa); ac_automata_reset(automa);
/*
As ac_automata_search can detect partial matches and continue the search process
in case rc == 0 (i.e. no match), we need to check if there is a partial match
and in this case return it
*/
if((rc == 0) && (match.number != 0)) rc = 1;
return(rc ? match.number : 0); return(rc ? match.number : 0);
} }
@@ -2236,6 +2247,13 @@ int ndpi_match_string_id(void *_automa, char *string_to_match, unsigned long *id
rc = ac_automata_search(automa, &ac_input_text, &match); rc = ac_automata_search(automa, &ac_input_text, &match);
ac_automata_reset(automa); ac_automata_reset(automa);
/*
As ac_automata_search can detect partial matches and continue the search process
in case rc == 0 (i.e. no match), we need to check if there is a partial match
and in this case return it
*/
if((rc == 0) && (match.number != 0)) rc = 1;
*id = rc ? match.number : NDPI_PROTOCOL_UNKNOWN; *id = rc ? match.number : NDPI_PROTOCOL_UNKNOWN;
return(*id != NDPI_PROTOCOL_UNKNOWN ? 0 : -1); return(*id != NDPI_PROTOCOL_UNKNOWN ? 0 : -1);
@@ -6115,6 +6133,13 @@ int ndpi_match_bigram(struct ndpi_detection_module_struct *ndpi_struct,
rc = ac_automata_search(((AC_AUTOMATA_t*)automa->ac_automa), &ac_input_text, &match); rc = ac_automata_search(((AC_AUTOMATA_t*)automa->ac_automa), &ac_input_text, &match);
ac_automata_reset(((AC_AUTOMATA_t*)automa->ac_automa)); ac_automata_reset(((AC_AUTOMATA_t*)automa->ac_automa));
/*
As ac_automata_search can detect partial matches and continue the search process
in case rc == 0 (i.e. no match), we need to check if there is a partial match
and in this case return it
*/
if((rc == 0) && (match.number != 0)) rc = 1;
return(rc ? match.number : 0); return(rc ? match.number : 0);
} }
+4 -2
View File
@@ -263,12 +263,14 @@ void ndpi_search_dns(struct ndpi_detection_module_struct *ndpi_struct, struct nd
if(j > 0) { if(j > 0) {
ndpi_protocol_match_result ret_match; ndpi_protocol_match_result ret_match;
u_int32_t subproto = ndpi_match_host_subprotocol(ndpi_struct, flow,
ndpi_match_host_subprotocol(ndpi_struct, flow,
(char *)flow->host_server_name, (char *)flow->host_server_name,
strlen((const char*)flow->host_server_name), strlen((const char*)flow->host_server_name),
&ret_match, &ret_match,
NDPI_PROTOCOL_DNS); NDPI_PROTOCOL_DNS);
if(ret_match.protocol_category != NDPI_PROTOCOL_CATEGORY_UNSPECIFIED)
flow->category = ret_match.protocol_category;
} }
#ifdef DNS_DEBUG #ifdef DNS_DEBUG