Fix for avoiding misdetection in TLS

This commit is contained in:
Luca
2020-01-09 10:30:14 +01:00
parent 498b346219
commit 757b0c602f
+33 -7
View File
@@ -490,6 +490,7 @@ static int ndpi_search_tls_tcp(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow) { struct ndpi_flow_struct *flow) {
struct ndpi_packet_struct *packet = &flow->packet; struct ndpi_packet_struct *packet = &flow->packet;
int rc = 1; int rc = 1;
u_int8_t something_went_wrong = 0;
#ifdef DEBUG_TLS_MEMORY #ifdef DEBUG_TLS_MEMORY
printf("[TLS Mem] ndpi_search_tls_tcp() [payload_packet_len: %u]\n", printf("[TLS Mem] ndpi_search_tls_tcp() [payload_packet_len: %u]\n",
@@ -501,7 +502,7 @@ static int ndpi_search_tls_tcp(struct ndpi_detection_module_struct *ndpi_struct,
ndpi_search_tls_tcp_memory(ndpi_struct, flow); ndpi_search_tls_tcp_memory(ndpi_struct, flow);
while(1) { while(!something_went_wrong) {
u_int16_t len, p_len; u_int16_t len, p_len;
const u_int8_t *p; const u_int8_t *p;
@@ -523,8 +524,10 @@ static int ndpi_search_tls_tcp(struct ndpi_detection_module_struct *ndpi_struct,
break; break;
} }
if(len == 0) /* Something went wrong */ if(len == 0) {
something_went_wrong = 1;
break; break;
}
#ifdef DEBUG_TLS_MEMORY #ifdef DEBUG_TLS_MEMORY
printf("[TLS Mem] Processing %u bytes message\n", len); printf("[TLS Mem] Processing %u bytes message\n", len);
@@ -540,10 +543,17 @@ static int ndpi_search_tls_tcp(struct ndpi_detection_module_struct *ndpi_struct,
const u_int8_t *block = (const u_int8_t *)&flow->l4.tcp.tls.message.buffer[processed]; const u_int8_t *block = (const u_int8_t *)&flow->l4.tcp.tls.message.buffer[processed];
u_int16_t block_len = (block[1] << 16) + (block[2] << 8) + block[3]; u_int16_t block_len = (block[1] << 16) + (block[2] << 8) + block[3];
if(block_len == 0) {
something_went_wrong = 1;
break;
}
packet->payload = block, packet->payload_packet_len = block_len+4; packet->payload = block, packet->payload_packet_len = block_len+4;
if((processed+packet->payload_packet_len) > len) if((processed+packet->payload_packet_len) > len) {
something_went_wrong = 1;
break; break;
}
#ifdef DEBUG_TLS_MEMORY #ifdef DEBUG_TLS_MEMORY
printf("*** [TLS Mem] Processing %u bytes block [%02X %02X %02X %02X %02X]\n", printf("*** [TLS Mem] Processing %u bytes block [%02X %02X %02X %02X %02X]\n",
@@ -572,7 +582,11 @@ static int ndpi_search_tls_tcp(struct ndpi_detection_module_struct *ndpi_struct,
printf("[TLS Mem] Returning %u\n", rc); printf("[TLS Mem] Returning %u\n", rc);
#endif #endif
return(rc); if(something_went_wrong) {
flow->check_extra_packets = 0, flow->extra_packets_func = NULL;
return(0); /* That's all */
} else
return(rc);
} }
/* **************************************** */ /* **************************************** */
@@ -621,6 +635,7 @@ static int ndpi_search_tls_udp(struct ndpi_detection_module_struct *ndpi_struct,
packet->payload = p, packet->payload_packet_len = p_len; /* Restore */ packet->payload = p, packet->payload_packet_len = p_len; /* Restore */
ndpi_int_tls_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_TLS); ndpi_int_tls_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_TLS);
return(1); /* Keep working */ return(1); /* Keep working */
} }
@@ -1137,7 +1152,7 @@ int processClientServerHello(struct ndpi_detection_module_struct *ndpi_struct,
/* **************************************** */ /* **************************************** */
static void ndpi_search_tls_tcp_udp(struct ndpi_detection_module_struct *ndpi_struct, static void ndpi_search_tls_wrapper(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow) { struct ndpi_flow_struct *flow) {
struct ndpi_packet_struct *packet = &flow->packet; struct ndpi_packet_struct *packet = &flow->packet;
@@ -1161,8 +1176,19 @@ void init_tls_dissector(struct ndpi_detection_module_struct *ndpi_struct,
u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask) { u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask) {
ndpi_set_bitmask_protocol_detection("TLS", ndpi_struct, detection_bitmask, *id, ndpi_set_bitmask_protocol_detection("TLS", ndpi_struct, detection_bitmask, *id,
NDPI_PROTOCOL_TLS, NDPI_PROTOCOL_TLS,
ndpi_search_tls_tcp_udp, ndpi_search_tls_wrapper,
NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_OR_UDP_WITH_PAYLOAD, NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION,
SAVE_DETECTION_BITMASK_AS_UNKNOWN,
ADD_TO_DETECTION_BITMASK);
*id += 1;
/* *************************************************** */
ndpi_set_bitmask_protocol_detection("TLS", ndpi_struct, detection_bitmask, *id,
NDPI_PROTOCOL_TLS,
ndpi_search_tls_wrapper,
NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_UDP_WITH_PAYLOAD,
SAVE_DETECTION_BITMASK_AS_UNKNOWN, SAVE_DETECTION_BITMASK_AS_UNKNOWN,
ADD_TO_DETECTION_BITMASK); ADD_TO_DETECTION_BITMASK);