-
Notifications
You must be signed in to change notification settings - Fork 2
/
ja4plus.c
1282 lines (1091 loc) · 43 KB
/
ja4plus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ja4plus.c -- ja4+ implementation
*
* Copyright 2023 AOL Inc. All rights reserved.
* Portions Copyright 2023 FoxIO
*
* SPDX-License-Identifier: FoxIO License 1.1
*
* This software requires a license to use. See
* https://github.com/FoxIO-LLC/ja4#licensing
* https://github.com/FoxIO-LLC/ja4/blob/main/License%20FAQ.md
*/
#include "arkime.h"
#include "../parsers/ssh_info.h"
#include <math.h>
extern ArkimeConfig_t config;
LOCAL int ja4sField;
LOCAL int ja4sRawField;
LOCAL int ja4sshField;
LOCAL int ja4lField;
LOCAL int ja4lsField;
LOCAL int ja4tField;
LOCAL int ja4tsField;
LOCAL int ja4hField;
LOCAL int ja4hRawField;
LOCAL int ja4plus_plugin_num;
LOCAL GChecksum *checksums256[ARKIME_MAX_PACKET_THREADS];
extern uint8_t arkime_char_to_hexstr[256][3];
LOCAL gboolean ja4Raw;
#define JA4PLUS_SYN_ACK_COUNT 4
typedef struct {
// Used for JA4L
// Timestamps are reference against firstPacket
uint32_t timestampA;
//timestampB = synAckTimes[synAckTimesCnt - 1]
uint32_t timestampC;
uint32_t timestampD;
uint32_t timestampE;
uint32_t synAckTimes[JA4PLUS_SYN_ACK_COUNT];
uint8_t client_ttl;
uint8_t server_ttl;
uint8_t synAckTimesCnt: 3;
} JA4PlusTCP_t;
typedef struct {
GString *header_value; // current header value
GString *header_fields;
uint16_t cookies;
uint16_t referer;
uint16_t headers;
char state;
gchar *sorted_cookie_fields;
gchar *sorted_cookie_values;
gchar accept_lang[4];
} JA4PlusHTTP_t;
#define JA4PLUS_TCP_DONE (void *)1UL
typedef struct {
JA4PlusTCP_t *tcp;
JA4PlusHTTP_t *http;
} JA4PlusData_t;
typedef struct {
char *field;
char *value;
uint16_t flen;
uint16_t vlen;
} JA4PlusCookie_t;
#define TIMESTAMP_TO_RUSEC(ts) (ts.tv_sec - session->firstPacket.tv_sec) * 1000000 + (ts.tv_usec - session->firstPacket.tv_usec)
/******************************************************************************/
LOCAL int cookie_cmp(const void *a, const void *b)
{
return strcmp(((JA4PlusCookie_t *)a)->field, ((JA4PlusCookie_t *)b)->field);
}
/******************************************************************************/
/* Actually process the cookie/accept-language header that has been saved up. */
LOCAL void ja4plus_http_process_headers (ArkimeSession_t *session)
{
JA4PlusData_t *ja4plus_data = (JA4PlusData_t *) session->pluginData[ja4plus_plugin_num];
JA4PlusHTTP_t *ja4_http = ja4plus_data->http;
if (ja4_http->state == 'c') {
int num = 0;
JA4PlusCookie_t cookies[100];
const char *start = ja4_http->header_value->str;
const char *end = start + ja4_http->header_value->len;
int totalFlen = 0;
int totalVlen = 0;
while (start < end) {
while (start < end && isspace(*start)) start++;
char *equal = memchr(start, '=', end - start);
if (!equal)
break;
int flen = equal - start;
cookies[num].field = g_strndup(start, flen); // COPY
cookies[num].flen = flen;
totalFlen += flen;
start = memchr(equal + 1, ';', end - (equal + 1));
equal++;
while (equal < end && isspace(*equal)) equal++;
if (equal < end && equal != start) {
int vlen = start ? start - equal : end - equal;
cookies[num].vlen = vlen;
totalVlen += vlen;
cookies[num].value = equal; // NO COPY
} else {
cookies[num].value = 0;
cookies[num].vlen = 0;
}
num++;
if (num == 99)
break;
if (!start)
break;
start++;
}
ja4_http->cookies = num;
if (num != 0) {
qsort(cookies, num, sizeof(JA4PlusCookie_t), cookie_cmp);
g_free(ja4_http->sorted_cookie_fields);
ja4_http->sorted_cookie_fields = g_malloc(totalFlen + num);
g_free(ja4_http->sorted_cookie_values);
ja4_http->sorted_cookie_values = g_malloc(totalFlen + num + totalVlen + num);
char *fpos = ja4_http->sorted_cookie_fields;
char *fvpos = ja4_http->sorted_cookie_values;
for (int i = 0; i < num; i++) {
memcpy(fpos, cookies[i].field, cookies[i].flen);
fpos += cookies[i].flen;
*(fpos++) = ',';
memcpy(fvpos, cookies[i].field, cookies[i].flen);
fvpos += cookies[i].flen;
if (cookies[i].value) {
*(fvpos++) = '=';
memcpy(fvpos, cookies[i].value, cookies[i].vlen);
fvpos += cookies[i].vlen;
}
*(fvpos++) = ',';
}
*(fpos - 1) = 0;
*(fvpos - 1) = 0;
for (int i = 0; i < num; i++) {
g_free(cookies[i].field);
}
}
} else if (ja4_http->state == 'a') {
const char *lang = ja4_http->header_value->str;
size_t l = 0, a = 0;;
while (l < ja4_http->header_value->len && a < 4) {
if (isspace(lang[l]) || lang[l] == '-') {
l++;
continue;
} else if (lang[l] == ',' || lang[l] == ';') {
break;
}
ja4_http->accept_lang[a] = tolower(lang[l]);
a++;
l++;
}
}
ja4_http->state = 0;
g_string_truncate(ja4_http->header_value, 0);
}
/******************************************************************************/
/* An http msg is complete, process the headers and create the ja4h */
LOCAL void ja4plus_http_complete(ArkimeSession_t *session, http_parser *parser)
{
if (parser->type != 0)
return;
JA4PlusData_t *ja4plus_data = (JA4PlusData_t *) session->pluginData[ja4plus_plugin_num];
if (!ja4plus_data)
return;
JA4PlusHTTP_t *ja4_http = ja4plus_data->http;
if (!ja4_http)
return;
char ja4h[52];
if (!ja4_http->header_fields)
return;
if (ja4_http->state != 0) {
ja4plus_http_process_headers(session);
}
char *method = g_ascii_strdown(http_method_str(parser->method), 2);
GChecksum *const checksum = checksums256[session->thread];
snprintf(ja4h, sizeof(ja4h), "%s%d%d%c%c%02d%4.4s_",
method,
parser->http_major,
parser->http_minor,
(ja4_http->cookies == 0) ? 'n' : 'c',
(ja4_http->referer == 0) ? 'n' : 'r',
ja4_http->headers,
ja4_http->accept_lang
);
g_checksum_update(checksum, (guchar *)ja4_http->header_fields->str, ja4_http->header_fields->len);
memcpy(ja4h + 13, g_checksum_get_string(checksum), 12);
g_checksum_reset(checksum);
ja4h[25] = '_';
if (ja4_http->cookies) {
g_checksum_update(checksum, (guchar *) ja4_http->sorted_cookie_fields, strlen(ja4_http->sorted_cookie_fields));
memcpy(ja4h + 26, g_checksum_get_string(checksum), 12);
g_checksum_reset(checksum);
ja4h[38] = '_';
g_checksum_update(checksum, (guchar *) ja4_http->sorted_cookie_values, strlen(ja4_http->sorted_cookie_values));
memcpy(ja4h + 39, g_checksum_get_string(checksum), 12);
g_checksum_reset(checksum);
} else {
g_strlcpy(ja4h + 26, "000000000000_000000000000", sizeof(ja4h) - 26);
}
ja4h[51] = 0;
arkime_field_string_add(ja4hField, session, ja4h, 51, TRUE);
if (ja4Raw) {
char ja4h_r[1024];
snprintf(ja4h_r, sizeof(ja4h_r), "%s%d%d%c%c%02d%4.4s_%s_%s_%s",
method,
parser->http_major,
parser->http_minor,
(ja4_http->cookies == 0) ? 'n' : 'c',
(ja4_http->referer == 0) ? 'n' : 'r',
ja4_http->headers,
ja4_http->accept_lang,
ja4_http->header_fields->str,
(ja4_http->sorted_cookie_fields != NULL) ? ja4_http->sorted_cookie_fields : "",
(ja4_http->sorted_cookie_values != NULL) ? ja4_http->sorted_cookie_values : ""
);
ja4h_r[sizeof(ja4h_r) - 1] = 0;
arkime_field_string_add(ja4hRawField, session, ja4h_r, -1, TRUE);
}
g_free(method);
g_string_truncate(ja4_http->header_fields, 0);
g_free(ja4_http->sorted_cookie_fields);
ja4_http->sorted_cookie_fields = 0;
g_free(ja4_http->sorted_cookie_values);
ja4_http->sorted_cookie_values = 0;
// Reset
ja4_http->state = 0;
memcpy(ja4_http->accept_lang, "0000", 4);
ja4_http->cookies = 0;
ja4_http->referer = 0;
ja4_http->headers = 0;
}
/******************************************************************************/
LOCAL void ja4plus_http_header_field_raw (ArkimeSession_t *session, http_parser *hp, const char *at, size_t length)
{
if (!at || hp->type != 0)
return;
JA4PlusData_t *ja4plus_data = (JA4PlusData_t *) session->pluginData[ja4plus_plugin_num];
if (!ja4plus_data) {
ja4plus_data = session->pluginData[ja4plus_plugin_num] = ARKIME_TYPE_ALLOC0 (JA4PlusData_t);
}
JA4PlusHTTP_t *ja4_http = ja4plus_data->http;
if (!ja4plus_data->http) {
ja4_http = ja4plus_data->http = ARKIME_TYPE_ALLOC0 (JA4PlusHTTP_t);
ja4_http->header_value = g_string_sized_new(100);
ja4_http->header_fields = g_string_sized_new(100);
memcpy(ja4_http->accept_lang, "0000", 4);
}
if (ja4_http->state != 0) {
ja4plus_http_process_headers(session);
}
char *header_field = g_ascii_strdown(at, length);
if (strcmp(header_field, "cookie") == 0) {
ja4_http->state = 'c';
} else if (strcmp(header_field, "referer") == 0) {
ja4_http->referer = 1;
} else {
if (ja4_http->headers > 0) {
g_string_append_len(ja4_http->header_fields, ",", 1);
}
g_string_append_len(ja4_http->header_fields, at, length);
ja4_http->headers++;
if (strcmp(header_field, "accept-language") == 0) {
ja4_http->state = 'a';
} else {
ja4_http->state = 0;
}
}
g_free(header_field);
}
/******************************************************************************/
/* New partial value is coming in, append it to the current value if we are in a cookie/accept-language */
LOCAL void ja4plus_http_header_value (ArkimeSession_t *session, http_parser *hp, const char *at, size_t length)
{
if (!at || hp->type != 0)
return;
JA4PlusData_t *ja4plus_data = (JA4PlusData_t *) session->pluginData[ja4plus_plugin_num];
JA4PlusHTTP_t *ja4_http = ja4plus_data->http;
if (ja4_http->state == 0)
return;
g_string_append_len(ja4_http->header_value, at, length);
}
/******************************************************************************/
// https://tools.ietf.org/html/draft-davidben-tls-grease-00
LOCAL int ja4plus_is_grease_value(uint32_t val)
{
if ((val & 0x0f) != 0x0a)
return 0;
if ((val & 0xff) != ((val >> 8) & 0xff))
return 0;
return 1;
}
/******************************************************************************/
LOCAL void ja4plus_ja4_version(uint16_t ver, char dtls, char vstr[3])
{
switch (ver) {
case 0x0100:
memcpy(vstr, "s1", 3);
break;
case 0x0200:
memcpy(vstr, "s2", 3);
break;
case 0x0300:
memcpy(vstr, "s3", 3);
break;
case 0x0301:
memcpy(vstr, "10", 3);
break;
case 0x0302:
memcpy(vstr, "11", 3);
break;
case 0x0303:
memcpy(vstr, "12", 3);
break;
case 0x0304:
memcpy(vstr, "13", 3);
break;
case 0xfeff:
if (dtls)
memcpy(vstr, "d1", 3);
else
memcpy(vstr, "00", 3);
break;
case 0xfefd:
if (dtls)
memcpy(vstr, "d2", 3);
else
memcpy(vstr, "00", 3);
break;
case 0xfefc:
if (dtls)
memcpy(vstr, "d3", 3);
else
memcpy(vstr, "00", 3);
break;
default:
memcpy(vstr, "00", 3);
break;
}
}
/******************************************************************************/
LOCAL void ja4plus_2digit_to_string(int val, char *str)
{
if (val >= 99) {
str[0] = '9';
str[1] = '9';
return;
}
str[0] = (val / 10) + '0';
str[1] = (val % 10) + '0';
}
/******************************************************************************/
LOCAL void ja4plus_alpn_to_ja4alpn(const uint8_t *alpn, int len, uint8_t *ja4alpn)
{
if (len == 0)
return;
len--; // len now the offset of last byte, which could be 0
if (isalnum(alpn[0]) && isalnum(alpn[len])) {
ja4alpn[0] = tolower(alpn[0]);
ja4alpn[1] = tolower(alpn[len]);
} else {
ja4alpn[0] = arkime_char_to_hexstr[alpn[0]][0];
ja4alpn[1] = arkime_char_to_hexstr[alpn[len]][1];
}
}
/******************************************************************************/
LOCAL uint32_t ja4plus_dtls_process_server_hello(ArkimeSession_t *session, const uint8_t *data, int len, void UNUSED(*uw))
{
// https://github.com/FoxIO-LLC/ja4/blob/main/technical_details/JA4S.md
uint8_t ja4NumExtensions = 0;
uint16_t ja4Extensions[256];
uint8_t ja4ALPN[2] = {'0', '0'};
BSB bsb;
BSB_INIT(bsb, data, len);
uint16_t ver = 0;
uint16_t supportedver;
BSB_IMPORT_u16(bsb, ver);
supportedver = ver;
BSB_IMPORT_skip(bsb, 32); // Random
if (BSB_IS_ERROR(bsb))
return -1;
int skiplen = 0;
BSB_IMPORT_u08(bsb, skiplen); // Session Id Length
BSB_IMPORT_skip(bsb, skiplen); // Session Id
uint16_t cipher = 0;
BSB_IMPORT_u16(bsb, cipher);
char cipherHex[5];
snprintf(cipherHex, sizeof(cipherHex), "%04x", cipher);
BSB_IMPORT_skip(bsb, 1);
if (BSB_REMAINING(bsb) > 2) {
int etotlen = 0;
BSB_IMPORT_u16(bsb, etotlen); // Extensions Length
etotlen = MIN(etotlen, BSB_REMAINING(bsb));
BSB ebsb;
BSB_INIT(ebsb, BSB_WORK_PTR(bsb), etotlen);
while (BSB_REMAINING(ebsb) > 0) {
int etype = 0, elen = 0;
BSB_IMPORT_u16 (ebsb, etype);
BSB_IMPORT_u16 (ebsb, elen);
if (ja4plus_is_grease_value(etype)) {
BSB_IMPORT_skip (ebsb, elen);
continue;
}
ja4Extensions[ja4NumExtensions] = etype;
ja4NumExtensions++;
if (elen > BSB_REMAINING(ebsb))
break;
if (etype == 0x2b && elen == 2) { // etype 0x2b is supported version
BSB_IMPORT_u16(ebsb, supportedver);
supportedver = MAX(ver, supportedver);
continue; // Already processed ebsb above
}
if (etype == 0x10) { // ALPN
BSB alpnBsb;
BSB_IMPORT_bsb (ebsb, alpnBsb, elen);
BSB_IMPORT_skip (alpnBsb, 2); // len
uint8_t plen = 0;
BSB_IMPORT_u08 (alpnBsb, plen); // len
const unsigned char *pstr = NULL;
BSB_IMPORT_ptr (alpnBsb, pstr, plen);
if (plen > 0 && pstr && !BSB_IS_ERROR(alpnBsb)) {
ja4plus_alpn_to_ja4alpn(pstr, plen, ja4ALPN);
}
continue; // Already processed ebsb above
}
BSB_IMPORT_skip (ebsb, elen);
}
}
// JA4s Creation
char vstr[3];
ja4plus_ja4_version(supportedver, TRUE, vstr);
char ja4s[26];
ja4s[25] = 0;
ja4s[0] = 'd';
ja4s[1] = vstr[0];
ja4s[2] = vstr[1];
ja4plus_2digit_to_string(ja4NumExtensions, ja4s + 3);
ja4s[5] = ja4ALPN[0];
ja4s[6] = ja4ALPN[1];
ja4s[7] = '_';
memcpy(ja4s + 8, cipherHex, 4);
ja4s[12] = '_';
char tmpBuf[5 * 256];
BSB tmpBSB;
BSB_INIT(tmpBSB, tmpBuf, sizeof(tmpBuf));
for (int i = 0; i < ja4NumExtensions; i++) {
BSB_EXPORT_sprintf(tmpBSB, "%04x,", ja4Extensions[i]);
}
if (ja4NumExtensions > 0) {
BSB_EXPORT_rewind(tmpBSB, 1); // Remove last ,
}
GChecksum *const checksum = checksums256[session->thread];
if (BSB_LENGTH(tmpBSB) > 0) {
g_checksum_update(checksum, (guchar *)tmpBuf, BSB_LENGTH(tmpBSB));
memcpy(ja4s + 13, g_checksum_get_string(checksum), 12);
g_checksum_reset(checksum);
} else {
memcpy(ja4s + 13, "000000000000", 12);
}
arkime_field_string_add(ja4sField, session, ja4s, 25, TRUE);
if (ja4Raw) {
char ja4s_r[13 + 5 * 256];
memcpy(ja4s_r, ja4s, 13);
memcpy(ja4s_r + 13, tmpBuf, BSB_LENGTH(tmpBSB));
arkime_field_string_add(ja4sRawField, session, ja4s_r, 13 + BSB_LENGTH(tmpBSB), TRUE);
}
return 0;
}
/******************************************************************************/
LOCAL uint32_t ja4plus_tls_process_server_hello(ArkimeSession_t *session, const uint8_t *data, int len, void UNUSED(*uw))
{
// https://github.com/FoxIO-LLC/ja4/blob/main/technical_details/JA4S.md
uint8_t ja4NumExtensions = 0;
uint16_t ja4Extensions[256];
uint8_t ja4ALPN[2] = {'0', '0'};
BSB bsb;
BSB_INIT(bsb, data, len);
uint16_t ver = 0;
uint16_t supportedver;
BSB_IMPORT_u16(bsb, ver);
supportedver = ver;
BSB_IMPORT_skip(bsb, 32); // Random
if (BSB_IS_ERROR(bsb))
return -1;
/* Parse sessionid, only for SSLv3 - TLSv1.2 */
if (ver >= 0x0300 && ver <= 0x0303) {
int skiplen = 0;
BSB_IMPORT_u08(bsb, skiplen); // Session Id Length
BSB_IMPORT_skip(bsb, skiplen); // Session Id
}
uint16_t cipher = 0;
BSB_IMPORT_u16(bsb, cipher);
char cipherHex[5];
snprintf(cipherHex, sizeof(cipherHex), "%04x", cipher);
/* Thanks wireshark - No compression with TLS 1.3 before draft -22 */
if (ver < 0x0700 || ver >= 0x7f16) {
BSB_IMPORT_skip(bsb, 1);
}
if (BSB_REMAINING(bsb) > 2) {
int etotlen = 0;
BSB_IMPORT_u16(bsb, etotlen); // Extensions Length
etotlen = MIN(etotlen, BSB_REMAINING(bsb));
BSB ebsb;
BSB_INIT(ebsb, BSB_WORK_PTR(bsb), etotlen);
while (BSB_REMAINING(ebsb) > 0) {
int etype = 0, elen = 0;
BSB_IMPORT_u16 (ebsb, etype);
BSB_IMPORT_u16 (ebsb, elen);
if (ja4plus_is_grease_value(etype)) {
BSB_IMPORT_skip (ebsb, elen);
continue;
}
ja4Extensions[ja4NumExtensions] = etype;
ja4NumExtensions++;
if (elen > BSB_REMAINING(ebsb))
break;
if (etype == 0x2b && elen == 2) { // etype 0x2b is supported version
BSB_IMPORT_u16(ebsb, supportedver);
supportedver = MAX(ver, supportedver);
continue; // Already processed ebsb above
}
if (etype == 0x10) { // ALPN
BSB alpnBsb;
BSB_IMPORT_bsb (ebsb, alpnBsb, elen);
BSB_IMPORT_skip (alpnBsb, 2); // len
uint8_t plen = 0;
BSB_IMPORT_u08 (alpnBsb, plen); // len
const unsigned char *pstr = NULL;
BSB_IMPORT_ptr (alpnBsb, pstr, plen);
if (plen > 0 && pstr && !BSB_IS_ERROR(alpnBsb)) {
ja4plus_alpn_to_ja4alpn(pstr, plen, ja4ALPN);
}
continue; // Already processed ebsb above
}
BSB_IMPORT_skip (ebsb, elen);
}
}
// JA4s Creation
char vstr[3];
ja4plus_ja4_version(supportedver, FALSE, vstr);
char ja4s[26];
ja4s[25] = 0;
ja4s[0] = (session->ipProtocol == IPPROTO_TCP) ? 't' : 'q';
ja4s[1] = vstr[0];
ja4s[2] = vstr[1];
ja4plus_2digit_to_string(ja4NumExtensions, ja4s + 3);
ja4s[5] = ja4ALPN[0];
ja4s[6] = ja4ALPN[1];
ja4s[7] = '_';
memcpy(ja4s + 8, cipherHex, 4);
ja4s[12] = '_';
char tmpBuf[5 * 256];
BSB tmpBSB;
BSB_INIT(tmpBSB, tmpBuf, sizeof(tmpBuf));
for (int i = 0; i < ja4NumExtensions; i++) {
BSB_EXPORT_sprintf(tmpBSB, "%04x,", ja4Extensions[i]);
}
if (ja4NumExtensions > 0) {
BSB_EXPORT_rewind(tmpBSB, 1); // Remove last ,
}
GChecksum *const checksum = checksums256[session->thread];
if (BSB_LENGTH(tmpBSB) > 0) {
g_checksum_update(checksum, (guchar *)tmpBuf, BSB_LENGTH(tmpBSB));
memcpy(ja4s + 13, g_checksum_get_string(checksum), 12);
g_checksum_reset(checksum);
} else {
memcpy(ja4s + 13, "000000000000", 12);
}
arkime_field_string_add(ja4sField, session, ja4s, 25, TRUE);
if (ja4Raw) {
char ja4s_r[13 + 5 * 256];
memcpy(ja4s_r, ja4s, 13);
memcpy(ja4s_r + 13, tmpBuf, BSB_LENGTH(tmpBSB));
arkime_field_string_add(ja4sRawField, session, ja4s_r, 13 + BSB_LENGTH(tmpBSB), TRUE);
}
return 0;
}
/******************************************************************************/
LOCAL void ja4plus_cert_process_rdn(BSB *bsb, BSB *out)
{
uint32_t apc, atag, alen;
while (BSB_REMAINING(*bsb) > 3) {
uint8_t *value = arkime_parsers_asn_get_tlv(bsb, &apc, &atag, &alen);
if (!value)
return;
if (apc) {
BSB tbsb;
BSB_INIT(tbsb, value, alen);
ja4plus_cert_process_rdn(&tbsb, out);
} else if (atag == 6 && alen >= 3) {
for (uint32_t i = 0; i < alen; i++) {
BSB_EXPORT_ptr(*out, arkime_char_to_hexstr[value[i]], 2);
}
BSB_EXPORT_u08(*out, ',');
return;
}
}
}
/******************************************************************************/
LOCAL void ja4plus_cert_print(int thread, int pos, char *ja4x, BSB *out)
{
GChecksum *const checksum = checksums256[thread];
if (BSB_LENGTH(*out) > 0) {
BSB_EXPORT_rewind(*out, 1);
g_checksum_update(checksum, (guchar *)out->buf, BSB_LENGTH(*out));
memcpy(ja4x + (13 * pos), g_checksum_get_string(checksum), 12);
g_checksum_reset(checksum);
} else {
memcpy(ja4x + (13 * pos), "000000000000", 12);
}
}
/******************************************************************************/
LOCAL uint32_t ja4plus_process_certificate_wInfo(ArkimeSession_t *session, const uint8_t *data, int len, void *uw)
{
// https://github.com/FoxIO-LLC/ja4/blob/main/technical_details/JA4X.md
uint32_t atag, alen, apc;
uint8_t *value;
BSB bsb;
BSB_INIT(bsb, data, len);
/* Certificate */
if (!(value = arkime_parsers_asn_get_tlv(&bsb, &apc, &atag, &alen))) {
goto bad_cert;
}
BSB_INIT(bsb, value, alen);
/* signedCertificate */
if (!(value = arkime_parsers_asn_get_tlv(&bsb, &apc, &atag, &alen))) {
goto bad_cert;
}
BSB_INIT(bsb, value, alen);
/* serialNumber or version*/
if (!(value = arkime_parsers_asn_get_tlv(&bsb, &apc, &atag, &alen))) {
goto bad_cert;
}
if (apc) {
if (!(value = arkime_parsers_asn_get_tlv(&bsb, &apc, &atag, &alen))) {
goto bad_cert;
}
}
/* signature */
if (!arkime_parsers_asn_get_tlv(&bsb, &apc, &atag, &alen)) {
goto bad_cert;
}
/* issuer */
if (!(value = arkime_parsers_asn_get_tlv(&bsb, &apc, &atag, &alen))) {
goto bad_cert;
}
BSB out;
char outbuf[1000];
char ja4x[39];
char ja4x_r[1000];
ja4x[12] = ja4x[25] = '_';
ja4x[38] = 0;
BSB ja4x_rbsb;
BSB_INIT(ja4x_rbsb, ja4x_r, sizeof(ja4x_r));
BSB tbsb;
BSB_INIT(tbsb, value, alen);
BSB_INIT(out, outbuf, sizeof(outbuf));
ja4plus_cert_process_rdn(&tbsb, &out);
if (BSB_LENGTH(out) > 0)
BSB_EXPORT_ptr(ja4x_rbsb, out.buf, BSB_LENGTH(out) - 1);
BSB_EXPORT_u08(ja4x_rbsb, '_');
ja4plus_cert_print(session->thread, 0, ja4x, &out);
/* validity */
if (!(value = arkime_parsers_asn_get_tlv(&bsb, &apc, &atag, &alen))) {
goto bad_cert;
}
BSB_INIT(tbsb, value, alen);
if (!(value = arkime_parsers_asn_get_tlv(&tbsb, &apc, &atag, &alen))) {
goto bad_cert;
}
if (!(value = arkime_parsers_asn_get_tlv(&tbsb, &apc, &atag, &alen))) {
goto bad_cert;
}
/* subject */
if (!(value = arkime_parsers_asn_get_tlv(&bsb, &apc, &atag, &alen))) {
goto bad_cert;
}
BSB_INIT(tbsb, value, alen);
BSB_INIT(out, outbuf, sizeof(outbuf));
ja4plus_cert_process_rdn(&tbsb, &out);
if (BSB_LENGTH(out) > 0)
BSB_EXPORT_ptr(ja4x_rbsb, out.buf, BSB_LENGTH(out) - 1);
BSB_EXPORT_u08(ja4x_rbsb, '_');
ja4plus_cert_print(session->thread, 1, ja4x, &out);
/* subjectPublicKeyInfo */
if (!(value = arkime_parsers_asn_get_tlv(&bsb, &apc, &atag, &alen))) {
goto bad_cert;
}
/* extensions */
BSB_INIT(out, outbuf, sizeof(outbuf));
ja4plus_cert_process_rdn(&bsb, &out);
if (BSB_LENGTH(out) > 0)
BSB_EXPORT_ptr(ja4x_rbsb, out.buf, BSB_LENGTH(out) - 1);
BSB_EXPORT_u08(ja4x_rbsb, 0);
ja4plus_cert_print(session->thread, 2, ja4x, &out);
arkime_field_certsinfo_update_extra(uw, g_strdup("ja4x"), g_strdup(ja4x));
if (ja4Raw) {
arkime_field_certsinfo_update_extra(uw, g_strdup("ja4x_r"), g_strdup(ja4x_r));
}
return 0;
bad_cert:
return 0;
}
/******************************************************************************/
// Given a list of numbers find the mode, we ignore numbers > 2048
LOCAL int ja4plus_ssh_mode(const uint16_t *nums, int num)
{
unsigned char count[2048];
unsigned short mode = 0;
unsigned char modeCount = 0;
memset(count, 0, sizeof(count));
for (int i = 0; i < num; i++) {
if (nums[i] >= 2048)
continue;
count[nums[i]]++;
if (count[nums[i]] == modeCount && nums[i] < mode) {
// new count same as old max, but lower mode
mode = nums[i];
} else if (count[nums[i]] > modeCount) {
mode = nums[i];
modeCount = count[nums[i]];
}
}
return mode;
}
/******************************************************************************/
LOCAL uint32_t ja4plus_ssh_ja4ssh(ArkimeSession_t *session, const uint8_t *UNUSED(data), int UNUSED(len), void *uw)
{
// https://github.com/FoxIO-LLC/ja4/blob/main/technical_details/JA4SSH.md
char ja4ssh[50];
BSB bsb;
const SSHInfo_t *ssh = uw;
BSB_INIT(bsb, ja4ssh, sizeof(ja4ssh));
BSB_EXPORT_sprintf(bsb, "c%ds%d_c%ds%d_c%ds%d",
ja4plus_ssh_mode(ssh->lens[0], ssh->packets200[0]), ja4plus_ssh_mode(ssh->lens[1], ssh->packets200[1]),
ssh->packets200[0], ssh->packets200[1],
session->tcpFlagAckCnt[0], session->tcpFlagAckCnt[1]);
session->tcpFlagAckCnt[0] = session->tcpFlagAckCnt[1] = 0;
arkime_field_string_add(ja4sshField, session, ja4ssh, BSB_LENGTH(bsb), TRUE);
return 0;
}
/******************************************************************************/
LOCAL void ja4plus_ja4ts(ArkimeSession_t *session, const JA4PlusTCP_t *data, const struct tcphdr *tcph)
{
uint8_t *p = (uint8_t *)tcph + 20;
const uint8_t *end = (uint8_t *)tcph + tcph->th_off * 4;
uint16_t mss = 0xffff;
uint8_t window_scale = 0xff;
char obuf[100];
BSB obsb;
BSB_INIT(obsb, obuf, sizeof(obuf));
BSB_EXPORT_sprintf(obsb, "%d_", ntohs(tcph->th_win));
if (p == end) {
BSB_EXPORT_cstr(obsb, "00");
} else {
BSB hbsb;
BSB_INIT(hbsb, p, (int)(end - p));
while (BSB_REMAINING(hbsb) > 0 && !BSB_IS_ERROR(hbsb)) {
uint8_t next = 0;
BSB_IMPORT_u08(hbsb, next);
BSB_EXPORT_sprintf(obsb, "%d-", next);
if (next == 0) { // End of list
while (BSB_REMAINING(hbsb) > 0) { // Just keep adding all 0s after
BSB_IMPORT_u08(hbsb, next);
if (next == 0) {
BSB_EXPORT_sprintf(obsb, "%d-", next);
} else {
break;
}
}
break;
}
if (next == 1) // NOOP
continue;
uint8_t size = 0;
BSB_IMPORT_u08(hbsb, size);
if (size < 2 || BSB_REMAINING(hbsb) < size - 2)
break;
if (next == 2)
BSB_IMPORT_u16(hbsb, mss);
else if (next == 3)
BSB_IMPORT_u08(hbsb, window_scale);
else
BSB_IMPORT_skip(hbsb, size - 2);
}
BSB_EXPORT_rewind(obsb, 1); // remove last -
}
if (mss == 0xffff) {
BSB_EXPORT_cstr(obsb, "_00");
} else {
BSB_EXPORT_sprintf(obsb, "_%d", mss);
}
if (window_scale == 0xff) {
BSB_EXPORT_cstr(obsb, "_00");
} else {
BSB_EXPORT_sprintf(obsb, "_%d", window_scale);
}
if (data->synAckTimesCnt > 1) {
BSB_EXPORT_cstr(obsb, "_");
for (int i = 1; i < data->synAckTimesCnt; i++) {
BSB_EXPORT_sprintf(obsb, "%.0f-", round ((data->synAckTimes[i] - data->synAckTimes[i - 1]) / 1000000));
}
BSB_EXPORT_rewind(obsb, 1); // remove last -
}
BSB_EXPORT_u08(obsb, 0);
arkime_field_string_add(ja4tsField, session, obuf, -1, TRUE);
}
/******************************************************************************/
LOCAL void ja4plus_ja4t(ArkimeSession_t *session, JA4PlusTCP_t UNUSED(*data), const struct tcphdr *tcph)
{
uint8_t *p = (uint8_t *)tcph + 20;
const uint8_t *end = (uint8_t *)tcph + tcph->th_off * 4;
uint16_t mss = 0xffff;
uint8_t window_scale = 0xff;
char obuf[100];
BSB obsb;
BSB_INIT(obsb, obuf, sizeof(obuf));
BSB_EXPORT_sprintf(obsb, "%d_", ntohs(tcph->th_win));
if (p == end) {
BSB_EXPORT_cstr(obsb, "00");
} else {
BSB hbsb;
BSB_INIT(hbsb, p, (int)(end - p));
while (BSB_REMAINING(hbsb) > 0 && !BSB_IS_ERROR(hbsb)) {
uint8_t next = 0;
BSB_IMPORT_u08(hbsb, next);
BSB_EXPORT_sprintf(obsb, "%d-", next);
if (next == 0) { // End of list
while (BSB_REMAINING(hbsb) > 0) { // Just keep adding all 0s after
BSB_IMPORT_u08(hbsb, next);
if (next == 0) {
BSB_EXPORT_sprintf(obsb, "%d-", next);
} else {
break;
}
}
break;
}
if (next == 1) // NOOP
continue;
uint8_t size = 0;