Update APIs and structures.

This commit is contained in:
aouinizied
2020-05-22 23:48:54 +02:00
parent bbbc5fdbae
commit e5c2c400ef
+114 -36
View File
@@ -16,9 +16,12 @@ If not, see <http://www.gnu.org/licenses/>.
from os.path import abspath, dirname from os.path import abspath, dirname
import cffi import cffi
import sys
cc_ndpi_network_headers = """ cc_ndpi_network_headers = """
struct ptr_uint32 {
uint32_t value;
};
struct ndpi_chdlc struct ndpi_chdlc
{ {
uint8_t addr; /* 0x0F (Unicast) - 0x8F (Broadcast) */ uint8_t addr; /* 0x0F (Unicast) - 0x8F (Broadcast) */
@@ -143,6 +146,11 @@ struct ndpi_mpls_header
uint32_t ttl:8, s:1, exp:3, label:20; uint32_t ttl:8, s:1, exp:3, label:20;
}; };
extern union mpls {
uint32_t u32;
struct ndpi_mpls_header mpls;
} mpls;
/* ++++++++++++++++++++++++ IP header ++++++++++++++++++++++++ */ /* ++++++++++++++++++++++++ IP header ++++++++++++++++++++++++ */
struct ndpi_iphdr { struct ndpi_iphdr {
uint8_t ihl:4, version:4; uint8_t ihl:4, version:4;
@@ -280,11 +288,26 @@ typedef enum {
} ndpi_packet_tunnel; } ndpi_packet_tunnel;
typedef enum { typedef enum {
ndpi_url_no_problem = 0, NDPI_NO_RISK = 0,
ndpi_url_possible_xss, NDPI_URL_POSSIBLE_XSS,
ndpi_url_possible_sql_injection, NDPI_URL_POSSIBLE_SQL_INJECTION,
ndpi_url_possible_rce_injection NDPI_URL_POSSIBLE_RCE_INJECTION,
} ndpi_url_risk; NDPI_BINARY_APPLICATION_TRANSFER,
NDPI_KNOWN_PROTOCOL_ON_NON_STANDARD_PORT,
NDPI_TLS_SELFSIGNED_CERTIFICATE,
NDPI_TLS_OBSOLETE_VERSION,
NDPI_TLS_WEAK_CIPHER,
NDPI_TLS_CERTIFICATE_EXPIRED,
NDPI_TLS_CERTIFICATE_MISMATCH,
NDPI_HTTP_SUSPICIOUS_USER_AGENT,
NDPI_HTTP_NUMERIC_IP_HOST,
NDPI_HTTP_SUSPICIOUS_URL,
NDPI_HTTP_SUSPICIOUS_HEADER,
/* Leave this as last member */
NDPI_MAX_RISK
} ndpi_risk_enum;
typedef uint32_t ndpi_risk;
/* NDPI_VISIT */ /* NDPI_VISIT */
typedef enum { typedef enum {
@@ -703,6 +726,7 @@ struct ndpi_packet_struct {
struct ndpi_int_one_line_struct forwarded_line; struct ndpi_int_one_line_struct forwarded_line;
struct ndpi_int_one_line_struct referer_line; struct ndpi_int_one_line_struct referer_line;
struct ndpi_int_one_line_struct content_line; struct ndpi_int_one_line_struct content_line;
struct ndpi_int_one_line_struct content_disposition_line;
struct ndpi_int_one_line_struct accept_line; struct ndpi_int_one_line_struct accept_line;
struct ndpi_int_one_line_struct user_agent_line; struct ndpi_int_one_line_struct user_agent_line;
struct ndpi_int_one_line_struct http_url_name; struct ndpi_int_one_line_struct http_url_name;
@@ -714,8 +738,7 @@ struct ndpi_packet_struct {
struct ndpi_int_one_line_struct http_x_session_type; struct ndpi_int_one_line_struct http_x_session_type;
struct ndpi_int_one_line_struct server_line; struct ndpi_int_one_line_struct server_line;
struct ndpi_int_one_line_struct http_method; struct ndpi_int_one_line_struct http_method;
struct ndpi_int_one_line_struct http_response; /* the first "word" in this pointer is the struct ndpi_int_one_line_struct http_response;
response code in the packet (200, etc) */
uint8_t http_num_headers; /* number of found (valid) header lines in HTTP request or response */ uint8_t http_num_headers; /* number of found (valid) header lines in HTTP request or response */
uint16_t l3_packet_len; uint16_t l3_packet_len;
@@ -756,8 +779,9 @@ typedef struct {
typedef enum { typedef enum {
NDPI_PROTOCOL_SAFE = 0, /* Surely doesn't provide risks for the network. (e.g., a news site) */ NDPI_PROTOCOL_SAFE = 0, /* Surely doesn't provide risks for the network. (e.g., a news site) */
NDPI_PROTOCOL_ACCEPTABLE, /* Probably doesn't provide risks, but could be malicious (e.g., Dropbox) */ NDPI_PROTOCOL_ACCEPTABLE, /* Probably doesn't provide risks, but could be malicious (e.g., Dropbox) */
NDPI_PROTOCOL_FUN, /* Pure fun protocol, which may be prohibited by the user policy (e.g., Netflix) */ NDPI_PROTOCOL_FUN, /* Pure fun protocol, which may be prohibited by the user policy */
NDPI_PROTOCOL_UNSAFE, /* Probably provides risks, but could be a normal traffic. Unencrypted protocols with clear pass should be here (e.g., telnet) */ NDPI_PROTOCOL_UNSAFE, /* Probably provides risks, but could be a normal traffic. Unencrypted protocols
with clear pass should be here (e.g., telnet) */
NDPI_PROTOCOL_POTENTIALLY_DANGEROUS, /* Possibly dangerous (ex. Tor). */ NDPI_PROTOCOL_POTENTIALLY_DANGEROUS, /* Possibly dangerous (ex. Tor). */
NDPI_PROTOCOL_DANGEROUS, /* Surely is dangerous (ex. smbv1). Be prepared to troubles */ NDPI_PROTOCOL_DANGEROUS, /* Surely is dangerous (ex. smbv1). Be prepared to troubles */
NDPI_PROTOCOL_TRACKER_ADS, /* Trackers, Advertisements... */ NDPI_PROTOCOL_TRACKER_ADS, /* Trackers, Advertisements... */
@@ -824,7 +848,8 @@ typedef enum {
in ndpi_main.c in ndpi_main.c
*/ */
NDPI_PROTOCOL_NUM_CATEGORIES /* NDPI_PROTOCOL_NUM_CATEGORIES
/*
NOTE: Keep this as last member NOTE: Keep this as last member
Unused as value but useful to getting the number of elements Unused as value but useful to getting the number of elements
in this datastructure in this datastructure
@@ -842,6 +867,7 @@ typedef struct ndpi_proto_defaults {
uint8_t can_have_a_subprotocol; uint8_t can_have_a_subprotocol;
uint16_t protoId, protoIdx; uint16_t protoId, protoIdx;
uint16_t master_tcp_protoId[2], master_udp_protoId[2]; /* The main protocols on which this sub-protocol sits on */ uint16_t master_tcp_protoId[2], master_udp_protoId[2]; /* The main protocols on which this sub-protocol sits on */
uint16_t tcp_default_ports[5], udp_default_ports[5];
ndpi_protocol_breed_t protoBreed; ndpi_protocol_breed_t protoBreed;
void (*func) (struct ndpi_detection_module_struct *, struct ndpi_flow_struct *flow); void (*func) (struct ndpi_detection_module_struct *, struct ndpi_flow_struct *flow);
} ndpi_proto_defaults_t; } ndpi_proto_defaults_t;
@@ -965,12 +991,13 @@ struct ndpi_detection_module_struct {
/* NDPI_PROTOCOL_STUN and subprotocols */ /* NDPI_PROTOCOL_STUN and subprotocols */
struct ndpi_lru_cache *stun_cache; struct ndpi_lru_cache *stun_cache;
/* NDPI_PROTOCOL_MSTEAMS */
struct ndpi_lru_cache *msteams_cache;
ndpi_proto_defaults_t proto_defaults[512]; ndpi_proto_defaults_t proto_defaults[512];
uint8_t direction_detect_disable:1, /* disable internal detection of packet direction */ uint8_t direction_detect_disable:1, /* disable internal detection of packet direction */
_pad:7; _pad:7;
void *hyperscan; /* Intel Hyperscan */
}; };
#define NDPI_CIPHER_SAFE 0 #define NDPI_CIPHER_SAFE 0
@@ -1019,6 +1046,9 @@ struct ndpi_flow_struct {
struct ndpi_id_struct *server_id; struct ndpi_id_struct *server_id;
/* HTTP host or DNS query */ /* HTTP host or DNS query */
uint8_t host_server_name[240]; uint8_t host_server_name[240];
uint8_t initial_binary_bytes[8], initial_binary_bytes_len;
uint8_t risk_checked;
uint32_t risk; /* Issues found with this flow [bitmask of ndpi_risk] */
/* /*
This structure below will not stay inside the protos This structure below will not stay inside the protos
@@ -1064,8 +1094,8 @@ struct ndpi_flow_struct {
struct { struct {
struct { struct {
uint16_t ssl_version, server_names_len; uint16_t ssl_version, server_names_len;
char client_requested_server_name[64], *server_names, server_organization[64], char client_requested_server_name[64], *server_names,
*alpn, *tls_supported_versions; *alpn, *tls_supported_versions, *issuerDN, *subjectDN;
uint32_t notBefore, notAfter; uint32_t notBefore, notAfter;
char ja3_client[33], ja3_server[33]; char ja3_client[33], ja3_server[33];
uint16_t server_cipher; uint16_t server_cipher;
@@ -1208,10 +1238,6 @@ struct ndpi_flow_struct {
/* NDPI_PROTOCOL_CSGO */ /* NDPI_PROTOCOL_CSGO */
uint8_t csgo_strid[18],csgo_state,csgo_s2; uint8_t csgo_strid[18],csgo_state,csgo_s2;
uint32_t csgo_id2; uint32_t csgo_id2;
/* NDPI_PROTOCOL_1KXUN || NDPI_PROTOCOL_IQIYI */
uint16_t kxun_counter, iqiyi_counter;
/* internal structures to save functions calls */ /* internal structures to save functions calls */
struct ndpi_packet_struct packet; struct ndpi_packet_struct packet;
struct ndpi_flow_struct *flow; struct ndpi_flow_struct *flow;
@@ -1220,14 +1246,14 @@ struct ndpi_flow_struct {
}; };
typedef struct { typedef struct {
char *string_to_match, *string2_to_match, *pattern_to_match, *proto_name; char *string_to_match, *proto_name;
int protocol_id; int protocol_id;
ndpi_protocol_category_t protocol_category; ndpi_protocol_category_t protocol_category;
ndpi_protocol_breed_t protocol_breed; ndpi_protocol_breed_t protocol_breed;
} ndpi_protocol_match; } ndpi_protocol_match;
typedef struct { typedef struct {
char *string_to_match, *hyperscan_string_to_match; char *string_to_match;
ndpi_protocol_category_t protocol_category; ndpi_protocol_category_t protocol_category;
} ndpi_category_match; } ndpi_category_match;
@@ -1239,11 +1265,10 @@ typedef struct {
typedef uint32_t ndpi_init_prefs; typedef uint32_t ndpi_init_prefs;
typedef enum typedef enum {
{
ndpi_no_prefs = 0, ndpi_no_prefs = 0,
ndpi_dont_load_tor_hosts, ndpi_dont_load_tor_hosts,
} ndpi_prefs; } ndpi_prefs;
typedef struct { typedef struct {
int protocol_id; int protocol_id;
@@ -1272,8 +1297,6 @@ struct ndpi_analyze_struct {
#define MAX_SERIES_LEN 512 #define MAX_SERIES_LEN 512
#define MIN_SERIES_LEN 8 #define MIN_SERIES_LEN 8
/* **************************************** */
typedef struct ndpi_ptree ndpi_ptree_t; typedef struct ndpi_ptree ndpi_ptree_t;
""" """
@@ -1300,23 +1323,44 @@ void ndpi_free(void *ptr);
void * ndpi_flow_malloc(size_t size); void * ndpi_flow_malloc(size_t size);
void ndpi_flow_free(void *ptr); void ndpi_flow_free(void *ptr);
void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_struct); void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_struct);
char* ndpi_protocol2name(struct ndpi_detection_module_struct *ndpi_mod, ndpi_protocol proto, char *buf, unsigned buf_len); char* ndpi_protocol2name(struct ndpi_detection_module_struct *ndpi_mod,
ndpi_protocol proto,
char *buf, unsigned buf_len);
const char* ndpi_category_get_name(struct ndpi_detection_module_struct *ndpi_mod, ndpi_protocol_category_t category); const char* ndpi_category_get_name(struct ndpi_detection_module_struct *ndpi_mod, ndpi_protocol_category_t category);
char* ndpi_revision(void); char* ndpi_revision(void);
void ndpi_finalize_initalization(struct ndpi_detection_module_struct *ndpi_str); void ndpi_finalize_initalization(struct ndpi_detection_module_struct *ndpi_str);
uint32_t ndpi_detection_get_sizeof_ndpi_flow_struct(void); uint32_t ndpi_detection_get_sizeof_ndpi_flow_struct(void);
uint32_t ndpi_detection_get_sizeof_ndpi_id_struct(void); uint32_t ndpi_detection_get_sizeof_ndpi_id_struct(void);
uint32_t ndpi_detection_get_sizeof_ndpi_flow_tcp_struct(void);
uint32_t ndpi_detection_get_sizeof_ndpi_flow_udp_struct(void);
uint8_t ndpi_extra_dissection_possible(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow);
""" """
def check_structures_size(flow_struct_defined, flow_struct_loaded,
id_struct_defined, id_struct_loaded,
tcp_flow_struct_defined, tcp_flow_struct_loaded,
udp_flow_struct_defined, udp_flow_struct_loaded):
""" Function used to check loaded structures sizes againt defined ones """
errors = []
if flow_struct_defined != flow_struct_loaded:
errors.append('ndpi_flow_struct')
if id_struct_defined != id_struct_loaded:
errors.append('ndpi_id_struct')
if tcp_flow_struct_defined != tcp_flow_struct_loaded:
errors.append('ndpi_tcp_flow_struct')
if udp_flow_struct_defined != udp_flow_struct_loaded:
errors.append('ndpi_udp_flow_struct')
return errors
class NDPI(): class NDPI():
def __init__(self, libpath=None, max_tcp_dissections=10, max_udp_dissections=16): """ ndpi module main class """
def __init__(self, libpath=None, max_tcp_dissections=80, max_udp_dissections=16, enable_guess=True):
self._ffi = cffi.FFI() self._ffi = cffi.FFI()
if libpath is None: if libpath is None:
if "win" in sys.platform[:3]: self._ndpi = self._ffi.dlopen(dirname(abspath(__file__)) + '/libndpi.so')
self._ndpi = self._ffi.dlopen(dirname(abspath(__file__)) + '/libs/libndpi.a')
else:
self._ndpi = self._ffi.dlopen(dirname(abspath(__file__)) + '/libs/libndpi.so')
else: else:
self._ndpi = self._ffi.dlopen(libpath) self._ndpi = self._ffi.dlopen(libpath)
self._ffi.cdef(cc_ndpi_network_headers, packed=True) self._ffi.cdef(cc_ndpi_network_headers, packed=True)
@@ -1329,47 +1373,81 @@ class NDPI():
all = self._ffi.new('NDPI_PROTOCOL_BITMASK*') all = self._ffi.new('NDPI_PROTOCOL_BITMASK*')
self._ndpi.memset(self._ffi.cast("char *", all), 0xFF, self._ffi.sizeof("NDPI_PROTOCOL_BITMASK")) self._ndpi.memset(self._ffi.cast("char *", all), 0xFF, self._ffi.sizeof("NDPI_PROTOCOL_BITMASK"))
self._ndpi.ndpi_set_protocol_detection_bitmask2(self._mod, all) self._ndpi.ndpi_set_protocol_detection_bitmask2(self._mod, all)
errors = check_structures_size(self._ffi.sizeof("struct ndpi_flow_struct"),
self._ndpi.ndpi_detection_get_sizeof_ndpi_flow_struct(),
self._ffi.sizeof("struct ndpi_id_struct"),
self._ndpi.ndpi_detection_get_sizeof_ndpi_id_struct(),
self._ffi.sizeof("struct ndpi_flow_tcp_struct"),
self._ndpi.ndpi_detection_get_sizeof_ndpi_flow_tcp_struct(),
self._ffi.sizeof("struct ndpi_flow_udp_struct"),
self._ndpi.ndpi_detection_get_sizeof_ndpi_flow_udp_struct())
if len(errors) != 0:
raise ValueError('nDPI error: mismatch in the headers of following structures{}'.format(', '.join(errors)))
else:
self.SIZEOF_FLOW_STRUCT = self._ffi.sizeof("struct ndpi_flow_struct") self.SIZEOF_FLOW_STRUCT = self._ffi.sizeof("struct ndpi_flow_struct")
self.SIZEOF_ID_STRUCT = self._ffi.sizeof("struct ndpi_id_struct") self.SIZEOF_ID_STRUCT = self._ffi.sizeof("struct ndpi_id_struct")
self.NULL = self._ffi.NULL self.NULL = self._ffi.NULL
self.max_tcp_dissections = max_tcp_dissections self.max_tcp_dissections = max_tcp_dissections
self.max_udp_dissections = max_udp_dissections self.max_udp_dissections = max_udp_dissections
self.enable_guess = enable_guess
def new_ndpi_flow(self): def new_ndpi_flow(self):
""" Create a new nDPI flow object """
f = self._ffi.cast('struct ndpi_flow_struct*', self._ndpi.ndpi_flow_malloc(self.SIZEOF_FLOW_STRUCT)) f = self._ffi.cast('struct ndpi_flow_struct*', self._ndpi.ndpi_flow_malloc(self.SIZEOF_FLOW_STRUCT))
self._ndpi.memset(f, 0, self.SIZEOF_FLOW_STRUCT) self._ndpi.memset(f, 0, self.SIZEOF_FLOW_STRUCT)
return f return f
def new_ndpi_id(self): def new_ndpi_id(self):
""" Create a new nDPI id object """
i = self._ffi.cast('struct ndpi_id_struct*', self._ndpi.ndpi_malloc(self.SIZEOF_ID_STRUCT)) i = self._ffi.cast('struct ndpi_id_struct*', self._ndpi.ndpi_malloc(self.SIZEOF_ID_STRUCT))
self._ndpi.memset(i, 0, self.SIZEOF_ID_STRUCT) self._ndpi.memset(i, 0, self.SIZEOF_ID_STRUCT)
return i return i
def ndpi_detection_process_packet(self, flow, packet, packetlen, current_tick, src, dst): def ndpi_detection_process_packet(self, flow, packet, packetlen, current_tick, src, dst):
return self._ndpi.ndpi_detection_process_packet(self._mod, flow, packet, packetlen, current_tick, src, dst) """ Main detection processing function """
p = self._ndpi.ndpi_detection_process_packet(self._mod, flow, packet, packetlen, current_tick, src, dst)
return p
def ndpi_detection_giveup(self, flow): def ndpi_detection_giveup(self, flow):
return self._ndpi.ndpi_detection_giveup(self._mod, flow, 1, self._ffi.new("uint8_t*", 0)) """ Giveup detection function """
return self._ndpi.ndpi_detection_giveup(self._mod, flow, self.enable_guess, self._ffi.new("uint8_t*", 0))
def ndpi_flow_free(self, flow): def ndpi_flow_free(self, flow):
""" Free nDPI flow object """
return self._ndpi.ndpi_flow_free(flow) return self._ndpi.ndpi_flow_free(flow)
def ndpi_free(self, ptr): def ndpi_free(self, ptr):
""" Free nDPI object """
return self._ndpi.ndpi_free(ptr) return self._ndpi.ndpi_free(ptr)
def get_str_field(self, ptr): def get_str_field(self, ptr):
""" Get fixed string size attribute """
if ptr == self._ffi.NULL:
return ''
else:
return self._ffi.string(ptr).decode('utf-8', errors='ignore') return self._ffi.string(ptr).decode('utf-8', errors='ignore')
def get_buffer_field(self, ptr, l): def get_buffer_field(self, ptr, li):
return bytes(self._ffi.buffer(ptr, l)).decode('utf-8', errors='ignore') """ Get variable string size attribute """
if ptr == self._ffi.NULL:
return ''
else:
return self._ffi.string(ptr, li).decode('utf-8', errors='ignore')
def ndpi_protocol2name(self, proto): def ndpi_protocol2name(self, proto):
""" Convert nDPI protocol object to readable name """
buf = self._ffi.new("char[32]") buf = self._ffi.new("char[32]")
self._ndpi.ndpi_protocol2name(self._mod, proto, buf, self._ffi.sizeof(buf)) self._ndpi.ndpi_protocol2name(self._mod, proto, buf, self._ffi.sizeof(buf))
return self._ffi.string(buf).decode('utf-8', errors='ignore') return self._ffi.string(buf).decode('utf-8', errors='ignore')
def ndpi_category_get_name(self, category): def ndpi_category_get_name(self, category):
""" Convert nDPI protocol object to readable name """
return self._ffi.string(self._ndpi.ndpi_category_get_name(self._mod, category)).decode('utf-8', errors='ignore') return self._ffi.string(self._ndpi.ndpi_category_get_name(self._mod, category)).decode('utf-8', errors='ignore')
def ndpi_extra_dissection_possible(self, flow):
return self._ndpi.ndpi_extra_dissection_possible(self._mod, flow)
def ndpi_exit_detection_module(self): def ndpi_exit_detection_module(self):
""" Exit function for nDPI module """
self._ndpi.ndpi_exit_detection_module(self._mod) self._ndpi.ndpi_exit_detection_module(self._mod)
self._ffi.dlclose(self._ndpi)