Fixes bug that prevents triggering alerts for traffic on non-standard ports that have been defined in the custom protocols file

Fixes https://github.com/ntop/ntopng/issues/6458
This commit is contained in:
Luca Deri
2022-05-04 16:07:05 +02:00
parent 81a367f1ae
commit 5996cf1725
+32 -20
View File
@@ -158,8 +158,8 @@ static ndpi_risk_info ndpi_known_risks[] = {
/* ****************************************** */ /* ****************************************** */
/* Forward */ /* Forward */
static void addDefaultPort(struct ndpi_detection_module_struct *ndpi_str, ndpi_port_range *range, static void addDefaultPort(ndpi_port_range *range, ndpi_proto_defaults_t *def,
ndpi_proto_defaults_t *def, u_int8_t customUserProto, ndpi_default_ports_tree_node_t **root, u_int8_t customUserProto, ndpi_default_ports_tree_node_t **root,
const char *_func, int _line); const char *_func, int _line);
static int removeDefaultPort(ndpi_port_range *range, ndpi_proto_defaults_t *def, ndpi_default_ports_tree_node_t **root); static int removeDefaultPort(ndpi_port_range *range, ndpi_proto_defaults_t *def, ndpi_default_ports_tree_node_t **root);
@@ -498,11 +498,11 @@ void ndpi_set_proto_defaults(struct ndpi_detection_module_struct *ndpi_str,
for(j = 0; j < MAX_DEFAULT_PORTS; j++) { for(j = 0; j < MAX_DEFAULT_PORTS; j++) {
if(udpDefPorts[j].port_low != 0) if(udpDefPorts[j].port_low != 0)
addDefaultPort(ndpi_str, &udpDefPorts[j], &ndpi_str->proto_defaults[protoId], 0, &ndpi_str->udpRoot, addDefaultPort(&udpDefPorts[j], &ndpi_str->proto_defaults[protoId], 0, &ndpi_str->udpRoot,
__FUNCTION__, __LINE__); __FUNCTION__, __LINE__);
if(tcpDefPorts[j].port_low != 0) if(tcpDefPorts[j].port_low != 0)
addDefaultPort(ndpi_str, &tcpDefPorts[j], &ndpi_str->proto_defaults[protoId], 0, &ndpi_str->tcpRoot, addDefaultPort(&tcpDefPorts[j], &ndpi_str->proto_defaults[protoId], 0, &ndpi_str->tcpRoot,
__FUNCTION__, __LINE__); __FUNCTION__, __LINE__);
/* No port range, just the lower port */ /* No port range, just the lower port */
@@ -538,9 +538,12 @@ void ndpi_default_ports_tree_node_t_walker(const void *node, const ndpi_VISIT wh
/* ******************************************************************** */ /* ******************************************************************** */
static void addDefaultPort(struct ndpi_detection_module_struct *ndpi_str, ndpi_port_range *range, static void addDefaultPort(ndpi_port_range *range,
ndpi_proto_defaults_t *def, u_int8_t customUserProto, ndpi_default_ports_tree_node_t **root, ndpi_proto_defaults_t *def,
const char *_func, int _line) { u_int8_t customUserProto,
ndpi_default_ports_tree_node_t **root,
const char *_func,
int _line) {
u_int16_t port; u_int16_t port;
for(port = range->port_low; port <= range->port_high; port++) { for(port = range->port_low; port <= range->port_high; port++) {
@@ -554,11 +557,13 @@ static void addDefaultPort(struct ndpi_detection_module_struct *ndpi_str, ndpi_p
} }
node->proto = def, node->default_port = port, node->customUserProto = customUserProto; node->proto = def, node->default_port = port, node->customUserProto = customUserProto;
ret = (ndpi_default_ports_tree_node_t *) ndpi_tsearch(node, (void *) root, ndpi_default_ports_tree_node_t_cmp); /* Add it to the tree */ ret = (ndpi_default_ports_tree_node_t *) ndpi_tsearch(node,
(void *) root,
ndpi_default_ports_tree_node_t_cmp); /* Add it to the tree */
if(ret != node) { if(ret != node) {
NDPI_LOG_DBG(ndpi_str, "[NDPI] %s:%d found duplicate for port %u: overwriting it with new value\n", _func, NDPI_LOG_DBG(ndpi_str, "[NDPI] %s:%d found duplicate for port %u: overwriting it with new value\n",
_line, port); _func, _line, port);
ret->proto = def; ret->proto = def;
ndpi_free(node); ndpi_free(node);
@@ -2940,10 +2945,8 @@ void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_str) {
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, free_ptree_data);
if(ndpi_str->udpRoot != NULL) if(ndpi_str->udpRoot != NULL) ndpi_tdestroy(ndpi_str->udpRoot, ndpi_free);
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);
if(ndpi_str->host_automa.ac_automa != NULL) if(ndpi_str->host_automa.ac_automa != NULL)
ac_automata_release((AC_AUTOMATA_t *) ndpi_str->host_automa.ac_automa, ac_automata_release((AC_AUTOMATA_t *) ndpi_str->host_automa.ac_automa,
@@ -3428,7 +3431,7 @@ int ndpi_handle_rule(struct ndpi_detection_module_struct *ndpi_str, char *rule,
range.port_low = range.port_high = atoi(&elem[4]); range.port_low = range.port_high = atoi(&elem[4]);
if(do_add) if(do_add)
addDefaultPort(ndpi_str, &range, def, 1 /* Custom user proto */, addDefaultPort(&range, def, 1 /* Custom user proto */,
is_tcp ? &ndpi_str->tcpRoot : &ndpi_str->udpRoot, __FUNCTION__, __LINE__); is_tcp ? &ndpi_str->tcpRoot : &ndpi_str->udpRoot, __FUNCTION__, __LINE__);
else else
removeDefaultPort(&range, def, is_tcp ? &ndpi_str->tcpRoot : &ndpi_str->udpRoot); removeDefaultPort(&range, def, is_tcp ? &ndpi_str->tcpRoot : &ndpi_str->udpRoot);
@@ -6080,7 +6083,8 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
if(!ndpi_check_protocol_port_mismatch_exceptions(ndpi_str, flow, found, &ret)) { if(!ndpi_check_protocol_port_mismatch_exceptions(ndpi_str, flow, found, &ret)) {
/* /*
Before triggering the alert we need to make some extra checks Before triggering the alert we need to make some extra checks
- the protocol found is not running on the port we have found (i.e. two or more protools share the same default port) - the protocol found is not running on the port we have found
(i.e. two or more protools share the same default port)
*/ */
u_int8_t found = 0, i; u_int8_t found = 0, i;
@@ -6091,8 +6095,13 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
} }
} /* for */ } /* for */
if(!found) if(!found) {
ndpi_set_risk(ndpi_str, flow, NDPI_KNOWN_PROTOCOL_ON_NON_STANDARD_PORT); ndpi_default_ports_tree_node_t *ret = ndpi_get_guessed_protocol_id(ndpi_str, packet->udp ? IPPROTO_UDP : IPPROTO_TCP,
sport, dport);
if(ret == NULL)
ndpi_set_risk(ndpi_str, flow, NDPI_KNOWN_PROTOCOL_ON_NON_STANDARD_PORT);
}
} }
} else if((!ndpi_is_ntop_protocol(&ret)) && default_ports && (default_ports[0] != 0)) { } else if((!ndpi_is_ntop_protocol(&ret)) && default_ports && (default_ports[0] != 0)) {
u_int8_t found = 0, i, num_loops = 0; u_int8_t found = 0, i, num_loops = 0;
@@ -6116,8 +6125,11 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
} }
if(!found) { if(!found) {
// printf("******** Invalid default port\n"); ndpi_default_ports_tree_node_t *ret = ndpi_get_guessed_protocol_id(ndpi_str, packet->udp ? IPPROTO_UDP : IPPROTO_TCP,
ndpi_set_risk(ndpi_str, flow, NDPI_KNOWN_PROTOCOL_ON_NON_STANDARD_PORT); sport, dport);
if(ret == NULL)
ndpi_set_risk(ndpi_str, flow, NDPI_KNOWN_PROTOCOL_ON_NON_STANDARD_PORT);
} }
} }