Added experiemntal JA3+ implementation that can be used with -z i ndpiReader

This commit is contained in:
Luca Deri
2021-03-09 23:38:29 +01:00
parent 8074bc8201
commit f6ad16d8f8
5 changed files with 95 additions and 25 deletions
+18 -13
View File
@@ -80,7 +80,7 @@ u_int8_t enable_protocol_guess = 1, enable_payload_analyzer = 0, num_bin_cluster
u_int8_t verbose = 0, enable_joy_stats = 0; u_int8_t verbose = 0, enable_joy_stats = 0;
int nDPI_LogLevel = 0; int nDPI_LogLevel = 0;
char *_debug_protocols = NULL; char *_debug_protocols = NULL;
u_int8_t human_readeable_string_len = 5; u_int8_t human_readeable_string_len = 5, enable_ja3_plus = 0;
u_int8_t max_num_udp_dissected_pkts = 24 /* 8 is enough for most protocols, Signal and SnapchatCall require more */, max_num_tcp_dissected_pkts = 80 /* due to telnet */; u_int8_t max_num_udp_dissected_pkts = 24 /* 8 is enough for most protocols, Signal and SnapchatCall require more */, max_num_tcp_dissected_pkts = 80 /* due to telnet */;
static u_int32_t pcap_analysis_duration = (u_int32_t)-1; static u_int32_t pcap_analysis_duration = (u_int32_t)-1;
static u_int16_t decode_tunnels = 0; static u_int16_t decode_tunnels = 0;
@@ -274,7 +274,7 @@ void ndpiCheckHostStringMatch(char *testChar) {
if(!testChar) if(!testChar)
return; return;
ndpi_str = ndpi_init_detection_module(ndpi_no_prefs); ndpi_str = ndpi_init_detection_module(enable_ja3_plus ? ndpi_enable_ja3_plus : ndpi_no_prefs);
ndpi_finalize_initialization(ndpi_str); ndpi_finalize_initialization(ndpi_str);
// Display ALL Host strings ie host_match[] ? // Display ALL Host strings ie host_match[] ?
@@ -440,7 +440,7 @@ static void help(u_int long_help) {
"[-f <filter>][-s <duration>][-m <duration>][-b <num bin clusters>]\n" "[-f <filter>][-s <duration>][-m <duration>][-b <num bin clusters>]\n"
" [-p <protos>][-l <loops> [-q][-d][-J][-h][-D][-e <len>][-t][-v <level>]\n" " [-p <protos>][-l <loops> [-q][-d][-J][-h][-D][-e <len>][-t][-v <level>]\n"
" [-n <threads>][-w <file>][-c <file>][-C <file>][-j <file>][-x <file>]\n" " [-n <threads>][-w <file>][-c <file>][-C <file>][-j <file>][-x <file>]\n"
" [-r <file>][-j <file>][-S <file>][-T <num>][-U <num>] [-x <domain>]\n\n" " [-r <file>][-j <file>][-S <file>][-T <num>][-U <num>] [-x <domain>][-z]\n\n"
"Usage:\n" "Usage:\n"
" -i <file.pcap|device> | Specify a pcap file/playlist to read packets from or a\n" " -i <file.pcap|device> | Specify a pcap file/playlist to read packets from or a\n"
" | device for live capture (comma-separated list)\n" " | device for live capture (comma-separated list)\n"
@@ -491,6 +491,7 @@ static void help(u_int long_help) {
" -D | Enable DoH traffic analysis based on content (no DPI)\n" " -D | Enable DoH traffic analysis based on content (no DPI)\n"
" -x <domain> | Check domain name [Test only]\n" " -x <domain> | Check domain name [Test only]\n"
" -I | Ignore VLAN id for flow hash calculation\n" " -I | Ignore VLAN id for flow hash calculation\n"
" -z | Enable JA3+\n"
, ,
human_readeable_string_len, human_readeable_string_len,
min_pattern_len, max_pattern_len, max_num_packets_per_flow, max_packet_payload_dissection, min_pattern_len, max_pattern_len, max_num_packets_per_flow, max_packet_payload_dissection,
@@ -767,7 +768,7 @@ static void parseOptions(int argc, char **argv) {
} }
#endif #endif
while((opt = getopt_long(argc, argv, "b:e:c:C:dDf:g:i:Ij:S:hp:P:l:r:s:tu:v:V:n:Jrp:x:w:q0123:456:7:89:m:T:U:", while((opt = getopt_long(argc, argv, "b:e:c:C:dDf:g:i:Ij:S:hp:pP:l:r:s:tu:v:V:n:Jrp:x:w:zq0123:456:7:89:m:T:U:",
longopts, &option_idx)) != EOF) { longopts, &option_idx)) != EOF) {
#ifdef DEBUG_TRACE #ifdef DEBUG_TRACE
if(trace) fprintf(trace, " #### -%c [%s] #### \n", opt, optarg ? optarg : ""); if(trace) fprintf(trace, " #### -%c [%s] #### \n", opt, optarg ? optarg : "");
@@ -966,6 +967,10 @@ static void parseOptions(int argc, char **argv) {
if(max_num_udp_dissected_pkts < 3) max_num_udp_dissected_pkts = 3; if(max_num_udp_dissected_pkts < 3) max_num_udp_dissected_pkts = 3;
break; break;
case 'z':
enable_ja3_plus = 1;
break;
default: default:
help(0); help(0);
break; break;
@@ -3516,7 +3521,7 @@ static void dgaUnitTest() {
}; };
int i; int i;
NDPI_PROTOCOL_BITMASK all; NDPI_PROTOCOL_BITMASK all;
struct ndpi_detection_module_struct *ndpi_str = ndpi_init_detection_module(ndpi_no_prefs); struct ndpi_detection_module_struct *ndpi_str = ndpi_init_detection_module(enable_ja3_plus ? ndpi_enable_ja3_plus : ndpi_no_prefs);
assert(ndpi_str != NULL); assert(ndpi_str != NULL);
@@ -3922,7 +3927,7 @@ void jitterUnitTest() {
@brief MAIN FUNCTION @brief MAIN FUNCTION
**/ **/
#ifdef APP_HAS_OWN_MAIN #ifdef APP_HAS_OWN_MAIN
int orginal_main(int argc, char **argv) { int original_main(int argc, char **argv) {
#else #else
int main(int argc, char **argv) { int main(int argc, char **argv) {
#endif #endif
@@ -3933,11 +3938,6 @@ int orginal_main(int argc, char **argv) {
return(-1); return(-1);
} }
gettimeofday(&startup_time, NULL);
ndpi_info_mod = ndpi_init_detection_module(ndpi_no_prefs);
if(ndpi_info_mod == NULL) return -1;
// hwUnitTest2(); // hwUnitTest2();
/* Internal checks */ /* Internal checks */
@@ -3954,17 +3954,22 @@ int orginal_main(int argc, char **argv) {
ndpi_self_check_host_match(); ndpi_self_check_host_match();
analysisUnitTest(); analysisUnitTest();
rulesUnitTest(); rulesUnitTest();
gettimeofday(&startup_time, NULL);
memset(ndpi_thread_info, 0, sizeof(ndpi_thread_info)); memset(ndpi_thread_info, 0, sizeof(ndpi_thread_info));
parseOptions(argc, argv); parseOptions(argc, argv);
ndpi_info_mod = ndpi_init_detection_module(enable_ja3_plus ? ndpi_enable_ja3_plus : ndpi_no_prefs);
if(ndpi_info_mod == NULL) return -1;
if(domain_to_check) { if(domain_to_check) {
ndpiCheckHostStringMatch(domain_to_check); ndpiCheckHostStringMatch(domain_to_check);
exit(0); exit(0);
} }
if(enable_doh_dot_detection) init_doh_bins();
if(!quiet_mode) { if(!quiet_mode) {
printf("\n-----------------------------------------------------------\n" printf("\n-----------------------------------------------------------\n"
"* NOTE: This is demo app to show *some* nDPI features.\n" "* NOTE: This is demo app to show *some* nDPI features.\n"
+3 -1
View File
@@ -402,8 +402,10 @@ static int parse_debug_proto(struct ndpi_detection_module_struct *ndpi_mod, char
/* ***************************************************** */ /* ***************************************************** */
extern char *_debug_protocols; extern char *_debug_protocols;
extern u_int8_t enable_ja3_plus;
static int _debug_protocols_ok = 0; static int _debug_protocols_ok = 0;
struct ndpi_workflow* ndpi_workflow_init(const struct ndpi_workflow_prefs * prefs, struct ndpi_workflow* ndpi_workflow_init(const struct ndpi_workflow_prefs * prefs,
pcap_t * pcap_handle) { pcap_t * pcap_handle) {
struct ndpi_detection_module_struct * module; struct ndpi_detection_module_struct * module;
@@ -413,7 +415,7 @@ struct ndpi_workflow* ndpi_workflow_init(const struct ndpi_workflow_prefs * pref
set_ndpi_flow_malloc(NULL), set_ndpi_flow_free(NULL); set_ndpi_flow_malloc(NULL), set_ndpi_flow_free(NULL);
/* TODO: just needed here to init ndpi ndpi_malloc wrapper */ /* TODO: just needed here to init ndpi ndpi_malloc wrapper */
module = ndpi_init_detection_module(ndpi_no_prefs); module = ndpi_init_detection_module(enable_ja3_plus ? ndpi_enable_ja3_plus : ndpi_no_prefs);
if(module == NULL) { if(module == NULL) {
LOG(NDPI_LOG_ERROR, "global structure initialization failed\n"); LOG(NDPI_LOG_ERROR, "global structure initialization failed\n");
+2 -1
View File
@@ -1050,7 +1050,7 @@ struct ndpi_detection_module_struct {
u_int32_t current_ts; u_int32_t current_ts;
u_int32_t ticks_per_second; u_int32_t ticks_per_second;
u_int16_t num_tls_blocks_to_follow; u_int16_t num_tls_blocks_to_follow;
u_int8_t skip_tls_blocks_until_change_cipher:1, _notused:7; u_int8_t skip_tls_blocks_until_change_cipher:1, enable_ja3_plus:1, _notused:6;
#ifdef NDPI_ENABLE_DEBUG_MESSAGES #ifdef NDPI_ENABLE_DEBUG_MESSAGES
void *user_data; void *user_data;
@@ -1453,6 +1453,7 @@ typedef enum
ndpi_no_prefs = 0, ndpi_no_prefs = 0,
ndpi_dont_load_tor_hosts, ndpi_dont_load_tor_hosts,
ndpi_dont_init_libgcrypt, ndpi_dont_init_libgcrypt,
ndpi_enable_ja3_plus
} ndpi_prefs; } ndpi_prefs;
typedef struct { typedef struct {
+3
View File
@@ -2156,6 +2156,9 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(ndpi_init_prefs
NDPI_BITMASK_RESET(ndpi_str->debug_bitmask); NDPI_BITMASK_RESET(ndpi_str->debug_bitmask);
#endif /* NDPI_ENABLE_DEBUG_MESSAGES */ #endif /* NDPI_ENABLE_DEBUG_MESSAGES */
if(prefs & ndpi_enable_ja3_plus)
ndpi_str->enable_ja3_plus = 1;
#ifdef HAVE_LIBGCRYPT #ifdef HAVE_LIBGCRYPT
if(!(prefs & ndpi_dont_init_libgcrypt)) { if(!(prefs & ndpi_dont_init_libgcrypt)) {
if(!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) { if(!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) {
+63 -4
View File
@@ -1006,8 +1006,9 @@ static void ndpi_int_tls_add_connection(struct ndpi_detection_module_struct *ndp
/* https://engineering.salesforce.com/tls-fingerprinting-with-ja3-and-ja3s-247362855967 */ /* https://engineering.salesforce.com/tls-fingerprinting-with-ja3-and-ja3s-247362855967 */
#define JA3_STR_LEN 1024 #define JA3_STR_LEN 1024
#define MAX_NUM_JA3 512 #define MAX_NUM_JA3 512
#define MAX_JA3_STRLEN 128
struct ja3_info { struct ja3_info {
u_int16_t tls_handshake_version; u_int16_t tls_handshake_version;
@@ -1015,6 +1016,7 @@ struct ja3_info {
u_int16_t num_tls_extension, tls_extension[MAX_NUM_JA3]; u_int16_t num_tls_extension, tls_extension[MAX_NUM_JA3];
u_int16_t num_elliptic_curve, elliptic_curve[MAX_NUM_JA3]; u_int16_t num_elliptic_curve, elliptic_curve[MAX_NUM_JA3];
u_int16_t num_elliptic_curve_point_format, elliptic_curve_point_format[MAX_NUM_JA3]; u_int16_t num_elliptic_curve_point_format, elliptic_curve_point_format[MAX_NUM_JA3];
char signature_algorithms[MAX_JA3_STRLEN], supported_versions[MAX_JA3_STRLEN], alpn[MAX_JA3_STRLEN];
}; };
/* **************************************** */ /* **************************************** */
@@ -1394,7 +1396,7 @@ int processClientServerHello(struct ndpi_detection_module_struct *ndpi_struct,
printf("Client SSL [EllipticCurveFormat: len=%u]\n", extension_len); printf("Client SSL [EllipticCurveFormat: len=%u]\n", extension_len);
#endif #endif
if((s_offset+extension_len-1) <= total_len) { if((s_offset+extension_len-1) <= total_len) {
for(i=0; i<extension_len-1;i++) { for(i=0; i<extension_len-1; i++) {
u_int8_t s_group = packet->payload[s_offset+i]; u_int8_t s_group = packet->payload[s_offset+i];
#ifdef DEBUG_TLS #ifdef DEBUG_TLS
@@ -1416,11 +1418,55 @@ int processClientServerHello(struct ndpi_detection_module_struct *ndpi_struct,
printf("Client SSL Invalid len %u vs %u\n", s_offset+extension_len, total_len); printf("Client SSL Invalid len %u vs %u\n", s_offset+extension_len, total_len);
#endif #endif
} }
} else if(extension_id == 13 /* signature algorithms */) {
u_int16_t s_offset = offset+extension_offset;
u_int16_t tot_signature_algorithms_len = ntohs(*((u_int16_t*)&packet->payload[s_offset]));
#ifdef DEBUG_TLS
printf("Client SSL [SIGNATURE_ALGORITHMS: block_len=%u/len=%u]\n", extension_len, tot_signature_algorithms_len);
#endif
s_offset += 2;
tot_signature_algorithms_len = ndpi_min((sizeof(ja3.signature_algorithms) / 2) - 1, tot_signature_algorithms_len);
for(i=0; i<tot_signature_algorithms_len; i++) {
int rc = snprintf(&ja3.signature_algorithms[i*2], sizeof(ja3.signature_algorithms)-i*2, "%02X", packet->payload[s_offset+i]);
if(rc < 0) break;
}
ja3.signature_algorithms[i*2] = '\0';
#ifdef DEBUG_TLS
printf("Client SSL [SIGNATURE_ALGORITHMS: %s]\n", ja3.signature_algorithms);
#endif
} else if(extension_id == 43 /* supported versions */) {
u_int16_t s_offset = offset+extension_offset;
u_int8_t tot_supported_versions_len = (u_int8_t)packet->payload[s_offset];
#ifdef DEBUG_TLS
printf("Client SSL [SUPPORTED_VERSIONS: block_len=%u/len=%u]\n", extension_len, tot_supported_versions_len);
#endif
s_offset += 1;
tot_supported_versions_len = ndpi_min((sizeof(ja3.supported_versions) / 2) - 1, tot_supported_versions_len);
for(i=0; i<tot_supported_versions_len; i++) {
int rc = snprintf(&ja3.supported_versions[i*2], sizeof(ja3.supported_versions)-i*2, "%02X", packet->payload[s_offset+i]);
if(rc < 0) break;
}
ja3.supported_versions[i*2] = '\0';
#ifdef DEBUG_TLS
printf("Client SSL [SUPPORTED_VERSIONS: %s]\n", ja3.supported_versions);
#endif
} else if(extension_id == 16 /* application_layer_protocol_negotiation */) { } else if(extension_id == 16 /* application_layer_protocol_negotiation */) {
u_int16_t s_offset = offset+extension_offset; u_int16_t s_offset = offset+extension_offset;
u_int16_t tot_alpn_len = ntohs(*((u_int16_t*)&packet->payload[s_offset])); u_int16_t tot_alpn_len = ntohs(*((u_int16_t*)&packet->payload[s_offset]));
char alpn_str[256]; char alpn_str[256];
u_int8_t alpn_str_len = 0; u_int8_t alpn_str_len = 0, i;
#ifdef DEBUG_TLS #ifdef DEBUG_TLS
printf("Client SSL [ALPN: block_len=%u/len=%u]\n", extension_len, tot_alpn_len); printf("Client SSL [ALPN: block_len=%u/len=%u]\n", extension_len, tot_alpn_len);
@@ -1459,6 +1505,13 @@ int processClientServerHello(struct ndpi_detection_module_struct *ndpi_struct,
#endif #endif
if(flow->protos.tls_quic_stun.tls_quic.alpn == NULL) if(flow->protos.tls_quic_stun.tls_quic.alpn == NULL)
flow->protos.tls_quic_stun.tls_quic.alpn = ndpi_strdup(alpn_str); flow->protos.tls_quic_stun.tls_quic.alpn = ndpi_strdup(alpn_str);
snprintf(ja3.alpn, sizeof(ja3.alpn), "%s", alpn_str);
/* Replace , with - as in JA3 */
for(i=0; ja3.alpn[i] != '\0'; i++)
if(ja3.alpn[i] == ',') ja3.alpn[i] = '-';
} else if(extension_id == 43 /* supported versions */) { } else if(extension_id == 43 /* supported versions */) {
u_int16_t s_offset = offset+extension_offset; u_int16_t s_offset = offset+extension_offset;
u_int8_t version_len = packet->payload[s_offset]; u_int8_t version_len = packet->payload[s_offset];
@@ -1656,6 +1709,12 @@ int processClientServerHello(struct ndpi_detection_module_struct *ndpi_struct,
if(rc > 0 && ja3_str_len + rc < JA3_STR_LEN) ja3_str_len += rc; else break; if(rc > 0 && ja3_str_len + rc < JA3_STR_LEN) ja3_str_len += rc; else break;
} }
if(ndpi_struct->enable_ja3_plus) {
rc = snprintf(&ja3_str[ja3_str_len], sizeof(ja3_str)-ja3_str_len,
",%s,%s,%s", ja3.signature_algorithms, ja3.supported_versions, ja3.alpn);
if(rc > 0 && ja3_str_len + rc < JA3_STR_LEN) ja3_str_len += rc;
}
#ifdef DEBUG_TLS #ifdef DEBUG_TLS
printf("[JA3] Client: %s \n", ja3_str); printf("[JA3] Client: %s \n", ja3_str);
#endif #endif