Replaced malicious JA3-md5/SSL-cert-sha1 ac automata with hashmaps.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
This commit is contained in:
@@ -971,6 +971,12 @@ typedef struct _ndpi_automa {
|
|||||||
void *ac_automa; /* Real type is AC_AUTOMATA_t */
|
void *ac_automa; /* Real type is AC_AUTOMATA_t */
|
||||||
} ndpi_automa;
|
} ndpi_automa;
|
||||||
|
|
||||||
|
typedef struct ndpi_str_hash {
|
||||||
|
unsigned int hash;
|
||||||
|
void *value;
|
||||||
|
u_int8_t private_data[0];
|
||||||
|
} ndpi_str_hash;
|
||||||
|
|
||||||
typedef struct ndpi_proto {
|
typedef struct ndpi_proto {
|
||||||
/*
|
/*
|
||||||
Note
|
Note
|
||||||
@@ -1060,8 +1066,8 @@ struct ndpi_detection_module_struct {
|
|||||||
/* HTTP/DNS/HTTPS/QUIC host matching */
|
/* HTTP/DNS/HTTPS/QUIC host matching */
|
||||||
ndpi_automa host_automa, /* Used for DNS/HTTPS */
|
ndpi_automa host_automa, /* Used for DNS/HTTPS */
|
||||||
risky_domain_automa, tls_cert_subject_automa,
|
risky_domain_automa, tls_cert_subject_automa,
|
||||||
malicious_ja3_automa, malicious_sha1_automa,
|
|
||||||
host_risk_mask_automa, common_alpns_automa;
|
host_risk_mask_automa, common_alpns_automa;
|
||||||
|
ndpi_str_hash *malicious_ja3_hashmap, *malicious_sha1_hashmap;
|
||||||
/* IMPORTANT: please update ndpi_finalize_initialization() whenever you add a new automa */
|
/* IMPORTANT: please update ndpi_finalize_initialization() whenever you add a new automa */
|
||||||
|
|
||||||
ndpi_list *trusted_issuer_dn;
|
ndpi_list *trusted_issuer_dn;
|
||||||
@@ -1623,14 +1629,6 @@ struct ndpi_bin {
|
|||||||
|
|
||||||
/* **************************************** */
|
/* **************************************** */
|
||||||
|
|
||||||
typedef struct ndpi_str_hash {
|
|
||||||
unsigned int hash;
|
|
||||||
void *value;
|
|
||||||
u_int8_t private_data[0];
|
|
||||||
} ndpi_str_hash;
|
|
||||||
|
|
||||||
/* **************************************** */
|
|
||||||
|
|
||||||
#define HW_HISTORY_LEN 4
|
#define HW_HISTORY_LEN 4
|
||||||
#define MAX_SQUARE_ERROR_ITERATIONS 64 /* MUST be < num_values_rollup (256 max) */
|
#define MAX_SQUARE_ERROR_ITERATIONS 64 /* MUST be < num_values_rollup (256 max) */
|
||||||
|
|
||||||
|
|||||||
+34
-66
@@ -2613,8 +2613,8 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(ndpi_init_prefs
|
|||||||
ndpi_str->common_alpns_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);
|
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_hashmap = NULL; /* Initialized on demand */
|
||||||
ndpi_str->malicious_sha1_automa.ac_automa = NULL; /* Initialized on demand */
|
ndpi_str->malicious_sha1_hashmap = NULL; /* Initialized on demand */
|
||||||
ndpi_str->risky_domain_automa.ac_automa = NULL; /* Initialized on demand */
|
ndpi_str->risky_domain_automa.ac_automa = NULL; /* Initialized on demand */
|
||||||
ndpi_str->trusted_issuer_dn = NULL;
|
ndpi_str->trusted_issuer_dn = NULL;
|
||||||
|
|
||||||
@@ -2744,42 +2744,19 @@ void ndpi_finalize_initialization(struct ndpi_detection_module_struct *ndpi_str)
|
|||||||
|
|
||||||
if(ndpi_str->ac_automa_finalized) return;
|
if(ndpi_str->ac_automa_finalized) return;
|
||||||
|
|
||||||
for(i = 0; i < 99; i++) {
|
ndpi_automa * const automa[] = { &ndpi_str->host_automa,
|
||||||
ndpi_automa *automa;
|
&ndpi_str->tls_cert_subject_automa,
|
||||||
|
&ndpi_str->host_risk_mask_automa,
|
||||||
|
&ndpi_str->common_alpns_automa };
|
||||||
|
|
||||||
switch(i) {
|
for(i = 0; i < NDPI_ARRAY_LENGTH(automa); ++i) {
|
||||||
case 0:
|
ndpi_automa *a = automa[i];
|
||||||
automa = &ndpi_str->host_automa;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 1:
|
if(a && a->ac_automa)
|
||||||
automa = &ndpi_str->tls_cert_subject_automa;
|
ac_automata_finalize((AC_AUTOMATA_t *) a->ac_automa);
|
||||||
break;
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
automa = &ndpi_str->malicious_ja3_automa;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 3:
|
|
||||||
automa = &ndpi_str->malicious_sha1_automa;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 4:
|
|
||||||
automa = &ndpi_str->host_risk_mask_automa;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 5:
|
|
||||||
automa = &ndpi_str->common_alpns_automa;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
ndpi_str->ac_automa_finalized = 1;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(automa && automa->ac_automa)
|
|
||||||
ac_automata_finalize((AC_AUTOMATA_t *) automa->ac_automa);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ndpi_str->ac_automa_finalized = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* *********************************************** */
|
/* *********************************************** */
|
||||||
@@ -3028,13 +3005,11 @@ void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_str) {
|
|||||||
if(ndpi_str->tls_cert_subject_automa.ac_automa != NULL)
|
if(ndpi_str->tls_cert_subject_automa.ac_automa != NULL)
|
||||||
ac_automata_release((AC_AUTOMATA_t *) ndpi_str->tls_cert_subject_automa.ac_automa, 0);
|
ac_automata_release((AC_AUTOMATA_t *) ndpi_str->tls_cert_subject_automa.ac_automa, 0);
|
||||||
|
|
||||||
if(ndpi_str->malicious_ja3_automa.ac_automa != NULL)
|
if(ndpi_str->malicious_ja3_hashmap != NULL)
|
||||||
ac_automata_release((AC_AUTOMATA_t *) ndpi_str->malicious_ja3_automa.ac_automa,
|
ndpi_hash_free(&ndpi_str->malicious_ja3_hashmap, NULL);
|
||||||
1 /* free patterns strings memory */);
|
|
||||||
|
|
||||||
if(ndpi_str->malicious_sha1_automa.ac_automa != NULL)
|
if(ndpi_str->malicious_sha1_hashmap != NULL)
|
||||||
ac_automata_release((AC_AUTOMATA_t *) ndpi_str->malicious_sha1_automa.ac_automa,
|
ndpi_hash_free(&ndpi_str->malicious_sha1_hashmap, NULL);
|
||||||
1 /* free patterns strings memory */);
|
|
||||||
|
|
||||||
if(ndpi_str->custom_categories.hostnames.ac_automa != NULL)
|
if(ndpi_str->custom_categories.hostnames.ac_automa != NULL)
|
||||||
ac_automata_release((AC_AUTOMATA_t *) ndpi_str->custom_categories.hostnames.ac_automa,
|
ac_automata_release((AC_AUTOMATA_t *) ndpi_str->custom_categories.hostnames.ac_automa,
|
||||||
@@ -3660,14 +3635,12 @@ int ndpi_load_risk_domain_file(struct ndpi_detection_module_struct *ndpi_str, co
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
int ndpi_load_malicious_ja3_file(struct ndpi_detection_module_struct *ndpi_str, const char *path) {
|
int ndpi_load_malicious_ja3_file(struct ndpi_detection_module_struct *ndpi_str, const char *path) {
|
||||||
char buffer[128], *line, *str;
|
char buffer[128], *line;
|
||||||
FILE *fd;
|
FILE *fd;
|
||||||
int len, num = 0;
|
int len, num = 0;
|
||||||
|
|
||||||
if(ndpi_str->malicious_ja3_automa.ac_automa == NULL)
|
if(ndpi_str->malicious_ja3_hashmap == NULL && ndpi_hash_init(&ndpi_str->malicious_ja3_hashmap) != 0)
|
||||||
ndpi_str->malicious_ja3_automa.ac_automa = ac_automata_init(NULL);
|
return(-1);
|
||||||
if(ndpi_str->malicious_ja3_automa.ac_automa)
|
|
||||||
ac_automata_name(ndpi_str->malicious_ja3_automa.ac_automa,"ja3",0);
|
|
||||||
|
|
||||||
fd = fopen(path, "r");
|
fd = fopen(path, "r");
|
||||||
|
|
||||||
@@ -3694,13 +3667,14 @@ int ndpi_load_malicious_ja3_file(struct ndpi_detection_module_struct *ndpi_str,
|
|||||||
if((comma = strchr(line, ',')) != NULL)
|
if((comma = strchr(line, ',')) != NULL)
|
||||||
comma[0] = '\0';
|
comma[0] = '\0';
|
||||||
|
|
||||||
str = ndpi_strdup(line);
|
len = strlen(line);
|
||||||
if (str == NULL) {
|
|
||||||
NDPI_LOG_ERR(ndpi_str, "Memory allocation failure\n");
|
|
||||||
return -1;
|
|
||||||
};
|
|
||||||
|
|
||||||
if(ndpi_add_string_to_automa(ndpi_str->malicious_ja3_automa.ac_automa, str) >= 0)
|
if(len != 32 /* size of MD5 hash */) {
|
||||||
|
NDPI_LOG_ERR(ndpi_str, "Not a JA3 md5 hash: [%s]\n", line);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(ndpi_hash_add_entry(&ndpi_str->malicious_ja3_hashmap, line, len, NULL) == 0)
|
||||||
num++;
|
num++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3722,15 +3696,13 @@ int ndpi_load_malicious_ja3_file(struct ndpi_detection_module_struct *ndpi_str,
|
|||||||
int ndpi_load_malicious_sha1_file(struct ndpi_detection_module_struct *ndpi_str, const char *path)
|
int ndpi_load_malicious_sha1_file(struct ndpi_detection_module_struct *ndpi_str, const char *path)
|
||||||
{
|
{
|
||||||
char buffer[128];
|
char buffer[128];
|
||||||
char *first_comma, *second_comma, *str;
|
char *first_comma, *second_comma;
|
||||||
FILE *fd;
|
FILE *fd;
|
||||||
size_t i, len;
|
size_t i, len;
|
||||||
int num = 0;
|
int num = 0;
|
||||||
|
|
||||||
if (ndpi_str->malicious_sha1_automa.ac_automa == NULL)
|
if (ndpi_str->malicious_sha1_hashmap == NULL && ndpi_hash_init(&ndpi_str->malicious_sha1_hashmap) != 0)
|
||||||
ndpi_str->malicious_sha1_automa.ac_automa = ac_automata_init(NULL);
|
return(-1);
|
||||||
if(ndpi_str->malicious_sha1_automa.ac_automa)
|
|
||||||
ac_automata_name(ndpi_str->malicious_sha1_automa.ac_automa,"sha1",0);
|
|
||||||
|
|
||||||
fd = fopen(path, "r");
|
fd = fopen(path, "r");
|
||||||
|
|
||||||
@@ -3756,20 +3728,16 @@ int ndpi_load_malicious_sha1_file(struct ndpi_detection_module_struct *ndpi_str,
|
|||||||
second_comma = &buffer[len - 1];
|
second_comma = &buffer[len - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((second_comma - first_comma) != 40)
|
|
||||||
continue;
|
|
||||||
second_comma[0] = '\0';
|
second_comma[0] = '\0';
|
||||||
|
if ((second_comma - first_comma) != 40) {
|
||||||
|
NDPI_LOG_ERR(ndpi_str, "Not a SSL certificate sha1 hash: [%s]\n", first_comma);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
for (i = 0; i < 40; ++i)
|
for (i = 0; i < 40; ++i)
|
||||||
first_comma[i] = toupper(first_comma[i]);
|
first_comma[i] = toupper(first_comma[i]);
|
||||||
|
|
||||||
str = ndpi_strdup(first_comma);
|
if(ndpi_hash_add_entry(&ndpi_str->malicious_sha1_hashmap, first_comma, second_comma - first_comma, NULL) == 0)
|
||||||
if (str == NULL) {
|
|
||||||
NDPI_LOG_ERR(ndpi_str, "Memory allocation failure\n");
|
|
||||||
return -1;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (ndpi_add_string_to_automa(ndpi_str->malicious_sha1_automa.ac_automa, str) >= 0)
|
|
||||||
num++;
|
num++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2130,7 +2130,10 @@ int ndpi_hash_find_entry(ndpi_str_hash *h, char *key, u_int key_len, void **valu
|
|||||||
HASH_FIND_INT(h_priv, &hash_value, found);
|
HASH_FIND_INT(h_priv, &hash_value, found);
|
||||||
if (found != NULL)
|
if (found != NULL)
|
||||||
{
|
{
|
||||||
*value = found->value;
|
if (value != NULL)
|
||||||
|
{
|
||||||
|
*value = found->value;
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
+10
-8
@@ -823,10 +823,10 @@ int processCertificate(struct ndpi_detection_module_struct *ndpi_struct,
|
|||||||
printf("[TLS] SHA-1: %s\n", sha1_str);
|
printf("[TLS] SHA-1: %s\n", sha1_str);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(ndpi_struct->malicious_sha1_automa.ac_automa != NULL) {
|
if(ndpi_struct->malicious_sha1_hashmap != NULL) {
|
||||||
u_int16_t rc1 = ndpi_match_string(ndpi_struct->malicious_sha1_automa.ac_automa, sha1_str);
|
u_int16_t rc1 = ndpi_hash_find_entry(ndpi_struct->malicious_sha1_hashmap, sha1_str, sha1_siz * 2, NULL);
|
||||||
|
|
||||||
if(rc1 > 0)
|
if(rc1 == 0)
|
||||||
ndpi_set_risk(ndpi_struct, flow, NDPI_MALICIOUS_SHA1_CERTIFICATE, sha1_str);
|
ndpi_set_risk(ndpi_struct, flow, NDPI_MALICIOUS_SHA1_CERTIFICATE, sha1_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2392,12 +2392,14 @@ int processClientServerHello(struct ndpi_detection_module_struct *ndpi_struct,
|
|||||||
printf("[JA3] Client: %s \n", flow->protos.tls_quic.ja3_client);
|
printf("[JA3] Client: %s \n", flow->protos.tls_quic.ja3_client);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(ndpi_struct->malicious_ja3_automa.ac_automa != NULL) {
|
if(ndpi_struct->malicious_ja3_hashmap != NULL) {
|
||||||
u_int16_t rc1 = ndpi_match_string(ndpi_struct->malicious_ja3_automa.ac_automa,
|
u_int16_t rc1 = ndpi_hash_find_entry(ndpi_struct->malicious_ja3_hashmap,
|
||||||
flow->protos.tls_quic.ja3_client);
|
flow->protos.tls_quic.ja3_client,
|
||||||
|
NDPI_ARRAY_LENGTH(flow->protos.tls_quic.ja3_client) - 1,
|
||||||
|
NULL);
|
||||||
|
|
||||||
if(rc1 > 0)
|
if(rc1 == 0)
|
||||||
ndpi_set_risk(ndpi_struct, flow, NDPI_MALICIOUS_JA3, flow->protos.tls_quic.ja3_client);
|
ndpi_set_risk(ndpi_struct, flow, NDPI_MALICIOUS_JA3, flow->protos.tls_quic.ja3_client);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user