Files
nDPI/src/lib/protocols/ookla.c
T
Ivan Nardi 4d11941d32 Ookla: rework detection (#1922)
The logic of the LRU cache has been changed: once we know an ip has
connected to an Ookla server, all the following (unknown) flows (for
a short time interval) from the same ip to the port 8080 are treated
as Ookla ones.

Most of the changes in this commit are about introducing the concept of
"aggressive detection". In some cases, to properly detect a
protocol we might use some statistical/behavior logic that, from one
side, let us to identify the protocol more often but, from the other
side, might lead to some false positives.
To allow the user/application to easily detect when such logic has been
triggered, the new confidence value `NDPI_CONFIDENCE_DPI_AGGRESSIVE` has been
added.
It is always possible to disable/configure this kind of logic via the
API.

Detection of Ookla flows using plain TLS over port 8080 is the first
example of aggressive detection in nDPI.

Tested with:
* Android 9.0 with app 4.8.3
* Ubuntu 20.04 with Firefox 110
* Win 10 with app 1.15 and 1.16
* Win 10 with Chrome 108, Edge 108 and Firefox 106
2023-03-30 17:13:51 +02:00

128 lines
3.8 KiB
C

/*
* ookla.c
*
* Copyright (C) 2018-22 - ntop.org
*
* nDPI is free software: you can redistribute 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_OOKLA
#include "ndpi_api.h"
/* #define DEBUG_OOKLA_LRU */
const u_int16_t ookla_port = 8080;
/* ************************************************************* */
static u_int32_t get_ookla_key(struct ndpi_flow_struct *flow)
{
if(flow->is_ipv6)
return ndpi_quick_hash(flow->c_address.v6, 16);
else
return ntohl(flow->c_address.v4);
}
/* ************************************************************* */
int ookla_search_into_cache(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow)
{
u_int32_t key;
u_int16_t dummy;
if(ndpi_struct->ookla_cache) {
key = get_ookla_key(flow);
#ifdef DEBUG_OOKLA_LRU
printf("[LRU OOKLA] Search %u\n", key);
#endif
if(ndpi_lru_find_cache(ndpi_struct->ookla_cache, key,
&dummy, 0 /* Don't remove it as it can be used for other connections */,
ndpi_get_current_time(flow))) {
#ifdef DEBUG_OOKLA_LRU
printf("[LRU OOKLA] Found\n");
#endif
return 1;
}
}
return 0;
}
/* ************************************************************* */
void ookla_add_to_cache(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow)
{
u_int32_t key;
if(ndpi_struct->ookla_cache) {
key = get_ookla_key(flow);
#ifdef DEBUG_OOKLA_LRU
printf("[LRU OOKLA] ADDING %u\n", key);
#endif
ndpi_lru_add_to_cache(ndpi_struct->ookla_cache, key, 1 /* dummy */,
ndpi_get_current_time(flow));
}
}
/* ************************************************************* */
void ndpi_search_ookla(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, "Ookla detection\n");
if(ntohs(flow->s_port) != ookla_port && ntohs(flow->c_port) != ookla_port) {
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
return;
}
if(flow->packet_counter == 1 &&
packet->payload_packet_len >= NDPI_STATICSTRING_LEN("HI") &&
memcmp(packet->payload, "HI", NDPI_STATICSTRING_LEN("HI")) == 0) {
flow->ookla_stage = 1;
return;
}
if(flow->packet_counter == 2 &&
flow->ookla_stage == 1 &&
packet->payload_packet_len >= NDPI_STATICSTRING_LEN("HELLO") &&
memcmp(packet->payload, "HELLO", NDPI_STATICSTRING_LEN("HELLO")) == 0) {
NDPI_LOG_INFO(ndpi_struct, "found ookla (Hi + Hello)\n");
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_OOKLA, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI);
ookla_add_to_cache(ndpi_struct, flow);
return;
}
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
}
/* ************************************************************* */
void init_ookla_dissector(struct ndpi_detection_module_struct *ndpi_struct,
u_int32_t *id) {
ndpi_set_bitmask_protocol_detection("Ookla", ndpi_struct, *id,
NDPI_PROTOCOL_OOKLA,
ndpi_search_ookla,
NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_OR_UDP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION,
SAVE_DETECTION_BITMASK_AS_UNKNOWN,
ADD_TO_DETECTION_BITMASK);
*id += 1;
}