Implemented EDNS(0) support in DNS dissector

Improved DNS dissection
This commit is contained in:
Luca Deri
2022-12-29 19:38:25 +01:00
parent 560280e6f0
commit 8f91b8ba72
6 changed files with 191 additions and 56 deletions
+1 -1
View File
@@ -1381,7 +1381,7 @@ struct ndpi_flow_struct {
/* the only fields useful for nDPI and ntopng */ /* the only fields useful for nDPI and ntopng */
struct { struct {
u_int8_t num_queries, num_answers, reply_code, is_query; u_int8_t num_queries, num_answers, reply_code, is_query;
u_int16_t query_type, query_class, rsp_type; u_int16_t query_type, query_class, rsp_type, edns0_udp_payload_size;
ndpi_ip_addr_t rsp_addr; /* The first address in a DNS response packet (A and AAAA) */ ndpi_ip_addr_t rsp_addr; /* The first address in a DNS response packet (A and AAAA) */
char ptr_domain_name[64 /* large enough but smaller than { } tls */]; char ptr_domain_name[64 /* large enough but smaller than { } tls */];
} dns; } dns;
+3 -1
View File
@@ -6447,7 +6447,9 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
struct ndpi_packet_struct *packet; struct ndpi_packet_struct *packet;
NDPI_SELECTION_BITMASK_PROTOCOL_SIZE ndpi_selection_packet; NDPI_SELECTION_BITMASK_PROTOCOL_SIZE ndpi_selection_packet;
u_int32_t num_calls = 0; u_int32_t num_calls = 0;
ndpi_protocol ret = { 0 }; ndpi_protocol ret;
memset(&ret, 0, sizeof(ret));
if(!flow || !ndpi_str) if(!flow || !ndpi_str)
return(ret); return(ret);
+4 -2
View File
@@ -2355,8 +2355,9 @@ static u_int8_t ndpi_check_hostname_risk_exception(struct ndpi_detection_module_
if(automa->ac_automa) { if(automa->ac_automa) {
AC_TEXT_t ac_input_text; AC_TEXT_t ac_input_text;
AC_REP_t match = {0}; AC_REP_t match;
memset(&match, 0, sizeof(match));
ac_input_text.astring = hostname, ac_input_text.length = strlen(hostname); ac_input_text.astring = hostname, ac_input_text.length = strlen(hostname);
ac_input_text.option = 0; ac_input_text.option = 0;
@@ -2666,8 +2667,9 @@ u_int8_t is_a_common_alpn(struct ndpi_detection_module_struct *ndpi_str,
if(automa->ac_automa) { if(automa->ac_automa) {
AC_TEXT_t ac_input_text; AC_TEXT_t ac_input_text;
AC_REP_t match = {0}; AC_REP_t match;
memset(&match, 0, sizeof(match));
ac_input_text.astring = (char*)alpn_to_check, ac_input_text.length = alpn_to_check_len; ac_input_text.astring = (char*)alpn_to_check, ac_input_text.length = alpn_to_check_len;
ac_input_text.option = 0; ac_input_text.option = 0;
+166 -35
View File
@@ -29,7 +29,7 @@
#define FLAGS_MASK 0x8000 #define FLAGS_MASK 0x8000
// #define DNS_DEBUG 1 /* #define DNS_DEBUG 1 */
#define DNS_PORT 53 #define DNS_PORT 53
#define LLMNR_PORT 5355 #define LLMNR_PORT 5355
@@ -353,24 +353,43 @@ static int search_valid_dns(struct ndpi_detection_module_struct *ndpi_struct,
) { ) {
/* This is a good reply: we dissect it both for request and response */ /* This is a good reply: we dissect it both for request and response */
/* Leave the statement below commented necessary in case of call to ndpi_get_partial_detection() */ if(dns_header->num_queries > 0) {
x++; u_int16_t rsp_type;
u_int16_t num;
if(x < packet->payload_packet_len && packet->payload[x] != '\0') { for(num = 0; num < dns_header->num_queries; num++) {
while((x < packet->payload_packet_len) u_int16_t data_len;
&& (packet->payload[x] != '\0')) {
x++; if((x+6) >= packet->payload_packet_len) {
break;
}
if((data_len = getNameLength(x, packet->payload,
packet->payload_packet_len)) == 0) {
break;
} else
x += data_len;
if((x+8) >= packet->payload_packet_len) {
break;
}
rsp_type = get16(&x, packet->payload);
#ifdef DNS_DEBUG
printf("[DNS] [response (query)] response_type=%d\n", rsp_type);
#endif
/* here x points to the response "class" field */
x += 2; /* Skip class */
} }
x++;
} }
x += 4;
if(dns_header->num_answers > 0) { if(dns_header->num_answers > 0) {
u_int16_t rsp_type; u_int16_t rsp_type;
u_int32_t rsp_ttl; u_int32_t rsp_ttl;
u_int16_t num; u_int16_t num;
u_int8_t found = 0;
for(num = 0; num < dns_header->num_answers; num++) { for(num = 0; num < dns_header->num_answers; num++) {
u_int16_t data_len; u_int16_t data_len;
@@ -400,54 +419,164 @@ static int search_valid_dns(struct ndpi_detection_module_struct *ndpi_struct,
printf("[DNS] [response] response_type=%d\n", rsp_type); printf("[DNS] [response] response_type=%d\n", rsp_type);
#endif #endif
ndpi_check_dns_type(ndpi_struct, flow, rsp_type); if(found == 0) {
ndpi_check_dns_type(ndpi_struct, flow, rsp_type);
flow->protos.dns.rsp_type = rsp_type;
}
flow->protos.dns.rsp_type = rsp_type; /* x points to the response "class" field */
/* here x points to the response "class" field */
if((x+12) <= packet->payload_packet_len) { if((x+12) <= packet->payload_packet_len) {
x += 6; x += 6;
data_len = get16(&x, packet->payload); data_len = get16(&x, packet->payload);
if((x + data_len) <= packet->payload_packet_len) { if((x + data_len) <= packet->payload_packet_len) {
// printf("[rsp_type: %u][data_len: %u]\n", rsp_type, data_len); #ifdef DNS_DEBUG
printf("[DNS] [rsp_type: %u][data_len: %u]\n", rsp_type, data_len);
#endif
if(rsp_type == 0x05 /* CNAME */) { if(rsp_type == 0x05 /* CNAME */) {
x += data_len; ;
continue; /* Skip CNAME */ } else if(rsp_type == 0x0C /* PTR */) {
}
if(rsp_type == 0x0C /* PTR */) {
u_int16_t ptr_len = (packet->payload[x-2] << 8) + packet->payload[x-1]; u_int16_t ptr_len = (packet->payload[x-2] << 8) + packet->payload[x-1];
if((x + ptr_len) <= packet->payload_packet_len) { if((x + ptr_len) <= packet->payload_packet_len) {
u_int len; if(found == 0) {
u_int len;
ndpi_grab_dns_name(packet, &x, ndpi_grab_dns_name(packet, &x,
flow->protos.dns.ptr_domain_name, flow->protos.dns.ptr_domain_name,
sizeof(flow->protos.dns.ptr_domain_name), &len); sizeof(flow->protos.dns.ptr_domain_name), &len);
found = 1;
}
} }
} else if((((rsp_type == 0x1) && (data_len == 4)) /* A */ } else if((((rsp_type == 0x1) && (data_len == 4)) /* A */
|| ((rsp_type == 0x1c) && (data_len == 16)) /* AAAA */ || ((rsp_type == 0x1c) && (data_len == 16)) /* AAAA */
)) { )) {
memcpy(&flow->protos.dns.rsp_addr, packet->payload + x, data_len); if(found == 0) {
memcpy(&flow->protos.dns.rsp_addr, packet->payload + x, data_len);
found = 1;
}
} }
x += data_len;
} }
} }
break; if(found && (dns_header->additional_rrs == 0)) {
/*
In case we have RR we need to iterate
all the answers and not just consider the
first one as we need to properly move 'x'
to the right offset
*/
break;
}
} }
} }
if((flow->detected_protocol_stack[0] == NDPI_PROTOCOL_DNS) if(dns_header->additional_rrs > 0) {
|| (flow->detected_protocol_stack[1] == NDPI_PROTOCOL_DNS)) { /*
/* Request already set the protocol */ Dissect the rest of the packet only if there are
// flow->extra_packets_func = NULL; /* Removed so the caller can keep dissecting DNS flows */ additional_rrs as we need to check fo EDNS(0)
} else {
/* We missed the request */
u_int16_t s_port = packet->udp ? ntohs(packet->udp->source) : ntohs(packet->tcp->source);
ndpi_set_detected_protocol(ndpi_struct, flow, checkPort(s_port), NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI); In this case we need to go through the whole packet
as we need to update the 'x' offset
*/
if(dns_header->authority_rrs > 0) {
u_int16_t rsp_type;
u_int32_t rsp_ttl;
u_int16_t num;
for(num = 0; num < dns_header->authority_rrs; num++) {
u_int16_t data_len;
if((x+6) >= packet->payload_packet_len) {
break;
}
if((data_len = getNameLength(x, packet->payload,
packet->payload_packet_len)) == 0) {
break;
} else
x += data_len;
if((x+8) >= packet->payload_packet_len) {
break;
}
rsp_type = get16(&x, packet->payload);
rsp_ttl = ntohl(*((u_int32_t*)&packet->payload[x+2]));
#ifdef DNS_DEBUG
printf("[DNS] [RRS response] response_type=%d\n", rsp_type);
#endif
/* here x points to the response "class" field */
if((x+12) <= packet->payload_packet_len) {
x += 6;
data_len = get16(&x, packet->payload);
if((x + data_len) <= packet->payload_packet_len)
x += data_len;
}
}
}
if(dns_header->additional_rrs > 0) {
u_int16_t rsp_type;
u_int16_t num;
for(num = 0; num < dns_header->additional_rrs; num++) {
u_int16_t data_len;
if((x+6) > packet->payload_packet_len) {
break;
}
if((data_len = getNameLength(x, packet->payload, packet->payload_packet_len)) == 0) {
break;
} else
x += data_len;
if((x+10) > packet->payload_packet_len) {
break;
}
rsp_type = get16(&x, packet->payload);
#ifdef DNS_DEBUG
printf("[DNS] [RR response] response_type=%d\n", rsp_type);
#endif
if(rsp_type == 41 /* OPT */) {
/* https://en.wikipedia.org/wiki/Extension_Mechanisms_for_DNS */
flow->protos.dns.edns0_udp_payload_size = ntohs(*((u_int16_t*)&packet->payload[x])); /* EDNS(0) */
#ifdef DNS_DEBUG
printf("[DNS] [response] edns0_udp_payload_size: %u\n", flow->protos.dns.edns0_udp_payload_size);
#endif
x += 6;
} else {
x += 6;
}
if((data_len = getNameLength(x, packet->payload, packet->payload_packet_len)) == 0) {
break;
} else
x += data_len;
}
}
if((flow->detected_protocol_stack[0] == NDPI_PROTOCOL_DNS)
|| (flow->detected_protocol_stack[1] == NDPI_PROTOCOL_DNS)) {
/* Request already set the protocol */
// flow->extra_packets_func = NULL; /* Removed so the caller can keep dissecting DNS flows */
} else {
/* We missed the request */
u_int16_t s_port = packet->udp ? ntohs(packet->udp->source) : ntohs(packet->tcp->source);
ndpi_set_detected_protocol(ndpi_struct, flow, checkPort(s_port), NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI);
}
} }
} }
} }
@@ -652,7 +781,9 @@ static void ndpi_search_dns(struct ndpi_detection_module_struct *ndpi_struct, st
|| (flow->detected_protocol_stack[1] == NDPI_PROTOCOL_DNS)) { || (flow->detected_protocol_stack[1] == NDPI_PROTOCOL_DNS)) {
/* TODO: add support to RFC6891 to avoid some false positives */ /* TODO: add support to RFC6891 to avoid some false positives */
if((packet->udp != NULL) if((packet->udp != NULL)
&& (packet->payload_packet_len > PKT_LEN_ALERT)) { && (packet->payload_packet_len > PKT_LEN_ALERT)
&& (packet->payload_packet_len > flow->protos.dns.edns0_udp_payload_size)
) {
char str[48]; char str[48];
snprintf(str, sizeof(str), "%u Bytes DNS Packet", packet->payload_packet_len); snprintf(str, sizeof(str), "%u Bytes DNS Packet", packet->payload_packet_len);
+1 -1
View File
@@ -96,7 +96,7 @@ SIP 85 39540 15
60 UDP 192.168.1.2:2810 -> 192.168.1.1:53 [proto: 5/DNS][IP: 0/Unknown][ClearText][Confidence: DPI][cat: Network/14][3 pkts/258 bytes -> 0 pkts/0 bytes][Goodput ratio: 51/0][4.01 sec][Hostname/SNI: _sip._udp.sip.nybercity.dk][::][Risk: ** Unidirectional Traffic **][Risk Score: 10][Risk Info: No server to client traffic][PLAIN TEXT (Mybercity)][Plen Bins: 0,100,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,0,0] 60 UDP 192.168.1.2:2810 -> 192.168.1.1:53 [proto: 5/DNS][IP: 0/Unknown][ClearText][Confidence: DPI][cat: Network/14][3 pkts/258 bytes -> 0 pkts/0 bytes][Goodput ratio: 51/0][4.01 sec][Hostname/SNI: _sip._udp.sip.nybercity.dk][::][Risk: ** Unidirectional Traffic **][Risk Score: 10][Risk Info: No server to client traffic][PLAIN TEXT (Mybercity)][Plen Bins: 0,100,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,0,0]
61 UDP 192.168.1.2:2814 -> 192.168.1.1:53 [proto: 5/DNS][IP: 0/Unknown][ClearText][Confidence: DPI][cat: Network/14][3 pkts/258 bytes -> 0 pkts/0 bytes][Goodput ratio: 51/0][9.01 sec][Hostname/SNI: _sib._udp.sip.cybercity.dk][::][Risk: ** Malformed Packet **** Unidirectional Traffic **][Risk Score: 20][Risk Info: No server to client traffic / Invalid DNS Query Lenght][PLAIN TEXT (cybercity)][Plen Bins: 0,100,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,0,0] 61 UDP 192.168.1.2:2814 -> 192.168.1.1:53 [proto: 5/DNS][IP: 0/Unknown][ClearText][Confidence: DPI][cat: Network/14][3 pkts/258 bytes -> 0 pkts/0 bytes][Goodput ratio: 51/0][9.01 sec][Hostname/SNI: _sib._udp.sip.cybercity.dk][::][Risk: ** Malformed Packet **** Unidirectional Traffic **][Risk Score: 20][Risk Info: No server to client traffic / Invalid DNS Query Lenght][PLAIN TEXT (cybercity)][Plen Bins: 0,100,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,0,0]
62 UDP 192.168.1.2:138 -> 192.168.1.251:138 [proto: 10.16/NetBIOS.SMBv1][IP: 0/Unknown][ClearText][Confidence: DPI][cat: System/18][1 pkts/243 bytes -> 0 pkts/0 bytes][Goodput ratio: 82/0][< 1 sec][Risk: ** Unsafe Protocol **** Unidirectional Traffic **][Risk Score: 20][Risk Info: No server to client traffic][Plen Bins: 0,0,0,0,0,0,100,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] 62 UDP 192.168.1.2:138 -> 192.168.1.251:138 [proto: 10.16/NetBIOS.SMBv1][IP: 0/Unknown][ClearText][Confidence: DPI][cat: System/18][1 pkts/243 bytes -> 0 pkts/0 bytes][Goodput ratio: 82/0][< 1 sec][Risk: ** Unsafe Protocol **** Unidirectional Traffic **][Risk Score: 20][Risk Info: No server to client traffic][Plen Bins: 0,0,0,0,0,0,100,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]
63 UDP 192.168.1.2:2719 <-> 192.168.1.1:53 [proto: 5/DNS][IP: 0/Unknown][ClearText][Confidence: DPI][cat: Network/14][1 pkts/75 bytes <-> 1 pkts/168 bytes][Goodput ratio: 43/75][1.01 sec][147.234.1.253][Risk: ** Malformed Packet **][Risk Score: 10][Risk Info: Invalid DNS Query Lenght][PLAIN TEXT (ecitele)][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] 63 UDP 192.168.1.2:2719 <-> 192.168.1.1:53 [proto: 5/DNS][IP: 0/Unknown][ClearText][Confidence: DPI][cat: Network/14][1 pkts/75 bytes <-> 1 pkts/168 bytes][Goodput ratio: 43/75][1.01 sec][::][Risk: ** Malformed Packet **][Risk Score: 10][Risk Info: Invalid DNS Query Lenght][PLAIN TEXT (ecitele)][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]
64 UDP 192.168.1.41:138 -> 192.168.1.255:394 [proto: 10/NetBIOS][IP: 0/Unknown][ClearText][Confidence: Match by port][cat: System/18][1 pkts/243 bytes -> 0 pkts/0 bytes][Goodput ratio: 82/0][< 1 sec][PLAIN TEXT (MEBECDBDBDBCACACACACACACACACACA)][Plen Bins: 0,0,0,0,0,0,100,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] 64 UDP 192.168.1.41:138 -> 192.168.1.255:394 [proto: 10/NetBIOS][IP: 0/Unknown][ClearText][Confidence: Match by port][cat: System/18][1 pkts/243 bytes -> 0 pkts/0 bytes][Goodput ratio: 82/0][< 1 sec][PLAIN TEXT (MEBECDBDBDBCACACACACACACACACACA)][Plen Bins: 0,0,0,0,0,0,100,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]
65 UDP 81.168.1.2:30000 -> 212.242.33.36:40392 [proto: 87/RTP][IP: 0/Unknown][ClearText][Confidence: DPI][cat: Media/1][1 pkts/214 bytes -> 0 pkts/0 bytes][Goodput ratio: 80/0][< 1 sec][RTP Stream Type: audio][Plen Bins: 0,0,0,0,0,100,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] 65 UDP 81.168.1.2:30000 -> 212.242.33.36:40392 [proto: 87/RTP][IP: 0/Unknown][ClearText][Confidence: DPI][cat: Media/1][1 pkts/214 bytes -> 0 pkts/0 bytes][Goodput ratio: 80/0][< 1 sec][RTP Stream Type: audio][Plen Bins: 0,0,0,0,0,100,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]
66 UDP 192.168.1.2:30000 -> 37.115.0.36:40392 [proto: 87/RTP][IP: 0/Unknown][ClearText][Confidence: DPI][cat: Media/1][1 pkts/214 bytes -> 0 pkts/0 bytes][Goodput ratio: 80/0][< 1 sec][RTP Stream Type: audio][PLAIN TEXT (njlndlj)][Plen Bins: 0,0,0,0,0,100,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] 66 UDP 192.168.1.2:30000 -> 37.115.0.36:40392 [proto: 87/RTP][IP: 0/Unknown][ClearText][Confidence: DPI][cat: Media/1][1 pkts/214 bytes -> 0 pkts/0 bytes][Goodput ratio: 80/0][< 1 sec][RTP Stream Type: audio][PLAIN TEXT (njlndlj)][Plen Bins: 0,0,0,0,0,100,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]
+1 -1
View File
@@ -22,4 +22,4 @@ Patricia protocols: 2/0 (search/found)
DNS 6 5860 1 DNS 6 5860 1
1 UDP 127.0.0.1:50435 <-> 127.0.0.1:53 [proto: 5/DNS][IP: 0/Unknown][ClearText][Confidence: DPI][cat: Network/14][2 pkts/140 bytes <-> 4 pkts/5720 bytes][Goodput ratio: 40/97][5.03 sec][Hostname/SNI: www.xt.com][0.0.0.0][bytes ratio: -0.952 (Download)][IAT c2s/s2c min/avg/max/stddev: 4999/13 4999/1670 4999/4983 0/2343][Pkt Len c2s/s2c min/avg/max/stddev: 70/1430 70/1430 70/1430 0/0][Risk: ** Malformed Packet **** Large DNS Packet (512+ bytes) **][Risk Score: 60][Risk Info: Invalid DNS Query Lenght / 1388 Bytes DNS Packet][PLAIN TEXT (AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA)][Plen Bins: 33,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,66,0,0,0,0] 1 UDP 127.0.0.1:50435 <-> 127.0.0.1:53 [proto: 5/DNS][IP: 0/Unknown][ClearText][Confidence: DPI][cat: Network/14][2 pkts/140 bytes <-> 4 pkts/5720 bytes][Goodput ratio: 40/97][5.03 sec][Hostname/SNI: www.xt.com][66.66.66.66][bytes ratio: -0.952 (Download)][IAT c2s/s2c min/avg/max/stddev: 4999/13 4999/1670 4999/4983 0/2343][Pkt Len c2s/s2c min/avg/max/stddev: 70/1430 70/1430 70/1430 0/0][Risk: ** Malformed Packet **** Suspicious DNS Traffic **** Large DNS Packet (512+ bytes) **][Risk Score: 160][Risk Info: DNS Record with zero TTL / Invalid DNS Query Lenght / 1388 Bytes DNS Packet][PLAIN TEXT (AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA)][Plen Bins: 33,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,66,0,0,0,0]