Implementation of flow risk eception (work in progress)

This commit is contained in:
Luca Deri
2021-07-22 01:35:57 +02:00
parent fc9901292d
commit b01b60a2b5
7 changed files with 370 additions and 130 deletions
+12
View File
@@ -39,3 +39,15 @@ ip:213.75.170.11/32:443@CustomProtocol
ip:8.248.73.247:443@AmazonPrime ip:8.248.73.247:443@AmazonPrime
ip:54.80.47.130@AmazonPrime ip:54.80.47.130@AmazonPrime
#
# Risk Exceptions
#
# ip_risk_mask: used to mask flow risks for IP addresses
# host_risk_mask: used to mask exceptions for domain names and hosts
#
# Syntax: <name>=<64 bit mask to be put in AND with the risk
#
# For IPs, the flow risk is put in AND (source IP mask OR destination IP mask)
# For Flows with a hostname (e.g. TLS) the risk is also put in AND with the host_risk_mask
ip_risk_mask:192.168.1.6=0
host_risk_mask:"api-global.netflix.com"=0
+5 -2
View File
@@ -1297,6 +1297,7 @@ void update_tcp_flags_count(struct ndpi_flow_info* flow, struct ndpi_tcphdr* tcp
} }
/* ****************************************************** */ /* ****************************************************** */
/** /**
Function to process the packet: Function to process the packet:
determine the flow of a packet and try to decode it determine the flow of a packet and try to decode it
@@ -1567,11 +1568,13 @@ static struct ndpi_proto packet_processing(struct ndpi_workflow * workflow,
#endif #endif
*flow_risk = flow->risk; *flow_risk = flow->risk;
return(flow->detected_protocol); return(flow->detected_protocol);
} }
int ndpi_is_datalink_supported(int datalink_type) /* ****************************************************** */
{
int ndpi_is_datalink_supported(int datalink_type) {
/* Keep in sync with the similar switch in ndpi_workflow_process_packet */ /* Keep in sync with the similar switch in ndpi_workflow_process_packet */
switch(datalink_type) { switch(datalink_type) {
case DLT_NULL: case DLT_NULL:
+28
View File
@@ -705,6 +705,28 @@ extern "C" {
int ndpi_load_protocols_file(struct ndpi_detection_module_struct *ndpi_mod, int ndpi_load_protocols_file(struct ndpi_detection_module_struct *ndpi_mod,
const char* path); const char* path);
/**
* Add an IP-address based risk mask
*
* @par ndpi_mod = the detection module
* @par ip = the IP address for which you wanna set the mask
* @par mask = the IP risk mask
* @return 0 if the rule is loaded correctly;
* -1 else
*/
int ndpi_add_ip_risk_mask(struct ndpi_detection_module_struct *ndpi_mod, char *ip, ndpi_risk mask);
/**
* Add a host-address based risk mask
*
* @par ndpi_mod = the detection module
* @par host = the hostname/domain for which you wanna set the mask
* @par mask = the host risk mask
* @return 0 if the rule is loaded correctly;
* -1 else
*/
int ndpi_add_host_risk_mask(struct ndpi_detection_module_struct *ndpi_mod, char *host, ndpi_risk mask);
/** /**
* Read a file and load the categories * Read a file and load the categories
* *
@@ -1579,6 +1601,12 @@ int ndpi_get_geoip_country_continent(struct ndpi_detection_module_struct *ndpi_s
char *country_code, u_int8_t country_code_len, char *country_code, u_int8_t country_code_len,
char *continent, u_int8_t continent_len); char *continent, u_int8_t continent_len);
/* ******************************* */
char* ndpi_get_flow_name(struct ndpi_flow_struct *flow);
/* ******************************* */
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
+4 -1
View File
@@ -1160,9 +1160,12 @@ struct ndpi_detection_module_struct {
content_automa, /* Used for HTTP subprotocol_detection */ content_automa, /* Used for HTTP subprotocol_detection */
subprotocol_automa, /* Used for HTTP subprotocol_detection */ subprotocol_automa, /* Used for HTTP subprotocol_detection */
risky_domain_automa, tls_cert_subject_automa, risky_domain_automa, tls_cert_subject_automa,
malicious_ja3_automa, malicious_sha1_automa; malicious_ja3_automa, malicious_sha1_automa,
host_risk_mask_automa;
/* IMPORTANT: please update ndpi_finalize_initialization() whenever you add a new automa */ /* IMPORTANT: please update ndpi_finalize_initialization() whenever you add a new automa */
void *ip_risk_mask_ptree;
struct { struct {
ndpi_automa hostnames, hostnames_shadow; ndpi_automa hostnames, hostnames_shadow;
void *ipAddresses, *ipAddresses_shadow; /* Patricia */ void *ipAddresses, *ipAddresses_shadow; /* Patricia */
+180 -2
View File
@@ -628,6 +628,7 @@ static int ndpi_string_to_automa(struct ndpi_detection_module_struct *ndpi_str,
if(rc != ACERR_SUCCESS) { if(rc != ACERR_SUCCESS) {
ndpi_free(value_dup); ndpi_free(value_dup);
if(rc != ACERR_DUPLICATE_PATTERN) if(rc != ACERR_DUPLICATE_PATTERN)
return (-2); return (-2);
} }
@@ -2316,6 +2317,8 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(ndpi_init_prefs
if((ndpi_str->protocols_ptree = ndpi_patricia_new(32 /* IPv4 */)) != NULL) if((ndpi_str->protocols_ptree = ndpi_patricia_new(32 /* IPv4 */)) != NULL)
ndpi_init_ptree_ipv4(ndpi_str, ndpi_str->protocols_ptree, host_protocol_list, prefs & ndpi_dont_load_tor_hosts); ndpi_init_ptree_ipv4(ndpi_str, ndpi_str->protocols_ptree, host_protocol_list, prefs & ndpi_dont_load_tor_hosts);
ndpi_str->ip_risk_mask_ptree = ndpi_patricia_new(32 /* IPv4 */);
NDPI_BITMASK_RESET(ndpi_str->detection_bitmask); NDPI_BITMASK_RESET(ndpi_str->detection_bitmask);
#ifdef NDPI_ENABLE_DEBUG_MESSAGES #ifdef NDPI_ENABLE_DEBUG_MESSAGES
ndpi_str->user_data = NULL; ndpi_str->user_data = NULL;
@@ -2341,6 +2344,7 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(ndpi_init_prefs
ndpi_str->host_automa.ac_automa = ac_automata_init(ac_domain_match_handler); ndpi_str->host_automa.ac_automa = ac_automata_init(ac_domain_match_handler);
ndpi_str->content_automa.ac_automa = ac_automata_init(ac_domain_match_handler); ndpi_str->content_automa.ac_automa = ac_automata_init(ac_domain_match_handler);
ndpi_str->host_risk_mask_automa.ac_automa = ac_automata_init(ac_domain_match_handler);
ndpi_str->tls_cert_subject_automa.ac_automa = ac_automata_init(NULL); ndpi_str->tls_cert_subject_automa.ac_automa = ac_automata_init(NULL);
ndpi_str->malicious_ja3_automa.ac_automa = NULL; /* Initialized on demand */ ndpi_str->malicious_ja3_automa.ac_automa = NULL; /* Initialized on demand */
ndpi_str->malicious_sha1_automa.ac_automa = NULL; /* Initialized on demand */ ndpi_str->malicious_sha1_automa.ac_automa = NULL; /* Initialized on demand */
@@ -2373,19 +2377,28 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(ndpi_init_prefs
if(ndpi_str->content_automa.ac_automa) if(ndpi_str->content_automa.ac_automa)
ac_automata_feature(ndpi_str->content_automa.ac_automa,AC_FEATURE_LC); ac_automata_feature(ndpi_str->content_automa.ac_automa,AC_FEATURE_LC);
if(ndpi_str->host_risk_mask_automa.ac_automa)
ac_automata_feature(ndpi_str->host_risk_mask_automa.ac_automa,AC_FEATURE_LC);
/* ahocorasick debug */ /* ahocorasick debug */
/* Needed ac_automata_enable_debug(1) for show debug */ /* Needed ac_automata_enable_debug(1) for show debug */
if(ndpi_str->host_automa.ac_automa) if(ndpi_str->host_automa.ac_automa)
ac_automata_name(ndpi_str->host_automa.ac_automa,"host",AC_FEATURE_DEBUG); ac_automata_name(ndpi_str->host_automa.ac_automa,"host",AC_FEATURE_DEBUG);
if(ndpi_str->custom_categories.hostnames.ac_automa) if(ndpi_str->custom_categories.hostnames.ac_automa)
ac_automata_name(ndpi_str->custom_categories.hostnames.ac_automa,"ccat",0); ac_automata_name(ndpi_str->custom_categories.hostnames.ac_automa,"ccat",0);
if(ndpi_str->custom_categories.hostnames_shadow.ac_automa) if(ndpi_str->custom_categories.hostnames_shadow.ac_automa)
ac_automata_name(ndpi_str->custom_categories.hostnames_shadow.ac_automa,"ccat_sh",0); ac_automata_name(ndpi_str->custom_categories.hostnames_shadow.ac_automa,"ccat_sh",0);
if(ndpi_str->tls_cert_subject_automa.ac_automa) if(ndpi_str->tls_cert_subject_automa.ac_automa)
ac_automata_name(ndpi_str->tls_cert_subject_automa.ac_automa,"tls_cert",AC_FEATURE_DEBUG); ac_automata_name(ndpi_str->tls_cert_subject_automa.ac_automa,"tls_cert",AC_FEATURE_DEBUG);
if(ndpi_str->content_automa.ac_automa) if(ndpi_str->content_automa.ac_automa)
ac_automata_name(ndpi_str->content_automa.ac_automa,"content",AC_FEATURE_DEBUG); ac_automata_name(ndpi_str->content_automa.ac_automa,"content",AC_FEATURE_DEBUG);
if(ndpi_str->host_risk_mask_automa.ac_automa)
ac_automata_name(ndpi_str->host_risk_mask_automa.ac_automa,"content",AC_FEATURE_DEBUG);
if((ndpi_str->custom_categories.ipAddresses == NULL) || (ndpi_str->custom_categories.ipAddresses_shadow == NULL)) { if((ndpi_str->custom_categories.ipAddresses == NULL) || (ndpi_str->custom_categories.ipAddresses_shadow == NULL)) {
NDPI_LOG_ERR(ndpi_str, "[NDPI] Error allocating Patricia trees\n"); NDPI_LOG_ERR(ndpi_str, "[NDPI] Error allocating Patricia trees\n");
@@ -2432,6 +2445,10 @@ void ndpi_finalize_initialization(struct ndpi_detection_module_struct *ndpi_str)
automa = &ndpi_str->malicious_sha1_automa; automa = &ndpi_str->malicious_sha1_automa;
break; break;
case 5:
automa = &ndpi_str->host_risk_mask_automa;
break;
default: default:
ndpi_str->ac_automa_finalized = 1; ndpi_str->ac_automa_finalized = 1;
return; return;
@@ -2531,7 +2548,8 @@ int ndpi_match_string(void *_automa, char *string_to_match) {
if(!string_to_match) if(!string_to_match)
return(-2); return(-2);
rc = ndpi_match_string_common(_automa,string_to_match,strlen(string_to_match), rc = ndpi_match_string_common(_automa, string_to_match,
strlen(string_to_match),
&proto_id, NULL, NULL); &proto_id, NULL, NULL);
if(rc < 0) return rc; if(rc < 0) return rc;
@@ -2662,6 +2680,9 @@ void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_str) {
if(ndpi_str->protocols_ptree) if(ndpi_str->protocols_ptree)
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)
ndpi_patricia_destroy((ndpi_patricia_tree_t *) ndpi_str->ip_risk_mask_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) if(ndpi_str->tcpRoot != NULL)
@@ -2703,6 +2724,10 @@ void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_str) {
if(ndpi_str->custom_categories.ipAddresses_shadow != NULL) if(ndpi_str->custom_categories.ipAddresses_shadow != NULL)
ndpi_patricia_destroy((ndpi_patricia_tree_t *) ndpi_str->custom_categories.ipAddresses_shadow, free_ptree_data); ndpi_patricia_destroy((ndpi_patricia_tree_t *) ndpi_str->custom_categories.ipAddresses_shadow, free_ptree_data);
if(ndpi_str->host_risk_mask_automa.ac_automa != NULL)
ac_automata_release((AC_AUTOMATA_t *) ndpi_str->host_risk_mask_automa.ac_automa,
1 /* free patterns strings memory */);
#ifdef CUSTOM_NDPI_PROTOCOLS #ifdef CUSTOM_NDPI_PROTOCOLS
#include "../../../nDPI-custom/ndpi_exit_detection_module.c" #include "../../../nDPI-custom/ndpi_exit_detection_module.c"
#endif #endif
@@ -2876,6 +2901,86 @@ char *strsep(char **sp, char *sep) {
} }
#endif #endif
/* ********************************************************************************* */
static u_int64_t ndpi_host_ip_risk_ptree_match(struct ndpi_detection_module_struct *ndpi_str,
struct in_addr *pin /* network byte order */) {
ndpi_prefix_t prefix;
ndpi_patricia_node_t *node;
/* Make sure all in network byte order otherwise compares wont work */
ndpi_fill_prefix_v4(&prefix, pin, 32, ((ndpi_patricia_tree_t *) ndpi_str->protocols_ptree)->maxbits);
node = ndpi_patricia_search_best(ndpi_str->ip_risk_mask_ptree, &prefix);
if(node)
return(node->value.u.uv64);
else
return((u_int64_t)-1);
}
/* ******************************************************************** */
int ndpi_add_ip_risk_mask(struct ndpi_detection_module_struct *ndpi_str,
char *ip, ndpi_risk mask) {
char *saveptr, *addr = strtok_r(ip, "/", &saveptr);
if(addr) {
char *cidr = strtok_r(NULL, "\n", &saveptr);
struct in_addr pin;
ndpi_patricia_node_t *node;
pin.s_addr = inet_addr(addr);
/* FIX: Add IPv6 support */
if((node = add_to_ptree(ndpi_str->ip_risk_mask_ptree, AF_INET,
&pin, cidr ? atoi(cidr) : 32 /* bits */)) != NULL) {
node->value.u.uv64 = (u_int64_t)mask;
return(0);
} else
return(-1);
} else
return(-2);
}
/* ******************************************************************** */
int ndpi_add_host_risk_mask(struct ndpi_detection_module_struct *ndpi_str,
char *host, ndpi_risk mask) {
/* host_risk_mask_automa */
AC_PATTERN_t ac_pattern;
AC_ERROR_t rc;
u_int len;
char *host_dup = NULL;
if((ndpi_str->host_risk_mask_automa.ac_automa == NULL) || (host == NULL))
return(-2);
host_dup = ndpi_strdup(host);
if(!host_dup)
return(-1);
memset(&ac_pattern, 0, sizeof(ac_pattern));
len = strlen(host);
ac_pattern.astring = host_dup;
ac_pattern.length = len;
ac_pattern.rep.number64 = (ndpi_risk)mask;
ac_pattern.rep.level = ndpi_domain_level(host);
ac_pattern.rep.at_end = 0;
ac_pattern.rep.dot = memchr(host,'.',len) != NULL;
rc = ac_automata_add(ndpi_str->host_risk_mask_automa.ac_automa, &ac_pattern);
if(rc != ACERR_SUCCESS) {
ndpi_free(host_dup);
if(rc != ACERR_DUPLICATE_PATTERN)
return (-2);
}
return(0);
}
/* ******************************************************************** */ /* ******************************************************************** */
int ndpi_handle_rule(struct ndpi_detection_module_struct *ndpi_str, char *rule, u_int8_t do_add) { int ndpi_handle_rule(struct ndpi_detection_module_struct *ndpi_str, char *rule, u_int8_t do_add) {
@@ -2885,8 +2990,35 @@ int ndpi_handle_rule(struct ndpi_detection_module_struct *ndpi_str, char *rule,
at = strrchr(rule, '@'); at = strrchr(rule, '@');
if(at == NULL) { if(at == NULL) {
/* This looks like a mask rule or an invalid rule */
char _rule[256], *rule_type, *key;
snprintf(_rule, sizeof(_rule), "%s", rule);
rule_type = strtok(rule, ":");
if(!rule_type) {
NDPI_LOG_ERR(ndpi_str, "Invalid rule '%s'\n", rule); NDPI_LOG_ERR(ndpi_str, "Invalid rule '%s'\n", rule);
return(-1); return(-1);
}
key = strtok(NULL, "=");
if(key) {
char *mask = strtok(NULL, "=");
if(mask) {
ndpi_risk risk_mask = (ndpi_risk)atoll(mask);
if(!strcmp(rule_type, "ip_risk_mask")) {
return(ndpi_add_ip_risk_mask(ndpi_str, key, risk_mask));
} else if(!strcmp(rule_type, "host_risk_mask")) {
return(ndpi_add_host_risk_mask(ndpi_str, key, risk_mask));
}
}
}
NDPI_LOG_ERR(ndpi_str, "Unknown rule '%s'\n", rule);
return(-1);
} else } else
at[0] = 0, proto = &at[1]; at[0] = 0, proto = &at[1];
@@ -4602,6 +4734,49 @@ u_int32_t ndpi_check_flow_func(struct ndpi_detection_module_struct *ndpi_str,
/* ********************************************************************************* */ /* ********************************************************************************* */
void ndpi_handle_risk_exceptions(struct ndpi_detection_module_struct *ndpi_str,
struct ndpi_flow_struct *flow) {
char *host;
if(flow->risk == 0) return; /* Nothing to do */
host = ndpi_get_flow_name(flow);
if(host) {
/* Check host exception */
ndpi_automa *automa = &ndpi_str->host_risk_mask_automa;
if(automa->ac_automa) {
AC_TEXT_t ac_input_text;
AC_REP_t match;
ac_input_text.astring = host, ac_input_text.length = strlen(host);
ac_input_text.option = 0;
if(ac_automata_search(automa->ac_automa, &ac_input_text, &match) > 0)
flow->risk &= match.number64;
}
}
/* TODO: add IPv6 support */
if(flow->packet.iph) {
struct ndpi_packet_struct *packet = &flow->packet;
struct in_addr pin;
if(flow->risk) {
pin.s_addr = packet->iph->saddr;
flow->risk &= ndpi_host_ip_risk_ptree_match(ndpi_str, &pin);
}
if(flow->risk) {
pin.s_addr = packet->iph->daddr;
flow->risk &= ndpi_host_ip_risk_ptree_match(ndpi_str, &pin);
}
}
}
/* ********************************************************************************* */
u_int16_t ndpi_guess_host_protocol_id(struct ndpi_detection_module_struct *ndpi_str, u_int16_t ndpi_guess_host_protocol_id(struct ndpi_detection_module_struct *ndpi_str,
struct ndpi_flow_struct *flow) { struct ndpi_flow_struct *flow) {
u_int16_t ret = NDPI_PROTOCOL_UNKNOWN; u_int16_t ret = NDPI_PROTOCOL_UNKNOWN;
@@ -5379,6 +5554,8 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
ret.app_protocol = flow->guessed_host_protocol_id; ret.app_protocol = flow->guessed_host_protocol_id;
} }
ndpi_reconcile_protocols(ndpi_str, flow, &ret);
if((!flow->risk_checked) && (ret.master_protocol != NDPI_PROTOCOL_UNKNOWN)) { if((!flow->risk_checked) && (ret.master_protocol != NDPI_PROTOCOL_UNKNOWN)) {
ndpi_default_ports_tree_node_t *found; ndpi_default_ports_tree_node_t *found;
u_int16_t *default_ports, sport, dport; u_int16_t *default_ports, sport, dport;
@@ -5432,10 +5609,11 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
} }
} }
ndpi_handle_risk_exceptions(ndpi_str, flow);
flow->risk_checked = 1; flow->risk_checked = 1;
} }
ndpi_reconcile_protocols(ndpi_str, flow, &ret); // printf("===> RISK=%llu\n", flow->risk);
if(num_calls == 0) if(num_calls == 0)
flow->fail_with_unknown = 1; flow->fail_with_unknown = 1;
+15
View File
@@ -2050,3 +2050,18 @@ float ndpi_calculate_entropy(u_int8_t const * const buf, size_t len)
entropy *= -1.0f; entropy *= -1.0f;
return entropy; return entropy;
} }
/* ******************************************* */
char* ndpi_get_flow_name(struct ndpi_flow_struct *flow) {
if(!flow) goto no_flow_info;
if(flow->host_server_name[0] != '\0')
return((char*)flow->host_server_name);
if(flow->protos.tls_quic_stun.tls_quic.client_requested_server_name[0] != '\0')
return(flow->protos.tls_quic_stun.tls_quic.client_requested_server_name);
no_flow_info:
return((char*)"");
}
+1
View File
@@ -54,6 +54,7 @@ typedef char AC_ALPHABET_t;
**/ **/
typedef struct { typedef struct {
uint32_t number; /* Often used to store procotolId */ uint32_t number; /* Often used to store procotolId */
uint64_t number64;
uint16_t breed, category; uint16_t breed, category;
uint16_t level, /* Domain level for comparison */ uint16_t level, /* Domain level for comparison */
from_start:1, /* match from start of string */ from_start:1, /* match from start of string */