Added ndpi_set_detection_preferences() APi call

This commit is contained in:
Luca Deri
2018-05-14 21:40:27 +02:00
parent 2d486aeed8
commit 1076455c01
15 changed files with 173 additions and 89 deletions
+4 -2
View File
@@ -1458,8 +1458,10 @@ static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle) {
ndpi_thread_info[thread_id].workflow = ndpi_workflow_init(&prefs, pcap_handle); ndpi_thread_info[thread_id].workflow = ndpi_workflow_init(&prefs, pcap_handle);
/* Preferences */ /* Preferences */
ndpi_thread_info[thread_id].workflow->ndpi_struct->http_dont_dissect_response = 0; ndpi_set_detection_preferences(ndpi_thread_info[thread_id].workflow->ndpi_struct,
ndpi_thread_info[thread_id].workflow->ndpi_struct->dns_dissect_response = 0; ndpi_pref_http_dont_dissect_response, 0);
ndpi_set_detection_preferences(ndpi_thread_info[thread_id].workflow->ndpi_struct,
ndpi_pref_dns_dissect_response, 0);
ndpi_workflow_set_flow_detected_callback(ndpi_thread_info[thread_id].workflow, ndpi_workflow_set_flow_detected_callback(ndpi_thread_info[thread_id].workflow,
on_protocol_discovered, (void *)(uintptr_t)thread_id); on_protocol_discovered, (void *)(uintptr_t)thread_id);
+1
View File
@@ -68,3 +68,4 @@ ndpi_check_flow_func
ndpi_fill_protocol_category ndpi_fill_protocol_category
ndpi_load_hostname_category ndpi_load_hostname_category
ndpi_enable_loaded_categories ndpi_enable_loaded_categories
ndpi_set_detection_preferences
+4
View File
@@ -757,6 +757,10 @@ extern "C" {
void ndpi_fill_protocol_category(struct ndpi_detection_module_struct *ndpi_struct, void ndpi_fill_protocol_category(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow, struct ndpi_flow_struct *flow,
ndpi_protocol *ret); ndpi_protocol *ret);
int ndpi_set_detection_preferences(struct ndpi_detection_module_struct *ndpi_mod,
ndpi_detection_preference pref,
int value);
/** /**
* Add a string to match to an automata * Add a string to match to an automata
* *
+11 -3
View File
@@ -797,6 +797,13 @@ typedef enum {
*/ */
} ndpi_protocol_category_t; } ndpi_protocol_category_t;
typedef enum {
ndpi_pref_http_dont_dissect_response = 0,
ndpi_pref_dns_dissect_response,
ndpi_pref_direction_detect_disable,
ndpi_pref_disable_metadata_export
} ndpi_detection_preference;
/* ntop extensions */ /* ntop extensions */
typedef struct ndpi_proto_defaults { typedef struct ndpi_proto_defaults {
char *protoName; char *protoName;
@@ -952,7 +959,9 @@ struct ndpi_detection_module_struct {
ndpi_proto_defaults_t proto_defaults[NDPI_MAX_SUPPORTED_PROTOCOLS+NDPI_MAX_NUM_CUSTOM_PROTOCOLS]; ndpi_proto_defaults_t proto_defaults[NDPI_MAX_SUPPORTED_PROTOCOLS+NDPI_MAX_NUM_CUSTOM_PROTOCOLS];
u_int8_t http_dont_dissect_response:1, dns_dissect_response:1, u_int8_t http_dont_dissect_response:1, dns_dissect_response:1,
direction_detect_disable:1; /* disable internal detection of packet direction */ direction_detect_disable:1, /* disable internal detection of packet direction */
disable_metadata_export:1 /* No metadata is exported */
;
void *hyperscan; /* Intel Hyperscan */ void *hyperscan; /* Intel Hyperscan */
}; };
@@ -989,8 +998,7 @@ struct ndpi_flow_struct {
} l4; } l4;
/* /*
Pointer to src or dst Pointer to src or dst that identifies the
that identifies the
server of this connection server of this connection
*/ */
struct ndpi_id_struct *server_id; struct ndpi_id_struct *server_id;
+29
View File
@@ -864,6 +864,35 @@ static void init_string_based_protocols(struct ndpi_detection_module_struct *ndp
/* ******************************************************************** */ /* ******************************************************************** */
int ndpi_set_detection_preferences(struct ndpi_detection_module_struct *ndpi_mod,
ndpi_detection_preference pref,
int value) {
switch(pref) {
case ndpi_pref_http_dont_dissect_response:
ndpi_mod->http_dont_dissect_response = (u_int8_t)value;
break;
case ndpi_pref_dns_dissect_response:
ndpi_mod->dns_dissect_response = (u_int8_t)value;
break;
case ndpi_pref_direction_detect_disable:
ndpi_mod->direction_detect_disable = (u_int8_t)value;
break;
case ndpi_pref_disable_metadata_export:
ndpi_mod->disable_metadata_export = (u_int8_t)value;
break;
default:
return(-1);
}
return(0);
}
/* ******************************************************************** */
/* This function is used to map protocol name and default ports and it MUST /* This function is used to map protocol name and default ports and it MUST
be updated whenever a new protocol is added to NDPI. be updated whenever a new protocol is added to NDPI.
+5 -2
View File
@@ -57,7 +57,8 @@ static u_int8_t is_utp_pkt(const u_int8_t *payload, u_int payload_len) {
return(1); return(1);
} }
static void ndpi_add_connection_as_bittorrent(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow, static void ndpi_add_connection_as_bittorrent(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow,
int bt_offset, int check_hash, int bt_offset, int check_hash,
const u_int8_t save_detection, const u_int8_t encrypted_connection) const u_int8_t save_detection, const u_int8_t encrypted_connection)
{ {
@@ -73,7 +74,9 @@ static void ndpi_add_connection_as_bittorrent(struct ndpi_detection_module_struc
} else } else
bt_hash = (const char*)&flow->packet.payload[28]; bt_hash = (const char*)&flow->packet.payload[28];
if(bt_hash) memcpy(flow->protos.bittorrent.hash, bt_hash, 20); if(!ndpi_struct->disable_metadata_export) {
if(bt_hash) memcpy(flow->protos.bittorrent.hash, bt_hash, 20);
}
} }
ndpi_int_change_protocol(ndpi_struct, flow, NDPI_PROTOCOL_BITTORRENT, NDPI_PROTOCOL_UNKNOWN); ndpi_int_change_protocol(ndpi_struct, flow, NDPI_PROTOCOL_BITTORRENT, NDPI_PROTOCOL_UNKNOWN);
+26 -20
View File
@@ -102,34 +102,40 @@ void ndpi_search_dhcp_udp(struct ndpi_detection_module_struct *ndpi_struct, stru
if(msg_type <= 8) foundValidMsgType = 1; if(msg_type <= 8) foundValidMsgType = 1;
} else if(id == 55 /* Parameter Request List / Fingerprint */) { } else if(id == 55 /* Parameter Request List / Fingerprint */) {
u_int idx, offset = 0; if(!ndpi_struct->disable_metadata_export) {
u_int idx, offset = 0;
for(idx = 0; idx < len && offset < sizeof(flow->protos.dhcp.fingerprint) - 2; idx++) { for(idx = 0; idx < len && offset < sizeof(flow->protos.dhcp.fingerprint) - 2; idx++) {
snprintf((char*)&flow->protos.dhcp.fingerprint[offset], snprintf((char*)&flow->protos.dhcp.fingerprint[offset],
sizeof(flow->protos.dhcp.fingerprint) - offset, sizeof(flow->protos.dhcp.fingerprint) - offset,
"%02X", dhcp->options[i+2+idx] & 0xFF); "%02X", dhcp->options[i+2+idx] & 0xFF);
offset += 2; offset += 2;
}
flow->protos.dhcp.fingerprint[sizeof(flow->protos.dhcp.fingerprint) - 1] = '\0';
} }
flow->protos.dhcp.fingerprint[sizeof(flow->protos.dhcp.fingerprint) - 1] = '\0';
} else if(id == 60 /* Class Identifier */) { } else if(id == 60 /* Class Identifier */) {
char *name = (char*)&dhcp->options[i+2]; if(!ndpi_struct->disable_metadata_export) {
int j = 0; char *name = (char*)&dhcp->options[i+2];
int j = 0;
j = ndpi_min(len, sizeof(flow->protos.dhcp.class_ident)-1); j = ndpi_min(len, sizeof(flow->protos.dhcp.class_ident)-1);
strncpy((char*)flow->protos.dhcp.class_ident, name, j); strncpy((char*)flow->protos.dhcp.class_ident, name, j);
flow->protos.dhcp.class_ident[j] = '\0'; flow->protos.dhcp.class_ident[j] = '\0';
}
} else if(id == 12 /* Host Name */) { } else if(id == 12 /* Host Name */) {
char *name = (char*)&dhcp->options[i+2]; if(!ndpi_struct->disable_metadata_export) {
int j = 0; char *name = (char*)&dhcp->options[i+2];
int j = 0;
#ifdef DHCP_DEBUG #ifdef DHCP_DEBUG
NDPI_LOG_DBG2(ndpi_struct, "[DHCP] '%.*s'\n",name,len); NDPI_LOG_DBG2(ndpi_struct, "[DHCP] '%.*s'\n",name,len);
// while(j < len) { printf( "%c", name[j]); j++; }; printf("\n"); // while(j < len) { printf( "%c", name[j]); j++; }; printf("\n");
#endif #endif
j = ndpi_min(len, sizeof(flow->host_server_name)-1); j = ndpi_min(len, sizeof(flow->host_server_name)-1);
strncpy((char*)flow->host_server_name, name, j); strncpy((char*)flow->host_server_name, name, j);
flow->host_server_name[j] = '\0'; flow->host_server_name[j] = '\0';
}
} }
i += len + 2; i += len + 2;
+17 -8
View File
@@ -156,7 +156,8 @@ static void rtsp_parse_packet_acceptline(struct ndpi_detection_module_struct
} }
#endif #endif
static void setHttpUserAgent(struct ndpi_flow_struct *flow, char *ua) { static void setHttpUserAgent(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow, char *ua) {
if ( !strcmp(ua, "Windows NT 5.0")) ua = "Windows 2000"; if ( !strcmp(ua, "Windows NT 5.0")) ua = "Windows 2000";
else if(!strcmp(ua, "Windows NT 5.1")) ua = "Windows XP"; else if(!strcmp(ua, "Windows NT 5.1")) ua = "Windows XP";
else if(!strcmp(ua, "Windows NT 5.2")) ua = "Windows Server 2003"; else if(!strcmp(ua, "Windows NT 5.2")) ua = "Windows Server 2003";
@@ -170,7 +171,9 @@ static void setHttpUserAgent(struct ndpi_flow_struct *flow, char *ua) {
* https://github.com/ua-parser/uap-core/blob/master/regexes.yaml */ * https://github.com/ua-parser/uap-core/blob/master/regexes.yaml */
//printf("==> %s\n", ua); //printf("==> %s\n", ua);
snprintf((char*)flow->protos.http.detected_os, sizeof(flow->protos.http.detected_os), "%s", ua); if(!ndpi_struct->disable_metadata_export) {
snprintf((char*)flow->protos.http.detected_os, sizeof(flow->protos.http.detected_os), "%s", ua);
}
} }
static void parseHttpSubprotocol(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { static void parseHttpSubprotocol(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) {
@@ -330,7 +333,7 @@ static void check_content_type_and_change_protocol(struct ndpi_detection_module_
} }
if(token) if(token)
setHttpUserAgent(flow, token); setHttpUserAgent(ndpi_struct, flow, token);
} }
} }
} }
@@ -360,14 +363,20 @@ static void check_content_type_and_change_protocol(struct ndpi_detection_module_
NDPI_PROTOCOL_HTTP); NDPI_PROTOCOL_HTTP);
/* Copy result for nDPI apps */ /* Copy result for nDPI apps */
len = ndpi_min(packet->host_line.len, sizeof(flow->host_server_name)-1); if(!ndpi_struct->disable_metadata_export) {
strncpy((char*)flow->host_server_name, (char*)packet->host_line.ptr, len); len = ndpi_min(packet->host_line.len, sizeof(flow->host_server_name)-1);
flow->host_server_name[len] = '\0', flow->server_id = flow->dst; strncpy((char*)flow->host_server_name, (char*)packet->host_line.ptr, len);
flow->host_server_name[len] = '\0';
}
flow->server_id = flow->dst;
if(packet->forwarded_line.ptr) { if(packet->forwarded_line.ptr) {
len = ndpi_min(packet->forwarded_line.len, sizeof(flow->protos.http.nat_ip)-1); len = ndpi_min(packet->forwarded_line.len, sizeof(flow->protos.http.nat_ip)-1);
strncpy((char*)flow->protos.http.nat_ip, (char*)packet->forwarded_line.ptr, len); if(!ndpi_struct->disable_metadata_export) {
flow->protos.http.nat_ip[len] = '\0'; strncpy((char*)flow->protos.http.nat_ip, (char*)packet->forwarded_line.ptr, len);
flow->protos.http.nat_ip[len] = '\0';
}
} }
if(ndpi_struct->http_dont_dissect_response) if(ndpi_struct->http_dont_dissect_response)
+5 -3
View File
@@ -86,9 +86,11 @@ static int ndpi_int_check_mdns_payload(struct ndpi_detection_module_struct
/* printf("==> [%d] %s\n", j, answer); */ /* printf("==> [%d] %s\n", j, answer); */
len = ndpi_min(sizeof(flow->protos.mdns.answer)-1, j); if(!ndpi_struct->disable_metadata_export) {
strncpy(flow->protos.mdns.answer, (const char *)answer, len); len = ndpi_min(sizeof(flow->protos.mdns.answer)-1, j);
flow->protos.mdns.answer[len] = '\0'; strncpy(flow->protos.mdns.answer, (const char *)answer, len);
flow->protos.mdns.answer[len] = '\0';
}
NDPI_LOG_INFO(ndpi_struct, "found MDNS with answer query\n"); NDPI_LOG_INFO(ndpi_struct, "found MDNS with answer query\n");
return 1; return 1;
+10 -4
View File
@@ -119,8 +119,11 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct, struc
NDPI_LOG_INFO(ndpi_struct, "found netbios with questions = 1 and answers = 0, authority = 0 and broadcast \n"); NDPI_LOG_INFO(ndpi_struct, "found netbios with questions = 1 and answers = 0, authority = 0 and broadcast \n");
if(ndpi_netbios_name_interpret((char*)&packet->payload[12], name, sizeof(name)) > 0) if(ndpi_netbios_name_interpret((char*)&packet->payload[12], name, sizeof(name)) > 0) {
snprintf((char*)flow->host_server_name, sizeof(flow->host_server_name)-1, "%s", name); if(!ndpi_struct->disable_metadata_export) {
snprintf((char*)flow->host_server_name, sizeof(flow->host_server_name)-1, "%s", name);
}
}
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow);
return; return;
@@ -336,8 +339,11 @@ void ndpi_search_netbios(struct ndpi_detection_module_struct *ndpi_struct, struc
if(ntohl(get_u_int32_t(packet->payload, 4)) == ntohl(packet->iph->saddr)) { if(ntohl(get_u_int32_t(packet->payload, 4)) == ntohl(packet->iph->saddr)) {
NDPI_LOG_INFO(ndpi_struct, "found netbios with checked ip-address\n"); NDPI_LOG_INFO(ndpi_struct, "found netbios with checked ip-address\n");
if(ndpi_netbios_name_interpret((char*)&packet->payload[12], name, sizeof(name)) > 0) if(ndpi_netbios_name_interpret((char*)&packet->payload[12], name, sizeof(name)) > 0) {
snprintf((char*)flow->host_server_name, sizeof(flow->host_server_name)-1, "%s", name); if(!ndpi_struct->disable_metadata_export) {
snprintf((char*)flow->host_server_name, sizeof(flow->host_server_name)-1, "%s", name);
}
}
ndpi_int_netbios_add_connection(ndpi_struct, flow); ndpi_int_netbios_add_connection(ndpi_struct, flow);
return; return;
+12 -11
View File
@@ -107,20 +107,21 @@ void ndpi_search_quic(struct ndpi_detection_module_struct *ndpi_struct,
sni_offset++; sni_offset++;
if((sni_offset+len) < udp_len) { if((sni_offset+len) < udp_len) {
int max_len = sizeof(flow->host_server_name)-1, j = 0; if(!ndpi_struct->disable_metadata_export) {
int max_len = sizeof(flow->host_server_name)-1, j = 0;
if(len > max_len) len = max_len; if(len > max_len) len = max_len;
while((len > 0) && (sni_offset < udp_len)) { while((len > 0) && (sni_offset < udp_len)) {
flow->host_server_name[j++] = packet->payload[sni_offset]; flow->host_server_name[j++] = packet->payload[sni_offset];
sni_offset++, len--; sni_offset++, len--;
}
ndpi_match_host_subprotocol(ndpi_struct, flow,
(char *)flow->host_server_name,
strlen((const char*)flow->host_server_name),
NDPI_PROTOCOL_QUIC);
} }
ndpi_match_host_subprotocol(ndpi_struct, flow,
(char *)flow->host_server_name,
strlen((const char*)flow->host_server_name),
NDPI_PROTOCOL_QUIC);
} }
break; break;
+14 -8
View File
@@ -54,10 +54,13 @@ void ndpi_search_ssh_tcp(struct ndpi_detection_module_struct *ndpi_struct, struc
if (flow->l4.tcp.ssh_stage == 0) { if (flow->l4.tcp.ssh_stage == 0) {
if (packet->payload_packet_len > 7 && packet->payload_packet_len < 100 if (packet->payload_packet_len > 7 && packet->payload_packet_len < 100
&& memcmp(packet->payload, "SSH-", 4) == 0) { && memcmp(packet->payload, "SSH-", 4) == 0) {
int len = ndpi_min(sizeof(flow->protos.ssh.client_signature)-1, packet->payload_packet_len); if(!ndpi_struct->disable_metadata_export) {
strncpy(flow->protos.ssh.client_signature, (const char *)packet->payload, len); int len = ndpi_min(sizeof(flow->protos.ssh.client_signature)-1, packet->payload_packet_len);
flow->protos.ssh.client_signature[len] = '\0'; strncpy(flow->protos.ssh.client_signature, (const char *)packet->payload, len);
ndpi_ssh_zap_cr(flow->protos.ssh.client_signature, len); flow->protos.ssh.client_signature[len] = '\0';
ndpi_ssh_zap_cr(flow->protos.ssh.client_signature, len);
}
NDPI_LOG_DBG2(ndpi_struct, "ssh stage 0 passed\n"); NDPI_LOG_DBG2(ndpi_struct, "ssh stage 0 passed\n");
flow->l4.tcp.ssh_stage = 1 + packet->packet_direction; flow->l4.tcp.ssh_stage = 1 + packet->packet_direction;
return; return;
@@ -65,10 +68,13 @@ void ndpi_search_ssh_tcp(struct ndpi_detection_module_struct *ndpi_struct, struc
} else if (flow->l4.tcp.ssh_stage == (2 - packet->packet_direction)) { } else if (flow->l4.tcp.ssh_stage == (2 - packet->packet_direction)) {
if (packet->payload_packet_len > 7 && packet->payload_packet_len < 500 if (packet->payload_packet_len > 7 && packet->payload_packet_len < 500
&& memcmp(packet->payload, "SSH-", 4) == 0) { && memcmp(packet->payload, "SSH-", 4) == 0) {
int len = ndpi_min(sizeof(flow->protos.ssh.server_signature)-1, packet->payload_packet_len); if(!ndpi_struct->disable_metadata_export) {
strncpy(flow->protos.ssh.server_signature, (const char *)packet->payload, len); int len = ndpi_min(sizeof(flow->protos.ssh.server_signature)-1, packet->payload_packet_len);
flow->protos.ssh.server_signature[len] = '\0'; strncpy(flow->protos.ssh.server_signature, (const char *)packet->payload, len);
ndpi_ssh_zap_cr(flow->protos.ssh.server_signature, len); flow->protos.ssh.server_signature[len] = '\0';
ndpi_ssh_zap_cr(flow->protos.ssh.server_signature, len);
}
NDPI_LOG_INFO(ndpi_struct, "found ssh\n"); NDPI_LOG_INFO(ndpi_struct, "found ssh\n");
ndpi_int_ssh_add_connection(ndpi_struct, flow); ndpi_int_ssh_add_connection(ndpi_struct, flow);
+9 -5
View File
@@ -225,9 +225,11 @@ int getSSLcertificate(struct ndpi_detection_module_struct *ndpi_struct,
} }
if(num_dots >= 2) { if(num_dots >= 2) {
stripCertificateTrailer(buffer, buffer_len); if(!ndpi_struct->disable_metadata_export) {
snprintf(flow->protos.ssl.server_certificate, stripCertificateTrailer(buffer, buffer_len);
sizeof(flow->protos.ssl.server_certificate), "%s", buffer); snprintf(flow->protos.ssl.server_certificate,
sizeof(flow->protos.ssl.server_certificate), "%s", buffer);
}
return(1 /* Server Certificate */); return(1 /* Server Certificate */);
} }
} }
@@ -289,8 +291,10 @@ int getSSLcertificate(struct ndpi_detection_module_struct *ndpi_struct,
buffer[len] = '\0'; buffer[len] = '\0';
stripCertificateTrailer(buffer, buffer_len); stripCertificateTrailer(buffer, buffer_len);
snprintf(flow->protos.ssl.client_certificate, if(!ndpi_struct->disable_metadata_export) {
sizeof(flow->protos.ssl.client_certificate), "%s", buffer); snprintf(flow->protos.ssl.client_certificate,
sizeof(flow->protos.ssl.client_certificate), "%s", buffer);
}
/* We're happy now */ /* We're happy now */
return(2 /* Client Certificate */); return(2 /* Client Certificate */);
+5 -3
View File
@@ -65,9 +65,11 @@ void ndpi_search_ubntac2(struct ndpi_detection_module_struct *ndpi_struct, struc
version[j] = '\0'; version[j] = '\0';
len = ndpi_min(sizeof(flow->protos.ubntac2.version)-1, j); if(!ndpi_struct->disable_metadata_export) {
strncpy(flow->protos.ubntac2.version, (const char *)version, len); len = ndpi_min(sizeof(flow->protos.ubntac2.version)-1, j);
flow->protos.ubntac2.version[len] = '\0'; strncpy(flow->protos.ubntac2.version, (const char *)version, len);
flow->protos.ubntac2.version[len] = '\0';
}
} }
NDPI_LOG_INFO(ndpi_struct, "UBNT AirControl 2 request\n"); NDPI_LOG_INFO(ndpi_struct, "UBNT AirControl 2 request\n");
+6 -5
View File
@@ -42,14 +42,15 @@ void ndpi_search_whois_das(struct ndpi_detection_module_struct *ndpi_struct, str
u_int max_len = sizeof(flow->host_server_name) - 1; u_int max_len = sizeof(flow->host_server_name) - 1;
u_int i, j; u_int i, j;
for(i=strlen((const char *)flow->host_server_name), j=0; (i<max_len) && (j<packet->payload_packet_len); i++, j++) { if(!ndpi_struct->disable_metadata_export) {
for(i=strlen((const char *)flow->host_server_name), j=0; (i<max_len) && (j<packet->payload_packet_len); i++, j++) {
if((packet->payload[j] == '\n') || (packet->payload[j] == '\r')) break;
flow->host_server_name[i] = packet->payload[j];
}
if((packet->payload[j] == '\n') || (packet->payload[j] == '\r')) break; flow->host_server_name[i] = '\0';
flow->host_server_name[i] = packet->payload[j];
} }
flow->host_server_name[i] = '\0';
flow->server_id = ((sport == 43) || (sport == 4343)) ? flow->src : flow->dst; flow->server_id = ((sport == 43) || (sport == 4343)) ? flow->src : flow->dst;
NDPI_LOG_INFO(ndpi_struct, "[WHOIS/DAS] %s\n", flow->host_server_name); NDPI_LOG_INFO(ndpi_struct, "[WHOIS/DAS] %s\n", flow->host_server_name);