aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorToni <matzeton@googlemail.com>2021-08-08 17:03:23 +0200
committerGitHub <noreply@github.com>2021-08-08 17:03:23 +0200
commit259b60f574fd3cb7ccd0fb205d0e91b7b5173c5c (patch)
tree3ce3309656b2fb1748c8d6b515b91903dec94178 /src/lib
parent4e856a41d66952dbcd17d59b8b78b25d56304559 (diff)
Added entropy calculation to check for suspicious (encrypted) payload. (#1270)
Signed-off-by: Toni Uhlig <matzeton@googlemail.com> Co-authored-by: Luca Deri <lucaderi@users.noreply.github.com>
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/ndpi_main.c11
-rw-r--r--src/lib/ndpi_utils.c17
2 files changed, 21 insertions, 7 deletions
diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c
index b3758a1aa..41caa8c40 100644
--- a/src/lib/ndpi_main.c
+++ b/src/lib/ndpi_main.c
@@ -105,6 +105,7 @@ static ndpi_risk_info ndpi_known_risks[] = {
{ NDPI_TLS_CERT_VALIDITY_TOO_LONG, NDPI_RISK_MEDIUM, CLIENT_FAIR_RISK_PERCENTAGE },
{ NDPI_TLS_SUSPICIOUS_EXTENSION, NDPI_RISK_HIGH, CLIENT_HIGH_RISK_PERCENTAGE },
{ NDPI_TLS_FATAL_ALERT, NDPI_RISK_LOW, CLIENT_FAIR_RISK_PERCENTAGE },
+ { NDPI_ENTROPY_SUSPICIOUS, NDPI_RISK_MEDIUM, CLIENT_FAIR_RISK_PERCENTAGE },
/* Leave this as last member */
{ NDPI_MAX_RISK, NDPI_RISK_LOW, CLIENT_FAIR_RISK_PERCENTAGE }
@@ -2841,6 +2842,7 @@ u_int16_t ndpi_guess_protocol_id(struct ndpi_detection_module_struct *ndpi_str,
break;
case NDPI_ICMP_PROTOCOL_TYPE:
if(flow) {
+ flow->entropy = 0.0f;
/* Run some basic consistency tests */
if(flow->packet.payload_packet_len < sizeof(struct ndpi_icmphdr))
@@ -2853,6 +2855,15 @@ u_int16_t ndpi_guess_protocol_id(struct ndpi_detection_module_struct *ndpi_str,
if(((icmp_type >= 44) && (icmp_type <= 252))
|| (icmp_code > 15))
ndpi_set_risk(ndpi_str, flow, NDPI_MALFORMED_PACKET);
+ if (flow->packet.payload_packet_len > sizeof(struct ndpi_icmphdr))
+ {
+ flow->entropy = ndpi_entropy(flow->packet.payload + sizeof(struct ndpi_icmphdr),
+ flow->packet.payload_packet_len - sizeof(struct ndpi_icmphdr));
+ if (NDPI_ENTROPY_ENCRYPTED_OR_RANDOM(flow->entropy) != 0)
+ {
+ ndpi_set_risk(ndpi_str, flow, NDPI_ENTROPY_SUSPICIOUS);
+ }
+ }
}
}
return(NDPI_PROTOCOL_IP_ICMP);
diff --git a/src/lib/ndpi_utils.c b/src/lib/ndpi_utils.c
index a753dc086..714a1f28c 100644
--- a/src/lib/ndpi_utils.c
+++ b/src/lib/ndpi_utils.c
@@ -1126,6 +1126,9 @@ int ndpi_dpi2json(struct ndpi_detection_module_struct *ndpi_struct,
ndpi_serialize_start_of_block(serializer, "ndpi");
ndpi_serialize_risk(serializer, flow);
+ if (l7_protocol.master_protocol == NDPI_PROTOCOL_IP_ICMP && flow->entropy > 0.0f) {
+ ndpi_serialize_string_float(serializer, "entropy", flow->entropy, "%.6f");
+ }
ndpi_serialize_string_string(serializer, "proto", ndpi_protocol2name(ndpi_struct, l7_protocol, buf, sizeof(buf)));
ndpi_protocol_breed_t breed =
ndpi_get_proto_breed(ndpi_struct,
@@ -1777,6 +1780,9 @@ const char* ndpi_risk2str(ndpi_risk_enum risk) {
case NDPI_TLS_FATAL_ALERT:
return("TLS fatal alert");
+ case NDPI_ENTROPY_SUSPICIOUS:
+ return("Entropy suspicious");
+
default:
snprintf(buf, sizeof(buf), "%d", (int)risk);
return(buf);
@@ -2129,16 +2135,14 @@ int ndpi_is_printable_string(char const * const str, size_t len) {
/* ******************************************************************** */
-float ndpi_calculate_entropy(u_int8_t const * const buf, size_t len) {
+float ndpi_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]++;
- }
+ byte_counters[buf[i]]++;
}
for (size_t i = 0; i < sizeof(byte_counters) / sizeof(byte_counters[0]); ++i) {
@@ -2146,11 +2150,10 @@ float ndpi_calculate_entropy(u_int8_t const * const buf, size_t len) {
continue;
}
- float p = 1.0f * byte_counters[i] / len;
- entropy -= p * log2f(p);
+ float const p = (float)byte_counters[i] / len;
+ entropy += p * log2f(1 / p);
}
- entropy *= -1.0f;
return entropy;
}