JA3 hardeding code
This commit is contained in:
+101
-50
@@ -341,7 +341,7 @@ static void stripCertificateTrailer(char *buffer, int buffer_len) {
|
|||||||
|
|
||||||
/* 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 512
|
#define JA3_STR_LEN 1024
|
||||||
#define MAX_NUM_JA3 128
|
#define MAX_NUM_JA3 128
|
||||||
|
|
||||||
struct ja3_info {
|
struct ja3_info {
|
||||||
@@ -358,6 +358,7 @@ int getSSLcertificate(struct ndpi_detection_module_struct *ndpi_struct,
|
|||||||
char *buffer, int buffer_len) {
|
char *buffer, int buffer_len) {
|
||||||
struct ndpi_packet_struct *packet = &flow->packet;
|
struct ndpi_packet_struct *packet = &flow->packet;
|
||||||
struct ja3_info ja3;
|
struct ja3_info ja3;
|
||||||
|
u_int8_t invalid_ja3 = 0;
|
||||||
u_int16_t ssl_version = (packet->payload[1] << 8) + packet->payload[2], ja3_str_len;
|
u_int16_t ssl_version = (packet->payload[1] << 8) + packet->payload[2], ja3_str_len;
|
||||||
char ja3_str[JA3_STR_LEN];
|
char ja3_str[JA3_STR_LEN];
|
||||||
MD5_CTX ctx;
|
MD5_CTX ctx;
|
||||||
@@ -545,23 +546,36 @@ int getSSLcertificate(struct ndpi_detection_module_struct *ndpi_struct,
|
|||||||
printf("SSL [client cypher_len: %u]\n", cypher_len);
|
printf("SSL [client cypher_len: %u]\n", cypher_len);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for(i=0; i<cypher_len;) {
|
if((cypher_offset+cypher_len) <= total_len) {
|
||||||
u_int16_t *id = (u_int16_t*)&packet->payload[cypher_offset+i];
|
for(i=0; i<cypher_len;) {
|
||||||
|
u_int16_t *id = (u_int16_t*)&packet->payload[cypher_offset+i];
|
||||||
|
|
||||||
#ifdef CERTIFICATE_DEBUG
|
#ifdef CERTIFICATE_DEBUG
|
||||||
printf("SSL [cypher suite: %u] [%u/%u]\n", ntohs(*id), i, cypher_len);
|
printf("SSL [cypher suite: %u] [%u/%u]\n", ntohs(*id), i, cypher_len);
|
||||||
#endif
|
#endif
|
||||||
if((*id == 0) || (packet->payload[cypher_offset+i] != packet->payload[cypher_offset+i+1])) {
|
if((*id == 0) || (packet->payload[cypher_offset+i] != packet->payload[cypher_offset+i+1])) {
|
||||||
/*
|
/*
|
||||||
Skip GREASE [https://tools.ietf.org/id/draft-ietf-tls-grease-01.html]
|
Skip GREASE [https://tools.ietf.org/id/draft-ietf-tls-grease-01.html]
|
||||||
https://engineering.salesforce.com/tls-fingerprinting-with-ja3-and-ja3s-247362855967
|
https://engineering.salesforce.com/tls-fingerprinting-with-ja3-and-ja3s-247362855967
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if(ja3.num_cipher < MAX_NUM_JA3)
|
if(ja3.num_cipher < MAX_NUM_JA3)
|
||||||
ja3.cipher[ja3.num_cipher++] = ntohs(*id);
|
ja3.cipher[ja3.num_cipher++] = ntohs(*id);
|
||||||
|
else {
|
||||||
|
invalid_ja3 = 1;
|
||||||
|
#ifdef CERTIFICATE_DEBUG
|
||||||
|
printf("SSL Invalid cypher %u\n", ja3.num_cipher);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 2;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
i += 2;
|
invalid_ja3 = 1;
|
||||||
|
#ifdef CERTIFICATE_DEBUG
|
||||||
|
printf("SSL Invalid len %u vs %u\n", (cypher_offset+cypher_len), total_len);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
offset = base_offset + session_id_len + cypher_len + 2;
|
offset = base_offset + session_id_len + cypher_len + 2;
|
||||||
@@ -615,6 +629,12 @@ int getSSLcertificate(struct ndpi_detection_module_struct *ndpi_struct,
|
|||||||
|
|
||||||
if(ja3.num_ssl_extension < MAX_NUM_JA3)
|
if(ja3.num_ssl_extension < MAX_NUM_JA3)
|
||||||
ja3.ssl_extension[ja3.num_ssl_extension++] = extension_id;
|
ja3.ssl_extension[ja3.num_ssl_extension++] = extension_id;
|
||||||
|
else {
|
||||||
|
invalid_ja3 = 1;
|
||||||
|
#ifdef CERTIFICATE_DEBUG
|
||||||
|
printf("SSL Invalid extensions %u\n", ja3.num_ssl_extension);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(extension_id == 0 /* server name */) {
|
if(extension_id == 0 /* server name */) {
|
||||||
@@ -638,19 +658,32 @@ int getSSLcertificate(struct ndpi_detection_module_struct *ndpi_struct,
|
|||||||
printf("SSL [EllipticCurve: len=%u]\n", extension_len);
|
printf("SSL [EllipticCurve: len=%u]\n", extension_len);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for(i=0; i<extension_len-2;) {
|
if((s_offset+extension_len-1) < total_len) {
|
||||||
u_int16_t s_group = ntohs(*((u_int16_t*)&packet->payload[s_offset+i]));
|
for(i=0; i<extension_len-2;) {
|
||||||
|
u_int16_t s_group = ntohs(*((u_int16_t*)&packet->payload[s_offset+i]));
|
||||||
|
|
||||||
#ifdef CERTIFICATE_DEBUG
|
#ifdef CERTIFICATE_DEBUG
|
||||||
printf("SSL [EllipticCurve: %u]\n", s_group);
|
printf("SSL [EllipticCurve: %u]\n", s_group);
|
||||||
#endif
|
#endif
|
||||||
if((s_group == 0) || (packet->payload[s_offset+i] != packet->payload[s_offset+i+1])) {
|
if((s_group == 0) || (packet->payload[s_offset+i] != packet->payload[s_offset+i+1])) {
|
||||||
/* Skip GREASE */
|
/* Skip GREASE */
|
||||||
if(ja3.num_elliptic_curve < MAX_NUM_JA3)
|
if(ja3.num_elliptic_curve < MAX_NUM_JA3)
|
||||||
ja3.elliptic_curve[ja3.num_elliptic_curve++] = s_group;
|
ja3.elliptic_curve[ja3.num_elliptic_curve++] = s_group;
|
||||||
}
|
else {
|
||||||
|
invalid_ja3 = 1;
|
||||||
|
#ifdef CERTIFICATE_DEBUG
|
||||||
|
printf("SSL Invalid num elliptic %u\n", ja3.num_elliptic_curve);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
i += 2;
|
i += 2;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
invalid_ja3 = 1;
|
||||||
|
#ifdef CERTIFICATE_DEBUG
|
||||||
|
printf("SSL Invalid len %u vs %u\n", (s_offset+extension_len-1), total_len);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
} else if(extension_id == 11 /* ec_point_formats groups */) {
|
} else if(extension_id == 11 /* ec_point_formats groups */) {
|
||||||
u_int16_t i, s_offset = offset+extension_offset + 1;
|
u_int16_t i, s_offset = offset+extension_offset + 1;
|
||||||
@@ -658,16 +691,28 @@ int getSSLcertificate(struct ndpi_detection_module_struct *ndpi_struct,
|
|||||||
#ifdef CERTIFICATE_DEBUG
|
#ifdef CERTIFICATE_DEBUG
|
||||||
printf("SSL [EllipticCurveFormat: len=%u]\n", extension_len);
|
printf("SSL [EllipticCurveFormat: len=%u]\n", extension_len);
|
||||||
#endif
|
#endif
|
||||||
|
if((s_offset+extension_len) < 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 CERTIFICATE_DEBUG
|
#ifdef CERTIFICATE_DEBUG
|
||||||
printf("SSL [EllipticCurveFormat: %u]\n", s_group);
|
printf("SSL [EllipticCurveFormat: %u]\n", s_group);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(ja3.num_elliptic_curve_point_format < MAX_NUM_JA3)
|
if(ja3.num_elliptic_curve_point_format < MAX_NUM_JA3)
|
||||||
ja3.elliptic_curve_point_format[ja3.num_elliptic_curve_point_format++] = s_group;
|
ja3.elliptic_curve_point_format[ja3.num_elliptic_curve_point_format++] = s_group;
|
||||||
|
else {
|
||||||
|
invalid_ja3 = 1;
|
||||||
|
#ifdef CERTIFICATE_DEBUG
|
||||||
|
printf("SSL Invalid num elliptic %u\n", ja3.num_elliptic_curve_point_format);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
invalid_ja3 = 1;
|
||||||
|
#ifdef CERTIFICATE_DEBUG
|
||||||
|
printf("SSL Invalid len %u vs %u\n", s_offset+extension_len, total_len);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -678,45 +723,51 @@ int getSSLcertificate(struct ndpi_detection_module_struct *ndpi_struct,
|
|||||||
#endif
|
#endif
|
||||||
} /* while */
|
} /* while */
|
||||||
|
|
||||||
ja3_str_len = snprintf(ja3_str, sizeof(ja3_str), "%u,", ja3.ssl_version);
|
if(!invalid_ja3) {
|
||||||
|
ja3_str_len = snprintf(ja3_str, sizeof(ja3_str), "%u,", ja3.ssl_version);
|
||||||
|
|
||||||
for(i=0; i<ja3.num_cipher; i++)
|
for(i=0; i<ja3.num_cipher; i++)
|
||||||
ja3_str_len += snprintf(&ja3_str[ja3_str_len], sizeof(ja3_str)-ja3_str_len, "%s%u", (i > 0) ? "-" : "", ja3.cipher[i]);
|
ja3_str_len += snprintf(&ja3_str[ja3_str_len], sizeof(ja3_str)-ja3_str_len, "%s%u",
|
||||||
|
(i > 0) ? "-" : "", ja3.cipher[i]);
|
||||||
|
|
||||||
ja3_str_len += snprintf(&ja3_str[ja3_str_len], sizeof(ja3_str)-ja3_str_len, ",");
|
ja3_str_len += snprintf(&ja3_str[ja3_str_len], sizeof(ja3_str)-ja3_str_len, ",");
|
||||||
|
|
||||||
/* ********** */
|
/* ********** */
|
||||||
|
|
||||||
for(i=0; i<ja3.num_ssl_extension; i++)
|
for(i=0; i<ja3.num_ssl_extension; i++)
|
||||||
ja3_str_len += snprintf(&ja3_str[ja3_str_len], sizeof(ja3_str)-ja3_str_len, "%s%u", (i > 0) ? "-" : "", ja3.ssl_extension[i]);
|
ja3_str_len += snprintf(&ja3_str[ja3_str_len], sizeof(ja3_str)-ja3_str_len, "%s%u",
|
||||||
|
(i > 0) ? "-" : "", ja3.ssl_extension[i]);
|
||||||
|
|
||||||
ja3_str_len += snprintf(&ja3_str[ja3_str_len], sizeof(ja3_str)-ja3_str_len, ",");
|
ja3_str_len += snprintf(&ja3_str[ja3_str_len], sizeof(ja3_str)-ja3_str_len, ",");
|
||||||
|
|
||||||
/* ********** */
|
/* ********** */
|
||||||
|
|
||||||
for(i=0; i<ja3.num_elliptic_curve; i++)
|
for(i=0; i<ja3.num_elliptic_curve; i++)
|
||||||
ja3_str_len += snprintf(&ja3_str[ja3_str_len], sizeof(ja3_str)-ja3_str_len, "%s%u", (i > 0) ? "-" : "", ja3.elliptic_curve[i]);
|
ja3_str_len += snprintf(&ja3_str[ja3_str_len], sizeof(ja3_str)-ja3_str_len, "%s%u",
|
||||||
|
(i > 0) ? "-" : "", ja3.elliptic_curve[i]);
|
||||||
|
|
||||||
ja3_str_len += snprintf(&ja3_str[ja3_str_len], sizeof(ja3_str)-ja3_str_len, ",");
|
ja3_str_len += snprintf(&ja3_str[ja3_str_len], sizeof(ja3_str)-ja3_str_len, ",");
|
||||||
|
|
||||||
for(i=0; i<ja3.num_elliptic_curve_point_format; i++)
|
for(i=0; i<ja3.num_elliptic_curve_point_format; i++)
|
||||||
ja3_str_len += snprintf(&ja3_str[ja3_str_len], sizeof(ja3_str)-ja3_str_len, "%s%u", (i > 0) ? "-" : "", ja3.elliptic_curve_point_format[i]);
|
ja3_str_len += snprintf(&ja3_str[ja3_str_len], sizeof(ja3_str)-ja3_str_len, "%s%u",
|
||||||
|
(i > 0) ? "-" : "", ja3.elliptic_curve_point_format[i]);
|
||||||
|
|
||||||
#ifdef CERTIFICATE_DEBUG
|
#ifdef CERTIFICATE_DEBUG
|
||||||
printf("[JA3] Client: %s \n", ja3_str);
|
printf("[JA3] Client: %s \n", ja3_str);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
MD5Init(&ctx);
|
MD5Init(&ctx);
|
||||||
MD5Update(&ctx, (const unsigned char *)ja3_str, strlen(ja3_str));
|
MD5Update(&ctx, (const unsigned char *)ja3_str, strlen(ja3_str));
|
||||||
MD5Final(md5_hash, &ctx);
|
MD5Final(md5_hash, &ctx);
|
||||||
|
|
||||||
for(i=0, j=0; i<16; i++)
|
for(i=0, j=0; i<16; i++)
|
||||||
j += snprintf(&flow->protos.stun_ssl.ssl.ja3_client[j],
|
j += snprintf(&flow->protos.stun_ssl.ssl.ja3_client[j],
|
||||||
sizeof(flow->protos.stun_ssl.ssl.ja3_client)-j, "%02x", md5_hash[i]);
|
sizeof(flow->protos.stun_ssl.ssl.ja3_client)-j, "%02x", md5_hash[i]);
|
||||||
|
|
||||||
#ifdef CERTIFICATE_DEBUG
|
#ifdef CERTIFICATE_DEBUG
|
||||||
printf("[JA3] Client: %s \n", flow->protos.stun_ssl.ssl.ja3_client);
|
printf("[JA3] Client: %s \n", flow->protos.stun_ssl.ssl.ja3_client);
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
return(2 /* Client Certificate */);
|
return(2 /* Client Certificate */);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user