Initial work towards custom BPF protocol definition

This commit is contained in:
Luca
2022-09-16 19:48:12 +02:00
parent a966d37a21
commit 0f07ad35be
4 changed files with 98 additions and 18 deletions
+15 -1
View File
@@ -72,6 +72,7 @@ LT_INIT
LT_LIB_M LT_LIB_M
PKG_PROG_PKG_CONFIG PKG_PROG_PKG_CONFIG
PWD=`pwd`
AC_PROG_CC AC_PROG_CC
SYSTEM=`uname -s` SYSTEM=`uname -s`
@@ -172,6 +173,7 @@ if test "x$ac_cv_lib_rrd_rrd_fetch_r" = xyes; then :
EXTRA_TARGETS="$EXTRA_TARGETS rrdtool" EXTRA_TARGETS="$EXTRA_TARGETS rrdtool"
fi fi
ADDITIONAL_INCS=
ADDITIONAL_LIBS="$LIBM" ADDITIONAL_LIBS="$LIBM"
PCAP_HOME=$HOME/PF_RING/userland PCAP_HOME=$HOME/PF_RING/userland
@@ -196,8 +198,19 @@ fi
MACHINE=`uname -m` MACHINE=`uname -m`
CUSTOM_NDPI= AC_MSG_CHECKING([PF_RING nBPF])
NBPF_HOME=${PWD}/../PF_RING/userland/nbpf
LIBNBPF=${NBPF_HOME}/libnbpf.a
if test -f ${LIBNBPF}; then :
ADDITIONAL_LIBS="${ADDITIONAL_LIBS} ${LIBNBPF}"
ADDITIONAL_INCS="${ADDITIONAL_INCS} -I${NBPF_HOME}"
AC_DEFINE_UNQUOTED(HAVE_NBPF, "1", [PF_RING nBPF is present])
AC_MSG_RESULT(yes)
else
AC_MSG_RESULT(no)
fi
CUSTOM_NDPI=
if test -d ../nDPI-custom; then : if test -d ../nDPI-custom; then :
CUSTOM_NDPI="-DCUSTOM_NDPI_PROTOCOLS" CUSTOM_NDPI="-DCUSTOM_NDPI_PROTOCOLS"
AC_MSG_RESULT([Compiling with custom nDPI protocols]) AC_MSG_RESULT([Compiling with custom nDPI protocols])
@@ -357,6 +370,7 @@ AC_SUBST(NDPI_VERSION_SHORT)
AC_SUBST(PCAP_INC) AC_SUBST(PCAP_INC)
AC_SUBST(PCAP_LIB) AC_SUBST(PCAP_LIB)
AC_SUBST(LIBRRD) AC_SUBST(LIBRRD)
AC_SUBST(ADDITIONAL_INCS)
AC_SUBST(ADDITIONAL_LIBS) AC_SUBST(ADDITIONAL_LIBS)
AC_SUBST(DL_LIB) AC_SUBST(DL_LIB)
AC_SUBST(DPDK_TARGET) AC_SUBST(DPDK_TARGET)
+12
View File
@@ -923,6 +923,7 @@ typedef enum {
NDPI_CONFIDENCE_DPI_PARTIAL_CACHE, /* Classification results based on some LRU cache with partial/incomplete DPI information */ NDPI_CONFIDENCE_DPI_PARTIAL_CACHE, /* Classification results based on some LRU cache with partial/incomplete DPI information */
NDPI_CONFIDENCE_DPI_CACHE, /* Classification results based on some LRU cache (i.e. correlation among sessions) */ NDPI_CONFIDENCE_DPI_CACHE, /* Classification results based on some LRU cache (i.e. correlation among sessions) */
NDPI_CONFIDENCE_DPI, /* Deep packet inspection */ NDPI_CONFIDENCE_DPI, /* Deep packet inspection */
NDPI_CONFIDENCE_NBPF, /* PF_RING nBPF (custom protocol) */
/* /*
IMPORTANT IMPORTANT
@@ -1105,6 +1106,13 @@ typedef struct ndpi_list_struct {
struct ndpi_list_struct *next; struct ndpi_list_struct *next;
} ndpi_list; } ndpi_list;
#ifdef HAVE_NBPF
typedef struct {
void *tree; /* cast to nbpf_filter* */
u_int16_t l7_protocol;
} nbpf_filter;
#endif
struct ndpi_detection_module_struct { struct ndpi_detection_module_struct {
NDPI_PROTOCOL_BITMASK detection_bitmask; NDPI_PROTOCOL_BITMASK detection_bitmask;
@@ -1230,6 +1238,10 @@ struct ndpi_detection_module_struct {
/* Current packet */ /* Current packet */
struct ndpi_packet_struct packet; struct ndpi_packet_struct packet;
const struct ndpi_flow_input_info *input_info; const struct ndpi_flow_input_info *input_info;
#ifdef HAVE_NBPF
nbpf_filter nbpf_custom_proto[MAX_NBPF_CUSTOM_PROTO];
#endif
}; };
#endif /* NDPI_LIB_COMPILATION */ #endif /* NDPI_LIB_COMPILATION */
+1 -1
View File
@@ -17,7 +17,7 @@ includedir = @includedir@/ndpi
ifneq ($(OS),Windows_NT) ifneq ($(OS),Windows_NT)
CFLAGS += -fPIC -DPIC CFLAGS += -fPIC -DPIC
endif endif
CFLAGS += -I../include -Ithird_party/include -DNDPI_LIB_COMPILATION @NDPI_CFLAGS@ @GPROF_CFLAGS@ @CUSTOM_NDPI@ CFLAGS += -I../include -Ithird_party/include -DNDPI_LIB_COMPILATION @NDPI_CFLAGS@ @GPROF_CFLAGS@ @CUSTOM_NDPI@ @ADDITIONAL_INCS@
LDFLAGS += @NDPI_LDFLAGS@ LDFLAGS += @NDPI_LDFLAGS@
LIBS = @ADDITIONAL_LIBS@ @LIBS@ @GPROF_LIBS@ LIBS = @ADDITIONAL_LIBS@ @LIBS@ @GPROF_LIBS@
+70 -16
View File
@@ -94,6 +94,10 @@
#include "third_party/include/ndpi_patricia.h" #include "third_party/include/ndpi_patricia.h"
#include "third_party/include/ndpi_md5.h" #include "third_party/include/ndpi_md5.h"
#ifdef HAVE_NBPF
#include "nbpf.h"
#endif
static int _ndpi_debug_callbacks = 0; static int _ndpi_debug_callbacks = 0;
/* #define DGA_DEBUG 1 */ /* #define DGA_DEBUG 1 */
@@ -442,7 +446,7 @@ void ndpi_set_proto_subprotocols(struct ndpi_detection_module_struct *ndpi_str,
va_list ap; va_list ap;
int current_arg = protoId; int current_arg = protoId;
size_t i = 0; size_t i = 0;
va_start(ap, protoId); va_start(ap, protoId);
while (current_arg != NDPI_PROTOCOL_NO_MORE_SUBPROTOCOLS) { while (current_arg != NDPI_PROTOCOL_NO_MORE_SUBPROTOCOLS) {
ndpi_str->proto_defaults[protoId].subprotocol_count++; ndpi_str->proto_defaults[protoId].subprotocol_count++;
@@ -464,14 +468,14 @@ void ndpi_set_proto_subprotocols(struct ndpi_detection_module_struct *ndpi_str,
va_start(ap, protoId); va_start(ap, protoId);
current_arg = va_arg(ap, int); current_arg = va_arg(ap, int);
while (current_arg != NDPI_PROTOCOL_NO_MORE_SUBPROTOCOLS) { while (current_arg != NDPI_PROTOCOL_NO_MORE_SUBPROTOCOLS) {
if(ndpi_str->proto_defaults[protoId].subprotocols != NULL) { if(ndpi_str->proto_defaults[protoId].subprotocols != NULL) {
ndpi_str->proto_defaults[protoId].subprotocols[i++] = current_arg; ndpi_str->proto_defaults[protoId].subprotocols[i++] = current_arg;
current_arg = va_arg(ap, int); current_arg = va_arg(ap, int);
} }
} }
va_end(ap); va_end(ap);
} }
@@ -1922,7 +1926,7 @@ static void ndpi_init_protocol_defaults(struct ndpi_detection_module_struct *ndp
"MpegDash", NDPI_PROTOCOL_CATEGORY_MEDIA, "MpegDash", NDPI_PROTOCOL_CATEGORY_MEDIA,
ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */, ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */,
ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */); ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */);
/* /*
Note: removed RSH port 514 as TCP/514 is often used for syslog and RSH is as such on;y Note: removed RSH port 514 as TCP/514 is often used for syslog and RSH is as such on;y
if both source and destination ports are 514. So we removed the default for RSH and used with syslog if both source and destination ports are 514. So we removed the default for RSH and used with syslog
*/ */
@@ -4826,7 +4830,7 @@ u_int8_t ndpi_iph_is_valid_and_not_fragmented(const struct ndpi_iphdr *iph, cons
} }
} }
//#endif //#endif
return(1); return(1);
} }
@@ -4924,10 +4928,10 @@ void ndpi_free_flow_data(struct ndpi_flow_struct* flow) {
if(flow->num_risk_infos) { if(flow->num_risk_infos) {
u_int i; u_int i;
for(i=0; i<flow->num_risk_infos; i++) for(i=0; i<flow->num_risk_infos; i++)
ndpi_free(flow->risk_infos[i].info); ndpi_free(flow->risk_infos[i].info);
} }
if(flow->http.url) if(flow->http.url)
ndpi_free(flow->http.url); ndpi_free(flow->http.url);
@@ -5161,7 +5165,7 @@ static u_int8_t ndpi_is_multi_or_broadcast(struct ndpi_packet_struct *packet) {
return(1); return(1);
} else if(packet->iphv6) { } else if(packet->iphv6) {
/* IPv6 */ /* IPv6 */
if((ntohl(packet->iphv6->ip6_dst.u6_addr.u6_addr32[0]) & 0xFF000000) == 0xFF000000) if((ntohl(packet->iphv6->ip6_dst.u6_addr.u6_addr32[0]) & 0xFF000000) == 0xFF000000)
return(1); return(1);
} }
@@ -6272,16 +6276,63 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
ret.master_protocol = flow->detected_protocol_stack[1], ret.master_protocol = flow->detected_protocol_stack[1],
ret.app_protocol = flow->detected_protocol_stack[0], ret.app_protocol = flow->detected_protocol_stack[0],
ret.category = flow->category; ret.category = flow->category;
return ret; return ret;
} else if(flow->detected_protocol_stack[0] != NDPI_PROTOCOL_UNKNOWN) { } else if(flow->detected_protocol_stack[0] != NDPI_PROTOCOL_UNKNOWN) {
if(ndpi_init_packet(ndpi_str, flow, current_time_ms, packet_data, packetlen, input_info) != 0) if(ndpi_init_packet(ndpi_str, flow, current_time_ms, packet_data, packetlen, input_info) != 0)
return ret; return ret;
goto ret_protocols; goto ret_protocols;
} }
if(ndpi_init_packet(ndpi_str, flow, current_time_ms, packet_data, packetlen, input_info) != 0) if(ndpi_init_packet(ndpi_str, flow, current_time_ms, packet_data, packetlen, input_info) != 0)
return ret; return ret;
#ifdef HAVE_NBPF
if((flow->num_processed_pkts == 1) /* first packet of this flow to be analyzed */
&& (ndpi_str->nbpf_custom_proto[0].tree != NULL)) {
#if 0
const char *filter = "tcp and port 80";
nbpf_tree_t *tree = nbpf_parse(filter, NULL);
nbpf_free(tree);
#endif
u_int8_t i;
nbpf_pkt_info_t t;
memset(&t, 0, sizeof(t));
if(packet->iphv6 != NULL) {
t.tuple.eth_type = 0x86DD;
t.tuple.ip_version = 6;
memcpy(&t.tuple.ip_src.v6, &packet->iphv6->ip6_src, 16);
memcpy(&t.tuple.ip_dst.v6, &packet->iphv6->ip6_dst, 16);
} else {
t.tuple.eth_type = 0x0800;
t.tuple.ip_version = 4;
t.tuple.ip_src.v4 = packet->iph->saddr;
t.tuple.ip_dst.v4 = packet->iph->daddr;
}
t.tuple.l3_proto = flow->l4_proto;
t.tuple.l4_src_port = packet->tcp ? packet->tcp->source : packet->udp->source;
t.tuple.l4_dst_port = packet->tcp ? packet->tcp->dest : packet->udp->dest;
for(i=0; (i<MAX_NBPF_CUSTOM_PROTO) && (ndpi_str->nbpf_custom_proto[i].tree != NULL); i++) {
if(nbpf_match(ndpi_str->nbpf_custom_proto[i].tree, &t)) {
/* match found */
ret.master_protocol = ret.app_protocol = ndpi_str->nbpf_custom_proto[i].l7_protocol;
ndpi_fill_protocol_category(ndpi_str, flow, &ret);
ndpi_reconcile_protocols(ndpi_str, flow, &ret);
flow->confidence = NDPI_CONFIDENCE_NBPF;
return(ret);
}
}
}
#endif
ndpi_connection_tracking(ndpi_str, flow); ndpi_connection_tracking(ndpi_str, flow);
/* build ndpi_selection packet bitmask */ /* build ndpi_selection packet bitmask */
@@ -6330,9 +6381,10 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
else else
ret.category = flow->category; ret.category = flow->category;
if((flow->num_processed_pkts == 1) && (ret.master_protocol == NDPI_PROTOCOL_UNKNOWN) && if((flow->num_processed_pkts == 1) /* first packet of this flow to be analyzed */
(ret.app_protocol == NDPI_PROTOCOL_UNKNOWN) && packet->tcp && (packet->tcp->syn == 0) && && (ret.master_protocol == NDPI_PROTOCOL_UNKNOWN)
(flow->guessed_protocol_id == 0)) { && (ret.app_protocol == NDPI_PROTOCOL_UNKNOWN) && packet->tcp && (packet->tcp->syn == 0)
&& (flow->guessed_protocol_id == 0)) {
u_int8_t protocol_was_guessed; u_int8_t protocol_was_guessed;
/* /*
@@ -6407,7 +6459,7 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
u_int8_t i, offset; u_int8_t i, offset;
offset = snprintf(str, sizeof(str), "Expected on port "); offset = snprintf(str, sizeof(str), "Expected on port ");
for(i=0; (i<MAX_DEFAULT_PORTS) && (default_ports[i] != 0); i++) { for(i=0; (i<MAX_DEFAULT_PORTS) && (default_ports[i] != 0); i++) {
int rc = snprintf(&str[offset], sizeof(str)-offset, "%s%u", int rc = snprintf(&str[offset], sizeof(str)-offset, "%s%u",
(i > 0) ? "," : "", default_ports[i]); (i > 0) ? "," : "", default_ports[i]);
@@ -6448,9 +6500,9 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
if(!found) { if(!found) {
ndpi_default_ports_tree_node_t *r = ndpi_get_guessed_protocol_id(ndpi_str, packet->udp ? IPPROTO_UDP : IPPROTO_TCP, ndpi_default_ports_tree_node_t *r = ndpi_get_guessed_protocol_id(ndpi_str, packet->udp ? IPPROTO_UDP : IPPROTO_TCP,
ntohs(flow->c_port), ntohs(flow->s_port)); ntohs(flow->c_port), ntohs(flow->s_port));
if((r == NULL) if((r == NULL)
|| ((r->proto->protoId != ret.app_protocol) && (r->proto->protoId != ret.master_protocol))) || ((r->proto->protoId != ret.app_protocol) && (r->proto->protoId != ret.master_protocol)))
ndpi_set_risk(ndpi_str, flow, NDPI_KNOWN_PROTOCOL_ON_NON_STANDARD_PORT,NULL); ndpi_set_risk(ndpi_str, flow, NDPI_KNOWN_PROTOCOL_ON_NON_STANDARD_PORT,NULL);
} }
} }
@@ -6472,7 +6524,7 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
addr.s_addr = packet->iph->daddr; addr.s_addr = packet->iph->daddr;
net_risk = ndpi_network_risk_ptree_match(ndpi_str, &addr); net_risk = ndpi_network_risk_ptree_match(ndpi_str, &addr);
} }
if(net_risk != NDPI_NO_RISK) if(net_risk != NDPI_NO_RISK)
ndpi_set_risk(ndpi_str, flow, net_risk, NULL); ndpi_set_risk(ndpi_str, flow, net_risk, NULL);
} }
@@ -7550,6 +7602,8 @@ const char *ndpi_confidence_get_name(ndpi_confidence_t confidence)
return "DPI (cache)"; return "DPI (cache)";
case NDPI_CONFIDENCE_DPI: case NDPI_CONFIDENCE_DPI:
return "DPI"; return "DPI";
case NDPI_CONFIDENCE_NBPF:
return "nBPF";
default: default:
return NULL; return NULL;
} }
@@ -8016,7 +8070,7 @@ u_int16_t ndpi_match_host_subprotocol(struct ndpi_detection_module_struct *ndpi_
/* Add punycode check */ /* Add punycode check */
if(ndpi_strnstr(string_to_match, "xn--", string_to_match_len)) { if(ndpi_strnstr(string_to_match, "xn--", string_to_match_len)) {
char str[64] = { '\0' }; char str[64] = { '\0' };
strncpy(str, string_to_match, ndpi_min(string_to_match_len, sizeof(str)-1)); strncpy(str, string_to_match, ndpi_min(string_to_match_len, sizeof(str)-1));
ndpi_set_risk(ndpi_str, flow, NDPI_PUNYCODE_IDN, str); ndpi_set_risk(ndpi_str, flow, NDPI_PUNYCODE_IDN, str);
} }