Added extension to detect nested subdomains as used in Browsertunnel attack tool

https://github.com/veggiedefender/browsertunnel
This commit is contained in:
Luca Deri
2020-09-09 23:25:19 +02:00
parent 842c00e681
commit 7086197047
10 changed files with 61 additions and 48 deletions
+2 -2
View File
@@ -3327,10 +3327,10 @@ static void dgaUnitTest() {
assert(ndpi_str != NULL); assert(ndpi_str != NULL);
for(i=0; dga[i] != NULL; i++) for(i=0; dga[i] != NULL; i++)
assert(ndpi_check_dga_name(ndpi_str, NULL, (char*)dga[i]) == 1); assert(ndpi_check_dga_name(ndpi_str, NULL, (char*)dga[i], 1) == 1);
for(i=0; non_dga[i] != NULL; i++) for(i=0; non_dga[i] != NULL; i++)
assert(ndpi_check_dga_name(ndpi_str, NULL, (char*)non_dga[i]) == 0); assert(ndpi_check_dga_name(ndpi_str, NULL, (char*)non_dga[i], 1) == 0);
ndpi_exit_detection_module(ndpi_str); ndpi_exit_detection_module(ndpi_str);
} }
+1 -1
View File
@@ -945,7 +945,7 @@ extern "C" {
/* DGA */ /* DGA */
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); char *name, u_int8_t is_hostname);
/* Serializer */ /* Serializer */
int ndpi_init_serializer_ll(ndpi_serializer *serializer, ndpi_serialization_format fmt, int ndpi_init_serializer_ll(ndpi_serializer *serializer, ndpi_serialization_format fmt,
+48 -36
View File
@@ -6665,10 +6665,10 @@ static int enough(int a, int b) {
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) { char *name, u_int8_t is_hostname) {
int len, rc = 0; int len, rc = 0;
u_int8_t max_num_char_repetitions = 0, last_char = 0, num_char_repetitions = 0; u_int8_t max_num_char_repetitions = 0, last_char = 0, num_char_repetitions = 0, num_dots = 0;
u_int8_t max_domain_element_len = 0, curr_domain_element_len = 0; u_int8_t max_domain_element_len = 0, curr_domain_element_len = 0, first_element_is_numeric = 1;
len = strlen(name); len = strlen(name);
@@ -6685,45 +6685,52 @@ int ndpi_check_dga_name(struct ndpi_detection_module_struct *ndpi_str,
} }
for(i=0, j=0; (i<len) && (j<(sizeof(tmp)-1)); i++) { for(i=0, j=0; (i<len) && (j<(sizeof(tmp)-1)); i++) {
tmp[j] = tolower(name[i]); tmp[j] = tolower(name[i]);
if(last_char == tmp[j]) { if(tmp[j] == '.')
if(++num_char_repetitions > max_num_char_repetitions) num_dots++;
max_num_char_repetitions = num_char_repetitions; else if(num_dots == 0) {
} else if(!isnumber(tmp[j]))
num_char_repetitions = 1, last_char = tmp[j]; first_element_is_numeric = 0;
}
if(last_char == tmp[j]) {
if(++num_char_repetitions > max_num_char_repetitions)
max_num_char_repetitions = num_char_repetitions;
} else
num_char_repetitions = 1, last_char = tmp[j];
switch(tmp[j]) { switch(tmp[j]) {
case '.': case '.':
case '-': case '-':
case '_': case '_':
case '/': case '/':
case ')': case ')':
case '(': case '(':
case ';': case ';':
case ':': case ':':
case '[': case '[':
case ']': case ']':
case ' ': case ' ':
/* /*
Domain/word separator chars Domain/word separator chars
NOTE: NOTE:
this function is used also to detect other type of issues this function is used also to detect other type of issues
such as invalid/suspiciuous user agent such as invalid/suspiciuous user agent
*/ */
if(curr_domain_element_len > max_domain_element_len) if(curr_domain_element_len > max_domain_element_len)
max_domain_element_len = curr_domain_element_len; max_domain_element_len = curr_domain_element_len;
curr_domain_element_len = 0; curr_domain_element_len = 0;
break; break;
default: default:
curr_domain_element_len++; curr_domain_element_len++;
break; break;
} }
j++; j++;
} }
if(curr_domain_element_len > max_domain_element_len) if(curr_domain_element_len > max_domain_element_len)
@@ -6735,7 +6742,12 @@ int ndpi_check_dga_name(struct ndpi_detection_module_struct *ndpi_str,
#endif #endif
if( if(
(max_num_char_repetitions > 5 /* num or consecutive repeated chars */) (is_hostname
&& (num_dots > 5)
&& (!first_element_is_numeric)
&& (strstr(tmp, "in-addr.arpa") == NULL)
)
|| (max_num_char_repetitions > 5 /* num or consecutive repeated chars */)
/* /*
In case of a name with too many consecutive chars an alert is triggered In case of a name with too many consecutive chars an alert is triggered
This is the case for instance of the wildcard DNS query used by NetBIOS This is the case for instance of the wildcard DNS query used by NetBIOS
+1 -1
View File
@@ -301,7 +301,7 @@ static void ndpi_search_dns(struct ndpi_detection_module_struct *ndpi_struct, st
if(j > 0) { if(j > 0) {
ndpi_protocol_match_result ret_match; ndpi_protocol_match_result ret_match;
ndpi_check_dga_name(ndpi_struct, flow, (char*)flow->host_server_name); ndpi_check_dga_name(ndpi_struct, flow, (char*)flow->host_server_name, 1);
ret.app_protocol = ndpi_match_host_subprotocol(ndpi_struct, flow, ret.app_protocol = ndpi_match_host_subprotocol(ndpi_struct, flow,
(char *)flow->host_server_name, (char *)flow->host_server_name,
+2 -5
View File
@@ -263,15 +263,12 @@ static void ndpi_check_user_agent(struct ndpi_detection_module_struct *ndpi_stru
char *ua) { char *ua) {
if((!ua) || (ua[0] == '\0')) return; if((!ua) || (ua[0] == '\0')) return;
// printf("***** [%s:%d] ==> '%s'\n", __FILE__, __LINE__, ua);
// printf("***** %u\n", ndpi_check_dga_name(ndpi_struct, NULL, "uclient-fetch]"));
if((strlen(ua) < 4) if((strlen(ua) < 4)
|| (!strncmp(ua, "test", 4)) || (!strncmp(ua, "test", 4))
|| (!strncmp(ua, "<?", 2)) || (!strncmp(ua, "<?", 2))
|| strchr(ua, '{') || strchr(ua, '{')
|| strchr(ua, '}') || strchr(ua, '}')
|| ndpi_check_dga_name(ndpi_struct, NULL, ua) || ndpi_check_dga_name(ndpi_struct, NULL, ua, 0)
// || ndpi_match_bigram(ndpi_struct, &ndpi_struct->impossible_bigrams_automa, ua) // || ndpi_match_bigram(ndpi_struct, &ndpi_struct->impossible_bigrams_automa, ua)
) { ) {
NDPI_SET_BIT(flow->risk, NDPI_HTTP_SUSPICIOUS_USER_AGENT); NDPI_SET_BIT(flow->risk, NDPI_HTTP_SUSPICIOUS_USER_AGENT);
@@ -438,7 +435,7 @@ static void check_content_type_and_change_protocol(struct ndpi_detection_module_
flow->host_server_name[len] = '\0'; flow->host_server_name[len] = '\0';
flow->extra_packets_func = NULL; /* We're good now */ flow->extra_packets_func = NULL; /* We're good now */
if(len > 0) ndpi_check_dga_name(ndpi_struct, flow, (char*)flow->host_server_name); if(len > 0) ndpi_check_dga_name(ndpi_struct, flow, (char*)flow->host_server_name, 1);
flow->server_id = flow->dst; flow->server_id = flow->dst;
if(packet->forwarded_line.ptr) { if(packet->forwarded_line.ptr) {
+1 -1
View File
@@ -85,7 +85,7 @@ static void ndpi_int_netbios_add_connection(struct ndpi_detection_module_struct
flow->packet.payload_packet_len - off, name, sizeof(name)) > 0) { flow->packet.payload_packet_len - off, name, sizeof(name)) > 0) {
snprintf((char*)flow->host_server_name, sizeof(flow->host_server_name)-1, "%s", name); snprintf((char*)flow->host_server_name, sizeof(flow->host_server_name)-1, "%s", name);
ndpi_check_dga_name(ndpi_struct, flow, (char*)flow->host_server_name); ndpi_check_dga_name(ndpi_struct, flow, (char*)flow->host_server_name, 1);
} }
if(sub_protocol == NDPI_PROTOCOL_UNKNOWN) if(sub_protocol == NDPI_PROTOCOL_UNKNOWN)
+2 -1
View File
@@ -1161,7 +1161,8 @@ int processClientServerHello(struct ndpi_detection_module_struct *ndpi_struct,
flow->l4.tcp.tls.subprotocol_detected = 1; flow->l4.tcp.tls.subprotocol_detected = 1;
} }
ndpi_check_dga_name(ndpi_struct, flow, flow->protos.stun_ssl.ssl.client_requested_server_name); ndpi_check_dga_name(ndpi_struct, flow,
flow->protos.stun_ssl.ssl.client_requested_server_name, 1);
} else { } else {
#ifdef DEBUG_TLS #ifdef DEBUG_TLS
printf("[TLS] Extensions server len too short: %u vs %u\n", printf("[TLS] Extensions server len too short: %u vs %u\n",
+1 -1
View File
@@ -48,7 +48,7 @@ int ndpi_is_tls_tor(struct ndpi_detection_module_struct *ndpi_struct,
if((dot = strrchr(dummy, '.')) == NULL) return(0); if((dot = strrchr(dummy, '.')) == NULL) return(0);
name = &dot[1]; name = &dot[1];
if(ndpi_check_dga_name(ndpi_struct, flow, name)) { if(ndpi_check_dga_name(ndpi_struct, flow, name, 1)) {
ndpi_int_tor_add_connection(ndpi_struct, flow); ndpi_int_tor_add_connection(ndpi_struct, flow);
return(1); return(1);
} else { } else {
Binary file not shown.
@@ -0,0 +1,3 @@
Google 2 262 1
1 UDP 192.168.1.168:65311 <-> 8.8.8.8:53 [proto: 5.126/DNS.Google][cat: Web/5][1 pkts/103 bytes <-> 1 pkts/159 bytes][Goodput ratio: 59/73][0.02 sec][Host: gmr02c.16.0.fhkfhsdkfhsk.tunnel.example.com][::][Risk: ** Suspicious DGA domain name **][PLAIN TEXT (fhkfhsdkfhsk)][Plen Bins: 0,50,0,50,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]