Added NDPI_MALICIOUS_JA3 flow risk

Added ndpi_load_malicious_ja3_file() API call
This commit is contained in:
Luca Deri
2021-02-22 23:19:23 +01:00
parent fc3db8f169
commit f1b22b199f
16 changed files with 328 additions and 132 deletions
+58 -4
View File
@@ -2142,6 +2142,7 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(ndpi_init_prefs
ndpi_str->bigrams_automa.ac_automa = ac_automata_init(ac_match_handler);
ndpi_str->impossible_bigrams_automa.ac_automa = ac_automata_init(ac_match_handler);
ndpi_str->tls_cert_subject_automa.ac_automa = ac_automata_init(ac_match_handler);
ndpi_str->malicious_ja3_automa.ac_automa = NULL; /* Initialized on demand */
ndpi_str->risky_domain_automa.ac_automa = NULL; /* Initialized on demand */
if((sizeof(categories) / sizeof(char *)) != NDPI_PROTOCOL_NUM_CATEGORIES) {
@@ -2196,13 +2197,13 @@ void ndpi_finalize_initialization(struct ndpi_detection_module_struct *ndpi_str)
break;
case 4:
automa = &ndpi_str->risky_domain_automa;
break;
case 5:
automa = &ndpi_str->tls_cert_subject_automa;
break;
case 5:
automa = &ndpi_str->malicious_ja3_automa;
break;
default:
return;
}
@@ -2465,6 +2466,9 @@ void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_str) {
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);
if(ndpi_str->malicious_ja3_automa.ac_automa != NULL)
ac_automata_release((AC_AUTOMATA_t *) ndpi_str->malicious_ja3_automa.ac_automa, 0);
if(ndpi_str->custom_categories.hostnames.ac_automa != NULL)
ac_automata_release((AC_AUTOMATA_t *) ndpi_str->custom_categories.hostnames.ac_automa,
1 /* free patterns strings memory */);
@@ -2911,6 +2915,56 @@ int ndpi_load_risk_domain_file(struct ndpi_detection_module_struct *ndpi_str, co
/* ******************************************************************** */
/*
* Format:
*
* <domain name>[,<other info>]
*
*/
int ndpi_load_malicious_ja3_file(struct ndpi_detection_module_struct *ndpi_str, const char *path) {
char buffer[128], *line;
FILE *fd;
int len, num = 0;
if(ndpi_str->malicious_ja3_automa.ac_automa == NULL)
ndpi_str->malicious_ja3_automa.ac_automa = ac_automata_init(ac_match_handler);
fd = fopen(path, "r");
if(fd == NULL) {
NDPI_LOG_ERR(ndpi_str, "Unable to open file %s [%s]\n", path, strerror(errno));
return(-1);
}
while(1) {
char *comma;
line = fgets(buffer, sizeof(buffer), fd);
if(line == NULL)
break;
len = strlen(line);
if((len <= 1) || (line[0] == '#'))
continue;
line[len - 1] = '\0';
if((comma = strchr(line, ',')) != NULL)
comma[0] = '\0';
if(ndpi_add_string_to_automa(ndpi_str->malicious_ja3_automa.ac_automa, line) >= 0)
num++;
}
fclose(fd);
return(num);
}
/* ******************************************************************** */
/*
Format:
<tcp|udp>:<port>,<tcp|udp>:<port>,.....@<proto>
+4 -1
View File
@@ -1758,7 +1758,10 @@ const char* ndpi_risk2str(ndpi_risk_enum risk) {
case NDPI_RISKY_DOMAIN:
return("Risky domain name");
case NDPI_MALICIOUS_JA3:
return("Malicious JA3 Fingerprint");
default:
snprintf(buf, sizeof(buf), "%d", (int)risk);
return(buf);
+9
View File
@@ -1576,9 +1576,18 @@ int processClientServerHello(struct ndpi_detection_module_struct *ndpi_struct,
md5_hash[i]);
if(rc > 0) j += rc; else break;
}
#ifdef DEBUG_TLS
printf("[JA3] Client: %s \n", flow->protos.tls_quic_stun.tls_quic.ja3_client);
#endif
if(ndpi_struct->malicious_ja3_automa.ac_automa != NULL) {
u_int16_t rc1 = ndpi_match_string(ndpi_struct->malicious_ja3_automa.ac_automa,
flow->protos.tls_quic_stun.tls_quic.ja3_client);
if(rc1 > 0)
NDPI_SET_BIT(flow->risk, NDPI_MALICIOUS_JA3);
}
}
/* Before returning to the caller we need to make a final check */