Implemented ALPN automa for checking uncommon ALPNs

This commit is contained in:
Luca Deri
2021-07-24 17:50:32 +02:00
parent 526568fcd5
commit 51e4888442
6 changed files with 108 additions and 64 deletions
+1 -1
View File
@@ -1473,7 +1473,7 @@ static struct ndpi_proto packet_processing(struct ndpi_workflow * workflow,
/* Copy packets entropy if num packets count == 10 */ /* Copy packets entropy if num packets count == 10 */
ndpi_clear_entropy_stats(flow); ndpi_clear_entropy_stats(flow);
if(!flow->has_human_readeable_strings) { if((human_readeable_string_len != 0) && (!flow->has_human_readeable_strings)) {
u_int8_t skip = 0; u_int8_t skip = 0;
if((proto == IPPROTO_TCP) if((proto == IPPROTO_TCP)
+3
View File
@@ -155,6 +155,9 @@ extern "C" {
struct ndpi_flow_struct *flow, ndpi_risk_enum r); struct ndpi_flow_struct *flow, ndpi_risk_enum r);
int ndpi_is_printable_string(char const * const str, size_t len); int ndpi_is_printable_string(char const * const str, size_t len);
float ndpi_calculate_entropy(u_int8_t const * const buf, size_t len); float ndpi_calculate_entropy(u_int8_t const * const buf, size_t len);
void load_common_alpns(struct ndpi_detection_module_struct *ndpi_str);
u_int8_t is_a_common_alpn(struct ndpi_detection_module_struct *ndpi_str,
const char *alpn_to_check, u_int alpn_to_check_len);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
+1 -1
View File
@@ -1161,7 +1161,7 @@ struct ndpi_detection_module_struct {
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; host_risk_mask_automa, common_alpns_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; void *ip_risk_mask_ptree;
+16 -1
View File
@@ -2345,6 +2345,8 @@ 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->host_risk_mask_automa.ac_automa = ac_automata_init(ac_domain_match_handler);
ndpi_str->common_alpns_automa.ac_automa = ac_automata_init(ac_domain_match_handler);
load_common_alpns(ndpi_str);
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 */
@@ -2380,6 +2382,9 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(ndpi_init_prefs
if(ndpi_str->host_risk_mask_automa.ac_automa) if(ndpi_str->host_risk_mask_automa.ac_automa)
ac_automata_feature(ndpi_str->host_risk_mask_automa.ac_automa,AC_FEATURE_LC); ac_automata_feature(ndpi_str->host_risk_mask_automa.ac_automa,AC_FEATURE_LC);
if(ndpi_str->common_alpns_automa.ac_automa)
ac_automata_feature(ndpi_str->common_alpns_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)
@@ -2400,6 +2405,9 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(ndpi_init_prefs
if(ndpi_str->host_risk_mask_automa.ac_automa) if(ndpi_str->host_risk_mask_automa.ac_automa)
ac_automata_name(ndpi_str->host_risk_mask_automa.ac_automa,"content",AC_FEATURE_DEBUG); ac_automata_name(ndpi_str->host_risk_mask_automa.ac_automa,"content",AC_FEATURE_DEBUG);
if(ndpi_str->common_alpns_automa.ac_automa)
ac_automata_name(ndpi_str->common_alpns_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");
return(NULL); return(NULL);
@@ -2449,6 +2457,10 @@ void ndpi_finalize_initialization(struct ndpi_detection_module_struct *ndpi_str)
automa = &ndpi_str->host_risk_mask_automa; automa = &ndpi_str->host_risk_mask_automa;
break; break;
case 6:
automa = &ndpi_str->common_alpns_automa;
break;
default: default:
ndpi_str->ac_automa_finalized = 1; ndpi_str->ac_automa_finalized = 1;
return; return;
@@ -2728,6 +2740,10 @@ void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_str) {
ac_automata_release((AC_AUTOMATA_t *) ndpi_str->host_risk_mask_automa.ac_automa, ac_automata_release((AC_AUTOMATA_t *) ndpi_str->host_risk_mask_automa.ac_automa,
1 /* free patterns strings memory */); 1 /* free patterns strings memory */);
if(ndpi_str->common_alpns_automa.ac_automa != NULL)
ac_automata_release((AC_AUTOMATA_t *) ndpi_str->common_alpns_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
@@ -2928,7 +2944,6 @@ int ndpi_add_ip_risk_mask(struct ndpi_detection_module_struct *ndpi_str,
int ndpi_add_host_risk_mask(struct ndpi_detection_module_struct *ndpi_str, int ndpi_add_host_risk_mask(struct ndpi_detection_module_struct *ndpi_str,
char *host, ndpi_risk mask) { char *host, ndpi_risk mask) {
/* host_risk_mask_automa */
AC_PATTERN_t ac_pattern; AC_PATTERN_t ac_pattern;
AC_ERROR_t rc; AC_ERROR_t rc;
u_int len; u_int len;
+61
View File
@@ -2171,3 +2171,64 @@ char* ndpi_get_flow_name(struct ndpi_flow_struct *flow) {
no_flow_info: no_flow_info:
return((char*)""); return((char*)"");
} }
/* ******************************************* */
void load_common_alpns(struct ndpi_detection_module_struct *ndpi_str) {
/* see: https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml */
const char* const common_alpns[] = {
"http/0.9", "http/1.0", "http/1.1",
"spdy/1", "spdy/2", "spdy/3", "spdy/3.1",
"stun.turn", "stun.nat-discovery",
"h2", "h2c", "h2-16", "h2-15", "h2-14", "h2-fb",
"webrtc", "c-webrtc",
"ftp", "imap", "pop3", "managesieve", "coap",
"xmpp-client", "xmpp-server",
"acme-tls/1",
"mqtt", "dot", "ntske/1", "sunrpc",
"h3",
"smb",
"irc",
/* QUIC ALPNs */
"h3-T051", "h3-T050",
"h3-32", "h3-30", "h3-29", "h3-28", "h3-27", "h3-24", "h3-22",
"hq-30", "hq-29", "hq-28", "hq-27",
"h3-fb-05", "h1q-fb",
"doq-i00",
NULL /* end */
};
u_int i;
for(i=0; common_alpns[i] != NULL; i++) {
AC_PATTERN_t ac_pattern;
memset(&ac_pattern, 0, sizeof(ac_pattern));
ac_pattern.astring = ndpi_strdup((char*)common_alpns[i]);
ac_pattern.length = strlen(common_alpns[i]);
if(ac_automata_add(ndpi_str->common_alpns_automa.ac_automa, &ac_pattern) != ACERR_SUCCESS)
printf("%s(): unable to add %s\n", __FUNCTION__, common_alpns[i]);
}
}
/* ******************************************* */
u_int8_t is_a_common_alpn(struct ndpi_detection_module_struct *ndpi_str,
const char *alpn_to_check, u_int alpn_to_check_len) {
ndpi_automa *automa = &ndpi_str->common_alpns_automa;
if(automa->ac_automa) {
AC_TEXT_t ac_input_text;
AC_REP_t match;
ac_input_text.astring = (char*)alpn_to_check, ac_input_text.length = alpn_to_check_len;
ac_input_text.option = 0;
if(ac_automata_search(automa->ac_automa, &ac_input_text, &match) > 0)
return(1);
}
return(0);
}
+6 -41
View File
@@ -1064,54 +1064,19 @@ static void tlsInitExtraPacketProcessing(struct ndpi_detection_module_struct *nd
static void tlsCheckUncommonALPN(struct ndpi_detection_module_struct *ndpi_struct, static void tlsCheckUncommonALPN(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow) { struct ndpi_flow_struct *flow) {
/* TODO: make search more efficient instead of a linear scan */
/* see: https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml */
static char const * const common_alpns[] = {
"http/0.9", "http/1.0", "http/1.1",
"spdy/1", "spdy/2", "spdy/3", "spdy/3.1",
"stun.turn", "stun.nat-discovery",
"h2", "h2c", "h2-16", "h2-15", "h2-14", "h2-fb",
"webrtc", "c-webrtc",
"ftp", "imap", "pop3", "managesieve", "coap",
"xmpp-client", "xmpp-server",
"acme-tls/1",
"mqtt", "dot", "ntske/1", "sunrpc",
"h3",
"smb",
"irc",
/* QUIC ALPNs */
"h3-T051", "h3-T050",
"h3-32", "h3-30", "h3-29", "h3-28", "h3-27", "h3-24", "h3-22",
"hq-30", "hq-29", "hq-28", "hq-27",
"h3-fb-05", "h1q-fb",
"doq-i00"
};
char * alpn_start = flow->protos.tls_quic_stun.tls_quic.alpn; char * alpn_start = flow->protos.tls_quic_stun.tls_quic.alpn;
char * comma_or_nul = alpn_start; char * comma_or_nul = alpn_start;
do { do {
int alpn_len;
comma_or_nul = strchr(comma_or_nul, ','); comma_or_nul = strchr(comma_or_nul, ',');
if (comma_or_nul == NULL)
{ if(comma_or_nul == NULL)
comma_or_nul = alpn_start + strlen(alpn_start); comma_or_nul = alpn_start + strlen(alpn_start);
}
int alpn_found = 0; alpn_len = comma_or_nul - alpn_start;
int alpn_len = comma_or_nul - alpn_start;
char const * const alpn = alpn_start;
for (size_t i = 0; i < sizeof(common_alpns)/sizeof(common_alpns[0]); ++i)
{
if (strlen(common_alpns[i]) == alpn_len &&
strncmp(alpn, common_alpns[i], alpn_len) == 0)
{
alpn_found = 1;
break;
}
}
if (alpn_found == 0) if(!is_a_common_alpn(ndpi_struct, alpn_start, alpn_len)) {
{
#ifdef DEBUG_TLS #ifdef DEBUG_TLS
printf("TLS uncommon ALPN found: %.*s\n", alpn_len, alpn); printf("TLS uncommon ALPN found: %.*s\n", alpn_len, alpn);
#endif #endif