diff options
author | Ivan Nardi <12729895+IvanNardi@users.noreply.github.com> | 2022-04-25 11:00:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-25 11:00:02 +0200 |
commit | 075bce5f3de463975464472158dca980a45f48a3 (patch) | |
tree | 90cccc07f7021880f2824e6245afae2d23986650 /src | |
parent | ac0f50b56115acc7c8107d9b2661cac6b0d7bf2b (diff) |
XIAOMI: add detection of Xiaomi traffic (#1529)
Most of the credits should go to @utoni (see #1521)
Diffstat (limited to 'src')
-rw-r--r-- | src/include/ndpi_protocol_ids.h | 1 | ||||
-rw-r--r-- | src/include/ndpi_protocols.h | 1 | ||||
-rw-r--r-- | src/include/ndpi_typedefs.h | 2 | ||||
-rw-r--r-- | src/lib/ndpi_content_match.c.inc | 5 | ||||
-rw-r--r-- | src/lib/ndpi_main.c | 19 | ||||
-rw-r--r-- | src/lib/protocols/xiaomi.c | 123 |
6 files changed, 144 insertions, 7 deletions
diff --git a/src/include/ndpi_protocol_ids.h b/src/include/ndpi_protocol_ids.h index 082db5cd0..146fd644b 100644 --- a/src/include/ndpi_protocol_ids.h +++ b/src/include/ndpi_protocol_ids.h @@ -315,6 +315,7 @@ typedef enum { NDPI_PROTOCOL_GOOGLE_CLOUD = 284, NDPI_PROTOCOL_TENCENT = 285, NDPI_PROTOCOL_RAKNET = 286, + NDPI_PROTOCOL_XIAOMI = 287, #ifdef CUSTOM_NDPI_PROTOCOLS #include "../../../nDPI-custom/custom_ndpi_protocol_ids.h" diff --git a/src/include/ndpi_protocols.h b/src/include/ndpi_protocols.h index ea83f13ec..0f0103e49 100644 --- a/src/include/ndpi_protocols.h +++ b/src/include/ndpi_protocols.h @@ -224,6 +224,7 @@ void init_ethernet_ip_dissector(struct ndpi_detection_module_struct *ndpi_struct void init_toca_boca_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_sd_rtn_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_raknet_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); +void init_xiaomi_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); /* ndpi_main.c */ extern u_int32_t ndpi_ip_port_hash_funct(u_int32_t ip, u_int16_t port); diff --git a/src/include/ndpi_typedefs.h b/src/include/ndpi_typedefs.h index b3036ffc1..109b3a0cc 100644 --- a/src/include/ndpi_typedefs.h +++ b/src/include/ndpi_typedefs.h @@ -1186,7 +1186,7 @@ struct ndpi_flow_struct { /* General purpose field used to save mainly hostname/SNI information. * In details it used for: DNS and NETBIOS name, HTTP and DHCP hostname, - * WHOIS request, TLS/QUIC server name and STUN realm. + * WHOIS request, TLS/QUIC server name, XIAOMI domain and STUN realm. * * Please, think *very* hard before increasing its size! */ diff --git a/src/lib/ndpi_content_match.c.inc b/src/lib/ndpi_content_match.c.inc index a2bd9a0d9..3371c1aa8 100644 --- a/src/lib/ndpi_content_match.c.inc +++ b/src/lib/ndpi_content_match.c.inc @@ -1604,6 +1604,11 @@ static ndpi_protocol_match host_match[] = { "zn6qzq6caaucudesr-pluralsight.siteintercept.qualtrics.com", "Pluralsight", NDPI_PROTOCOL_PLURALSIGHT, NDPI_PROTOCOL_CATEGORY_STREAMING, NDPI_PROTOCOL_FUN, NDPI_PROTOCOL_DEFAULT_LEVEL }, { "ip-video-course-exercise-files-us-west-2.s3.us-west-2.amazonaws.com", "Pluralsight", NDPI_PROTOCOL_PLURALSIGHT, NDPI_PROTOCOL_CATEGORY_STREAMING, NDPI_PROTOCOL_FUN, NDPI_PROTOCOL_DEFAULT_LEVEL }, + { "xiaomi.com", "Xiaomi", NDPI_PROTOCOL_XIAOMI, NDPI_PROTOCOL_CATEGORY_WEB, NDPI_PROTOCOL_SAFE, NDPI_PROTOCOL_DEFAULT_LEVEL }, + { "xiaomi.net", "Xiaomi", NDPI_PROTOCOL_XIAOMI, NDPI_PROTOCOL_CATEGORY_WEB, NDPI_PROTOCOL_SAFE, NDPI_PROTOCOL_DEFAULT_LEVEL }, + { "mi.com", "Xiaomi", NDPI_PROTOCOL_XIAOMI, NDPI_PROTOCOL_CATEGORY_WEB, NDPI_PROTOCOL_SAFE, NDPI_PROTOCOL_DEFAULT_LEVEL }, + { "miui.com", "Xiaomi", NDPI_PROTOCOL_XIAOMI, NDPI_PROTOCOL_CATEGORY_WEB, NDPI_PROTOCOL_SAFE, NDPI_PROTOCOL_DEFAULT_LEVEL }, + #ifdef CUSTOM_NDPI_PROTOCOLS #include "../../../nDPI-custom/custom_ndpi_content_match_host_match.c.inc" #endif diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index 9432ce64b..5a6376c6f 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -1859,13 +1859,17 @@ static void ndpi_init_protocol_defaults(struct ndpi_detection_module_struct *ndp ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */, ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */); ndpi_set_proto_defaults(ndpi_str, 1 /* cleartext */, NDPI_PROTOCOL_ACCEPTABLE, NDPI_PROTOCOL_TENCENT, - "Tencent", NDPI_PROTOCOL_CATEGORY_SOCIAL_NETWORK, - ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0), /* TCP */ - ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */); + "Tencent", NDPI_PROTOCOL_CATEGORY_SOCIAL_NETWORK, + ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */, + ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */); ndpi_set_proto_defaults(ndpi_str, 1 /* cleartext */, NDPI_PROTOCOL_ACCEPTABLE, NDPI_PROTOCOL_RAKNET, - "RakNet", NDPI_PROTOCOL_CATEGORY_GAME, - ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0), /* TCP */ - ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */); + "RakNet", NDPI_PROTOCOL_CATEGORY_GAME, + ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0), /* TCP */ + ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */); + ndpi_set_proto_defaults(ndpi_str, 0 /* encrypted */, NDPI_PROTOCOL_ACCEPTABLE, NDPI_PROTOCOL_XIAOMI, + "Xiaomi", NDPI_PROTOCOL_CATEGORY_WEB, + ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */, + ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */); #ifdef CUSTOM_NDPI_PROTOCOLS #include "../../../nDPI-custom/custom_ndpi_main.c" @@ -4345,6 +4349,9 @@ static int ndpi_callback_init(struct ndpi_detection_module_struct *ndpi_str) { /* RakNet */ init_raknet_dissector(ndpi_str, &a, detection_bitmask); + /* Xiaomi */ + init_xiaomi_dissector(ndpi_str, &a, detection_bitmask); + #ifdef CUSTOM_NDPI_PROTOCOLS #include "../../../nDPI-custom/custom_ndpi_main_init.c" #endif diff --git a/src/lib/protocols/xiaomi.c b/src/lib/protocols/xiaomi.c new file mode 100644 index 000000000..831478e9e --- /dev/null +++ b/src/lib/protocols/xiaomi.c @@ -0,0 +1,123 @@ +/* + * xiaomi.c + * + * Copyright (C) 2022 - ntop.org + * + * nDPI is free software: you can zmqtribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * nDPI is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with nDPI. If not, see <http://www.gnu.org/licenses/>. + * + */ +#include "ndpi_protocol_ids.h" + +#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_XIAOMI + +#include "ndpi_api.h" + + +static void xiaomi_dissect_metadata(struct ndpi_detection_module_struct *ndpi_struct, + struct ndpi_flow_struct * const flow, + u_int8_t const * const payload, + u_int32_t payload_len) +{ + size_t offset = 16; + + while(offset + 1 < payload_len) { + char *ptr; + u_int8_t op = payload[offset]; + u_int8_t len = payload[offset + 1]; + + offset += 2; + + /* "Strage" types which don't respect the TLV format */ + if(op == 0x28 || op == 0x08) { + continue; + } + + if(offset + len >= payload_len) { + break; + } + + NDPI_LOG_DBG(ndpi_struct, "TLV: 0x%x len %d [%.*s]\n", + op, len, len, &payload[offset]); + + switch(op) { + case 0x12: + flow->http.user_agent = ndpi_malloc(len + 1); + if(flow->http.user_agent != NULL) { + memcpy(flow->http.user_agent, &payload[offset], len); + flow->http.user_agent[len] = '\0'; + } + break; + + case 0x3a: + /* If "domain:port", strip the port */ + ptr = ndpi_strnstr((const char *)&payload[offset], ":", len); + if(ptr == NULL) + ndpi_hostname_sni_set(flow, &payload[offset], len); + else + ndpi_hostname_sni_set(flow, &payload[offset], (const u_int8_t *)ptr - &payload[offset]); + break; + + case 0x32: /* Radio access technology (+ APN) */ + case 0x1A: /* Build version */ + case 0x42: /* Locale */ + default: + break; + } + offset += len; + } +} + +static void ndpi_search_xiaomi(struct ndpi_detection_module_struct *ndpi_struct, + struct ndpi_flow_struct *flow) +{ + struct ndpi_packet_struct *packet = &ndpi_struct->packet; + + NDPI_LOG_DBG(ndpi_struct, "search Xiaomi\n"); + + if(packet->payload_packet_len >= 12) { + uint32_t len; + + len = ntohl(get_u_int32_t(packet->payload, 4)); + if(len + 12 == packet->payload_packet_len && + ntohl(get_u_int32_t(packet->payload, 0)) == 0xC2FE0005 && + ntohl(get_u_int32_t(packet->payload, 8)) == 0x00020016) { + NDPI_LOG_INFO(ndpi_struct, "found Xiaomi\n"); + ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_XIAOMI, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI); + + /* Better way to detect "client" packets? */ + if(ntohs(packet->tcp->dest) == 5222) { + /* It seems that the "TLV list" is different for client and for server messages. + For example, the type 0x12 is used as user-agent by the client and + as something else by the server. We are interested in the metadata sent by the client */ + xiaomi_dissect_metadata(ndpi_struct, flow, packet->payload, packet->payload_packet_len); + } + + return; + } + } + + NDPI_EXCLUDE_PROTO(ndpi_struct, flow); +} + +void init_xiaomi_dissector(struct ndpi_detection_module_struct *ndpi_struct, + u_int32_t *id, + NDPI_PROTOCOL_BITMASK *detection_bitmask) { + ndpi_set_bitmask_protocol_detection("Xiaomi", ndpi_struct, detection_bitmask, *id, + NDPI_PROTOCOL_XIAOMI, + ndpi_search_xiaomi, + NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION, + SAVE_DETECTION_BITMASK_AS_UNKNOWN, + ADD_TO_DETECTION_BITMASK); + *id += 1; +} |