Added the ability to define a custom DGA detection function by overwriting

the value of the function pointer ndpi_dga_function curently set to NULL
(that means the nDPI internal DGA function will be used)
This commit is contained in:
Luca Deri
2021-03-08 22:57:30 +01:00
parent c3490e80a7
commit bb6423a79f
3 changed files with 267 additions and 243 deletions
+5
View File
@@ -47,6 +47,11 @@ extern "C" {
#define SAVE_DETECTION_BITMASK_AS_UNKNOWN 1 #define SAVE_DETECTION_BITMASK_AS_UNKNOWN 1
#define NO_SAVE_DETECTION_BITMASK_AS_UNKNOWN 0 #define NO_SAVE_DETECTION_BITMASK_AS_UNKNOWN 0
/*
In case a custom DGA function is used, the fucntion
below must be overwritten,
*/
extern ndpi_custom_dga_predict_fctn ndpi_dga_function;
/** /**
* Check if a string is encoded with punycode * Check if a string is encoded with punycode
+3
View File
@@ -1662,6 +1662,9 @@ struct ndpi_hw_struct {
double *s; double *s;
}; };
/* Prototype used to define custom DGA detection function */
typedef int (*ndpi_custom_dga_predict_fctn)(const char* domain, int domain_length);
/* **************************************** */ /* **************************************** */
#endif /* __NDPI_TYPEDEFS_H__ */ #endif /* __NDPI_TYPEDEFS_H__ */
+16
View File
@@ -84,6 +84,10 @@ static int removeDefaultPort(ndpi_port_range *range, ndpi_proto_defaults_t *def,
/* ****************************************** */ /* ****************************************** */
ndpi_custom_dga_predict_fctn ndpi_dga_function = NULL;
/* ****************************************** */
static inline uint8_t flow_is_proto(struct ndpi_flow_struct *flow, u_int16_t p) { static inline uint8_t flow_is_proto(struct ndpi_flow_struct *flow, u_int16_t p) {
return((flow->detected_protocol_stack[0] == p) || (flow->detected_protocol_stack[1] == p)); return((flow->detected_protocol_stack[0] == p) || (flow->detected_protocol_stack[1] == p));
} }
@@ -7311,6 +7315,17 @@ uint8_t ndpi_connection_tracking(struct ndpi_detection_module_struct *ndpi_str,
int ndpi_check_dga_name(struct ndpi_detection_module_struct *ndpi_str, int ndpi_check_dga_name(struct ndpi_detection_module_struct *ndpi_str,
struct ndpi_flow_struct *flow, struct ndpi_flow_struct *flow,
char *name, u_int8_t is_hostname) { char *name, u_int8_t is_hostname) {
if(ndpi_dga_function != NULL) {
/* A custom DGA function is defined */
int rc = ndpi_dga_function(name, is_hostname);
if(rc) {
if(flow)
ndpi_set_risk(flow, NDPI_SUSPICIOUS_DGA_DOMAIN);
}
return(1);
} else {
int len, rc = 0, trigram_char_skip = 0; int len, rc = 0, trigram_char_skip = 0;
u_int8_t max_num_char_repetitions = 0, last_char = 0, num_char_repetitions = 0, num_dots = 0, num_trigram_dots = 0; u_int8_t max_num_char_repetitions = 0, last_char = 0, num_char_repetitions = 0, num_dots = 0, num_trigram_dots = 0;
u_int8_t max_domain_element_len = 0, curr_domain_element_len = 0, first_element_is_numeric = 1; u_int8_t max_domain_element_len = 0, curr_domain_element_len = 0, first_element_is_numeric = 1;
@@ -7574,5 +7589,6 @@ uint8_t ndpi_connection_tracking(struct ndpi_detection_module_struct *ndpi_str,
return(rc); return(rc);
} }
}
/* ******************************************************************** */ /* ******************************************************************** */