diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2021-07-19 13:58:22 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2021-07-19 16:05:51 +0200 |
commit | 370ea972c127c6a8ed704f8ca237aae6e9eb2660 (patch) | |
tree | 6c71a429d37be7ed0e8be939702cd91545266b8d /src/lib/ndpi_utils.c | |
parent | b95bd0358fd43d9fdfdc5266e3c8923b91e1d4db (diff) |
Added risk: TLS_EXTENSION_SUSPICIOUSadded/sus_tls_ext_risk
* validates client/server hello TLS extensions
* inspects content for some extensions
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'src/lib/ndpi_utils.c')
-rw-r--r-- | src/lib/ndpi_utils.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/lib/ndpi_utils.c b/src/lib/ndpi_utils.c index 3a3c18aff..7c27f8aed 100644 --- a/src/lib/ndpi_utils.c +++ b/src/lib/ndpi_utils.c @@ -24,6 +24,7 @@ #include <stdlib.h> #include <errno.h> +#include <math.h> #include <sys/types.h> @@ -1735,6 +1736,8 @@ const char* ndpi_risk2str(ndpi_risk_enum risk) { case NDPI_TLS_CERT_VALIDITY_TOO_LONG: return("TLS certificate validity longer than 13 months"); + case NDPI_TLS_EXTENSION_SUSPICIOUS: + return("TLS extension suspicious"); default: snprintf(buf, sizeof(buf), "%d", (int)risk); @@ -2001,4 +2004,49 @@ void ndpi_set_risk(struct ndpi_flow_struct *flow, ndpi_risk_enum r) { flow->risk |= v; } +/* ******************************************************************** */ + +int ndpi_is_printable_string(char const * const str, size_t len) +{ + for (size_t i = 0; i < len; ++i) + { + if (ndpi_isprint(str[i]) == 0) + { + return 0; + } + } + + return 1; +} + +/* ******************************************************************** */ + +float ndpi_calculate_entropy(u_int8_t const * const buf, size_t len) +{ + float entropy = 0.0f; + u_int32_t byte_counters[256]; + memset(byte_counters, 0, sizeof(byte_counters)); + + for (size_t i = 0; i < len; ++i) + { + if (buf[i] == i) + { + byte_counters[i]++; + } + } + + for (size_t i = 0; i < sizeof(byte_counters) / sizeof(byte_counters[0]); ++i) + { + if (byte_counters[i] == 0) + { + continue; + } + + float p = 1.0f * byte_counters[i] / len; + entropy -= p * log2f(p); + } + + entropy *= -1.0f; + return entropy; +} |