-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathhttp.c
More file actions
1154 lines (986 loc) · 45.5 KB
/
Copy pathhttp.c
File metadata and controls
1154 lines (986 loc) · 45.5 KB
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
/* Copyright 2012-2017 AOL Inc. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "arkime.h"
#include <sys/socket.h>
#include <arpa/inet.h>
//#define HTTPDEBUG 1
#define MAX_URL_LENGTH 4096
#define HTTP_FIELD_MAX 8192
#define HTTP_GSTR_APPEND(gs, at, length) do { \
if ((gs)->len < HTTP_FIELD_MAX) { \
size_t _avail = HTTP_FIELD_MAX - (gs)->len; \
size_t _add = (length) < _avail ? (length) : _avail; \
g_string_append_len((gs), (at), _add); \
} \
} while (0)
// Only track DELETE, GET, HEAD, POST, PUT, CONNECT, OPTIONS
#define HTTP_MAX_METHOD 6
typedef struct {
ArkimeSession_t *session;
GString *urlString;
GString *hostString;
GString *cookieString;
GString *authString;
GString *proxyAuthString;
GString *valueString[2];
char header[2][40];
short pos[2];
http_parser parsers[2];
uint16_t methodCounts[HTTP_MAX_METHOD + 1];
GChecksum *checksum[4];
const char *magicString[2];
uint16_t wParsers: 2;
uint16_t inHeader: 2;
uint16_t inValue: 2;
uint16_t inBody: 2;
uint16_t urlWhich: 1;
uint16_t which: 1;
uint16_t isConnect: 2; // Keep track of each side that is CONNECT and completed headers
uint16_t reclassify: 2; // Keep track of each side that needs to reclassify still
uint16_t http2Upgrade: 1;
uint16_t websocketUpgrade: 2;
} HTTPInfo_t;
extern ArkimeConfig_t config;
LOCAL http_parser_settings parserSettings;
extern uint32_t pluginsCbs;
LOCAL ArkimeStringHashStd_t httpReqHeaders;
LOCAL ArkimeStringHashStd_t httpResHeaders;
LOCAL GHashTable *httpSubParsers;
LOCAL int cookieKeyField;
LOCAL int cookieValueField;
LOCAL int hostField;
LOCAL int userField;
LOCAL int atField;
LOCAL int urlsField;
LOCAL int xffField;
LOCAL int uaField;
LOCAL int tagsReqField;
LOCAL int tagsResField;
LOCAL int md5Field;
LOCAL int sha256Field;
LOCAL int verReqField;
LOCAL int verResField;
LOCAL int pathField;
LOCAL int keyField;
LOCAL int valueField;
LOCAL int magicField;
LOCAL int statuscodeField;
LOCAL int methodField;
LOCAL int reqBodyField;
LOCAL int headerReqField;
LOCAL int headerReqValue;
LOCAL int headerResField;
LOCAL int headerResValue;
LOCAL int methodCountFields[HTTP_MAX_METHOD + 1];
LOCAL int parseHTTPHeaderValueMaxLen;
/******************************************************************************/
void http_common_parse_cookie(ArkimeSession_t *session, char *cookie, int len)
{
char *start = cookie;
char *end = cookie + len;
while (start < end) {
while (start < end && isspace(*start)) start++;
char *equal = memchr(start, '=', end - start);
if (!equal)
break;
arkime_field_string_add(cookieKeyField, session, start, equal - start, TRUE);
start = memchr(equal + 1, ';', end - (equal + 1));
if (config.parseCookieValue) {
equal++;
while (equal < end && isspace(*equal)) equal++;
if (equal < end && equal != start)
arkime_field_string_add(cookieValueField, session, equal, start ? start - equal : end - equal, TRUE);
}
if (!start)
break;
start++;
}
}
/******************************************************************************/
void http_common_add_header_value(ArkimeSession_t *session, int pos, const char *s, int l)
{
while (l > 0 && isspace(*s)) {
s++;
l--;
}
switch (config.fields[pos]->type) {
case ARKIME_FIELD_TYPE_INT:
case ARKIME_FIELD_TYPE_INT_ARRAY:
case ARKIME_FIELD_TYPE_INT_ARRAY_UNIQUE:
case ARKIME_FIELD_TYPE_INT_HASH:
case ARKIME_FIELD_TYPE_INT_GHASH:
arkime_field_int_add(pos, session, arkime_atoin(s, l));
break;
case ARKIME_FIELD_TYPE_FLOAT:
case ARKIME_FIELD_TYPE_FLOAT_ARRAY:
case ARKIME_FIELD_TYPE_FLOAT_GHASH: {
char fbuf[32];
int flen = MIN(l, (int)sizeof(fbuf) - 1);
memcpy(fbuf, s, flen);
fbuf[flen] = 0;
arkime_field_float_add(pos, session, atof(fbuf));
break;
}
case ARKIME_FIELD_TYPE_STR:
case ARKIME_FIELD_TYPE_STR_ARRAY:
case ARKIME_FIELD_TYPE_STR_HASH:
case ARKIME_FIELD_TYPE_STR_GHASH:
if (pos == headerReqValue || pos == headerResValue)
arkime_field_string_add_lower(pos, session, s, MIN(l, parseHTTPHeaderValueMaxLen));
else
arkime_field_string_add(pos, session, s, l, TRUE);
break;
case ARKIME_FIELD_TYPE_IP:
case ARKIME_FIELD_TYPE_IP_GHASH: {
char *ipstr = g_strndup(s, l);
gchar **parts = g_strsplit(ipstr, ",", 0);
for (int i = 0; parts[i]; i++) {
arkime_field_ip_add_str(pos, session, parts[i]);
/* Add back maybe
if (ia == 0 || ia == 0xffffffff) {
arkime_session_add_tag(session, "http:bad-xff");
if (config.debug)
LOG("INFO - Didn't understand ip: %s %s %d", s, ip, ia);
continue;
}
*/
}
g_strfreev(parts);
g_free(ipstr);
break;
}
case ARKIME_FIELD_TYPE_OBJECT:
// Unsupported
break;
} /* SWITCH */
}
/******************************************************************************/
void http_common_add_header(ArkimeSession_t *session, int pos, int isReq, const char *name, int namelen, const char *value, int valuelen)
{
ArkimeString_t *hstring;
char *lower = g_ascii_strdown(name, namelen);
if (isReq)
arkime_field_string_add(tagsReqField, session, (const char *)lower, namelen, TRUE);
else
arkime_field_string_add(tagsResField, session, (const char *)lower, namelen, TRUE);
if (pos == 0) {
if (isReq)
HASH_FIND(s_, httpReqHeaders, lower, hstring);
else
HASH_FIND(s_, httpResHeaders, lower, hstring);
if (hstring) {
pos = (long)hstring->uw;
} else if (isReq && config.parseHTTPHeaderRequestAll) { // Header in request
arkime_field_string_add(headerReqField, session, lower, -1, TRUE);
pos = headerReqValue;
} else if (!isReq && config.parseHTTPHeaderResponseAll) { // Header in response
arkime_field_string_add(headerResField, session, lower, -1, TRUE);
pos = headerResValue;
}
}
g_free(lower);
if (pos == 0)
return;
http_common_add_header_value(session, pos, (char *)value, valuelen);
}
/******************************************************************************/
void http_common_parse_url(ArkimeSession_t *session, char *url, int len)
{
const char *end = url + len;
char *question = memchr(url, '?', len);
const char *pathStart = url;
int pathLen = question ? (int)(question - url) : len;
if (question) {
arkime_field_string_add(pathField, session, url, question - url, TRUE);
char *start = question + 1;
char *ch;
int field = keyField;
for (ch = start; ch < end; ch++) {
if (*ch == '&') {
if (ch != start && (config.parseQSValue || field == keyField)) {
char *str = g_uri_unescape_segment(start, ch, NULL);
if (!str) {
arkime_field_string_add(field, session, start, ch - start, TRUE);
} else if (!arkime_field_string_add(field, session, str, -1, FALSE)) {
g_free(str);
}
}
start = ch + 1;
field = keyField;
continue;
} else if (*ch == '=') {
if (ch != start && (config.parseQSValue || field == keyField)) {
char *str = g_uri_unescape_segment(start, ch, NULL);
if (!str) {
arkime_field_string_add(field, session, start, ch - start, TRUE);
} else if (!arkime_field_string_add(field, session, str, -1, FALSE)) {
g_free(str);
}
}
start = ch + 1;
field = valueField;
}
}
if (config.parseQSValue && field == valueField && ch > start) {
char *str = g_uri_unescape_segment(start, ch, NULL);
if (!str) {
arkime_field_string_add(field, session, start, ch - start, TRUE);
} else if (!arkime_field_string_add(field, session, str, -1, FALSE)) {
g_free(str);
}
}
} else {
arkime_field_string_add(pathField, session, url, len, TRUE);
}
// Lookup http sub-parser by path (lowercased, no query).
if (pathLen > 0 && pathLen < 256 && g_hash_table_size(httpSubParsers) > 0) {
char key[256];
for (int i = 0; i < pathLen; i++)
key[i] = tolower((unsigned char)pathStart[i]);
key[pathLen] = 0;
ArkimeParserInfo_t *info = g_hash_table_lookup(httpSubParsers, key);
if (info && info->parserFunc) {
info->parserFunc(session, info->uw, (const uint8_t *)url, len, 0);
}
}
}
/******************************************************************************/
LOCAL int arkime_hp_cb_on_message_begin (http_parser *parser)
{
HTTPInfo_t *http = parser->data;
ArkimeSession_t *session = http->session;
#ifdef HTTPDEBUG
LOG("HTTPDEBUG: which: %d", http->which);
#endif
http->magicString[http->which] = NULL;
http->inHeader &= ~(1 << http->which);
http->inValue &= ~(1 << http->which);
http->inBody &= ~(1 << http->which);
g_checksum_reset(http->checksum[http->which]);
if (config.supportSha256) {
g_checksum_reset(http->checksum[http->which + 2]);
}
if (pluginsCbs & ARKIME_PLUGIN_HP_OMB)
arkime_plugins_cb_hp_omb(session, parser);
return 0;
}
/******************************************************************************/
LOCAL int arkime_hp_cb_on_url (http_parser *parser, const char *at, size_t length)
{
HTTPInfo_t *http = parser->data;
#ifdef HTTPDEBUG
LOG("HTTPDEBUG: which:%d url %.*s", http->which, (int)length, at);
#endif
if (!http->urlString) {
http->urlString = g_string_new_len(at, length);
http->urlWhich = http->which;
} else
HTTP_GSTR_APPEND(http->urlString, at, length);
return 0;
}
/******************************************************************************/
LOCAL int arkime_hp_cb_on_body (http_parser *parser, const char *at, size_t length)
{
HTTPInfo_t *http = parser->data;
ArkimeSession_t *session = http->session;
#ifdef HTTPDEBUG
LOG("HTTPDEBUG: which: %d", http->which);
#endif
if (!(http->inBody & (1 << http->which))) {
if (arkime_memcasestr(at, length, "password=", 9) ||
arkime_memcasestr(at, length, "passwd=", 7) ||
arkime_memcasestr(at, length, "pass=", 5)
) {
arkime_session_add_tag(session, "http:password");
}
http->magicString[http->which] = arkime_parsers_magic(session, magicField, at, length);
http->inBody |= (1 << http->which);
/* Put small requests in a field. */
if (http->which == http->urlWhich && length <= config.maxReqBody && length > 0) {
if (!config.reqBodyOnlyUtf8 || g_utf8_validate(at, length, NULL) == TRUE) {
arkime_field_string_add(reqBodyField, session, at, length, TRUE);
}
}
}
g_checksum_update(http->checksum[http->which], (guchar *)at, length);
if (config.supportSha256) {
g_checksum_update(http->checksum[http->which + 2], (guchar *)at, length);
}
if (pluginsCbs & ARKIME_PLUGIN_HP_OB)
arkime_plugins_cb_hp_ob(session, parser, at, length);
return 0;
}
/******************************************************************************/
LOCAL void arkime_http_parse_authorization(ArkimeSession_t *session, char *str)
{
gsize olen;
while (isspace(*str)) str++;
const char *space = strchr(str, ' ');
if (!space)
return;
arkime_field_string_add_lower(atField, session, str, space - str);
if (strncasecmp("ntlm", str, 4) == 0 || strncasecmp("negotiate", str, 9) == 0) {
const char *scheme_end = space;
const char *b64 = scheme_end;
while (*b64 && isspace((unsigned char) * b64)) b64++;
if (*b64)
arkime_parsers_ntlm_decode_base64(session, b64, strlen(b64));
return;
}
if (strncasecmp("basic", str, 5) == 0) {
str += 5;
while (isspace(*str)) str++;
int len = strlen(str);
if (len < 2)
return;
// Yahoo reused Basic
if (len < 6 || memcmp("token=", str, 6) != 0) {
g_base64_decode_inplace(str, &olen);
if (olen == 0)
return;
char *colon = strchr(str, ':');
if (colon)
*colon = 0;
arkime_field_string_add(userField, session, str, -1, TRUE);
}
} else if (strncasecmp("digest", str, 6) == 0) {
str += 5;
while (isspace(*str)) str++;
char *username = strstr(str, "username");
if (!username) return;
str = username + 8;
while (isspace(*str)) str++;
if (*str != '=') return;
str++; // equal
while (isspace(*str)) str++;
int quote = 0;
if (*str == '"') {
quote = 1;
str++;
}
const char *end = str;
while (*end && (*end != '"' || !quote) && (*end != ',' || quote)) {
end++;
}
arkime_field_string_add(userField, session, str, end - str, TRUE);
}
}
/******************************************************************************/
LOCAL int arkime_hp_cb_on_message_complete (http_parser *parser)
{
HTTPInfo_t *http = parser->data;
ArkimeSession_t *session = http->session;
#ifdef HTTPDEBUG
LOG("HTTPDEBUG: which: %d", http->which);
#endif
if (pluginsCbs & ARKIME_PLUGIN_HP_OMC)
arkime_plugins_cb_hp_omc(session, parser);
if (http->inBody & (1 << http->which)) {
const char *md5 = g_checksum_get_string(http->checksum[http->which]);
arkime_field_string_uw_add(md5Field, session, (char *)md5, 32, (gpointer)http->magicString[http->which], TRUE);
if (config.supportSha256) {
const char *sha256 = g_checksum_get_string(http->checksum[http->which + 2]);
arkime_field_string_uw_add(sha256Field, session, (char *)sha256, 64, (gpointer)http->magicString[http->which], TRUE);
}
}
return 0;
}
/******************************************************************************/
LOCAL void http_add_value(ArkimeSession_t *session, HTTPInfo_t *http)
{
int pos = http->pos[http->which];
const char *s = http->valueString[http->which]->str;
int l = http->valueString[http->which]->len;
http_common_add_header_value(session, pos, s, l);
g_string_truncate(http->valueString[http->which], 0);
http->pos[http->which] = 0;
}
/******************************************************************************/
LOCAL int arkime_hp_cb_on_header_field (http_parser *parser, const char *at, size_t length)
{
HTTPInfo_t *http = parser->data;
ArkimeSession_t *session = http->session;
#ifdef HTTPDEBUG
LOG("HTTPDEBUG: which: %d field: %.*s", http->which, (int)length, at);
#endif
if (http->inValue & (1 << http->which)) {
http->inValue &= ~(1 << http->which);
http->header[http->which][0] = 0;
if (http->pos[http->which]) {
http_add_value(session, http);
}
}
if ((http->inHeader & (1 << http->which)) == 0) {
http->inHeader |= (1 << http->which);
if (http->urlString && parser->status_code == 0 && pluginsCbs & ARKIME_PLUGIN_HP_OU) {
arkime_plugins_cb_hp_ou(session, parser, http->urlString->str, http->urlString->len);
}
}
int len = strlen(http->header[http->which]);
size_t remaining = sizeof(http->header[http->which]) - len;
if (remaining > 1) {
int copy = MIN(length, remaining - 1);
memcpy(http->header[http->which] + len, at, copy);
http->header[http->which][len + copy] = 0;
}
return 0;
}
/******************************************************************************/
LOCAL int arkime_hp_cb_on_header_value (http_parser *parser, const char *at, size_t length)
{
HTTPInfo_t *http = parser->data;
ArkimeSession_t *session = http->session;
ArkimeString_t *hstring;
#ifdef HTTPDEBUG
LOG("HTTPDEBUG: which: %d value: %.*s", http->which, (int)length, at);
#endif
if ((http->inValue & (1 << http->which)) == 0) {
http->inValue |= (1 << http->which);
const char *header = http->header[http->which];
const int headerLen = strlen(header);
arkime_plugins_cb_hp_ohfr(session, parser, header, headerLen);
char *lower = g_ascii_strdown(header, headerLen);
arkime_plugins_cb_hp_ohf(session, parser, lower, headerLen);
if (http->which == http->urlWhich)
HASH_FIND(s_, httpReqHeaders, lower, hstring);
else
HASH_FIND(s_, httpResHeaders, lower, hstring);
http->pos[http->which] = (long)(hstring ? hstring->uw : 0);
if (http->pos[http->which] == 0) { // Header was not defined
if ((http->which == http->urlWhich) && config.parseHTTPHeaderRequestAll) { // Header in request
arkime_field_string_add(headerReqField, session, lower, -1, TRUE);
http->pos[http->which] = (long) headerReqValue;
} else if ((http->which != http->urlWhich) && config.parseHTTPHeaderResponseAll) { // Header in response
arkime_field_string_add(headerResField, session, lower, -1, TRUE);
http->pos[http->which] = (long) headerResValue;
}
}
if (http->which == http->urlWhich)
arkime_field_string_add(tagsReqField, session, lower, -1, TRUE);
else {
arkime_field_string_add(tagsResField, session, lower, -1, TRUE);
if (strcmp(lower, "upgrade") == 0 && length >= 3 && memcmp(at, "h2c", 3) == 0) {
http->http2Upgrade = 1;
}
}
if (strcmp(lower, "upgrade") == 0 && length >= 9 && strncasecmp((const char *)at, "websocket", 9) == 0) {
http->websocketUpgrade = 1;
}
g_free(lower);
}
arkime_plugins_cb_hp_ohv(session, parser, at, length);
// Request side
if (http->which == http->urlWhich) {
if (strcasecmp("host", http->header[http->which]) == 0) {
if (!http->hostString)
http->hostString = g_string_new_len(at, length);
else
HTTP_GSTR_APPEND(http->hostString, at, length);
} else if (strcasecmp("cookie", http->header[http->which]) == 0) {
if (!http->cookieString)
http->cookieString = g_string_new_len(at, length);
else
HTTP_GSTR_APPEND(http->cookieString, at, length);
} else if (strcasecmp("authorization", http->header[http->which]) == 0) {
if (!http->authString)
http->authString = g_string_new_len(at, length);
else
HTTP_GSTR_APPEND(http->authString, at, length);
} else if (strcasecmp("proxy-authorization", http->header[http->which]) == 0) {
if (!http->proxyAuthString)
http->proxyAuthString = g_string_new_len(at, length);
else
HTTP_GSTR_APPEND(http->proxyAuthString, at, length);
}
} else {
if (strcasecmp("www-authenticate", http->header[http->which]) == 0) {
if (!http->authString)
http->authString = g_string_new_len(at, length);
else
HTTP_GSTR_APPEND(http->authString, at, length);
} else if (strcasecmp("proxy-authenticate", http->header[http->which]) == 0) {
if (!http->proxyAuthString)
http->proxyAuthString = g_string_new_len(at, length);
else
HTTP_GSTR_APPEND(http->proxyAuthString, at, length);
}
}
if (http->pos[http->which]) {
if (!http->valueString[http->which])
http->valueString[http->which] = g_string_new_len(at, length);
else
HTTP_GSTR_APPEND(http->valueString[http->which], at, length);
}
return 0;
}
/******************************************************************************/
LOCAL int arkime_hp_cb_on_headers_complete (http_parser *parser)
{
HTTPInfo_t *http = parser->data;
ArkimeSession_t *session = http->session;
char version[20];
#ifdef HTTPDEBUG
LOG("HTTPDEBUG: which: %d code: %d method: %d upgrade: %d", http->which, parser->status_code, parser->method, parser->upgrade);
#endif
if (parser->method == HTTP_CONNECT) {
http->reclassify |= (1 << http->which);
http->isConnect |= (1 << http->which);
}
int len = arkime_snprintf_len(version, sizeof(version), "%d.%d", parser->http_major, parser->http_minor);
if (parser->status_code == 0) {
if (parser->method <= HTTP_MAX_METHOD)
http->methodCounts[parser->method]++;
arkime_field_string_add(methodField, session, http_method_str(parser->method), -1, TRUE);
arkime_field_string_add(verReqField, session, version, len, TRUE);
} else {
arkime_field_int_add(statuscodeField, session, parser->status_code);
arkime_field_string_add(verResField, session, version, len, TRUE);
}
if (http->inValue & (1 << http->which) && http->pos[http->which]) {
http_add_value(session, http);
}
http->header[0][0] = http->header[1][0] = 0;
if (http->urlString) {
const char *ch = http->urlString->str;
while (*ch) {
if (*ch < 32) {
arkime_session_add_tag(session, "http:control-char");
break;
}
ch++;
}
}
if (http->cookieString && http->cookieString->str[0]) {
http_common_parse_cookie(session, http->cookieString->str, http->cookieString->len);
g_string_truncate(http->cookieString, 0);
}
if (http->authString && http->authString->str[0]) {
arkime_http_parse_authorization(session, http->authString->str);
g_string_truncate(http->authString, 0);
}
/* Adding an additional check for proxy-authorization string*/
if (http->proxyAuthString && http->proxyAuthString->str[0]) {
arkime_http_parse_authorization(session, http->proxyAuthString->str);
g_string_truncate(http->proxyAuthString, 0);
}
if (http->hostString) {
g_string_ascii_down(http->hostString);
}
gboolean truncated = FALSE;
if (http->urlString && http->hostString) {
const char *colon = strchr(http->hostString->str, ':');
if (colon) {
arkime_field_string_add(hostField, session, http->hostString->str, colon - http->hostString->str, TRUE);
} else {
arkime_field_string_add(hostField, session, http->hostString->str, http->hostString->len, TRUE);
}
http_common_parse_url(session, http->urlString->str, http->urlString->len);
if (http->urlString->str[0] != '/') {
const char *result = strstr(http->urlString->str, http->hostString->str);
/* If the host header is in the first 8 bytes of url then just use the url */
if (result && result - http->urlString->str <= 8) {
if (http->urlString->len > MAX_URL_LENGTH) {
truncated = TRUE;
g_string_truncate(http->urlString, MAX_URL_LENGTH);
}
if (!arkime_field_string_add(urlsField, session, http->urlString->str, http->urlString->len, FALSE))
g_free(http->urlString->str);
(g_string_free)(http->urlString, FALSE);
g_string_free(http->hostString, TRUE);
} else {
/* Host header doesn't match the url */
g_string_append(http->hostString, ";");
g_string_append(http->hostString, http->urlString->str);
if (http->hostString->len > MAX_URL_LENGTH) {
truncated = TRUE;
g_string_truncate(http->hostString, MAX_URL_LENGTH);
}
if (!arkime_field_string_add(urlsField, session, http->hostString->str, http->hostString->len, FALSE))
g_free(http->hostString->str);
g_string_free(http->urlString, TRUE);
(g_string_free)(http->hostString, FALSE);
}
} else {
/* Normal case, url starts with /, so no extra host in url */
g_string_append(http->hostString, http->urlString->str);
if (http->hostString->len > MAX_URL_LENGTH) {
truncated = TRUE;
g_string_truncate(http->hostString, MAX_URL_LENGTH);
}
if (!arkime_field_string_add(urlsField, session, http->hostString->str, http->hostString->len, FALSE))
g_free(http->hostString->str);
g_string_free(http->urlString, TRUE);
(g_string_free)(http->hostString, FALSE);
}
http->urlString = NULL;
http->hostString = NULL;
} else if (http->urlString) {
if (http->urlString->len > MAX_URL_LENGTH) {
truncated = TRUE;
g_string_truncate(http->urlString, MAX_URL_LENGTH);
}
if (!arkime_field_string_add(urlsField, session, http->urlString->str, http->urlString->len, FALSE))
g_free(http->urlString->str);
(g_string_free)(http->urlString, FALSE);
http->urlString = NULL;
} else if (http->hostString) {
const char *colon = strchr(http->hostString->str, ':');
if (colon) {
arkime_field_string_add(hostField, session, http->hostString->str, colon - http->hostString->str, TRUE);
} else {
arkime_field_string_add(hostField, session, http->hostString->str, http->hostString->len, TRUE);
}
g_string_free(http->hostString, TRUE);
http->hostString = NULL;
}
if (truncated)
arkime_session_add_tag(session, "http:url-truncated");
arkime_session_add_protocol(session, "http");
if (http->websocketUpgrade && parser->status_code == 101) {
arkime_session_add_protocol(session, "websocket");
http->websocketUpgrade = 2; // signal http_parse to hand off to websocket parser
}
if (pluginsCbs & ARKIME_PLUGIN_HP_OHC)
arkime_plugins_cb_hp_ohc(session, parser);
return 0;
}
/*############################## SHARED ##############################*/
/******************************************************************************/
LOCAL int http_parse(ArkimeSession_t *session, void *uw, const uint8_t *data, int remaining, int which)
{
HTTPInfo_t *http = uw;
if (http->http2Upgrade) {
arkime_parsers_classify_tcp(session, data, remaining, which);
return ARKIME_PARSER_UNREGISTER;
}
if (http->websocketUpgrade == 2) {
ArkimeParserInfo_t *info = g_hash_table_lookup(httpSubParsers, "websocket");
if (info && info->parserFunc) {
info->parserFunc(session, info->uw, data, remaining, which);
}
return ARKIME_PARSER_UNREGISTER;
}
int dir = ARKIME_WHICH_GET_DIR(which);
http->which = dir;
#ifdef HTTPDEBUG
LOG("HTTPDEBUG: enter %d - %d %.*s", http->dir, remaining, remaining, data);
#endif
if (http->isConnect) {
// Check if either side needs to be classified
if (http->reclassify & (1 << dir)) {
http->reclassify &= ~(1 << dir);
arkime_parsers_classify_tcp(session, data, remaining, which);
// Both sides have been reclassified, remove http parser
if (http->reclassify == 0 && http->isConnect == 0x3) {
arkime_parsers_unregister(session, uw);
}
return 0;
}
}
if ((http->wParsers & (1 << dir)) == 0) {
return 0;
}
while (remaining > 0) {
int len = http_parser_execute(&http->parsers[dir], &parserSettings, (char *)data, remaining);
#ifdef HTTPDEBUG
LOG("HTTPDEBUG: parse result: %d input: %d errno: %d", len, remaining, http->parsers[dir].http_errno);
#endif
if (len <= 0) {
http->wParsers &= ~(1 << dir);
if (!http->wParsers) {
arkime_parsers_unregister(session, uw);
}
break;
}
data += len;
remaining -= len;
}
return 0;
}
/******************************************************************************/
LOCAL void http_save(ArkimeSession_t *session, void *uw, int final)
{
HTTPInfo_t *http = uw;
for (int i = 0; i <= HTTP_MAX_METHOD; i++) {
if (!http->methodCounts[i])
continue;
arkime_field_int_add(methodCountFields[i], session, http->methodCounts[i]);
http->methodCounts[i] = 0;
}
if (!final)
return;
#ifdef HTTPDEBUG
LOG("Save callback %d", final);
#endif
if (http->wParsers & 0x1) {
http_parser_execute(&http->parsers[0], &parserSettings, 0, 0);
}
if (http->wParsers & 0x2) {
http_parser_execute(&http->parsers[1], &parserSettings, 0, 0);
}
}
/******************************************************************************/
LOCAL void http_free(ArkimeSession_t UNUSED(*session), void *uw)
{
HTTPInfo_t *http = uw;
if (http->urlString)
g_string_free(http->urlString, TRUE);
if (http->hostString)
g_string_free(http->hostString, TRUE);
if (http->cookieString)
g_string_free(http->cookieString, TRUE);
if (http->authString)
g_string_free(http->authString, TRUE);
if (http->proxyAuthString)
g_string_free(http->proxyAuthString, TRUE);
if (http->valueString[0])
g_string_free(http->valueString[0], TRUE);
if (http->valueString[1])
g_string_free(http->valueString[1], TRUE);
g_checksum_free(http->checksum[0]);
g_checksum_free(http->checksum[1]);
if (config.supportSha256) {
g_checksum_free(http->checksum[2]);
g_checksum_free(http->checksum[3]);
}
ARKIME_TYPE_FREE(HTTPInfo_t, http);
}
/******************************************************************************/
LOCAL void http_classify(ArkimeSession_t *session, const uint8_t *UNUSED(data), int UNUSED(len), int UNUSED(which), void *UNUSED(uw))
{
if (arkime_session_has_protocol(session, "http"))
return;
arkime_session_add_protocol(session, "http");
HTTPInfo_t *http = ARKIME_TYPE_ALLOC0(HTTPInfo_t);
http->checksum[0] = g_checksum_new(G_CHECKSUM_MD5);
http->checksum[1] = g_checksum_new(G_CHECKSUM_MD5);
if (config.supportSha256) {
http->checksum[2] = g_checksum_new(G_CHECKSUM_SHA256);
http->checksum[3] = g_checksum_new(G_CHECKSUM_SHA256);
}
http_parser_init(&http->parsers[0], HTTP_BOTH);
http_parser_init(&http->parsers[1], HTTP_BOTH);
http->wParsers = 3;
http->parsers[0].data = http;
http->parsers[1].data = http;
http->session = session;
arkime_parsers_register2(session, http_parse, http, http_free, http_save);
}
/******************************************************************************/
void arkime_parser_init()
{
httpSubParsers = arkime_parsers_get_sub("http");
static const char *method_strings[] = {
#define XX(num, name, string) #string,
HTTP_METHOD_MAP(XX)
#undef XX
0
};
hostField = arkime_field_define("http", "lotermfield",
"host.http", "HTTP Host", "http.host",
"HTTP host header field",
ARKIME_FIELD_TYPE_STR_HASH, ARKIME_FIELD_FLAG_CNT,
"aliases", "[\"http.host\"]",
"category", "host",
(char *)NULL);
arkime_field_define("http", "lotextfield",
"host.http.tokens", "HTTP Host Tokens", "http.hostTokens",
"HTTP host Tokens header field",
ARKIME_FIELD_TYPE_STR_HASH, ARKIME_FIELD_FLAG_FAKE,
"aliases", "[\"http.host.tokens\"]",
(char *)NULL);
urlsField = arkime_field_define("http", "termfield",
"http.uri", "HTTP URI", "http.uri",
"URIs for request",
ARKIME_FIELD_TYPE_STR_HASH, ARKIME_FIELD_FLAG_CNT,
"category", "[\"url\",\"host\"]",
(char *)NULL);
arkime_field_define("http", "lotextfield",
"http.uri.tokens", "HTTP URI Tokens", "http.uriTokens",
"URI tokens for request",
ARKIME_FIELD_TYPE_STR_HASH, ARKIME_FIELD_FLAG_FAKE,
(char *)NULL);
xffField = arkime_field_define("http", "ip",
"ip.xff", "HTTP XFF IP", "http.xffIp",
"X-Forwarded-For Header",
ARKIME_FIELD_TYPE_IP_GHASH, ARKIME_FIELD_FLAG_CNT | ARKIME_FIELD_FLAG_IPPRE,
"category", "ip",
(char *)NULL);
uaField = arkime_field_define("http", "termfield",
"http.user-agent", "HTTP Useragent", "http.useragent",
"User-Agent Header",
ARKIME_FIELD_TYPE_STR_HASH, ARKIME_FIELD_FLAG_CNT,
(char *)NULL);
arkime_field_define("http", "lotextfield",
"http.user-agent.tokens", "HTTP Useragent Tokens", "http.useragentTokens",
"User-Agent Header Tokens",
ARKIME_FIELD_TYPE_STR_HASH, ARKIME_FIELD_FLAG_FAKE,
(char *)NULL);
tagsReqField = arkime_field_define("http", "lotermfield",
"http.hasheader.src", "HTTP Src Header", "http.requestHeader",
"Request has header present",
ARKIME_FIELD_TYPE_STR_HASH, ARKIME_FIELD_FLAG_CNT,
(char *)NULL);
tagsResField = arkime_field_define("http", "lotermfield",
"http.hasheader.dst", "HTTP Dst Header", "http.responseHeader",
"Response has header present",
ARKIME_FIELD_TYPE_STR_HASH, ARKIME_FIELD_FLAG_CNT,
(char *)NULL);
headerReqField = arkime_field_define("http", "lotermfield",
"http.header.request.field", "Request Header Fields", "http.requestHeaderField",
"Contains Request header fields",
ARKIME_FIELD_TYPE_STR_ARRAY, ARKIME_FIELD_FLAG_NODB,
(char *)NULL);
headerReqValue = arkime_field_define("http", "lotermfield",
"http.hasheader.src.value", "Request Header Values", "http.requestHeaderValue",
"Contains request header values",
ARKIME_FIELD_TYPE_STR_ARRAY, ARKIME_FIELD_FLAG_CNT,
(char *)NULL);
headerResField = arkime_field_define("http", "lotermfield",
"http.header.response.field", "Response Header fields", "http.responseHeaderField",
"Contains response header fields",
ARKIME_FIELD_TYPE_STR_ARRAY, ARKIME_FIELD_FLAG_NODB,
(char *)NULL);
headerResValue = arkime_field_define("http", "lotermfield",
"http.hasheader.dst.value", "Response Header Values", "http.responseHeaderValue",
"Contains response header values",
ARKIME_FIELD_TYPE_STR_ARRAY, ARKIME_FIELD_FLAG_CNT,
(char *)NULL);
arkime_field_define("http", "lotermfield",
"http.hasheader", "Has Src or Dst Header", "hhall",
"Shorthand for http.hasheader.src or http.hasheader.dst",
0, ARKIME_FIELD_FLAG_FAKE,
"regex", "^http\\\\.hasheader\\\\.(?:(?!(cnt|value)$).)*$",
(char *)NULL);