From 890f17788bb4295b466f70bf8cd4908fd60f2b30 Mon Sep 17 00:00:00 2001 From: Ivan Nardi <12729895+IvanNardi@users.noreply.github.com> Date: Fri, 14 Jul 2023 23:20:06 +0200 Subject: ndpireader: fix detection of DoH traffic based on packet distributions (#2045) --- example/ndpiReader.c | 60 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 24 deletions(-) (limited to 'example/ndpiReader.c') diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 053cfe38b..dfde22d61 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -269,33 +269,37 @@ FILE *trace = NULL; #define NUM_DOH_BINS 2 -struct ndpi_bin doh_ndpi_bins[NUM_DOH_BINS]; +static struct ndpi_bin doh_ndpi_bins[NUM_DOH_BINS]; -u_int8_t doh_centroids[NUM_DOH_BINS][PLEN_NUM_BINS] = { +static u_int8_t doh_centroids[NUM_DOH_BINS][PLEN_NUM_BINS] = { { 23,25,3,0,26,0,0,0,0,0,0,0,0,0,2,0,0,15,3,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 35,30,21,0,0,0,2,4,0,0,5,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } }; -float doh_max_distance = 35.5; +static float doh_max_distance = 35.5; -void init_doh_bins() { +static void init_doh_bins() { u_int i; for(i=0; i %s:%u\t[", - i, - ndpi_protocol2name(ndpi_thread_info[0].workflow->ndpi_struct, - all_flows[i].flow->detected_protocol, buf, sizeof(buf)), - all_flows[i].flow->src_name, - ntohs(all_flows[i].flow->src_port), - all_flows[i].flow->dst_name, - ntohs(all_flows[i].flow->dst_port)); + fprintf(out, "\t%u\t%-10s\t%s:%u <-> %s:%u\t[", + i, + ndpi_protocol2name(ndpi_thread_info[0].workflow->ndpi_struct, + all_flows[i].flow->detected_protocol, buf, sizeof(buf)), + all_flows[i].flow->src_name, + ntohs(all_flows[i].flow->src_port), + all_flows[i].flow->dst_name, + ntohs(all_flows[i].flow->dst_port)); print_bin(out, NULL, &bins[i]); - printf("][similarity: %f]", - (similarity = ndpi_bin_similarity(¢roids[j], &bins[i], 0, 0))); + fprintf(out, "][similarity: %f]", + (similarity = ndpi_bin_similarity(¢roids[j], &bins[i], 0, 0))); if(all_flows[i].flow->host_server_name[0] != '\0') fprintf(out, "[%s]", all_flows[i].flow->host_server_name); @@ -3445,23 +3449,23 @@ static void printFlowsStats() { && all_flows[i].flow->ssh_tls.advertised_alpns /* ALPN */ ) { if(check_bin_doh_similarity(&bins[i], &s)) - printf("[DoH (%f distance)]", s); + fprintf(out, "[DoH (%f distance)]", s); else - printf("[NO DoH (%f distance)]", s); + fprintf(out, "[NO DoH (%f distance)]", s); } else { if(all_flows[i].flow->ssh_tls.advertised_alpns == NULL) - printf("[NO DoH check: missing ALPN]"); + fprintf(out, "[NO DoH check: missing ALPN]"); } } - printf("\n"); + fprintf(out, "\n"); num_printed++; if(similarity > max_similarity) max_similarity = similarity; } if(num_printed) { - printf("\tMax similarity: %f\n", max_similarity); - printf("\n"); + fprintf(out, "\tMax similarity: %f\n", max_similarity); + fprintf(out, "\n"); } } @@ -5414,6 +5418,14 @@ int main(int argc, char **argv) { exit(0); } + if(enable_doh_dot_detection) { + init_doh_bins(); + /* Clusters are not really used in DoH/DoT detection, but because of how + the code has been written, we need to enable also clustering feature */ + if(num_bin_clusters == 0) + num_bin_clusters = 1; + } + if(!quiet_mode) { printf("\n-----------------------------------------------------------\n" "* NOTE: This is demo app to show *some* nDPI features.\n" -- cgit v1.2.3