Further WhatsApp improvements

This commit is contained in:
Luca Deri
2019-06-21 19:06:41 +02:00
parent 179b93d625
commit 00ed2653fe
3 changed files with 98 additions and 82 deletions
+13 -1
View File
@@ -1438,6 +1438,7 @@ static void node_idle_scan_walker(const void *node, ndpi_VISIT which, int depth,
} }
} }
/* *********************************************** */
/** /**
* @brief On Protocol Discover - demo callback * @brief On Protocol Discover - demo callback
@@ -1448,6 +1449,8 @@ static void on_protocol_discovered(struct ndpi_workflow * workflow,
; ;
} }
/* *********************************************** */
#if 0 #if 0
/** /**
* @brief Print debug * @brief Print debug
@@ -1455,7 +1458,6 @@ static void on_protocol_discovered(struct ndpi_workflow * workflow,
static void debug_printf(u_int32_t protocol, void *id_struct, static void debug_printf(u_int32_t protocol, void *id_struct,
ndpi_log_level_t log_level, ndpi_log_level_t log_level,
const char *format, ...) { const char *format, ...) {
va_list va_ap; va_list va_ap;
#ifndef WIN32 #ifndef WIN32
struct tm result; struct tm result;
@@ -1489,6 +1491,8 @@ static void debug_printf(u_int32_t protocol, void *id_struct,
} }
#endif #endif
/* *********************************************** */
/** /**
* @brief Setup for detection begin * @brief Setup for detection begin
*/ */
@@ -1576,6 +1580,7 @@ static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle) {
} }
} }
/* *********************************************** */
/** /**
* @brief End of detection and free flow * @brief End of detection and free flow
@@ -1584,6 +1589,7 @@ static void terminateDetection(u_int16_t thread_id) {
ndpi_workflow_free(ndpi_thread_info[thread_id].workflow); ndpi_workflow_free(ndpi_thread_info[thread_id].workflow);
} }
/* *********************************************** */
/** /**
* @brief Traffic stats format * @brief Traffic stats format
@@ -1620,6 +1626,7 @@ char* formatTraffic(float numBits, int bits, char *buf) {
return(buf); return(buf);
} }
/* *********************************************** */
/** /**
* @brief Packets stats format * @brief Packets stats format
@@ -1638,6 +1645,7 @@ char* formatPackets(float numPkts, char *buf) {
return(buf); return(buf);
} }
/* *********************************************** */
/** /**
* @brief JSON function init * @brief JSON function init
@@ -1649,6 +1657,8 @@ static void json_init() {
jArray_topStats = json_object_new_array(); jArray_topStats = json_object_new_array();
} }
/* *********************************************** */
static void json_open_stats_file() { static void json_open_stats_file() {
if((file_first_time && ((stats_fp = fopen(_statsFilePath,"w")) == NULL)) if((file_first_time && ((stats_fp = fopen(_statsFilePath,"w")) == NULL))
|| ||
@@ -1659,6 +1669,8 @@ static void json_open_stats_file() {
else file_first_time = 0; else file_first_time = 0;
} }
/* *********************************************** */
static void json_close_stats_file() { static void json_close_stats_file() {
json_object *jObjFinal = json_object_new_object(); json_object *jObjFinal = json_object_new_object();
+13 -13
View File
@@ -246,21 +246,21 @@ int ndpi_workflow_node_cmp(const void *a, const void *b) {
if( if(
( (
(fa->src_ip == fb->src_ip ) (fa->src_ip == fb->src_ip )
&& (fa->src_port == fb->src_port) && (fa->src_port == fb->src_port)
&& (fa->dst_ip == fb->dst_ip ) && (fa->dst_ip == fb->dst_ip )
&& (fa->dst_port == fb->dst_port) && (fa->dst_port == fb->dst_port)
) )
|| ||
( (
(fa->src_ip == fb->dst_ip ) (fa->src_ip == fb->dst_ip )
&& (fa->src_port == fb->dst_port) && (fa->src_port == fb->dst_port)
&& (fa->dst_ip == fb->src_ip ) && (fa->dst_ip == fb->src_ip )
&& (fa->dst_port == fb->src_port) && (fa->dst_port == fb->src_port)
) )
) )
return(0); return(0);
if(fa->src_ip < fb->src_ip ) return(-1); else { if(fa->src_ip > fb->src_ip ) return(1); } if(fa->src_ip < fb->src_ip ) return(-1); else { if(fa->src_ip > fb->src_ip ) return(1); }
if(fa->src_port < fb->src_port) return(-1); else { if(fa->src_port > fb->src_port) return(1); } if(fa->src_port < fb->src_port) return(-1); else { if(fa->src_port > fb->src_port) return(1); }
if(fa->dst_ip < fb->dst_ip ) return(-1); else { if(fa->dst_ip > fb->dst_ip ) return(1); } if(fa->dst_ip < fb->dst_ip ) return(-1); else { if(fa->dst_ip > fb->dst_ip ) return(1); }
@@ -376,10 +376,10 @@ static struct ndpi_flow_info *get_ndpi_flow_info(struct ndpi_workflow * workflow
flow.src_ip = iph->saddr, flow.dst_ip = iph->daddr; flow.src_ip = iph->saddr, flow.dst_ip = iph->daddr;
flow.src_port = htons(*sport), flow.dst_port = htons(*dport); flow.src_port = htons(*sport), flow.dst_port = htons(*dport);
flow.hashval = hashval = flow.protocol + flow.vlan_id + flow.src_ip + flow.dst_ip + flow.src_port + flow.dst_port; flow.hashval = hashval = flow.protocol + flow.vlan_id + flow.src_ip + flow.dst_ip + flow.src_port + flow.dst_port;
/* printf("hashval=%u [%u][%u][%u:%u][%u:%u]\n", hashval, flow.protocol, flow.vlan_id, flow.src_ip, flow.src_port, flow.dst_ip, flow.dst_port); */
idx = hashval % workflow->prefs.num_roots; idx = hashval % workflow->prefs.num_roots;
ret = ndpi_tfind(&flow, &workflow->ndpi_flows_root[idx], ndpi_workflow_node_cmp); ret = ndpi_tfind(&flow, &workflow->ndpi_flows_root[idx], ndpi_workflow_node_cmp);
/* to avoid two nodes in one binary tree for a flow */ /* to avoid two nodes in one binary tree for a flow */
int is_changed = 0; int is_changed = 0;
if(ret == NULL) { if(ret == NULL) {
+72 -68
View File
@@ -76,7 +76,7 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
if(ntohs(h->msg_type) == 0x01 /* Binding Request */) if(ntohs(h->msg_type) == 0x01 /* Binding Request */)
flow->protos.stun_ssl.stun.num_binding_requests++; flow->protos.stun_ssl.stun.num_binding_requests++;
// printf("[%02X][%02X][payload_length: %u]\n", payload[0], payload[1], payload_length); /* printf("[%02X][%02X][msg_len: %u][payload_length: %u]\n", payload[0], payload[1], msg_len, payload_length); */
if(((payload[0] == 0x80) && ((msg_len+20) <= payload_length)) /* WhatsApp Voice */) { if(((payload[0] == 0x80) && ((msg_len+20) <= payload_length)) /* WhatsApp Voice */) {
*is_whatsapp = 1; *is_whatsapp = 1;
@@ -89,86 +89,90 @@ static ndpi_int_stun_t ndpi_int_check_stun(struct ndpi_detection_module_struct *
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);
if(((payload_length == (msg_len+20)) if(payload_length == (msg_len+20)) {
&& ((msg_type <= 0x000b) /* http://www.3cx.com/blog/voip-howto/stun-details/ */))) { if(msg_type <= 0x000b) /* http://www.3cx.com/blog/voip-howto/stun-details/ */ {
u_int offset = 20; u_int offset = 20;
// printf("[%02X][%02X][%02X][%02X][payload_length: %u]\n", payload[offset], payload[offset+1], payload[offset+2], payload[offset+3],payload_length); // printf("[%02X][%02X][%02X][%02X][payload_length: %u]\n", payload[offset], payload[offset+1], payload[offset+2], payload[offset+3],payload_length);
/* /*
This can either be the standard RTCP or Ms Lync RTCP that This can either be the standard RTCP or Ms Lync RTCP that
later will become Ms Lync RTP. In this case we need to later will become Ms Lync RTP. In this case we need to
be careful before deciding about the protocol before dissecting the packet be careful before deciding about the protocol before dissecting the packet
MS Lync = Skype MS Lync = Skype
https://en.wikipedia.org/wiki/Skype_for_Business https://en.wikipedia.org/wiki/Skype_for_Business
*/ */
while((offset+2) < payload_length) { while((offset+2) < payload_length) {
u_int16_t attribute = ntohs(*((u_int16_t*)&payload[offset])); u_int16_t attribute = ntohs(*((u_int16_t*)&payload[offset]));
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 != 0) if(x != 0)
len += 4-x; len += 4-x;
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 0x4002: case 0x4002:
/* These are the only messages apparently whatsapp voice can use */ /* These are the only messages apparently whatsapp voice can use */
break; break;
case 0x8054: /* Candidate Identifier */ case 0x8054: /* Candidate Identifier */
if((len == 4) if((len == 4)
&& ((offset+7) < payload_length) && ((offset+7) < payload_length)
&& (payload[offset+5] == 0x00) && (payload[offset+5] == 0x00)
&& (payload[offset+6] == 0x00) && (payload[offset+6] == 0x00)
&& (payload[offset+7] == 0x00)) { && (payload[offset+7] == 0x00)) {
/* Either skype for business or "normal" skype with multiparty call */ /* Either skype for business or "normal" skype with multiparty call */
flow->protos.stun_ssl.stun.is_skype = 1;
return(NDPI_IS_STUN);
}
break;
case 0x8055: /* MS Service Quality (skype?) */
break;
/* Proprietary fields found on skype calls */
case 0x24DF:
case 0x3802:
case 0x8036:
case 0x8095:
case 0x0800:
/* printf("====>>>> %04X\n", attribute); */
flow->protos.stun_ssl.stun.is_skype = 1; flow->protos.stun_ssl.stun.is_skype = 1;
return(NDPI_IS_STUN); return(NDPI_IS_STUN);
break;
case 0x8070: /* Implementation Version */
if((len == 4)
&& ((offset+7) < payload_length)
&& (payload[offset+4] == 0x00)
&& (payload[offset+5] == 0x00)
&& (payload[offset+6] == 0x00)
&& ((payload[offset+7] == 0x02) || (payload[offset+7] == 0x03))
) {
flow->protos.stun_ssl.stun.is_skype = 1;
return(NDPI_IS_STUN);
}
break;
default:
/* This means this STUN packet cannot be confused with whatsapp voice */
/* printf("==> %04X\n", attribute); */
can_this_be_whatsapp_voice = 0;
break;
} }
break;
case 0x8055: /* MS Service Quality (skype?) */ offset += len + 4;
break;
/* Proprietary fields found on skype calls */
case 0x24DF:
case 0x3802:
case 0x8036:
case 0x8095:
case 0x0800:
/* printf("====>>>> %04X\n", attribute); */
flow->protos.stun_ssl.stun.is_skype = 1;
return(NDPI_IS_STUN);
break;
case 0x8070: /* Implementation Version */
if((len == 4)
&& ((offset+7) < payload_length)
&& (payload[offset+4] == 0x00)
&& (payload[offset+5] == 0x00)
&& (payload[offset+6] == 0x00)
&& ((payload[offset+7] == 0x02) || (payload[offset+7] == 0x03))
) {
flow->protos.stun_ssl.stun.is_skype = 1;
return(NDPI_IS_STUN);
}
break;
default:
/* This means this STUN packet cannot be confused with whatsapp voice */
printf("==> %04X\n", attribute);
can_this_be_whatsapp_voice = 0;
break;
} }
goto udp_stun_found;
offset += len + 4; } else if(msg_type == 0x0800) {
*is_whatsapp = 1;
return NDPI_IS_STUN; /* This is WhatsApp */
} }
goto udp_stun_found;
} }
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)) {