Category matching fix

This commit is contained in:
Luca Deri
2020-02-05 08:37:24 +01:00
parent 2009361b89
commit 4613515635
+16 -4
View File
@@ -1826,7 +1826,11 @@ static int ac_match_handler(AC_MATCH_t *m, AC_TEXT_t *txt, AC_REP_t *match) {
if((whatfound != buf) if((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) */) {
if(whatfound[-1] != '.') { int len = strlen(m->patterns->astring);
if((whatfound[-1] != '.')
|| ((m->patterns->astring[len-1] != '.') && (whatfound[len] != '\0') /* endsWith does not hold here */)
) {
return(0); return(0);
} else { } else {
memcpy(match, &m->patterns[0].rep, sizeof(AC_REP_t)); /* Partial match? */ memcpy(match, &m->patterns[0].rep, sizeof(AC_REP_t)); /* Partial match? */
@@ -4716,7 +4720,7 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
if(flow->check_extra_packets) { if(flow->check_extra_packets) {
ndpi_process_extra_packet(ndpi_str, flow, packet, packetlen, current_tick_l, src, dst); ndpi_process_extra_packet(ndpi_str, flow, packet, packetlen, current_tick_l, src, dst);
/* Update in case of new match */ /* Update in case of new match */
ret.master_protocol = flow->detected_protocol_stack[1], ret.app_protocol = flow->detected_protocol_stack[0], ret.category = flow->category;; ret.master_protocol = flow->detected_protocol_stack[1], ret.app_protocol = flow->detected_protocol_stack[0], ret.category = flow->category;
goto invalidate_ptr; goto invalidate_ptr;
} else } else
goto ret_protocols; goto ret_protocols;
@@ -6183,6 +6187,7 @@ int ndpi_match_string_subprotocol(struct ndpi_detection_module_struct *ndpi_str,
AC_TEXT_t ac_input_text; AC_TEXT_t ac_input_text;
ndpi_automa *automa = is_host_match ? &ndpi_str->host_automa : &ndpi_str->content_automa; ndpi_automa *automa = is_host_match ? &ndpi_str->host_automa : &ndpi_str->content_automa;
AC_REP_t match = { NDPI_PROTOCOL_UNKNOWN, NDPI_PROTOCOL_CATEGORY_UNSPECIFIED, NDPI_PROTOCOL_UNRATED }; AC_REP_t match = { NDPI_PROTOCOL_UNKNOWN, NDPI_PROTOCOL_CATEGORY_UNSPECIFIED, NDPI_PROTOCOL_UNRATED };
int rc;
if((automa->ac_automa == NULL) || (string_to_match_len == 0)) if((automa->ac_automa == NULL) || (string_to_match_len == 0))
return(NDPI_PROTOCOL_UNKNOWN); return(NDPI_PROTOCOL_UNKNOWN);
@@ -6193,14 +6198,21 @@ int ndpi_match_string_subprotocol(struct ndpi_detection_module_struct *ndpi_str,
} }
ac_input_text.astring = string_to_match, ac_input_text.length = string_to_match_len; ac_input_text.astring = string_to_match, ac_input_text.length = string_to_match_len;
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);
/*
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;
/* We need to take into account also rc == 0 that is used for partial matches */ /* We need to take into account also rc == 0 that is used for partial matches */
ret_match->protocol_id = match.number, ret_match->protocol_id = match.number,
ret_match->protocol_category = match.category, ret_match->protocol_category = match.category,
ret_match->protocol_breed = match.breed; ret_match->protocol_breed = match.breed;
return(match.number); return(rc ? match.number : 0);
} }
#ifdef HAVE_HYPERSCAN #ifdef HAVE_HYPERSCAN