Implemented NDPI_UNRESOLVED_HOSTNAME flow risk for detecting flows (TLS/QUIC/HTTP) whose hostname

was not previously resolved via DNS.

Added new API calls
-  bool ndpi_cache_hostname_ip(struct ndpi_detection_module_struct *ndpi_struct, ndpi_ip_addr_t *ip_addr, char *hostname);
-  bool ndpi_cache_find_hostname_ip(struct ndpi_detection_module_struct *ndpi_struct, ndpi_ip_addr_t *ip_addr, char *hostname);
-  void ndpi_cache_hostname_ip_swap(struct ndpi_detection_module_struct *ndpi_struct);

Usage example:
- ./example/ndpiReader --cfg hostname_dns_check,1 -i tests/pcap/netflix.pcap
This commit is contained in:
Luca 2025-08-04 18:56:59 +02:00
parent eb5f8a037c
commit 8e1b17215d
11 changed files with 190 additions and 21 deletions

View file

@ -313,9 +313,9 @@ Relevant TCP connection issues such as connection refused, scan, or probe attemp
.. _Risk 051
NDPI_FREE_51
NDPI_UNRESOLVED_HOSTNAME
============
Placeholder; not really used.
Risk set when talking (TLS, QUIC, HTTP) with a symbolic hostname not previously observed on a DNS resolution. Unless the resolution was not observed as it was performed before nDPI started, it can indicate fake hosts contacted.
.. _Risk 052

View file

