Fixed some false positivies with skype and stun-based protocols

This commit is contained in:
Luca Deri
2019-10-27 12:23:57 +01:00
parent b603cb4530
commit 64804a2a24
2 changed files with 35 additions and 23 deletions
+6
View File
@@ -32,6 +32,12 @@ static void ndpi_check_skype(struct ndpi_detection_module_struct *ndpi_struct, s
// const u_int8_t *packet_payload = packet->payload; // const u_int8_t *packet_payload = packet->payload;
u_int32_t payload_len = packet->payload_packet_len; u_int32_t payload_len = packet->payload_packet_len;
/* No need to do ntohl() with 0xFFFFFFFF */
if(packet->iph && (packet->iph->daddr == 0xFFFFFFFF /* 255.255.255.255 */)) {
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
return;
}
if(flow->host_server_name[0] != '\0') if(flow->host_server_name[0] != '\0')
return; return;
+27 -21
View File
@@ -146,9 +146,15 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
int rc; int rc;
/* STUN over TCP does not look good */ /* STUN over TCP does not look good */
if (flow->packet.tcp) if(flow->packet.tcp)
return(NDPI_IS_NOT_STUN); return(NDPI_IS_NOT_STUN);
/* No need to do ntohl() with 0xFFFFFFFF */
if(flow->packet.iph && (flow->packet.iph->daddr == 0xFFFFFFFF /* 255.255.255.255 */)) {
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
return(NDPI_IS_NOT_STUN);;
}
if(payload_length >= 512) { if(payload_length >= 512) {
return(NDPI_IS_NOT_STUN); return(NDPI_IS_NOT_STUN);
} else if(payload_length < sizeof(struct stun_packet_header)) { } else if(payload_length < sizeof(struct stun_packet_header)) {
@@ -173,7 +179,7 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
return(NDPI_IS_NOT_STUN); return(NDPI_IS_NOT_STUN);
/* https://www.iana.org/assignments/stun-parameters/stun-parameters.xhtml */ /* https://www.iana.org/assignments/stun-parameters/stun-parameters.xhtml */
if ((msg_type & 0x3EEF) > 0x000B && msg_type != 0x0800) { if((msg_type & 0x3EEF) > 0x000B && msg_type != 0x0800) {
#ifdef DEBUG_STUN #ifdef DEBUG_STUN
printf("[STUN] msg_type = %04X\n", msg_type); printf("[STUN] msg_type = %04X\n", msg_type);
#endif #endif
@@ -183,7 +189,7 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
as this was a flow that started as STUN and turned into something as this was a flow that started as STUN and turned into something
else. Let's investigate what is that about else. Let's investigate what is that about
*/ */
if (payload[0] == 0x16) { if(payload[0] == 0x16) {
/* Let's check if this is DTLS used by some socials */ /* Let's check if this is DTLS used by some socials */
struct ndpi_packet_struct *packet = &flow->packet; struct ndpi_packet_struct *packet = &flow->packet;
u_int16_t total_len, version = htons(*((u_int16_t*) &packet->payload[1])); u_int16_t total_len, version = htons(*((u_int16_t*) &packet->payload[1]));
@@ -193,7 +199,7 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
case 0xFEFD: /* DTLS 1.2 */ case 0xFEFD: /* DTLS 1.2 */
total_len = ntohs(*((u_int16_t*) &packet->payload[11])) + 13; total_len = ntohs(*((u_int16_t*) &packet->payload[11])) + 13;
if (payload_length == total_len) { if(payload_length == total_len) {
/* This is DTLS and the only protocol we know behaves like this is signal */ /* This is DTLS and the only protocol we know behaves like this is signal */
flow->guessed_host_protocol_id = NDPI_PROTOCOL_SIGNAL; flow->guessed_host_protocol_id = NDPI_PROTOCOL_SIGNAL;
return(NDPI_IS_STUN); return(NDPI_IS_STUN);
@@ -211,7 +217,7 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
printf("[STUN] Here we go\n");; printf("[STUN] Here we go\n");;
#endif #endif
if (ndpi_struct->stun_cache) { if(ndpi_struct->stun_cache) {
u_int16_t proto; u_int16_t proto;
u_int32_t key = get_stun_lru_key(flow, 0); u_int32_t key = get_stun_lru_key(flow, 0);
int rc = ndpi_lru_find_cache(ndpi_struct->stun_cache, key, &proto, int rc = ndpi_lru_find_cache(ndpi_struct->stun_cache, key, &proto,
@@ -221,7 +227,7 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
printf("[LRU] Searching %u\n", key); printf("[LRU] Searching %u\n", key);
#endif #endif
if (!rc) { if(!rc) {
key = get_stun_lru_key(flow, 1); key = get_stun_lru_key(flow, 1);
rc = ndpi_lru_find_cache(ndpi_struct->stun_cache, key, &proto, rc = ndpi_lru_find_cache(ndpi_struct->stun_cache, key, &proto,
0 /* Don't remove it as it can be used for other connections */); 0 /* Don't remove it as it can be used for other connections */);
@@ -231,7 +237,7 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
#endif #endif
} }
if (rc) { if(rc) {
#ifdef DEBUG_LRU #ifdef DEBUG_LRU
printf("[LRU] Cache FOUND %u / %u\n", key, proto); printf("[LRU] Cache FOUND %u / %u\n", key, proto);
#endif #endif
@@ -252,18 +258,18 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
if(msg_type == 0x01 /* Binding Request */) { if(msg_type == 0x01 /* Binding Request */) {
flow->protos.stun_ssl.stun.num_binding_requests++; flow->protos.stun_ssl.stun.num_binding_requests++;
if (!msg_len && flow->guessed_host_protocol_id == NDPI_PROTOCOL_GOOGLE) if(!msg_len && flow->guessed_host_protocol_id == NDPI_PROTOCOL_GOOGLE)
flow->guessed_host_protocol_id = NDPI_PROTOCOL_HANGOUT_DUO; flow->guessed_host_protocol_id = NDPI_PROTOCOL_HANGOUT_DUO;
else else
flow->guessed_protocol_id = NDPI_PROTOCOL_STUN; flow->guessed_protocol_id = NDPI_PROTOCOL_STUN;
if (!msg_len) { if(!msg_len) {
/* flow->protos.stun_ssl.stun.num_udp_pkts++; */ /* flow->protos.stun_ssl.stun.num_udp_pkts++; */
return(NDPI_IS_NOT_STUN); /* This to keep analyzing STUN instead of giving up */ return(NDPI_IS_NOT_STUN); /* This to keep analyzing STUN instead of giving up */
} }
} }
if (!msg_len && flow->guessed_host_protocol_id == NDPI_PROTOCOL_UNKNOWN) { if(!msg_len && flow->guessed_host_protocol_id == NDPI_PROTOCOL_UNKNOWN) {
NDPI_EXCLUDE_PROTO(ndpi_struct, flow); NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
return(NDPI_IS_NOT_STUN); return(NDPI_IS_NOT_STUN);
} }
@@ -279,7 +285,7 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
return(NDPI_IS_STUN); /* This is WhatsApp Call */ return(NDPI_IS_STUN); /* This is WhatsApp Call */
} }
if (payload[0] != 0x80 && (msg_len + 20) > payload_length) if(payload[0] != 0x80 && (msg_len + 20) > payload_length)
return(NDPI_IS_NOT_STUN); return(NDPI_IS_NOT_STUN);
else { else {
switch(flow->guessed_protocol_id) { switch(flow->guessed_protocol_id) {
@@ -295,8 +301,8 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
} }
} }
if (payload_length == (msg_len+20)) { if(payload_length == (msg_len+20)) {
if ((msg_type & 0x3EEF) <= 0x000B) /* http://www.3cx.com/blog/voip-howto/stun-details/ */ { if((msg_type & 0x3EEF) <= 0x000B) /* http://www.3cx.com/blog/voip-howto/stun-details/ */ {
u_int offset = 20; u_int offset = 20;
/* /*
@@ -313,7 +319,7 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
u_int16_t len = ntohs(*((u_int16_t*)&payload[offset+2])); u_int16_t len = ntohs(*((u_int16_t*)&payload[offset+2]));
u_int16_t x = (len + 4) % 4; u_int16_t x = (len + 4) % 4;
if (x) if(x)
len += 4-x; len += 4-x;
#ifdef DEBUG_STUN #ifdef DEBUG_STUN
@@ -351,10 +357,10 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
printf("==> [%s]\n", flow->host_server_name); printf("==> [%s]\n", flow->host_server_name);
#endif #endif
if (strstr((char*) flow->host_server_name, "google.com") != NULL) { if(strstr((char*) flow->host_server_name, "google.com") != NULL) {
flow->guessed_host_protocol_id = NDPI_PROTOCOL_HANGOUT_DUO; flow->guessed_host_protocol_id = NDPI_PROTOCOL_HANGOUT_DUO;
return(NDPI_IS_STUN); return(NDPI_IS_STUN);
} else if (strstr((char*) flow->host_server_name, "whispersystems.org") != NULL) { } else if(strstr((char*) flow->host_server_name, "whispersystems.org") != NULL) {
flow->guessed_host_protocol_id = NDPI_PROTOCOL_SIGNAL; flow->guessed_host_protocol_id = NDPI_PROTOCOL_SIGNAL;
return(NDPI_IS_STUN); return(NDPI_IS_STUN);
} }
@@ -363,8 +369,8 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
break; break;
case 0xC057: /* Messeger */ case 0xC057: /* Messeger */
if (msg_type == 0x0001) { if(msg_type == 0x0001) {
if ((msg_len == 100) || (msg_len == 104)) { if((msg_len == 100) || (msg_len == 104)) {
flow->guessed_host_protocol_id = NDPI_PROTOCOL_MESSENGER; flow->guessed_host_protocol_id = NDPI_PROTOCOL_MESSENGER;
return(NDPI_IS_STUN); return(NDPI_IS_STUN);
} else if(msg_len == 76) { } else if(msg_len == 76) {
@@ -415,7 +421,7 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
break; break;
case 0x8070: /* Implementation Version */ case 0x8070: /* Implementation Version */
if (len == 4 && ((offset+7) < payload_length) if(len == 4 && ((offset+7) < payload_length)
&& (payload[offset+4] == 0x00) && (payload[offset+5] == 0x00) && (payload[offset+6] == 0x00) && && (payload[offset+4] == 0x00) && (payload[offset+5] == 0x00) && (payload[offset+6] == 0x00) &&
((payload[offset+7] == 0x02) || (payload[offset+7] == 0x03))) { ((payload[offset+7] == 0x02) || (payload[offset+7] == 0x03))) {
#ifdef DEBUG_STUN #ifdef DEBUG_STUN
@@ -449,7 +455,7 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
} }
} }
if ((flow->protos.stun_ssl.stun.num_udp_pkts > 0) && (msg_type <= 0x00FF)) { if((flow->protos.stun_ssl.stun.num_udp_pkts > 0) && (msg_type <= 0x00FF)) {
flow->guessed_host_protocol_id = NDPI_PROTOCOL_WHATSAPP_CALL; flow->guessed_host_protocol_id = NDPI_PROTOCOL_WHATSAPP_CALL;
return(NDPI_IS_STUN); return(NDPI_IS_STUN);
} else } else
@@ -506,7 +512,7 @@ void ndpi_search_stun(struct ndpi_detection_module_struct *ndpi_struct, struct n
if(ndpi_int_check_stun(ndpi_struct, flow, packet->payload, if(ndpi_int_check_stun(ndpi_struct, flow, packet->payload,
packet->payload_packet_len) == NDPI_IS_STUN) { packet->payload_packet_len) == NDPI_IS_STUN) {
udp_stun_match: udp_stun_match:
if (flow->guessed_protocol_id == NDPI_PROTOCOL_UNKNOWN) if(flow->guessed_protocol_id == NDPI_PROTOCOL_UNKNOWN)
flow->guessed_protocol_id = NDPI_PROTOCOL_STUN; flow->guessed_protocol_id = NDPI_PROTOCOL_STUN;
if(flow->guessed_host_protocol_id == NDPI_PROTOCOL_UNKNOWN) { if(flow->guessed_host_protocol_id == NDPI_PROTOCOL_UNKNOWN) {