Update Python cffi bindings.

This commit is contained in:
Zied Aouini
2020-02-21 17:28:44 +01:00
parent e67604a583
commit dcb6fde0c9
2 changed files with 843 additions and 766 deletions
+19 -20
View File
@@ -1,7 +1,25 @@
nDPI Python bindings nDPI Python bindings
-------------------- --------------------
This directory contains the Python3 bindings for nDPI. We provide both ctypes and cffi based bindings. This directory contains the Python3 bindings for nDPI. We provide both cffi and ctypes based bindings.
**cffi bindings**
Files:
* ndpi.py
Example (using `nfstream <https://github.com/aouinizied/nfstream>`_ package):
.. code-block:: bash
pip3 install nfstream
python3 flow_printer.py <interface>
python3 flow_printer.py <pcap_file>
Code courtesy:
* Zied Aouini
**ctypes bindings** **ctypes bindings**
@@ -23,22 +41,3 @@ Code courtesy:
* Massimo Puddu * Massimo Puddu
* Zied Aouini * Zied Aouini
**cffi bindings**
Files:
* ndpi.py
Example (using NFStream package):
.. code-block:: bash
pip3 install nfstream
python3 flow_printer.py <interface>
python3 flow_printer.py <pcap_file>
Code courtesy:
* Zied Aouini
+275 -197
View File
@@ -2,7 +2,7 @@
file: ndpi.py file: ndpi.py
This file is part of nfstream. This file is part of nfstream.
Copyright (C) 2019 - Zied Aouini <aouinizied@gmail.com> Copyright (C) 2019-20 - Zied Aouini <aouinizied@gmail.com>
nfstream is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License nfstream is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
@@ -18,119 +18,6 @@ from os.path import abspath, dirname
import cffi import cffi
import sys import sys
cc = """
typedef enum {
ndpi_l4_proto_unknown = 0,
ndpi_l4_proto_tcp_only,
ndpi_l4_proto_udp_only,
ndpi_l4_proto_tcp_and_udp,
} ndpi_l4_proto_info;
typedef enum {
ndpi_no_tunnel = 0,
ndpi_gtp_tunnel,
ndpi_capwap_tunnel,
ndpi_tzsp_tunnel,
ndpi_l2tp_tunnel,
} ndpi_packet_tunnel;
typedef enum {
ndpi_url_no_problem = 0,
ndpi_url_possible_xss,
ndpi_url_possible_sql_injection,
ndpi_url_possible_rce_injection
} ndpi_url_risk;
/* NDPI_VISIT */
typedef enum {
ndpi_preorder,
ndpi_postorder,
ndpi_endorder,
ndpi_leaf
} ndpi_VISIT;
/* NDPI_NODE */
typedef struct node_t {
char *key;
struct node_t *left, *right;
} ndpi_node;
/* NDPI_MASK_SIZE */
typedef uint32_t ndpi_ndpi_mask;
/* NDPI_PROTO_BITMASK_STRUCT */
typedef struct ndpi_protocol_bitmask_struct {
ndpi_ndpi_mask fds_bits[16];
} NDPI_PROTOCOL_BITMASK;
typedef struct spinlock {
volatile int val;
} spinlock_t;
typedef struct atomic {
volatile int counter;
} atomic_t;
typedef long int time_t;
struct hash_ip4p_node {
struct hash_ip4p_node *next,*prev;
time_t lchg;
uint16_t port,count:12,flag:4;
uint32_t ip;
// + 12 bytes for ipv6
};
struct hash_ip4p {
struct hash_ip4p_node *top;
spinlock_t lock;
size_t len;
};
struct hash_ip4p_table {
size_t size;
int ipv6;
spinlock_t lock;
atomic_t count;
struct hash_ip4p tbl;
};
struct bt_announce { // 192 bytes
uint32_t hash[5];
uint32_t ip[4];
uint32_t time;
uint16_t port;
uint8_t name_len,
name[149]; // 149 bytes
};
/* NDPI_PROTOCOL_TINC */
#define TINC_CACHE_MAX_SIZE 10
typedef enum {
NDPI_HTTP_METHOD_UNKNOWN = 0,
NDPI_HTTP_METHOD_OPTIONS,
NDPI_HTTP_METHOD_GET,
NDPI_HTTP_METHOD_HEAD,
NDPI_HTTP_METHOD_PATCH,
NDPI_HTTP_METHOD_POST,
NDPI_HTTP_METHOD_PUT,
NDPI_HTTP_METHOD_DELETE,
NDPI_HTTP_METHOD_TRACE,
NDPI_HTTP_METHOD_CONNECT
} ndpi_http_method;
struct ndpi_lru_cache_entry {
uint32_t key; /* Store the whole key to avoid ambiguities */
uint32_t is_full:1, value:16, pad:15;
};
struct ndpi_lru_cache {
uint32_t num_entries;
struct ndpi_lru_cache_entry *entries;
};
"""
cc_ndpi_network_headers = """ cc_ndpi_network_headers = """
struct ndpi_chdlc struct ndpi_chdlc
{ {
@@ -170,8 +57,7 @@ struct ndpi_ethhdr
}; };
/* +++++++++++++++ ARP header +++++++++++++++ */ /* +++++++++++++++ ARP header +++++++++++++++ */
struct ndpi_arphdr struct ndpi_arphdr {
{
uint16_t ar_hrd;/* Format of hardware address. */ uint16_t ar_hrd;/* Format of hardware address. */
uint16_t ar_pro;/* Format of protocol address. */ uint16_t ar_pro;/* Format of protocol address. */
uint8_t ar_hln;/* Length of hardware address. */ uint8_t ar_hln;/* Length of hardware address. */
@@ -252,14 +138,11 @@ struct ndpi_wifi_header
/* +++++++++++++++++++++++ MPLS header +++++++++++++++++++++++ */ /* +++++++++++++++++++++++ MPLS header +++++++++++++++++++++++ */
struct ndpi_mpls_header struct ndpi_mpls_header
{ {
/* Before using this strcut to parse an MPLS header, you will need to convert
* the 4-byte data to the correct endianess with ntohl(). */
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;
@@ -319,7 +202,6 @@ struct ndpi_udphdr
uint16_t len; uint16_t len;
uint16_t check; uint16_t check;
}; };
struct ndpi_dns_packet_header { struct ndpi_dns_packet_header {
uint16_t tr_id; uint16_t tr_id;
uint16_t flags; uint16_t flags;
@@ -329,14 +211,6 @@ struct ndpi_dns_packet_header {
uint16_t additional_rrs; uint16_t additional_rrs;
}; };
typedef union
{
uint32_t ipv4;
uint8_t ipv4_uint8_t[4];
struct ndpi_in6_addr ipv6;
} ndpi_ip_addr_t;
/* +++++++++++++++++++++++ ICMP header +++++++++++++++++++++++ */ /* +++++++++++++++++++++++ ICMP header +++++++++++++++++++++++ */
struct ndpi_icmphdr { struct ndpi_icmphdr {
uint8_t type;/* message type */ uint8_t type;/* message type */
@@ -382,9 +256,135 @@ struct tinc_cache_entry {
}; };
""" """
cc_ndpi_id_struct = """ cc_ndpi_stuctures = """
typedef enum {
NDPI_LOG_ERROR,
NDPI_LOG_TRACE,
NDPI_LOG_DEBUG,
NDPI_LOG_DEBUG_EXTRA
} ndpi_log_level_t;
typedef enum {
ndpi_l4_proto_unknown = 0,
ndpi_l4_proto_tcp_only,
ndpi_l4_proto_udp_only,
ndpi_l4_proto_tcp_and_udp,
} ndpi_l4_proto_info;
typedef enum {
ndpi_no_tunnel = 0,
ndpi_gtp_tunnel,
ndpi_capwap_tunnel,
ndpi_tzsp_tunnel,
ndpi_l2tp_tunnel,
} ndpi_packet_tunnel;
typedef enum {
ndpi_url_no_problem = 0,
ndpi_url_possible_xss,
ndpi_url_possible_sql_injection,
ndpi_url_possible_rce_injection
} ndpi_url_risk;
/* NDPI_VISIT */
typedef enum {
ndpi_preorder,
ndpi_postorder,
ndpi_endorder,
ndpi_leaf
} ndpi_VISIT;
/* NDPI_NODE */
typedef struct node_t {
char *key;
struct node_t *left, *right;
} ndpi_node;
/* NDPI_MASK_SIZE */
typedef uint32_t ndpi_ndpi_mask;
/* NDPI_PROTO_BITMASK_STRUCT */
typedef struct ndpi_protocol_bitmask_struct {
ndpi_ndpi_mask fds_bits[16];
} NDPI_PROTOCOL_BITMASK;
/* NDPI_PROTOCOL_BITTORRENT */
typedef struct spinlock {
volatile int val;
} spinlock_t;
typedef struct atomic {
volatile int counter;
} atomic_t;
typedef long int time_t;
struct hash_ip4p_node {
struct hash_ip4p_node *next, *prev;
time_t lchg;
uint16_t port,count:12,flag:4;
uint32_t ip;
};
struct hash_ip4p {
struct hash_ip4p_node *top;
spinlock_t lock;
size_t len;
};
struct hash_ip4p_table {
size_t size;
int ipv6;
spinlock_t lock;
atomic_t count;
struct hash_ip4p tbl;
};
struct bt_announce { // 192 bytes
uint32_t hash[5];
uint32_t ip[4];
uint32_t time;
uint16_t port;
uint8_t name_len,
name[149]; // 149 bytes
};
/* NDPI_PROTOCOL_TINC */
#define TINC_CACHE_MAX_SIZE 10
typedef enum {
NDPI_HTTP_METHOD_UNKNOWN = 0,
NDPI_HTTP_METHOD_OPTIONS,
NDPI_HTTP_METHOD_GET,
NDPI_HTTP_METHOD_HEAD,
NDPI_HTTP_METHOD_PATCH,
NDPI_HTTP_METHOD_POST,
NDPI_HTTP_METHOD_PUT,
NDPI_HTTP_METHOD_DELETE,
NDPI_HTTP_METHOD_TRACE,
NDPI_HTTP_METHOD_CONNECT
} ndpi_http_method;
struct ndpi_lru_cache_entry {
uint32_t key; /* Store the whole key to avoid ambiguities */
uint32_t is_full:1, value:16, pad:15;
};
struct ndpi_lru_cache {
uint32_t num_entries;
struct ndpi_lru_cache_entry *entries;
};
typedef union
{
uint32_t ipv4;
uint8_t ipv4_uint8_t[4];
struct ndpi_in6_addr ipv6;
} ndpi_ip_addr_t;
struct ndpi_id_struct { struct ndpi_id_struct {
/** detected_protocol_bitmask: /**
detected_protocol_bitmask:
access this bitmask to find out whether an id has used skype or not access this bitmask to find out whether an id has used skype or not
if a flag is set here, it will not be reset if a flag is set here, it will not be reset
to compare this, use: to compare this, use:
@@ -392,77 +392,103 @@ struct ndpi_id_struct {
NDPI_PROTOCOL_BITMASK detected_protocol_bitmask; NDPI_PROTOCOL_BITMASK detected_protocol_bitmask;
/* NDPI_PROTOCOL_RTSP */ /* NDPI_PROTOCOL_RTSP */
ndpi_ip_addr_t rtsp_ip_address; ndpi_ip_addr_t rtsp_ip_address;
/* NDPI_PROTOCOL_YAHOO */ /* NDPI_PROTOCOL_YAHOO */
uint32_t yahoo_video_lan_timer; uint32_t yahoo_video_lan_timer;
/* NDPI_PROTOCOL_IRC_MAXPORT % 2 must be 0 */ /* NDPI_PROTOCOL_IRC_MAXPORT % 2 must be 0 */
/* NDPI_PROTOCOL_IRC */ /* NDPI_PROTOCOL_IRC */
#define NDPI_PROTOCOL_IRC_MAXPORT 8 #define NDPI_PROTOCOL_IRC_MAXPORT 8
uint16_t irc_port[NDPI_PROTOCOL_IRC_MAXPORT]; uint16_t irc_port[NDPI_PROTOCOL_IRC_MAXPORT];
uint32_t last_time_port_used[NDPI_PROTOCOL_IRC_MAXPORT]; uint32_t last_time_port_used[NDPI_PROTOCOL_IRC_MAXPORT];
uint32_t irc_ts; uint32_t irc_ts;
/* NDPI_PROTOCOL_GNUTELLA */ /* NDPI_PROTOCOL_GNUTELLA */
uint32_t gnutella_ts; uint32_t gnutella_ts;
/* NDPI_PROTOCOL_BATTLEFIELD */ /* NDPI_PROTOCOL_BATTLEFIELD */
uint32_t battlefield_ts; uint32_t battlefield_ts;
/* NDPI_PROTOCOL_THUNDER */ /* NDPI_PROTOCOL_THUNDER */
uint32_t thunder_ts; uint32_t thunder_ts;
/* NDPI_PROTOCOL_RTSP */ /* NDPI_PROTOCOL_RTSP */
uint32_t rtsp_timer; uint32_t rtsp_timer;
/* NDPI_PROTOCOL_OSCAR */ /* NDPI_PROTOCOL_OSCAR */
uint32_t oscar_last_safe_access_time; uint32_t oscar_last_safe_access_time;
/* NDPI_PROTOCOL_ZATTOO */ /* NDPI_PROTOCOL_ZATTOO */
uint32_t zattoo_ts; uint32_t zattoo_ts;
/* NDPI_PROTOCOL_UNENCRYPTED_JABBER */ /* NDPI_PROTOCOL_UNENCRYPTED_JABBER */
uint32_t jabber_stun_or_ft_ts; uint32_t jabber_stun_or_ft_ts;
/* NDPI_PROTOCOL_DIRECTCONNECT */ /* NDPI_PROTOCOL_DIRECTCONNECT */
uint32_t directconnect_last_safe_access_time; uint32_t directconnect_last_safe_access_time;
/* NDPI_PROTOCOL_SOULSEEK */ /* NDPI_PROTOCOL_SOULSEEK */
uint32_t soulseek_last_safe_access_time; uint32_t soulseek_last_safe_access_time;
/* NDPI_PROTOCOL_DIRECTCONNECT */ /* NDPI_PROTOCOL_DIRECTCONNECT */
uint16_t detected_directconnect_port; uint16_t detected_directconnect_port;
uint16_t detected_directconnect_udp_port; uint16_t detected_directconnect_udp_port;
uint16_t detected_directconnect_ssl_port; uint16_t detected_directconnect_ssl_port;
/* NDPI_PROTOCOL_BITTORRENT */ /* NDPI_PROTOCOL_BITTORRENT */
#define NDPI_BT_PORTS 8 #define NDPI_BT_PORTS 8
uint16_t bt_port_t[NDPI_BT_PORTS]; uint16_t bt_port_t[NDPI_BT_PORTS];
uint16_t bt_port_u[NDPI_BT_PORTS]; uint16_t bt_port_u[NDPI_BT_PORTS];
/* NDPI_PROTOCOL_UNENCRYPTED_JABBER */ /* NDPI_PROTOCOL_UNENCRYPTED_JABBER */
#define JABBER_MAX_STUN_PORTS 6 #define JABBER_MAX_STUN_PORTS 6
uint16_t jabber_voice_stun_port[JABBER_MAX_STUN_PORTS]; uint16_t jabber_voice_stun_port[JABBER_MAX_STUN_PORTS];
uint16_t jabber_file_transfer_port[2]; uint16_t jabber_file_transfer_port[2];
/* NDPI_PROTOCOL_GNUTELLA */ /* NDPI_PROTOCOL_GNUTELLA */
uint16_t detected_gnutella_port; uint16_t detected_gnutella_port;
/* NDPI_PROTOCOL_GNUTELLA */ /* NDPI_PROTOCOL_GNUTELLA */
uint16_t detected_gnutella_udp_port1; uint16_t detected_gnutella_udp_port1;
uint16_t detected_gnutella_udp_port2; uint16_t detected_gnutella_udp_port2;
/* NDPI_PROTOCOL_SOULSEEK */ /* NDPI_PROTOCOL_SOULSEEK */
uint16_t soulseek_listen_port; uint16_t soulseek_listen_port;
/* NDPI_PROTOCOL_IRC */ /* NDPI_PROTOCOL_IRC */
uint8_t irc_number_of_port; uint8_t irc_number_of_port;
/* NDPI_PROTOCOL_OSCAR */ /* NDPI_PROTOCOL_OSCAR */
uint8_t oscar_ssl_session_id[33]; uint8_t oscar_ssl_session_id[33];
/* NDPI_PROTOCOL_UNENCRYPTED_JABBER */ /* NDPI_PROTOCOL_UNENCRYPTED_JABBER */
uint8_t jabber_voice_stun_used_ports; uint8_t jabber_voice_stun_used_ports;
/* NDPI_PROTOCOL_SIP */ /* NDPI_PROTOCOL_SIP */
/* NDPI_PROTOCOL_YAHOO */ /* NDPI_PROTOCOL_YAHOO */
uint32_t yahoo_video_lan_dir:1; uint32_t yahoo_video_lan_dir:1;
/* NDPI_PROTOCOL_YAHOO */ /* NDPI_PROTOCOL_YAHOO */
uint32_t yahoo_conf_logged_in:1; uint32_t yahoo_conf_logged_in:1;
uint32_t yahoo_voice_conf_logged_in:1; uint32_t yahoo_voice_conf_logged_in:1;
/* NDPI_PROTOCOL_RTSP */ /* NDPI_PROTOCOL_RTSP */
uint32_t rtsp_ts_set:1; uint32_t rtsp_ts_set:1;
}; };
"""
cc_ndpi_flow_tcp_struct = """
struct ndpi_flow_tcp_struct { struct ndpi_flow_tcp_struct {
/* NDPI_PROTOCOL_MAIL_SMTP */ /* NDPI_PROTOCOL_MAIL_SMTP */
uint16_t smtp_command_bitmask; uint16_t smtp_command_bitmask;
/* NDPI_PROTOCOL_MAIL_POP */ /* NDPI_PROTOCOL_MAIL_POP */
uint16_t pop_command_bitmask; uint16_t pop_command_bitmask;
/* NDPI_PROTOCOL_QQ */ /* NDPI_PROTOCOL_QQ */
uint16_t qq_nxt_len; uint16_t qq_nxt_len;
/* NDPI_PROTOCOL_WHATSAPP */ /* NDPI_PROTOCOL_WHATSAPP */
uint8_t wa_matched_so_far; uint8_t wa_matched_so_far;
/* NDPI_PROTOCOL_TDS */ /* NDPI_PROTOCOL_TDS */
uint8_t tds_login_version; uint8_t tds_login_version;
@@ -502,8 +528,10 @@ struct ndpi_flow_tcp_struct {
/* NDPI_PROTOCOL_GNUTELLA */ /* NDPI_PROTOCOL_GNUTELLA */
uint32_t gnutella_stage:2; // 0 - 2 uint32_t gnutella_stage:2; // 0 - 2
/* NDPI_CONTENT_MMS */ /* NDPI_CONTENT_MMS */
uint32_t mms_stage:2; uint32_t mms_stage:2;
/* NDPI_PROTOCOL_YAHOO */ /* NDPI_PROTOCOL_YAHOO */
uint32_t yahoo_sip_comm:1; uint32_t yahoo_sip_comm:1;
uint32_t yahoo_http_proxy_stage:2; uint32_t yahoo_http_proxy_stage:2;
@@ -520,17 +548,19 @@ struct ndpi_flow_tcp_struct {
/* NDPI_PROTOCOL_TELNET */ /* NDPI_PROTOCOL_TELNET */
uint32_t telnet_stage:2; // 0 - 2 uint32_t telnet_stage:2; // 0 - 2
void* tls_srv_cert_fingerprint_ctx;
struct {
struct {
uint8_t *buffer;
unsigned buffer_len, buffer_used;
} message;
void* srv_cert_fingerprint_ctx; /* SHA-1 */
/* NDPI_PROTOCOL_TLS */ /* NDPI_PROTOCOL_TLS */
uint8_t tls_seen_client_cert:1, uint8_t hello_processed:1, certificate_processed:1, subprotocol_detected:1, fingerprint_set:1, _pad:4;
tls_seen_server_cert:1, uint8_t sha1_certificate_fingerprint[20];
tls_seen_certificate:1, } tls;
tls_srv_cert_fingerprint_found:1,
tls_srv_cert_fingerprint_processed:1,
tls_stage:2, _pad:1; // 0 - 5
int16_t tls_record_offset, tls_fingerprint_len; /* Need to be signed */
uint8_t tls_sha1_certificate_fingerprint[20];
/* NDPI_PROTOCOL_POSTGRES */ /* NDPI_PROTOCOL_POSTGRES */
uint32_t postgres_stage:3; uint32_t postgres_stage:3;
@@ -573,6 +603,7 @@ struct ndpi_flow_tcp_struct {
/* NDPI_PROTOCOL_CITRIX */ /* NDPI_PROTOCOL_CITRIX */
uint8_t citrix_packet_id; uint8_t citrix_packet_id;
/* NDPI_PROTOCOL_LOTUS_NOTES */ /* NDPI_PROTOCOL_LOTUS_NOTES */
uint8_t lotus_notes_packet_id; uint8_t lotus_notes_packet_id;
@@ -592,67 +623,65 @@ struct ndpi_flow_tcp_struct {
/* NDPI_PROTOCOL_NEST_LOG_SINK */ /* NDPI_PROTOCOL_NEST_LOG_SINK */
uint8_t nest_log_sink_matches; uint8_t nest_log_sink_matches;
}; };
"""
cc_ndpi_flow_udp_struct = """
struct ndpi_flow_udp_struct { struct ndpi_flow_udp_struct {
/* NDPI_PROTOCOL_BATTLEFIELD */ /* NDPI_PROTOCOL_BATTLEFIELD */
uint32_t battlefield_msg_id; uint32_t battlefield_msg_id;
/* NDPI_PROTOCOL_SNMP */ /* NDPI_PROTOCOL_SNMP */
uint32_t snmp_msg_id; uint32_t snmp_msg_id;
/* NDPI_PROTOCOL_BATTLEFIELD */ /* NDPI_PROTOCOL_BATTLEFIELD */
uint32_t battlefield_stage:3; uint32_t battlefield_stage:3;
/* NDPI_PROTOCOL_SNMP */ /* NDPI_PROTOCOL_SNMP */
uint32_t snmp_stage:2; uint32_t snmp_stage:2;
/* NDPI_PROTOCOL_PPSTREAM */ /* NDPI_PROTOCOL_PPSTREAM */
uint32_t ppstream_stage:3; // 0 - 7 uint32_t ppstream_stage:3; // 0 - 7
/* NDPI_PROTOCOL_HALFLIFE2 */ /* NDPI_PROTOCOL_HALFLIFE2 */
uint32_t halflife2_stage:2; // 0 - 2 uint32_t halflife2_stage:2; // 0 - 2
/* NDPI_PROTOCOL_TFTP */ /* NDPI_PROTOCOL_TFTP */
uint32_t tftp_stage:1; uint32_t tftp_stage:1;
/* NDPI_PROTOCOL_AIMINI */ /* NDPI_PROTOCOL_AIMINI */
uint32_t aimini_stage:5; uint32_t aimini_stage:5;
/* NDPI_PROTOCOL_XBOX */ /* NDPI_PROTOCOL_XBOX */
uint32_t xbox_stage:1; uint32_t xbox_stage:1;
/* NDPI_PROTOCOL_WINDOWS_UPDATE */ /* NDPI_PROTOCOL_WINDOWS_UPDATE */
uint32_t wsus_stage:1; uint32_t wsus_stage:1;
/* NDPI_PROTOCOL_SKYPE */ /* NDPI_PROTOCOL_SKYPE */
uint8_t skype_packet_id; uint8_t skype_packet_id;
/* NDPI_PROTOCOL_TEAMVIEWER */ /* NDPI_PROTOCOL_TEAMVIEWER */
uint8_t teamviewer_stage; uint8_t teamviewer_stage;
/* NDPI_PROTOCOL_EAQ */ /* NDPI_PROTOCOL_EAQ */
uint8_t eaq_pkt_id; uint8_t eaq_pkt_id;
uint32_t eaq_sequence; uint32_t eaq_sequence;
/* NDPI_PROTOCOL_RX */ /* NDPI_PROTOCOL_RX */
uint32_t rx_conn_epoch; uint32_t rx_conn_epoch;
uint32_t rx_conn_id; uint32_t rx_conn_id;
/* NDPI_PROTOCOL_MEMCACHED */ /* NDPI_PROTOCOL_MEMCACHED */
uint8_t memcached_matches; uint8_t memcached_matches;
/* NDPI_PROTOCOL_WIREGUARD */ /* NDPI_PROTOCOL_WIREGUARD */
uint8_t wireguard_stage; uint8_t wireguard_stage;
uint32_t wireguard_peer_index[2]; uint32_t wireguard_peer_index[2];
}; };
"""
cc_ndpi_int_one_line_struct = """
struct ndpi_int_one_line_struct { struct ndpi_int_one_line_struct {
const uint8_t *ptr; const uint8_t *ptr;
uint16_t len; uint16_t len;
}; };
"""
cc_ndpi_packet_struct_stack = """
struct ndpi_packet_struct_stack {
uint8_t detected_subprotocol_stack[2];
uint16_t protocol_stack_info;
};
struct ndpi_flow_struct_stack {
uint16_t detected_protocol_stack[2];
uint16_t protocol_stack_info;
};
"""
cc_ndpi_packet_struct = """
struct ndpi_packet_struct { struct ndpi_packet_struct {
const struct ndpi_iphdr *iph; const struct ndpi_iphdr *iph;
const struct ndpi_ipv6hdr *iphv6; const struct ndpi_ipv6hdr *iphv6;
@@ -660,9 +689,14 @@ struct ndpi_packet_struct {
const struct ndpi_udphdr *udp; const struct ndpi_udphdr *udp;
const uint8_t *generic_l4_ptr; /* is set only for non tcp-udp traffic */ const uint8_t *generic_l4_ptr; /* is set only for non tcp-udp traffic */
const uint8_t *payload; const uint8_t *payload;
uint32_t tick_timestamp; uint32_t tick_timestamp;
uint64_t tick_timestamp_l; uint64_t tick_timestamp_l;
struct ndpi_packet_struct_stack ndpi_packet_stack;
uint16_t detected_protocol_stack[2];
uint8_t detected_subprotocol_stack[2];
uint16_t protocol_stack_info;
struct ndpi_int_one_line_struct line[64]; struct ndpi_int_one_line_struct line[64];
/* HTTP headers */ /* HTTP headers */
struct ndpi_int_one_line_struct host_line; struct ndpi_int_one_line_struct host_line;
@@ -680,9 +714,10 @@ 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 response code in the struct ndpi_int_one_line_struct http_response; /* the first "word" in this pointer is the
packet (200, etc) */ 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;
uint16_t l4_packet_len; uint16_t l4_packet_len;
uint16_t payload_packet_len; uint16_t payload_packet_len;
@@ -693,12 +728,15 @@ struct ndpi_packet_struct {
uint16_t empty_line_position; uint16_t empty_line_position;
uint8_t tcp_retransmission; uint8_t tcp_retransmission;
uint8_t l4_protocol; uint8_t l4_protocol;
uint8_t tls_certificate_detected:4, tls_certificate_num_checks:4; uint8_t tls_certificate_detected:4, tls_certificate_num_checks:4;
uint8_t packet_lines_parsed_complete:1, uint8_t packet_lines_parsed_complete:1,
packet_direction:1, empty_line_position_set:1, pad:5; packet_direction:1, empty_line_position_set:1, pad:5;
}; };
struct ndpi_detection_module_struct; struct ndpi_detection_module_struct;
struct ndpi_flow_struct; struct ndpi_flow_struct;
struct ndpi_call_function_struct { struct ndpi_call_function_struct {
NDPI_PROTOCOL_BITMASK detection_bitmask; NDPI_PROTOCOL_BITMASK detection_bitmask;
NDPI_PROTOCOL_BITMASK excluded_protocol_bitmask; NDPI_PROTOCOL_BITMASK excluded_protocol_bitmask;
@@ -706,6 +744,7 @@ struct ndpi_call_function_struct {
void (*func) (struct ndpi_detection_module_struct *, struct ndpi_flow_struct *flow); void (*func) (struct ndpi_detection_module_struct *, struct ndpi_flow_struct *flow);
uint8_t detection_feature; uint8_t detection_feature;
}; };
struct ndpi_subprotocol_conf_struct { struct ndpi_subprotocol_conf_struct {
void (*func) (struct ndpi_detection_module_struct *, char *attr, char *value, int protocol_id); void (*func) (struct ndpi_detection_module_struct *, char *attr, char *value, int protocol_id);
}; };
@@ -777,14 +816,15 @@ typedef enum {
to track malware, spam etc. to track malware, spam etc.
*/ */
CUSTOM_CATEGORY_ANTIMALWARE = 105, CUSTOM_CATEGORY_ANTIMALWARE = 105,
/* /*
IMPORTANT IMPORTANT
Please keep in sync with Please keep in sync with
static const char* categories[] = { ..} static const char* categories[] = { ..}
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
@@ -793,7 +833,6 @@ typedef enum {
typedef enum { typedef enum {
ndpi_pref_direction_detect_disable = 0, ndpi_pref_direction_detect_disable = 0,
ndpi_pref_disable_metadata_export,
} ndpi_detection_preference; } ndpi_detection_preference;
/* ntop extensions */ /* ntop extensions */
@@ -831,40 +870,46 @@ typedef struct ndpi_proto {
#define NUM_CUSTOM_CATEGORIES 5 #define NUM_CUSTOM_CATEGORIES 5
#define CUSTOM_CATEGORY_LABEL_LEN 32 #define CUSTOM_CATEGORY_LABEL_LEN 32
typedef enum {
NDPI_LOG_ERROR,
NDPI_LOG_TRACE,
NDPI_LOG_DEBUG,
NDPI_LOG_DEBUG_EXTRA
} ndpi_log_level_t;
struct ndpi_detection_module_struct { struct ndpi_detection_module_struct {
NDPI_PROTOCOL_BITMASK detection_bitmask; NDPI_PROTOCOL_BITMASK detection_bitmask;
NDPI_PROTOCOL_BITMASK generic_http_packet_bitmask; NDPI_PROTOCOL_BITMASK generic_http_packet_bitmask;
uint32_t current_ts; uint32_t current_ts;
uint32_t ticks_per_second; uint32_t ticks_per_second;
char custom_category_labels[NUM_CUSTOM_CATEGORIES][CUSTOM_CATEGORY_LABEL_LEN]; char custom_category_labels[NUM_CUSTOM_CATEGORIES][CUSTOM_CATEGORY_LABEL_LEN];
/* callback function buffer */ /* callback function buffer */
struct ndpi_call_function_struct callback_buffer[250]; struct ndpi_call_function_struct callback_buffer[250];
uint32_t callback_buffer_size; uint32_t callback_buffer_size;
struct ndpi_call_function_struct callback_buffer_tcp_no_payload[250]; struct ndpi_call_function_struct callback_buffer_tcp_no_payload[250];
uint32_t callback_buffer_size_tcp_no_payload; uint32_t callback_buffer_size_tcp_no_payload;
struct ndpi_call_function_struct callback_buffer_tcp_payload[250]; struct ndpi_call_function_struct callback_buffer_tcp_payload[250];
uint32_t callback_buffer_size_tcp_payload; uint32_t callback_buffer_size_tcp_payload;
struct ndpi_call_function_struct callback_buffer_udp[250]; struct ndpi_call_function_struct callback_buffer_udp[250];
uint32_t callback_buffer_size_udp; uint32_t callback_buffer_size_udp;
struct ndpi_call_function_struct callback_buffer_non_tcp_udp[250]; struct ndpi_call_function_struct callback_buffer_non_tcp_udp[250];
uint32_t callback_buffer_size_non_tcp_udp; uint32_t callback_buffer_size_non_tcp_udp;
ndpi_default_ports_tree_node_t *tcpRoot, *udpRoot; ndpi_default_ports_tree_node_t *tcpRoot, *udpRoot;
ndpi_log_level_t ndpi_log_level; /* default error */ ndpi_log_level_t ndpi_log_level; /* default error */
/* misc parameters */ /* misc parameters */
uint32_t tcp_max_retransmission_window_size; uint32_t tcp_max_retransmission_window_size;
uint32_t directconnect_connection_ip_tick_timeout; uint32_t directconnect_connection_ip_tick_timeout;
/* subprotocol registration handler */ /* subprotocol registration handler */
struct ndpi_subprotocol_conf_struct subprotocol_conf[250]; struct ndpi_subprotocol_conf_struct subprotocol_conf[250];
unsigned ndpi_num_supported_protocols; unsigned ndpi_num_supported_protocols;
unsigned ndpi_num_custom_protocols; unsigned ndpi_num_custom_protocols;
/* HTTP/DNS/HTTPS host matching */ /* HTTP/DNS/HTTPS host matching */
ndpi_automa host_automa, /* Used for DNS/HTTPS */ ndpi_automa host_automa, /* Used for DNS/HTTPS */
content_automa, /* Used for HTTP subprotocol_detection */ content_automa, /* Used for HTTP subprotocol_detection */
@@ -907,21 +952,24 @@ struct ndpi_detection_module_struct {
/* NDPI_PROTOCOL_BITTORRENT */ /* NDPI_PROTOCOL_BITTORRENT */
struct hash_ip4p_table *bt_ht; struct hash_ip4p_table *bt_ht;
struct hash_ip4p_table *bt6_ht; struct hash_ip4p_table *bt6_ht;
/* BT_ANNOUNCE */ /* BT_ANNOUNCE */
struct bt_announce *bt_ann; struct bt_announce *bt_ann;
int bt_ann_len; int bt_ann_len;
/* NDPI_PROTOCOL_OOKLA */ /* NDPI_PROTOCOL_OOKLA */
struct ndpi_lru_cache *ookla_cache; struct ndpi_lru_cache *ookla_cache;
/* NDPI_PROTOCOL_TINC */ /* NDPI_PROTOCOL_TINC */
struct cache *tinc_cache; struct cache *tinc_cache;
/* NDPI_PROTOCOL_STUN and subprotocols */ /* NDPI_PROTOCOL_STUN and subprotocols */
struct ndpi_lru_cache *stun_cache; struct ndpi_lru_cache *stun_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 */
disable_metadata_export:1 /* No metadata is exported */ _pad:7;
;
void *hyperscan; /* Intel Hyperscan */ void *hyperscan; /* Intel Hyperscan */
}; };
@@ -936,23 +984,22 @@ typedef enum {
} ndpi_cipher_weakness; } ndpi_cipher_weakness;
struct ndpi_flow_struct { struct ndpi_flow_struct {
struct ndpi_flow_struct_stack ndpi_flow_stack; uint16_t detected_protocol_stack[2];
uint16_t protocol_stack_info;
/* init parameter, internal used to set up timestamp,... */ /* init parameter, internal used to set up timestamp,... */
uint16_t guessed_protocol_id, guessed_host_protocol_id, guessed_category, guessed_header_category; uint16_t guessed_protocol_id, guessed_host_protocol_id, guessed_category, guessed_header_category;
uint8_t l4_proto, protocol_id_already_guessed:1, host_already_guessed:1, init_finished:1, setup_packet_direction:1, packet_direction:1, check_extra_packets:1; uint8_t l4_proto, protocol_id_already_guessed:1, host_already_guessed:1,
init_finished:1, setup_packet_direction:1, packet_direction:1, check_extra_packets:1;
/* /*
if ndpi_struct->direction_detect_disable == 1 if ndpi_struct->direction_detect_disable == 1
tcp sequence number connection tracking tcp sequence number connection tracking
*/ */
uint32_t next_tcp_seq_nr[2]; uint32_t next_tcp_seq_nr[2];
uint8_t max_extra_packets_to_check; uint8_t max_extra_packets_to_check;
uint8_t num_extra_packets_checked; uint8_t num_extra_packets_checked;
uint8_t num_processed_pkts; /* <= WARNING it can wrap but we do expect people to giveup earlier */ uint8_t num_processed_pkts; /* <= WARNING it can wrap but we do expect people to giveup earlier */
int (*extra_packets_func) (struct ndpi_detection_module_struct *, struct ndpi_flow_struct *flow); int (*extra_packets_func) (struct ndpi_detection_module_struct *, struct ndpi_flow_struct *flow);
/* /*
the tcp / udp / other l4 value union the tcp / udp / other l4 value union
used to reduce the number of bytes for tcp or udp protocol states used to reduce the number of bytes for tcp or udp protocol states
@@ -962,18 +1009,21 @@ struct ndpi_flow_struct {
struct ndpi_flow_udp_struct udp; struct ndpi_flow_udp_struct udp;
} l4; } l4;
/* Place textual flow info here */
char flow_extra_info[16];
/* /*
Pointer to src or dst that identifies the Pointer to src or dst that identifies the
server of this connection server of this connection
*/ */
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[256]; uint8_t host_server_name[240];
/* /*
This structure below will not not stay inside the protos This structure below will not stay inside the protos
structure below as HTTP is used by many subprotocols structure below as HTTP is used by many subprotocols
such as FaceBook, Google... so it is hard to know such as Facebook, Google... so it is hard to know
when to use it or not. Thus we leave it outside for the when to use it or not. Thus we leave it outside for the
time being. time being.
*/ */
@@ -985,11 +1035,15 @@ struct ndpi_flow_struct {
uint16_t response_status_code; /* 200, 404, etc. */ uint16_t response_status_code; /* 200, 404, etc. */
} http; } http;
/*
Put outside of the union to avoid issues in case the protocol
is remapped to somethign pther than Kerberos due to a faulty
dissector
*/
struct { struct {
char *pktbuf; char *pktbuf;
uint16_t pktbuf_maxlen, pktbuf_currlen; uint16_t pktbuf_maxlen, pktbuf_currlen;
} kerberos_buf; } kerberos_buf;
union { union {
/* the only fields useful for nDPI and ntopng */ /* the only fields useful for nDPI and ntopng */
struct { struct {
@@ -1009,8 +1063,9 @@ struct ndpi_flow_struct {
struct { struct {
struct { struct {
uint16_t ssl_version; uint16_t ssl_version, server_names_len;
char client_certificate[64], server_certificate[64], server_organization[64]; char client_requested_server_name[64], *server_names, server_organization[64],
*alpn, *tls_supported_versions;
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;
@@ -1034,7 +1089,9 @@ struct ndpi_flow_struct {
} imo; } imo;
struct { struct {
uint8_t username_detected:1, username_found:1, password_detected:1, password_found:1, _pad:4; uint8_t username_detected:1, username_found:1,
password_detected:1, password_found:1,
pad:4;
uint8_t character_id; uint8_t character_id;
char username[32], password[32]; char username[32], password[32];
} telnet; } telnet;
@@ -1094,7 +1151,6 @@ struct ndpi_flow_struct {
/* NDPI_PROTOCOL_HTTP */ /* NDPI_PROTOCOL_HTTP */
uint8_t http_detected:1; uint8_t http_detected:1;
uint16_t http_upper_protocol, http_lower_protocol;
/* NDPI_PROTOCOL_RTSP */ /* NDPI_PROTOCOL_RTSP */
uint8_t rtsprdt_stage:2, rtsp_control_flow:1; uint8_t rtsprdt_stage:2, rtsp_control_flow:1;
@@ -1162,6 +1218,7 @@ struct ndpi_flow_struct {
struct ndpi_id_struct *src; struct ndpi_id_struct *src;
struct ndpi_id_struct *dst; struct ndpi_id_struct *dst;
}; };
typedef struct { typedef struct {
char *string_to_match, *string2_to_match, *pattern_to_match, *proto_name; char *string_to_match, *string2_to_match, *pattern_to_match, *proto_name;
int protocol_id; int protocol_id;
@@ -1194,9 +1251,31 @@ typedef struct {
ndpi_protocol_breed_t protocol_breed; ndpi_protocol_breed_t protocol_breed;
} ndpi_protocol_match_result; } ndpi_protocol_match_result;
typedef struct {
char *str;
uint16_t str_len;
} ndpi_string;
/* **************************************** */
struct ndpi_analyze_struct {
uint32_t *values;
uint32_t min_val, max_val, sum_total, num_data_entries, next_value_insert_index;
uint16_t num_values_array_len /* lenght of the values array */;
struct {
float mu, q;
} stddev;
};
#define DEFAULT_SERIES_LEN 64 #define DEFAULT_SERIES_LEN 64
#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;
""" """
cc_ndpi_apis = """ cc_ndpi_apis = """
@@ -1225,6 +1304,8 @@ char* ndpi_protocol2name(struct ndpi_detection_module_struct *ndpi_mod, ndpi_pro
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_id_struct(void);
""" """
@@ -1233,19 +1314,13 @@ class NDPI():
self._ffi = cffi.FFI() self._ffi = cffi.FFI()
if libpath is None: if libpath is None:
if "win" in sys.platform[:3]: if "win" in sys.platform[:3]:
self._ndpi = self._ffi.dlopen(dirname(abspath(__file__)) + '/libs/libndpi.dll') self._ndpi = self._ffi.dlopen(dirname(abspath(__file__)) + '/libs/libndpi.a')
else: else:
self._ndpi = self._ffi.dlopen(dirname(abspath(__file__)) + '/libs/libndpi.so') 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)
self._ffi.cdef(cc_ndpi_network_headers, packed=True) self._ffi.cdef(cc_ndpi_network_headers, packed=True)
self._ffi.cdef(cc_ndpi_id_struct) self._ffi.cdef(cc_ndpi_stuctures)
self._ffi.cdef(cc_ndpi_flow_tcp_struct, packed=True)
self._ffi.cdef(cc_ndpi_flow_udp_struct, packed=True)
self._ffi.cdef(cc_ndpi_int_one_line_struct)
self._ffi.cdef(cc_ndpi_packet_struct_stack, packed=True)
self._ffi.cdef(cc_ndpi_packet_struct)
self._ffi.cdef(cc_ndpi_apis) self._ffi.cdef(cc_ndpi_apis)
self._mod = self._ndpi.ndpi_init_detection_module() self._mod = self._ndpi.ndpi_init_detection_module()
ndpi_revision = self._ffi.string(self._ndpi.ndpi_revision()).decode('utf-8', errors='ignore') ndpi_revision = self._ffi.string(self._ndpi.ndpi_revision()).decode('utf-8', errors='ignore')
@@ -1285,6 +1360,9 @@ class NDPI():
def get_str_field(self, ptr): def get_str_field(self, ptr):
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):
return bytes(self._ffi.buffer(ptr, l)).decode('utf-8', errors='ignore')
def ndpi_protocol2name(self, proto): def ndpi_protocol2name(self, proto):
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))