@ -2294,6 +2294,13 @@ extern "C" {
u_int32_t ndpi_cache_address_restore(struct ndpi_detection_module_struct *ndpi_struct, char *path, u_int32_t epoch_now);
u_int32_t ndpi_cache_address_flush_expired(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t epoch_now);
/* Scaffolding code for triggering risk NDPI_UNRESOLVED_HOSTNAME */
bool ndpi_cache_hostname_ip(struct ndpi_detection_module_struct *ndpi_struct,
ndpi_ip_addr_t *ip_addr, char *hostname);
bool ndpi_cache_find_hostname_ip(struct ndpi_detection_module_struct *ndpi_struct,
ndpi_ip_addr_t *ip_addr, char *hostname);
void ndpi_cache_hostname_ip_swap(struct ndpi_detection_module_struct *ndpi_struct);
/* Protocol normalization functions */
/**
* Checks if the specified protocol identifier can be placed only on the master_protocol field of ndpi_master_app_protocol

View file

@ -221,6 +221,7 @@ struct ndpi_detection_module_config_struct {
int compute_entropy;
int address_cache_size;
int fpc_enabled;
int hostname_dns_check_enabled;
int guess_ip_before_port;
int use_client_ip_in_guess;
int use_client_port_in_guess;
@ -471,9 +472,11 @@ struct ndpi_detection_module_struct {
ndpi_str_hash *public_domain_suffixes;
struct ndpi_address_cache *address_cache;
struct {
ndpi_filter *cache, *cache_shadow;
} dns_hostname;
};
/* Used by ndpi_set_proto_subprotocols */
#define NDPI_PROTOCOL_NO_MORE_SUBPROTOCOLS (-1)
#define NDPI_PROTOCOL_MATCHED_BY_CONTENT (-2)

View file

@ -162,7 +162,7 @@ typedef enum {
NDPI_PERIODIC_FLOW, /* Set in case a flow repeats at a specific pace [used by apps on top of nDPI] */
NDPI_MINOR_ISSUES, /* Generic packet issues (e.g. DNS with 0 TTL) */
NDPI_TCP_ISSUES, /* 50 */ /* TCP issues such as connection failed or scan */
NDPI_FREE_51, /* FREE */
NDPI_UNRESOLVED_HOSTNAME, /* Set in case a client issues a request towards a symbolic hostname not previously resolved */
NDPI_TLS_ALPN_SNI_MISMATCH, /* Invalid ALPN/SNI combination */
NDPI_MALWARE_HOST_CONTACTED, /* Flow client contacted a malware host */
NDPI_BINARY_DATA_TRANSFER, /* Attempt to transfer something in binary format */

View file

@ -529,3 +529,100 @@ u_int32_t ndpi_cache_address_flush_expired(struct ndpi_detection_module_struct *
else
return(ndpi_address_cache_flush_expired(ndpi_struct->address_cache, epoch_now));
}
/* ***************************************************** */
/* ***************************************************** */
/*
Used to cache resolved IP addresses in order to trigger
risk NDPI_UNRESOLVED_HOSTNAME
*/
static u_int32_t ndpi_cache_hash_hostname_ip(ndpi_ip_addr_t *ip_addr, char *hostname) {
u_char buf[128];
u_int32_t len = snprintf((char*)buf, sizeof(buf), "%s", hostname);
if(len < sizeof(buf)) {
int32_t leftover = sizeof(buf) - len;
if(leftover > 0) {
u_int32_t to_add = ndpi_min((u_int32_t)leftover, sizeof(ndpi_ip_addr_t));
if((u_int32_t)leftover >= to_add) {
memcpy(&buf[len], ip_addr, to_add);
len += to_add;
}
#ifdef DEBUG_ADDR_CACHE
printf("[DEBUG] Hashing %s [len=%u]\n", hostname, len);
#endif
}
}
return(ndpi_quick_hash(buf, len));
}
// #define DEBUG_ADDR_CACHE
/* ***************************************************** */
/*
Used to cache resolved IP addresses in order to trigger
risk NDPI_UNRESOLVED_HOSTNAME
*/
bool ndpi_cache_hostname_ip(struct ndpi_detection_module_struct *ndpi_struct,
ndpi_ip_addr_t *ip_addr, char *hostname) {
if(ndpi_struct->dns_hostname.cache == NULL)
ndpi_struct->dns_hostname.cache = ndpi_filter_alloc();
if(ndpi_struct->dns_hostname.cache) {
u_int32_t hashval = ndpi_cache_hash_hostname_ip(ip_addr, hostname);
NDPI_LOG_DBG2(ndpi_struct, "[DEBUG] ->>> %s [%u]\n", hostname, hashval);
#ifdef DEBUG_ADDR_CACHE
printf("[DEBUG] Adding %s [%u][%u]\n", hostname, ip_addr->ipv4, hashval);
#endif
return(ndpi_filter_add(ndpi_struct->dns_hostname.cache, hashval));
}
return(false);
}
/* ***************************************************** */
/*
Check if the combination hostname/IP was found
Return value
- true if was found, false otherwise
*/
bool ndpi_cache_find_hostname_ip(struct ndpi_detection_module_struct *ndpi_struct,
ndpi_ip_addr_t *ip_addr, char *hostname) {
if(ndpi_struct->dns_hostname.cache) {
u_int32_t hashval = ndpi_cache_hash_hostname_ip(ip_addr, hostname);
bool ret;
#ifdef DEBUG_ADDR_CACHE
printf("[DEBUG] ->>> Searching %s [%u][%u]\n", hostname, ip_addr->ipv4, hashval);
#endif
ret = ndpi_filter_contains(ndpi_struct->dns_hostname.cache, hashval);
if((!ret) && (ndpi_struct->dns_hostname.cache_shadow != NULL))
ret = ndpi_filter_contains(ndpi_struct->dns_hostname.cache_shadow, hashval);
return(ret);
}
return(false);
}
/* ***************************************************** */
void ndpi_cache_hostname_ip_swap(struct ndpi_detection_module_struct *ndpi_struct) {
if(ndpi_struct->dns_hostname.cache_shadow)
ndpi_filter_free(ndpi_struct->dns_hostname.cache_shadow);
ndpi_struct->dns_hostname.cache_shadow = ndpi_struct->dns_hostname.cache;
ndpi_struct->dns_hostname.cache = ndpi_filter_alloc();
}

View file

@ -201,7 +201,7 @@ static ndpi_risk_info ndpi_known_risks[] = {
{ NDPI_PERIODIC_FLOW, NDPI_RISK_LOW, CLIENT_LOW_RISK_PERCENTAGE, NDPI_CLIENT_ACCOUNTABLE },
{ NDPI_MINOR_ISSUES, NDPI_RISK_LOW, CLIENT_LOW_RISK_PERCENTAGE, NDPI_BOTH_ACCOUNTABLE },
{ NDPI_TCP_ISSUES, NDPI_RISK_MEDIUM, CLIENT_FAIR_RISK_PERCENTAGE, NDPI_CLIENT_ACCOUNTABLE },
{ NDPI_FREE_51, NDPI_RISK_MEDIUM, CLIENT_FAIR_RISK_PERCENTAGE, NDPI_CLIENT_ACCOUNTABLE },
{ NDPI_UNRESOLVED_HOSTNAME, NDPI_RISK_MEDIUM, CLIENT_FAIR_RISK_PERCENTAGE, NDPI_CLIENT_ACCOUNTABLE },
{ NDPI_TLS_ALPN_SNI_MISMATCH, NDPI_RISK_MEDIUM, CLIENT_FAIR_RISK_PERCENTAGE, NDPI_CLIENT_ACCOUNTABLE },
{ NDPI_MALWARE_HOST_CONTACTED, NDPI_RISK_SEVERE, CLIENT_HIGH_RISK_PERCENTAGE, NDPI_CLIENT_ACCOUNTABLE },
{ NDPI_BINARY_DATA_TRANSFER, NDPI_RISK_MEDIUM, CLIENT_FAIR_RISK_PERCENTAGE, NDPI_CLIENT_ACCOUNTABLE },
@ -5028,6 +5028,12 @@ void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_str) {
if(ndpi_str->address_cache)
ndpi_term_address_cache(ndpi_str->address_cache);
if(ndpi_str->dns_hostname.cache)
ndpi_filter_free(ndpi_str->dns_hostname.cache);
if(ndpi_str->dns_hostname.cache_shadow)
ndpi_filter_free(ndpi_str->dns_hostname.cache_shadow);
ndpi_free(ndpi_str);
}
@ -12973,6 +12979,7 @@ static const struct cfg_param {
{ NULL, "dpi.compute_entropy", "1", NULL, NULL, CFG_PARAM_ENABLE_DISABLE, __OFF(compute_entropy), NULL },
{ NULL, "dpi.address_cache_size", "0", "0", "16777215", CFG_PARAM_INT, __OFF(address_cache_size), NULL },
{ NULL, "fpc", "1", NULL, NULL, CFG_PARAM_ENABLE_DISABLE, __OFF(fpc_enabled), NULL },
{ NULL, "hostname_dns_check", "0", NULL, NULL, CFG_PARAM_ENABLE_DISABLE, __OFF(hostname_dns_check_enabled), NULL },
{ NULL, "metadata.tcp_fingerprint", "enable", NULL, NULL, CFG_PARAM_ENABLE_DISABLE, __OFF(tcp_fingerprint_enabled), NULL },
{ NULL, "metadata.tcp_fingerprint_raw", "disable", NULL, NULL, CFG_PARAM_ENABLE_DISABLE, __OFF(tcp_fingerprint_raw_enabled), NULL },

View file

@ -2303,8 +2303,8 @@ const char* ndpi_risk2str(ndpi_risk_enum risk) {
case NDPI_TCP_ISSUES:
return("TCP Connection Issues");
case NDPI_FREE_51:
return("FREE51");
case NDPI_UNRESOLVED_HOSTNAME:
return("Unresolved hostname");
case NDPI_TLS_ALPN_SNI_MISMATCH:
return("ALPN/SNI Mismatch");
@ -2435,8 +2435,8 @@ const char* ndpi_risk2code(ndpi_risk_enum risk) {
return STRINGIFY(NDPI_MINOR_ISSUES);
case NDPI_TCP_ISSUES:
return STRINGIFY(NDPI_MINOR_ISSUES);
case NDPI_FREE_51:
return STRINGIFY(NDPI_FREE_51);
case NDPI_UNRESOLVED_HOSTNAME:
return STRINGIFY(NDPI_UNRESOLVED_HOSTNAME);
case NDPI_TLS_ALPN_SNI_MISMATCH:
return STRINGIFY(NDPI_TLS_ALPN_SNI_MISMATCH);
case NDPI_MALWARE_HOST_CONTACTED:
@ -2558,8 +2558,8 @@ ndpi_risk_enum ndpi_code2risk(const char* risk) {
return(NDPI_MINOR_ISSUES);
else if(strcmp(STRINGIFY(NDPI_TCP_ISSUES), risk) == 0)
return(NDPI_MINOR_ISSUES);
else if(strcmp(STRINGIFY(NDPI_FREE_51), risk) == 0)
return(NDPI_FREE_51);
else if(strcmp(STRINGIFY(NDPI_UNRESOLVED_HOSTNAME), risk) == 0)
return(NDPI_UNRESOLVED_HOSTNAME);
else if(strcmp(STRINGIFY(NDPI_TLS_ALPN_SNI_MISMATCH), risk) == 0)
return(NDPI_TLS_ALPN_SNI_MISMATCH);
else if(strcmp(STRINGIFY(NDPI_MALWARE_HOST_CONTACTED), risk) == 0)
@ -2707,7 +2707,7 @@ const char *ndpi_risk_shortnames[NDPI_MAX_RISK] = {
"periodic_flow",
"minor_issues",
"tcp_issues", /* NDPI_TCP_ISSUES */
"free51",
"unresolved_hostname",
"tls_alpn_mismatch",
"malware_host",
"binary_data_transfer",

View file

@ -174,7 +174,7 @@ static u_int getNameLength(u_int i, const u_int8_t *payload, u_int payloadLen) {
See
- RFC 1035
- https://learn.microsoft.com/en-us/troubleshoot/windows-server/identity/naming-conventions-for-computer-domain-site-ou
Allowed chars for dns names A-Z 0-9 _ -
Perl script for generation map:
my @M;
@ -269,16 +269,16 @@ static u_int8_t ndpi_grab_dns_name(struct ndpi_packet_struct *packet,
_hostname[j++] = tolower(c);
else {
u_int32_t shift;
shift = ((u_int32_t) 1) << (c & 0x1f);
if((dns_validchar[c >> 5] & shift)) {
_hostname[j++] = tolower(c);
} else {
/* printf("---?? '%c'\n", c); */
hostname_is_valid = 0;
if (ndpi_isprint(c) == 0) {
_hostname[j++] = '?';
} else {
@ -286,7 +286,7 @@ static u_int8_t ndpi_grab_dns_name(struct ndpi_packet_struct *packet,
}
}
}
cl--;
}
}
@ -437,6 +437,11 @@ static int process_answers(struct ndpi_detection_module_struct *ndpi_struct,
packet->current_time_ms/1000,
flow->protos.dns.rsp_addr_ttl[flow->protos.dns.num_rsp_addr]);
if(ndpi_struct->cfg.hostname_dns_check_enabled)
ndpi_cache_hostname_ip(ndpi_struct,
&flow->protos.dns.rsp_addr[flow->protos.dns.num_rsp_addr],
flow->host_server_name);
++flow->protos.dns.num_rsp_addr;
}

View file

@ -1127,10 +1127,36 @@ static void check_content_type_and_change_protocol(struct ndpi_detection_module_
if(ndpi_struct->cfg.http_referer_enabled)
flow->http.referer = ndpi_strndup((const char *)packet->referer_line.ptr, packet->referer_line.len);
if((packet->host_line.ptr != NULL) && (flow->http.host == NULL))
if(ndpi_struct->cfg.http_host_enabled)
if((packet->host_line.ptr != NULL) && (flow->http.host == NULL)) {
if(ndpi_struct->cfg.http_host_enabled) {
flow->http.host = ndpi_strndup((const char *)packet->host_line.ptr, packet->host_line.len);
if(ndpi_struct->cfg.hostname_dns_check_enabled && (flow->http.host != NULL)) {
ndpi_ip_addr_t ip_addr;
memset(&ip_addr, 0, sizeof(ip_addr));
if(packet->iph)
ip_addr.ipv4 = packet->iph->daddr;
else
memcpy(&ip_addr.ipv6, &packet->iphv6->ip6_dst, sizeof(struct ndpi_in6_addr));
if(!ndpi_cache_find_hostname_ip(ndpi_struct, &ip_addr, flow->http.host)) {
#ifdef DEBUG_HTTP
printf("[HTTP] Not found host %s\n", flow->http.host);
#endif
ndpi_set_risk(ndpi_struct, flow, NDPI_UNRESOLVED_HOSTNAME, flow->http.host);
} else {
#ifdef DEBUG_HTTP
printf("[HTTP] Found host %s\n", flow->http.host);
#endif
}
}
}
}
if(packet->content_line.ptr != NULL) {
NDPI_LOG_DBG2(ndpi_struct, "Content Type line found %.*s\n",
packet->content_line.len, packet->content_line.ptr);

View file

@ -2914,6 +2914,30 @@ int processClientServerHello(struct ndpi_detection_module_struct *ndpi_struct,
printf("[TLS] SNI: (NO DGA) [%s]\n", sni);
#endif
}
if(ndpi_struct->cfg.hostname_dns_check_enabled) {
ndpi_ip_addr_t ip_addr;
memset(&ip_addr, 0, sizeof(ip_addr));
if(packet->iph)
ip_addr.ipv4 = packet->iph->daddr;
else
memcpy(&ip_addr.ipv6, &packet->iphv6->ip6_dst,
sizeof(struct ndpi_in6_addr));
if(!ndpi_cache_find_hostname_ip(ndpi_struct, &ip_addr, sni)) {
#ifdef DEBUG_TLS
printf("[TLS] Not found SNI %s\n", sni);
#endif
ndpi_set_risk(ndpi_struct, flow, NDPI_UNRESOLVED_HOSTNAME, sni);
} else {
#ifdef DEBUG_TLS
printf("[TLS] Found SNI %s\n", sni);
#endif
}
}
} else {
#ifdef DEBUG_TLS
printf("[TLS] Extensions server len too short: %u vs %u\n",
@ -3327,7 +3351,7 @@ int processClientServerHello(struct ndpi_detection_module_struct *ndpi_struct,
}
} else if(extension_id == 22) { /* Encrypt-then-MAC */
if(extension_len == 0) {
char *sni = flow->host_server_name;
char *sni = flow->host_server_name;
if(sni != NULL) {
u_int sni_len = strlen(sni);

View file

@ -104,7 +104,7 @@ flow_risks[17] = ProtoField.bool("ndpi.flow_risk.malformed_packet", "Malformed p
flow_risks[18] = ProtoField.bool("ndpi.flow_risk.ssh_obsolete_client", "SSH Obsolete Client Version/Cipher", num_bits_flow_risks, nil, bit(18), "nDPI Flow Risk: SSH Obsolete Client Version/Cipher")
flow_risks[19] = ProtoField.bool("ndpi.flow_risk.ssh_obsolete_server", "SSH Obsolete Server Version/Cipher", num_bits_flow_risks, nil, bit(19), "nDPI Flow Risk: SSH Obsolete Server Version/Cipher")
flow_risks[20] = ProtoField.bool("ndpi.flow_risk.smb_insecure_version", "SMB Insecure Version", num_bits_flow_risks, nil, bit(20), "nDPI Flow Risk: SMB Insecure Version")
flow_risks[21] = ProtoField.bool("ndpi.flow_risk.free_21", "FREE21", num_bits_flow_risks, nil, bit(21), "nDPI Flow Risk: FREE21")
flow_risks[21] = ProtoField.bool("ndpi.flow_risk.unresolved_hostname", "Unresolved Hostname", num_bits_flow_risks, nil, bit(21), "nDPI Flow Risk: Unresolved Hostname")
flow_risks[22] = ProtoField.bool("ndpi.flow_risk.unsafe_protocol", "Unsafe Protocol", num_bits_flow_risks, nil, bit(22), "nDPI Flow Risk: Unsafe Protocol")
flow_risks[23] = ProtoField.bool("ndpi.flow_risk.suspicious_dns_traffic", "Suspicious DNS traffic", num_bits_flow_risks, nil, bit(23), "nDPI Flow Risk: Suspicious DNS traffic")
flow_risks[24] = ProtoField.bool("ndpi.flow_risk.sni_tls_extension_missing", "SNI TLS extension was missing", num_bits_flow_risks, nil, bit(24), "nDPI Flow Risk: SNI TLS extension was missing")