Reworked TLS dissection
This commit is contained in:
@@ -506,6 +506,21 @@ extern "C" {
|
||||
u_int8_t ndpi_is_subprotocol_informative(struct ndpi_detection_module_struct *ndpi_mod,
|
||||
u_int16_t protoId);
|
||||
|
||||
/**
|
||||
* Set hostname-based protocol
|
||||
*
|
||||
* @par ndpi_mod = the detection module
|
||||
* @par flow = the flow to which this communication belongs to
|
||||
* @par master_protocol = the master protocol for this flow
|
||||
* @par name = the host name
|
||||
* @par name_len = length of the host name
|
||||
*
|
||||
*/
|
||||
int ndpi_match_hostname_protocol(struct ndpi_detection_module_struct *ndpi_mod,
|
||||
struct ndpi_flow_struct *flow,
|
||||
u_int16_t master_protocol,
|
||||
char *name, u_int name_len);
|
||||
|
||||
/**
|
||||
* Get protocol category as string
|
||||
*
|
||||
|
||||
+14
-12
@@ -637,17 +637,19 @@ struct ndpi_flow_tcp_struct {
|
||||
/* NDPI_PROTOCOL_TELNET */
|
||||
u_int32_t telnet_stage:2; // 0 - 2
|
||||
|
||||
void* tls_srv_cert_fingerprint_ctx;
|
||||
struct {
|
||||
struct {
|
||||
u_int8_t *buffer;
|
||||
u_int buffer_len, buffer_used;
|
||||
} message;
|
||||
|
||||
void* srv_cert_fingerprint_ctx; /* SHA-1 */
|
||||
|
||||
/* NDPI_PROTOCOL_TLS */
|
||||
u_int8_t tls_seen_client_cert:1,
|
||||
tls_seen_server_cert:1,
|
||||
tls_seen_certificate:1,
|
||||
tls_srv_cert_fingerprint_found:1,
|
||||
tls_srv_cert_fingerprint_processed:1,
|
||||
tls_stage:2, _pad:1; // 0 - 5
|
||||
int16_t tls_record_offset, tls_fingerprint_len; /* Need to be signed */
|
||||
u_int8_t tls_sha1_certificate_fingerprint[20];
|
||||
/* NDPI_PROTOCOL_TLS */
|
||||
u_int8_t hello_processed:1, certificate_processed:1, subprotocol_detected:1, _pad:5;
|
||||
int16_t fingerprint_len; /* Need to be signed */
|
||||
u_int8_t sha1_certificate_fingerprint[20];
|
||||
} tls;
|
||||
|
||||
/* NDPI_PROTOCOL_POSTGRES */
|
||||
u_int32_t postgres_stage:3;
|
||||
@@ -1217,8 +1219,8 @@ struct ndpi_flow_struct {
|
||||
|
||||
struct {
|
||||
struct {
|
||||
u_int16_t ssl_version;
|
||||
char client_certificate[64], server_certificate[64], server_organization[64];
|
||||
u_int16_t ssl_version, server_names_len;
|
||||
char client_certificate[64], *server_names, server_organization[64];
|
||||
u_int32_t notBefore, notAfter;
|
||||
char ja3_client[33], ja3_server[33];
|
||||
u_int16_t server_cipher;
|
||||
|
||||
@@ -8754,7 +8754,7 @@ static ndpi_protocol_match host_match[] = {
|
||||
// { ".googlezip.net", NULL, "\\.googlezip" TLD, "Google", NDPI_PROTOCOL_GOOGLE, NDPI_PROTOCOL_CATEGORY_WEB, NDPI_PROTOCOL_SAFE },
|
||||
|
||||
{ "dns.google", NULL, "dns\\.google" TLD, "DoH_DoT", NDPI_PROTOCOL_DOH_DOT, NDPI_PROTOCOL_CATEGORY_NETWORK, NDPI_PROTOCOL_ACCEPTABLE },
|
||||
// { "mozilla.cloudflare-dns.com", NULL, "mozilla\\.cloudflare-dns\\.com" TLD, "DoH_DoT", NDPI_PROTOCOL_DOH_DOT, NDPI_PROTOCOL_CATEGORY_NETWORK, NDPI_PROTOCOL_ACCEPTABLE }, /* Firefox */
|
||||
{ "mozilla.cloudflare-dns.com", NULL, "mozilla\\.cloudflare-dns\\.com" TLD, "DoH_DoT", NDPI_PROTOCOL_DOH_DOT, NDPI_PROTOCOL_CATEGORY_NETWORK, NDPI_PROTOCOL_ACCEPTABLE }, /* Firefox */
|
||||
{ "cloudflare-dns.com", NULL, "cloudflare-dns\\.com" TLD, "DoH_DoT", NDPI_PROTOCOL_DOH_DOT, NDPI_PROTOCOL_CATEGORY_NETWORK, NDPI_PROTOCOL_ACCEPTABLE },
|
||||
{ "commons.host", NULL, "commons\\.host" TLD, "DoH_DoT", NDPI_PROTOCOL_DOH_DOT, NDPI_PROTOCOL_CATEGORY_NETWORK, NDPI_PROTOCOL_ACCEPTABLE },
|
||||
{ "doh.li", NULL, "doh\\.li" TLD, "DoH_DoT", NDPI_PROTOCOL_DOH_DOT, NDPI_PROTOCOL_CATEGORY_NETWORK, NDPI_PROTOCOL_ACCEPTABLE },
|
||||
|
||||
+47
-12
@@ -651,7 +651,6 @@ static int init_hyperscan(struct ndpi_detection_module_struct *ndpi_str) {
|
||||
ndpi_free(expressions[i]);
|
||||
|
||||
ndpi_free(expressions), ndpi_free(ids);
|
||||
|
||||
ndpi_free(need_to_be_free);
|
||||
|
||||
return(rc);
|
||||
@@ -3834,6 +3833,9 @@ static int ndpi_init_packet_header(struct ndpi_detection_module_struct *ndpi_str
|
||||
if(flow->http.content_type) ndpi_free(flow->http.content_type);
|
||||
if(flow->http.user_agent) ndpi_free(flow->http.user_agent);
|
||||
|
||||
if(flow->l4.tcp.tls.message.buffer)
|
||||
ndpi_free(flow->l4.tcp.tls.message.buffer);
|
||||
|
||||
backup = flow->num_processed_pkts;
|
||||
backup1 = flow->guessed_protocol_id;
|
||||
backup2 = flow->guessed_host_protocol_id;
|
||||
@@ -4216,13 +4218,13 @@ ndpi_protocol ndpi_detection_giveup(struct ndpi_detection_module_struct *ndpi_st
|
||||
|| (flow->guessed_protocol_id == NDPI_PROTOCOL_MESSENGER)
|
||||
|| (flow->guessed_protocol_id == NDPI_PROTOCOL_WHATSAPP_CALL))
|
||||
ndpi_set_detected_protocol(ndpi_str, flow, flow->guessed_protocol_id, NDPI_PROTOCOL_UNKNOWN);
|
||||
else if((flow->l4.tcp.tls_seen_client_cert == 1)
|
||||
else if((flow->l4.tcp.tls.hello_processed == 1)
|
||||
&& (flow->protos.stun_ssl.ssl.client_certificate[0] != '\0')) {
|
||||
ndpi_set_detected_protocol(ndpi_str, flow, NDPI_PROTOCOL_TLS, NDPI_PROTOCOL_UNKNOWN);
|
||||
} else {
|
||||
if((flow->guessed_protocol_id == NDPI_PROTOCOL_UNKNOWN)
|
||||
&& (flow->packet.l4_protocol == IPPROTO_TCP)
|
||||
&& (flow->l4.tcp.tls_stage > 1))
|
||||
&& flow->l4.tcp.tls.hello_processed)
|
||||
flow->guessed_protocol_id = NDPI_PROTOCOL_TLS;
|
||||
|
||||
guessed_protocol_id = flow->guessed_protocol_id, guessed_host_protocol_id = flow->guessed_host_protocol_id;
|
||||
@@ -4270,7 +4272,10 @@ ndpi_protocol ndpi_detection_giveup(struct ndpi_detection_module_struct *ndpi_st
|
||||
}
|
||||
}
|
||||
} else {
|
||||
flow->detected_protocol_stack[1] = flow->guessed_protocol_id,
|
||||
if(flow->guessed_protocol_id != NDPI_PROTOCOL_UNKNOWN)
|
||||
flow->detected_protocol_stack[1] = flow->guessed_protocol_id;
|
||||
|
||||
if(flow->guessed_host_protocol_id != NDPI_PROTOCOL_UNKNOWN)
|
||||
flow->detected_protocol_stack[0] = flow->guessed_host_protocol_id;
|
||||
|
||||
if(flow->detected_protocol_stack[1] == flow->detected_protocol_stack[0])
|
||||
@@ -4347,9 +4352,10 @@ void ndpi_process_extra_packet(struct ndpi_detection_module_struct *ndpi_str,
|
||||
if(flow->extra_packets_func) {
|
||||
if((flow->extra_packets_func(ndpi_str, flow)) == 0)
|
||||
flow->check_extra_packets = 0;
|
||||
}
|
||||
|
||||
flow->num_extra_packets_checked++;
|
||||
if(++flow->num_extra_packets_checked == flow->max_extra_packets_to_check)
|
||||
flow->extra_packets_func = NULL; /* Enough packets detected */
|
||||
}
|
||||
}
|
||||
|
||||
/* ********************************************************************************* */
|
||||
@@ -4614,7 +4620,7 @@ void ndpi_fill_protocol_category(struct ndpi_detection_module_struct *ndpi_str,
|
||||
}
|
||||
}
|
||||
|
||||
if((flow->l4.tcp.tls_seen_client_cert == 1) && (flow->protos.stun_ssl.ssl.client_certificate[0] != '\0')) {
|
||||
if(flow->protos.stun_ssl.ssl.client_certificate[0] != '\0') {
|
||||
unsigned long id;
|
||||
int rc = ndpi_match_custom_category(ndpi_str,
|
||||
(char *)flow->protos.stun_ssl.ssl.client_certificate,
|
||||
@@ -6329,6 +6335,33 @@ u_int16_t ndpi_match_host_subprotocol(struct ndpi_detection_module_struct *ndpi_
|
||||
return(rc);
|
||||
}
|
||||
|
||||
/* **************************************** */
|
||||
|
||||
int ndpi_match_hostname_protocol(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
struct ndpi_flow_struct *flow,
|
||||
u_int16_t master_protocol,
|
||||
char *name, u_int name_len) {
|
||||
ndpi_protocol_match_result ret_match;
|
||||
u_int16_t subproto, what_len;
|
||||
char *what;
|
||||
|
||||
if((name_len > 2) && (name[0] == '*') && (name[1] == '.'))
|
||||
what = &name[1], what_len = name_len - 1;
|
||||
else
|
||||
what = name, what_len = name_len;
|
||||
|
||||
subproto = ndpi_match_host_subprotocol(ndpi_struct, flow,
|
||||
what, what_len,
|
||||
&ret_match, master_protocol);
|
||||
|
||||
if(subproto != NDPI_PROTOCOL_UNKNOWN) {
|
||||
ndpi_set_detected_protocol(ndpi_struct, flow, subproto, master_protocol);
|
||||
ndpi_int_change_category(ndpi_struct, flow, ret_match.protocol_category);
|
||||
return(1);
|
||||
} else
|
||||
return(0);
|
||||
}
|
||||
|
||||
/* ****************************************************** */
|
||||
|
||||
u_int16_t ndpi_match_content_subprotocol(struct ndpi_detection_module_struct *ndpi_str,
|
||||
@@ -6378,10 +6411,12 @@ void ndpi_free_flow(struct ndpi_flow_struct *flow) {
|
||||
if(flow->http.content_type) ndpi_free(flow->http.content_type);
|
||||
if(flow->http.user_agent) ndpi_free(flow->http.user_agent);
|
||||
if(flow->kerberos_buf.pktbuf) ndpi_free(flow->kerberos_buf.pktbuf);
|
||||
|
||||
if(flow->protos.stun_ssl.ssl.server_names)
|
||||
ndpi_free(flow->protos.stun_ssl.ssl.server_names);
|
||||
|
||||
if(flow->l4_proto == IPPROTO_TCP) {
|
||||
if(flow->l4.tcp.tls_srv_cert_fingerprint_ctx)
|
||||
ndpi_free(flow->l4.tcp.tls_srv_cert_fingerprint_ctx);
|
||||
if(flow->l4.tcp.tls.srv_cert_fingerprint_ctx)
|
||||
ndpi_free(flow->l4.tcp.tls.srv_cert_fingerprint_ctx);
|
||||
}
|
||||
|
||||
ndpi_free(flow);
|
||||
@@ -6561,8 +6596,8 @@ u_int8_t ndpi_extra_dissection_possible(struct ndpi_detection_module_struct *ndp
|
||||
|
||||
switch(proto) {
|
||||
case NDPI_PROTOCOL_TLS:
|
||||
if(!flow->l4.tcp.tls_srv_cert_fingerprint_processed)
|
||||
return(1);
|
||||
if(!flow->l4.tcp.tls.certificate_processed)
|
||||
return(1); /* TODO: add check for TLS 1.3 */
|
||||
break;
|
||||
|
||||
case NDPI_PROTOCOL_HTTP:
|
||||
|
||||
@@ -1044,7 +1044,8 @@ int ndpi_flow2json(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
ndpi_serialize_start_of_block(serializer, "tls");
|
||||
ndpi_serialize_string_string(serializer, "version", version);
|
||||
ndpi_serialize_string_string(serializer, "client_cert", flow->protos.stun_ssl.ssl.client_certificate);
|
||||
ndpi_serialize_string_string(serializer, "server_cert", flow->protos.stun_ssl.ssl.server_certificate);
|
||||
if(flow->protos.stun_ssl.ssl.server_names)
|
||||
ndpi_serialize_string_string(serializer, "server_names", flow->protos.stun_ssl.ssl.server_names);
|
||||
ndpi_serialize_string_string(serializer, "issuer", flow->protos.stun_ssl.ssl.server_organization);
|
||||
|
||||
if(before) {
|
||||
@@ -1061,10 +1062,10 @@ int ndpi_flow2json(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
ndpi_serialize_string_uint32(serializer, "unsafe_cipher", flow->protos.stun_ssl.ssl.server_unsafe_cipher);
|
||||
ndpi_serialize_string_string(serializer, "cipher", ndpi_cipher2str(flow->protos.stun_ssl.ssl.server_cipher));
|
||||
|
||||
if(flow->l4.tcp.tls_sha1_certificate_fingerprint[0] != '\0') {
|
||||
if(flow->l4.tcp.tls.sha1_certificate_fingerprint[0] != '\0') {
|
||||
for(i=0, off=0; i<20; i++) {
|
||||
int rc = snprintf(&buf[off], sizeof(buf)-off,"%s%02X", (i > 0) ? ":" : "",
|
||||
flow->l4.tcp.tls_sha1_certificate_fingerprint[i] & 0xFF);
|
||||
flow->l4.tcp.tls.sha1_certificate_fingerprint[i] & 0xFF);
|
||||
|
||||
if(rc <= 0) break; else off += rc;
|
||||
}
|
||||
|
||||
@@ -159,14 +159,12 @@ static void ndpi_http_parse_subprotocol(struct ndpi_detection_module_struct *ndp
|
||||
struct ndpi_flow_struct *flow) {
|
||||
if((flow->l4.tcp.http_stage == 0) || (flow->http.url && flow->http_detected)) {
|
||||
char *double_col = strchr((char*)flow->host_server_name, ':');
|
||||
ndpi_protocol_match_result ret_match;
|
||||
|
||||
if(double_col) double_col[0] = '\0';
|
||||
|
||||
ndpi_match_host_subprotocol(ndpi_struct, flow, (char *)flow->host_server_name,
|
||||
strlen((const char *)flow->host_server_name),
|
||||
&ret_match,
|
||||
NDPI_PROTOCOL_HTTP);
|
||||
ndpi_match_hostname_protocol(ndpi_struct, flow, NDPI_PROTOCOL_HTTP,
|
||||
(char *)flow->host_server_name,
|
||||
strlen((const char *)flow->host_server_name));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+879
-1270
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user