STUN protocol dissector code cleanup

This commit is contained in:
Luca
2019-09-20 18:47:39 +02:00
parent f9716c5139
commit bdcc6dc173
5 changed files with 42 additions and 109 deletions
+1 -1
View File
@@ -1182,7 +1182,7 @@ struct ndpi_flow_struct {
} ssl; } ssl;
struct { struct {
u_int8_t num_udp_pkts, num_processed_pkts, num_binding_requests, is_skype; u_int8_t num_udp_pkts, num_processed_pkts, num_binding_requests;
} stun; } stun;
/* We can have STUN over SSL/TLS thus they need to live together */ /* We can have STUN over SSL/TLS thus they need to live together */
+3 -7
View File
@@ -4131,13 +4131,9 @@ ndpi_protocol ndpi_detection_giveup(struct ndpi_detection_module_struct *ndpi_st
check_stun_export: check_stun_export:
if(flow->protos.stun_ssl.stun.num_processed_pkts || flow->protos.stun_ssl.stun.num_udp_pkts) { if(flow->protos.stun_ssl.stun.num_processed_pkts || flow->protos.stun_ssl.stun.num_udp_pkts) {
// if(/* (flow->protos.stun_ssl.stun.num_processed_pkts >= NDPI_MIN_NUM_STUN_DETECTION) */ // if(/* (flow->protos.stun_ssl.stun.num_processed_pkts >= NDPI_MIN_NUM_STUN_DETECTION) */
if(flow->protos.stun_ssl.stun.num_processed_pkts && flow->protos.stun_ssl.stun.is_skype) { ndpi_set_detected_protocol(ndpi_struct, flow,
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_SKYPE_CALL, NDPI_PROTOCOL_SKYPE); flow->guessed_host_protocol_id,
} else { NDPI_PROTOCOL_STUN);
ndpi_set_detected_protocol(ndpi_struct, flow,
flow->guessed_host_protocol_id,
NDPI_PROTOCOL_STUN);
}
} }
} }
+28 -92
View File
@@ -44,7 +44,6 @@ struct stun_packet_header {
static u_int8_t is_stun_based_proto(u_int16_t proto) { static u_int8_t is_stun_based_proto(u_int16_t proto) {
switch(proto) { switch(proto) {
case NDPI_PROTOCOL_WHATSAPP:
case NDPI_PROTOCOL_WHATSAPP_CALL: case NDPI_PROTOCOL_WHATSAPP_CALL:
case NDPI_PROTOCOL_MESSENGER: case NDPI_PROTOCOL_MESSENGER:
case NDPI_PROTOCOL_HANGOUT_DUO: case NDPI_PROTOCOL_HANGOUT_DUO:
@@ -70,7 +69,7 @@ u_int32_t get_stun_lru_key(struct ndpi_flow_struct *flow, u_int8_t rev) {
void ndpi_int_stun_add_connection(struct ndpi_detection_module_struct *ndpi_struct, void ndpi_int_stun_add_connection(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow, struct ndpi_flow_struct *flow,
u_int app_proto, u_int proto) { u_int proto, u_int app_proto) {
if(ndpi_struct->stun_cache == NULL) if(ndpi_struct->stun_cache == NULL)
ndpi_struct->stun_cache = ndpi_lru_cache_init(1024); ndpi_struct->stun_cache = ndpi_lru_cache_init(1024);
@@ -159,11 +158,7 @@ static int is_messenger_ip_address(u_int32_t host) {
static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *ndpi_struct, static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow, struct ndpi_flow_struct *flow,
const u_int8_t * payload, const u_int8_t * payload,
const u_int16_t payload_length, const u_int16_t payload_length) {
u_int8_t *is_whatsapp,
u_int8_t *is_messenger,
u_int8_t *is_duo
) {
u_int16_t msg_type, msg_len; u_int16_t msg_type, msg_len;
struct stun_packet_header *h = (struct stun_packet_header*)payload; struct stun_packet_header *h = (struct stun_packet_header*)payload;
u_int8_t can_this_be_whatsapp_voice = 1; u_int8_t can_this_be_whatsapp_voice = 1;
@@ -171,15 +166,13 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
/* STUN over TCP does not look good */ /* STUN over TCP does not look good */
if(flow->packet.tcp) return(NDPI_IS_NOT_STUN); if(flow->packet.tcp) return(NDPI_IS_NOT_STUN);
*is_whatsapp = 0, *is_messenger = 0, *is_duo = 0;
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)) {
/* This looks like an invalid packet */ /* This looks like an invalid packet */
if(flow->protos.stun_ssl.stun.num_udp_pkts > 0) { if(flow->protos.stun_ssl.stun.num_udp_pkts > 0) {
*is_whatsapp = 1; flow->guessed_host_protocol_id = NDPI_PROTOCOL_WHATSAPP_CALL;
return(NDPI_IS_STUN); /* This is WhatsApp Voice */ return(NDPI_IS_STUN); /* This is WhatsApp Voice */
} else } else
return(NDPI_IS_NOT_STUN); return(NDPI_IS_NOT_STUN);
@@ -206,8 +199,8 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
something else that has nothing to do with STUN anymore something else that has nothing to do with STUN anymore
*/ */
ndpi_int_stun_add_connection(ndpi_struct, flow, ndpi_int_stun_add_connection(ndpi_struct, flow,
flow->guessed_host_protocol_id, flow->guessed_protocol_id,
NDPI_PROTOCOL_STUN); flow->guessed_host_protocol_id);
return(NDPI_IS_STUN); return(NDPI_IS_STUN);
} }
@@ -246,21 +239,6 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
flow->guessed_host_protocol_id = proto, flow->guessed_protocol_id = NDPI_PROTOCOL_STUN; flow->guessed_host_protocol_id = proto, flow->guessed_protocol_id = NDPI_PROTOCOL_STUN;
switch(proto) {
case NDPI_PROTOCOL_WHATSAPP:
*is_whatsapp = 1;
break;
case NDPI_PROTOCOL_MESSENGER:
*is_messenger = 1;
break;
case NDPI_PROTOCOL_HANGOUT_DUO:
*is_duo = 1;
break;
case NDPI_PROTOCOL_SKYPE_CALL:
flow->protos.stun_ssl.stun.is_skype = 1;
break;
}
return(NDPI_IS_STUN); return(NDPI_IS_STUN);
} else { } else {
#ifdef DEBUG_LRU #ifdef DEBUG_LRU
@@ -279,7 +257,7 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
if((msg_len == 0) && (flow->guessed_host_protocol_id == NDPI_PROTOCOL_GOOGLE)) if((msg_len == 0) && (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_host_protocol_id = NDPI_PROTOCOL_STUN; flow->guessed_protocol_id = NDPI_PROTOCOL_STUN;
if(msg_len == 0) { if(msg_len == 0) {
/* flow->protos.stun_ssl.stun.num_udp_pkts++; */ /* flow->protos.stun_ssl.stun.num_udp_pkts++; */
@@ -302,12 +280,12 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
if(((payload[0] == 0x80) if(((payload[0] == 0x80)
&& (payload_length < 512) && (payload_length < 512)
&& ((msg_len+20) <= payload_length)) /* WhatsApp Voice */) { && ((msg_len+20) <= payload_length)) /* WhatsApp Voice */) {
*is_whatsapp = 1; flow->guessed_host_protocol_id = NDPI_PROTOCOL_WHATSAPP_CALL;
return(NDPI_IS_STUN); /* This is WhatsApp Voice */ return(NDPI_IS_STUN); /* This is WhatsApp Voice */
} else if((payload[0] == 0x90) } else if((payload[0] == 0x90)
&& (((msg_len+11) == payload_length) /* WhatsApp Video */ && (((msg_len+11) == payload_length) /* WhatsApp Video */
|| (flow->protos.stun_ssl.stun.num_binding_requests >= 4))) { || (flow->protos.stun_ssl.stun.num_binding_requests >= 4))) {
*is_whatsapp = 2; flow->guessed_host_protocol_id = NDPI_PROTOCOL_WHATSAPP_CALL;
return(NDPI_IS_STUN); /* This is WhatsApp Video */ return(NDPI_IS_STUN); /* This is WhatsApp Video */
} }
@@ -355,8 +333,8 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
#endif #endif
switch(attribute) { switch(attribute) {
case 0x0008: /* Message Integrity */ // case 0x0008: /* Message Integrity */
case 0x0020: /* XOR-MAPPED-ADDRESSES */ // case 0x0020: /* XOR-MAPPED-ADDRESSES */
case 0x4000: case 0x4000:
case 0x4001: case 0x4001:
case 0x4002: case 0x4002:
@@ -381,7 +359,6 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
#endif #endif
if(strstr((char*)flow->host_server_name, "google.com") != NULL) { if(strstr((char*)flow->host_server_name, "google.com") != NULL) {
*is_duo = 1;
flow->guessed_host_protocol_id = NDPI_PROTOCOL_HANGOUT_DUO, flow->guessed_protocol_id = NDPI_PROTOCOL_STUN; flow->guessed_host_protocol_id = NDPI_PROTOCOL_HANGOUT_DUO, flow->guessed_protocol_id = NDPI_PROTOCOL_STUN;
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) {
@@ -395,12 +372,10 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
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)) {
*is_messenger = 1; flow->guessed_host_protocol_id = NDPI_PROTOCOL_MESSENGER, flow->guessed_protocol_id = NDPI_PROTOCOL_STUN;
return(NDPI_IS_STUN); return(NDPI_IS_STUN);
} else if(msg_len == 76) { } else if(msg_len == 76) {
#if 0 #if 0
*is_duo = 1;
if(1) { if(1) {
flow->guessed_host_protocol_id = NDPI_PROTOCOL_HANGOUT_DUO, flow->guessed_protocol_id = NDPI_PROTOCOL_STUN; flow->guessed_host_protocol_id = NDPI_PROTOCOL_HANGOUT_DUO, flow->guessed_protocol_id = NDPI_PROTOCOL_STUN;
return(NDPI_IS_NOT_STUN); /* This case is found also with signal traffic */ return(NDPI_IS_NOT_STUN); /* This case is found also with signal traffic */
@@ -422,7 +397,6 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
printf("==> Skype found\n"); printf("==> Skype found\n");
#endif #endif
flow->guessed_protocol_id = NDPI_PROTOCOL_SKYPE_CALL; flow->guessed_protocol_id = NDPI_PROTOCOL_SKYPE_CALL;
flow->protos.stun_ssl.stun.is_skype = 1;
return(NDPI_IS_STUN); return(NDPI_IS_STUN);
} }
break; break;
@@ -443,7 +417,6 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
#endif #endif
flow->guessed_protocol_id = NDPI_PROTOCOL_SKYPE_CALL; flow->guessed_protocol_id = NDPI_PROTOCOL_SKYPE_CALL;
flow->protos.stun_ssl.stun.is_skype = 1;
return(NDPI_IS_STUN); return(NDPI_IS_STUN);
break; break;
@@ -456,7 +429,6 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
&& ((payload[offset+7] == 0x02) || (payload[offset+7] == 0x03)) && ((payload[offset+7] == 0x02) || (payload[offset+7] == 0x03))
) { ) {
flow->guessed_protocol_id = NDPI_PROTOCOL_SKYPE_CALL; flow->guessed_protocol_id = NDPI_PROTOCOL_SKYPE_CALL;
flow->protos.stun_ssl.stun.is_skype = 1;
#ifdef DEBUG_STUN #ifdef DEBUG_STUN
printf("==> Skype (3) found\n"); printf("==> Skype (3) found\n");
#endif #endif
@@ -483,13 +455,13 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
} }
goto udp_stun_found; goto udp_stun_found;
} else if(msg_type == 0x0800) { } else if(msg_type == 0x0800) {
*is_whatsapp = 1; flow->guessed_host_protocol_id = NDPI_PROTOCOL_WHATSAPP_CALL;
return(NDPI_IS_STUN); /* This is WhatsApp */ return(NDPI_IS_STUN); /* This is WhatsApp */
} }
} }
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)) {
*is_whatsapp = 1; flow->guessed_host_protocol_id = NDPI_PROTOCOL_WHATSAPP_CALL;
return(NDPI_IS_STUN); /* This is WhatsApp Voice */ return(NDPI_IS_STUN); /* This is WhatsApp Voice */
} else } else
return(NDPI_IS_NOT_STUN); return(NDPI_IS_NOT_STUN);
@@ -514,7 +486,9 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
rc = (flow->protos.stun_ssl.stun.num_udp_pkts < MAX_NUM_STUN_PKTS) ? NDPI_IS_NOT_STUN : NDPI_IS_STUN; rc = (flow->protos.stun_ssl.stun.num_udp_pkts < MAX_NUM_STUN_PKTS) ? NDPI_IS_NOT_STUN : NDPI_IS_STUN;
if(rc == NDPI_IS_STUN) if(rc == NDPI_IS_STUN)
ndpi_int_stun_add_connection(ndpi_struct, flow, flow->guessed_host_protocol_id, NDPI_IS_STUN); ndpi_int_stun_add_connection(ndpi_struct, flow,
NDPI_IS_STUN,
flow->guessed_host_protocol_id);
return(rc); return(rc);
} else { } else {
@@ -530,7 +504,6 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
void ndpi_search_stun(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) void ndpi_search_stun(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow)
{ {
struct ndpi_packet_struct *packet = &flow->packet; struct ndpi_packet_struct *packet = &flow->packet;
u_int8_t is_whatsapp = 0, is_messenger = 0, is_duo = 0;
NDPI_LOG_DBG(ndpi_struct, "search stun\n"); NDPI_LOG_DBG(ndpi_struct, "search stun\n");
@@ -549,64 +522,27 @@ void ndpi_search_stun(struct ndpi_detection_module_struct *ndpi_struct, struct n
* improved by checking only the STUN packet of given length */ * improved by checking only the STUN packet of given length */
if(ndpi_int_check_stun(ndpi_struct, flow, packet->payload + 2, if(ndpi_int_check_stun(ndpi_struct, flow, packet->payload + 2,
packet->payload_packet_len - 2, packet->payload_packet_len - 2) == NDPI_IS_STUN) {
&is_whatsapp, &is_messenger, &is_duo) == NDPI_IS_STUN) { goto udp_stun_match;
if(flow->guessed_protocol_id == NDPI_PROTOCOL_UNKNOWN) flow->guessed_protocol_id = NDPI_PROTOCOL_STUN;
if(is_messenger) {
ndpi_int_stun_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_MESSENGER, NDPI_PROTOCOL_STUN);
return;
} else if(is_duo) {
ndpi_int_stun_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_HANGOUT_DUO, NDPI_PROTOCOL_STUN);
return;
} else if(flow->guessed_host_protocol_id == NDPI_PROTOCOL_SIGNAL) {
ndpi_int_stun_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_SIGNAL, NDPI_PROTOCOL_STUN);
return;
} else if(flow->protos.stun_ssl.stun.is_skype || (flow->guessed_host_protocol_id = NDPI_PROTOCOL_SKYPE_CALL)) {
NDPI_LOG_INFO(ndpi_struct, "found Skype\n");
// if((flow->protos.stun_ssl.stun.num_processed_pkts >= 8) || (flow->protos.stun_ssl.stun.num_binding_requests >= 4))
ndpi_int_stun_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_SKYPE_CALL, NDPI_PROTOCOL_SKYPE);
} else {
NDPI_LOG_INFO(ndpi_struct, "found UDP stun\n"); /* Ummmmm we're in the TCP branch. This code looks bad */
ndpi_int_stun_add_connection(ndpi_struct, flow,
is_whatsapp ? NDPI_PROTOCOL_WHATSAPP_CALL : NDPI_PROTOCOL_STUN,
NDPI_PROTOCOL_UNKNOWN);
}
return;
} }
} }
} }
/* UDP */ /* UDP */
if(ndpi_int_check_stun(ndpi_struct, flow, packet->payload, if(ndpi_int_check_stun(ndpi_struct, flow, packet->payload,
packet->payload_packet_len, packet->payload_packet_len) == NDPI_IS_STUN) {
&is_whatsapp, &is_messenger, &is_duo) == NDPI_IS_STUN) { udp_stun_match:
if(flow->guessed_protocol_id == NDPI_PROTOCOL_UNKNOWN) flow->guessed_protocol_id = NDPI_PROTOCOL_STUN; if(flow->guessed_protocol_id == NDPI_PROTOCOL_UNKNOWN)
flow->guessed_protocol_id = NDPI_PROTOCOL_STUN;
if(is_messenger) { if(flow->guessed_host_protocol_id == NDPI_PROTOCOL_UNKNOWN) {
ndpi_int_stun_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_MESSENGER, NDPI_PROTOCOL_STUN); flow->guessed_host_protocol_id = flow->guessed_protocol_id;
return; flow->guessed_protocol_id = NDPI_PROTOCOL_STUN;
} else if(is_duo) {
ndpi_int_stun_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_HANGOUT_DUO, NDPI_PROTOCOL_STUN);
return;
} else if(flow->guessed_host_protocol_id == NDPI_PROTOCOL_SIGNAL) {
ndpi_int_stun_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_SIGNAL, NDPI_PROTOCOL_STUN);
return;
} else if(flow->protos.stun_ssl.stun.is_skype) {
NDPI_LOG_INFO(ndpi_struct, "Found Skype\n");
/* flow->protos.stun_ssl.stun.num_binding_requests < 4) ? NDPI_PROTOCOL_SKYPE_CALL_IN : NDPI_PROTOCOL_SKYPE_CALL_OUT */
// if((flow->protos.stun_ssl.stun.num_udp_pkts >= 6) || (flow->protos.stun_ssl.stun.num_binding_requests >= 3))
ndpi_int_stun_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_SKYPE_CALL, NDPI_PROTOCOL_SKYPE);
} else {
NDPI_LOG_INFO(ndpi_struct, "found UDP stun\n");
ndpi_int_stun_add_connection(ndpi_struct, flow,
is_whatsapp ? NDPI_PROTOCOL_WHATSAPP_CALL : NDPI_PROTOCOL_STUN,
NDPI_PROTOCOL_UNKNOWN);
} }
ndpi_int_stun_add_connection(ndpi_struct, flow,
flow->guessed_protocol_id,
flow->guessed_host_protocol_id);
return; return;
} }
+2 -1
View File
@@ -628,7 +628,7 @@ int getTLScertificate(struct ndpi_detection_module_struct *ndpi_struct,
u_int8_t version_len = packet->payload[offset+4]; u_int8_t version_len = packet->payload[offset+4];
if(version_len == (extension_len-1)) { if(version_len == (extension_len-1)) {
/* Sanity check */ #ifdef DEBUG_TLS
u_int8_t j; u_int8_t j;
for(j=0; j<version_len; j += 2) { for(j=0; j<version_len; j += 2) {
@@ -636,6 +636,7 @@ int getTLScertificate(struct ndpi_detection_module_struct *ndpi_struct,
printf("Client SSL [TLS version: 0x%04X]\n", tls_version); printf("Client SSL [TLS version: 0x%04X]\n", tls_version);
} }
#endif
} }
} }
+1 -1
View File
@@ -1,3 +1,3 @@
SkypeCall 200 39687 1 SkypeCall 200 39687 1
1 UDP 192.168.2.20:49282 <-> 104.46.40.49:60642 [proto: 125.38/Skype.SkypeCall][cat: VoIP/10][133 pkts/24845 bytes <-> 67 pkts/14842 bytes][bytes ratio: 0.252 (Upload)][IAT c2s/s2c min/avg/max/stddev: 0/0 10.8/22.4 159/203 24.8/47.7][Pkt Len c2s/s2c min/avg/max/stddev: 74/77 186.8/221.5 957/957 244.4/233.0][PLAIN TEXT (vRKSoR)] 1 UDP 192.168.2.20:49282 <-> 104.46.40.49:60642 [proto: 78.38/STUN.SkypeCall][cat: VoIP/10][133 pkts/24845 bytes <-> 67 pkts/14842 bytes][bytes ratio: 0.252 (Upload)][IAT c2s/s2c min/avg/max/stddev: 0/0 10.8/22.4 159/203 24.8/47.7][Pkt Len c2s/s2c min/avg/max/stddev: 74/77 186.8/221.5 957/957 244.4/233.0][PLAIN TEXT (vRKSoR)]