-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacketCapture.c
More file actions
2536 lines (2400 loc) · 107 KB
/
packetCapture.c
File metadata and controls
2536 lines (2400 loc) · 107 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
#include <arpa/inet.h> //network 정보 변환
#include <ctype.h> //isdigit
#include <netinet/if_ether.h> //etherrnet 구조체
#include <netinet/ip.h> //ip header 구조체
#include <netinet/ip_icmp.h> //icmp header 구조체
#include <netinet/tcp.h> //tcp header 구조체
#include <netinet/udp.h> //udp header 구조체
#include <pthread.h> //thread
#include <stdio.h> //basic
#include <stdlib.h> //malloc 동적할당
#include <string.h> //strlen, strcmp, strcpy
#include <sys/socket.h> //소켓의 주소 설정 (sockaddr 구조체)
#include <sys/timeb.h> //msec
#include <time.h> //저장한 file 이름을 현재 날짜로 하기 위해
#define BUFFER_SIZE 65536 // buffer 사이즈 2^16 크기만큼 생성
typedef enum { false, true } bool; // bool 자료형 선언
enum port { dns = 53, http = 80, https = 443 }; //캡쳐할 port번호
enum CaptureOptions {
A = 1, // ascii
X, // hex
S, // summary (no detail)
F // file
}; // capture option
typedef struct arpheader {
u_int16_t htype; /* Hardware Type */
u_int16_t ptype; /* Protocol Type */
u_char hlen; /* Hardware Address Length */
u_char plen; /* Protocol Address Length */
u_int16_t oper; /* Operation Code */
u_char sha[6]; /* Sender hardware address */
u_char spa[4]; /* Sender IP address */
u_char tha[6]; /* Target hardware address */
u_char tpa[4]; /* Target IP address */
char *strhType; //hType to String.
char *strpType; //pType to String
char *strOp; //Operation Code to String
}arphdr_t;
void *PacketCapture_thread(void *arg); //캡쳐 스레드
void ARP_header_capture(FILE *captureData, struct ethhdr *etherHeader, struct arpheader *arpHeader, unsigned char *Buffer, int Size);
void Arp_header_print(FILE *captureData, struct ethhdr *etherHeader, struct arpheader *arpHeader, unsigned char *Buffer, int Size);
void Capture_helper(FILE *captureFile, unsigned char *, int); //캡쳐한 패킷 프로토콜 분류
void Ethernet_header_fprint(FILE *captureFile, struct ethhdr *etherHeader); // Ethernet 헤더 정보 fprint
void Ip_header_fprint(FILE *captureFile, struct iphdr *, struct sockaddr_in, struct sockaddr_in); // ip 헤더 정보 fprint
void Tcp_header_capture(FILE *captureFile, struct ethhdr *, struct iphdr *, unsigned char *, int); // tcp 헤더 정보 capture
void Tcp_header_fprint(FILE *, unsigned char *, struct ethhdr *, struct iphdr *, struct tcphdr *, struct sockaddr_in, struct sockaddr_in,
int); // tcp 헤더 정보 fprint
void Udp_header_capture(FILE *captureFile, struct ethhdr *, struct iphdr *, unsigned char *, int); // udp 헤더 정보 capture
void Udp_header_fprint(FILE *, unsigned char *, struct ethhdr *, struct iphdr *, struct udphdr *, struct sockaddr_in, struct sockaddr_in, int Size); // udp 헤더 정보 fprint
void Dns_header_frpint();
// icmp 헤더 정보 fprint
void Change_hex_to_ascii(FILE *captureFile, unsigned char *, int, int); // payload값 hex/ascii/file option에 맞게 출력
void MenuBoard(); // menu board
void Menu_helper(); // menu board exception handling
void StartMenuBoard(); // start menu board
bool start_helper(char *); // start menu exception handling
bool IsPort(char *); //포트 형식 검사 | 맞으면 true
bool IsIpAddress(char *); // ip 형식 검사 | 맞으면 true
bool IsDigit(); // string 이 숫자인지 검사 | 맞으면 true
void buffer_flush(); //입력 버퍼 지우기
void http_header_capture(FILE *captureData, unsigned char *response, int Size);
void https_header_capture(FILE *captureData, unsigned char *httpsHeader, int Size);
void https_header_print(FILE *captureData, unsigned char *httpsHeader, int Size);
void https_handshake_capture(FILE *captureData, unsigned char *httpsHeader, int idx);
void https_ccs_capture(FILE *captureData, unsigned char *httpsHeader, int idx);
void https_appdata_capture(FILE *captureData, unsigned char *httpsHeader, int idx);
void https_encalert_capture(FILE *captureData, unsigned char *httpsHeader, int idx);
void dhcp_header_fprint(FILE *captureData, unsigned char *dhcpHeader, int Size);
bool captureStart = false; //캡쳐 스레드 시작flag 변수
int total = 0, filter = 0, drop = 0; //캡쳐한 패킷 갯수
int arpMode = 0, dnsMode = 0, httpMode = 0, httpsMode = 0, dhcpMode = 0; //각각의 Mode값에 따라서, 캡쳐하는게 달라질꺼임.
char protocolOption[128], portOption[128], ipOption[128], printOption[128]; // filter option 변수
int main() {
Menu_helper();
return 0;
}
void *PacketCapture_thread(void *arg) {
int rawSocket = *(int *)arg; // raw socket 전달 받기
int dataSize; //받은 데이터 정보 크기
unsigned char *buffer = (unsigned char *)malloc(BUFFER_SIZE); // buffer 공간 할당
char filename[40];
time_t t = time(NULL);
struct tm tm = *localtime(&t);
sprintf(filename, "captureFile(%d-%d-%dT%d:%d:%d).txt", tm.tm_year - 100, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
FILE *captureData = fopen(filename, "a+"); //파일 이어서 작성
if (captureData == NULL) {
printf(" !! 파일 열기 실패 하여 종료됩니다.\n"); //에러 처리
exit(1);
}
//캡쳐 시작
while (captureStart) {
if ((dataSize = recvfrom(rawSocket, buffer, BUFFER_SIZE, 0, NULL, NULL)) == -1) //패킷 recv
{
drop++;
printf("packet 받기 실패\n"); // packet drop시
continue;
}
Capture_helper(captureData, buffer, dataSize); //받은 패킷을 프로토콜 종류에따라 처리
}
free(buffer); //버퍼 공간 해제
fclose(captureData); // file close
}
void Capture_helper(FILE *captureData, unsigned char *buffer, int size) {
struct ethhdr *etherHeader = (struct ethhdr *)buffer; //버퍼에서 이더넷 정보 get
struct iphdr *ipHeader = (struct iphdr *)(buffer + ETH_HLEN); //받은 패킷의 ip header 부분 get
struct arpheader *arpHeader = (struct arpheader *)(buffer + ETH_HLEN);
total++; // recv한 모든 패킷 수 증가
/*IPv4의 모든 프로토콜 (ETH_P_IP == 0800)*/
char *Ptr = (char*)ðerHeader->h_proto;
//ARP DETECTION. ARP == 0806
if (*Ptr == 8 && *(Ptr+1)==6){
//printf("ARP HEADER DETECTED\n\n");
ARP_header_capture(captureData, etherHeader, arpHeader, buffer, size);
}
else if (etherHeader->h_proto == 8) {
/* all 프로토콜 선택시*/
if (!strcmp(protocolOption, "*")) {
if (ipHeader->protocol == 1) {
} else if (ipHeader->protocol == 6) {
Tcp_header_capture(captureData, etherHeader, ipHeader, buffer, size);
} else if (ipHeader->protocol == 17) {
Udp_header_capture(captureData, etherHeader, ipHeader, buffer, size);
}
} else if (!strcmp(protocolOption, "tcp") && (ipHeader->protocol == 6)) // tcp
{
Tcp_header_capture(captureData, etherHeader, ipHeader, buffer, size);
} else if (!strcmp(protocolOption, "udp") && (ipHeader->protocol == 17)) // udp
{
Udp_header_capture(captureData, etherHeader, ipHeader, buffer, size);
}
}
}
void Ethernet_header_fprint(FILE *captureData, struct ethhdr *etherHeader) {
filter++; // filter 거쳐 캡쳐한 패킷은 이 함수는 무조건 한번씩 거치기 때문에 filter 패킷 값 증가
fprintf(captureData, "\n --------------------------------------------------------\n");
fprintf(captureData, " | Ethernet Header |\n");
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " Ethernet Type | 0x%02X00\n",
etherHeader->h_proto); // L3 패킷 타입 IPv4 : 0x0800 | ARP 패킷 : 0x0806 | VLAN Tag : 0x8100
fprintf(captureData, " Src MAC Addr | [%02x:%02x:%02x:%02x:%02x:%02x]\n", // 6 byte for src
etherHeader->h_source[0], etherHeader->h_source[1], etherHeader->h_source[2], etherHeader->h_source[3],
etherHeader->h_source[4], etherHeader->h_source[5]);
fprintf(captureData, " Dst MAC Addr | [%02x:%02x:%02x:%02x:%02x:%02x]\n", // 6 byte for dest
etherHeader->h_dest[0], etherHeader->h_dest[1], etherHeader->h_dest[2], etherHeader->h_dest[3], etherHeader->h_dest[4],
etherHeader->h_dest[5]);
fprintf(captureData, " --------------------------------------------------------\n\n");
}
void Ip_header_fprint(FILE *captureData, struct iphdr *ipHeader, struct sockaddr_in source, struct sockaddr_in dest) {
fprintf(captureData, "\n --------------------------------------------------------\n");
fprintf(captureData, " | IP Header |\n");
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " IP Version | IPv%d\n", (unsigned int)ipHeader->version);
fprintf(captureData, " IP Header Length | %d DWORDS ( %d Bytes )\n", (unsigned int)ipHeader->ihl,
((unsigned int)(ipHeader->ihl)) * 4);
fprintf(captureData, " Type Of Service | %d\n", (unsigned int)ipHeader->tos);
fprintf(captureData, " IP Total Length | %d Bytes\n", ntohs(ipHeader->tot_len));
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " Identification | %d\n", ntohs(ipHeader->id));
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " Time To Live (TTL) | %d\n", (unsigned int)ipHeader->ttl);
fprintf(captureData, " Protocol | %d\n", (unsigned int)ipHeader->protocol);
fprintf(captureData, " Checksum | 0x%04X\n", ntohs(ipHeader->check));
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " Src IP Addr | %s\n", inet_ntoa(source.sin_addr));
fprintf(captureData, " Dst IP Addr | %s\n", inet_ntoa(dest.sin_addr));
fprintf(captureData, " --------------------------------------------------------\n\n");
}
void ARP_header_capture(FILE *captureData, struct ethhdr *etherHeader, struct arpheader *arpHeader, unsigned char *Buffer, int Size){
u_int8_t sourceMAC[6];
u_int8_t destMAC[6];
struct sockaddr_in source, dest;
printf("ARP PACKET\n");
printf("HARDWARE IDENTIFIER: [");
switch(ntohs(arpHeader->htype))
{
case ARPHRD_NETROM:
printf("NET/ROM pseud]\n");
arpHeader->strhType="NET/ROM pseud";
break;
case ARPHRD_ETHER:
printf("Ethernet 100Mbps/1Gbps]\n");
arpHeader->strhType="Ethernet 100Mbps/1Gbps";
break;
case ARPHRD_EETHER:
printf("Experimental Ethernet]\n");
arpHeader->strhType="Experimental Ethernet";
break;
case ARPHRD_AX25:
printf("AX.25 Level 2]\n");
arpHeader->strhType="AX.25 Level 2";
break;
case ARPHRD_IEEE802:
printf("IEEE 802.2 Ethernet/TR/TB]\n");
arpHeader->strhType="IEEE 802.2 Ethernet/TR/TB";
break;
default:
printf("Other type...]\n");
arpHeader->strhType="Unknown Type";
break;
}
if(arpHeader->ptype==8){
printf("Format of protocol type: IPv4\n");
arpHeader->strpType="IPv4";
}
printf("Length of hardware address: %d\n", arpHeader->hlen);
printf("Length of protocol address: %d\n", arpHeader->plen);
printf("ARP Command: [");
switch(ntohs(arpHeader->oper)){
case ARPOP_REQUEST:
printf("ARP Request]\n");
arpHeader->strOp = "ARP Request";
break;
case ARPOP_REPLY:
printf("ARP Reply]\n");
arpHeader->strOp = "ARP Reply";
break;
case ARPOP_RREQUEST:
printf("RARP Request]\n");
arpHeader->strOp = "RARP Request";
break;
case ARPOP_RREPLY:
printf("RARP Reply]\n");
arpHeader->strOp = "RARP Reply";
break;
case ARPOP_InREQUEST:
printf("InARP Request]\n");
arpHeader->strOp = "InARP Request";
break;
case ARPOP_InREPLY:
printf("InARP Reply]\n");
arpHeader->strOp = "InARP Reply";
break;
case ARPOP_NAK:
printf("(ATM)ARP NAK]\n");
arpHeader->strOp = "(ATM)ARP NAK";
break;
default:
printf("Unknown Type]\n");
arpHeader->strOp = "Unknown Type";
break;
}
printf("MAC: [%02X:%02X:%02X:%02X:%02X:%02X]-> ", arpHeader->sha[0],arpHeader->sha[1],arpHeader->sha[2],arpHeader->sha[3],arpHeader->sha[4],arpHeader->sha[5]);
printf("MAC: [%02X:%02X:%02X:%02X:%02X:%02X]\n",arpHeader->tha[0], arpHeader->tha[1], arpHeader->tha[2], arpHeader->tha[3], arpHeader->tha[4], arpHeader->tha[5]);
printf("IP: [%d.%d.%d.%d] -> ", arpHeader->spa[0], arpHeader->spa[1], arpHeader->spa[2], arpHeader->spa[3]);
printf("[%d.%d.%d.%d]\n", arpHeader->tpa[0],arpHeader->tpa[1],arpHeader->tpa[2],arpHeader->tpa[3]);
Arp_header_print(captureData, etherHeader, arpHeader, Buffer, Size);
}
void Arp_header_print(FILE *captureData, struct ethhdr *etherHeader, struct arpheader *arpHeader, unsigned char *Buffer, int Size){
fprintf(captureData, "\n############################## ARP Packet #####################################\n");
Ethernet_header_fprint(captureData, etherHeader);
fprintf(captureData, "\n --------------------------------------------------------\n");
fprintf(captureData, " | ARP Packet |\n");
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " Hardware ID | %s\n", arpHeader->strhType);
fprintf(captureData, " Format of Protocol Type | %s\n", arpHeader->strpType);
fprintf(captureData, " Length of hardware address| %d\n", arpHeader->hlen);
fprintf(captureData, " Length of Protocol Address| %d\n", arpHeader->plen);
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " ARP Command | %s\n", arpHeader->strOp);
fprintf(captureData, " Source MAC Address | [%02X:%02X:%02X:%02X:%02X:%02X]\n", arpHeader->sha[0], arpHeader->sha[1], arpHeader->sha[2], arpHeader->sha[3], arpHeader->sha[4], arpHeader->sha[5]);
fprintf(captureData, " Dest MAC Address | [%02X:%02X:%02X:%02X:%02X:%02X]\n", arpHeader->tha[0], arpHeader->tha[1], arpHeader->tha[2], arpHeader->tha[3], arpHeader->tha[4], arpHeader->tha[5]);
fprintf(captureData, " Source IP Address | [%u.%u.%u.%u]\n", arpHeader->spa[0], arpHeader->spa[1], arpHeader->spa[2], arpHeader->spa[3] );
fprintf(captureData, " Dest IP Address | [%u.%u.%u.%u]\n", arpHeader->tpa[0], arpHeader->tpa[1], arpHeader->tpa[2], arpHeader->tpa[3] );
fprintf(captureData, " --------------------------------------------------------\n");
//Change_hex_to_ascii(captureData, Buffer + ETH_HLEN + , F, (Size - ETH_HLEN));
}
void Tcp_header_capture(FILE *captureData, struct ethhdr *etherHeader, struct iphdr *ipHeader, unsigned char *Buffer, int Size) {
struct tcphdr *tcpHeader = (struct tcphdr *)(Buffer + (ipHeader->ihl * 4) + ETH_HLEN); //버퍼에서 tcp 헤더 정보 get
struct sockaddr_in source, dest; //출발, 목적지 주소 정보 저장할 변수
source.sin_addr.s_addr = ipHeader->saddr;
dest.sin_addr.s_addr = ipHeader->daddr;
// filter ip 검사
if (!strcmp(ipOption, "*") || !strcmp(inet_ntoa(source.sin_addr), ipOption) ||
!strcmp(inet_ntoa(dest.sin_addr), ipOption)) {
// filter port번호 검사
if (!strcmp(portOption, "*") || (atoi(portOption) == (int)ntohs(tcpHeader->source)) ||
(atoi(portOption) == (int)ntohs(tcpHeader->dest))) {
/*현재 시간 get*/
struct timeb itb;
ftime(&itb);
struct tm *tm = localtime(&itb.time);
fprintf(stdout, "\n%02d:%02d:%02d:%03d IPv", tm->tm_hour, tm->tm_min, tm->tm_sec, itb.millitm);
if (ntohs(tcpHeader->source) == http) {
fprintf(stdout, "[%s]:%u(http) > ", inet_ntoa(source.sin_addr), ntohs(tcpHeader->source));
fprintf(stdout, "[%s]:%u = TCP Flags [", inet_ntoa(dest.sin_addr), ntohs(tcpHeader->dest));
} else if (ntohs(tcpHeader->dest) == http) {
fprintf(stdout, "[%s]:%u > ", inet_ntoa(source.sin_addr), ntohs(tcpHeader->source));
fprintf(stdout, "[%s]:%u(http) = TCP Flags [", inet_ntoa(dest.sin_addr), ntohs(tcpHeader->dest));
} else if (ntohs(tcpHeader->source) == https){
fprintf(stdout, "[%s]:%u(https) > ", inet_ntoa(source.sin_addr), ntohs(tcpHeader->source));
fprintf(stdout, "[%s]:%u = TCP Flags [", inet_ntoa(dest.sin_addr), ntohs(tcpHeader->dest));
} else if (ntohs(tcpHeader->dest) == https){
fprintf(stdout, "[%s]:%u > ", inet_ntoa(source.sin_addr), ntohs(tcpHeader->source));
fprintf(stdout, "[%s]:%u(https) = TCP Flags [", inet_ntoa(dest.sin_addr), ntohs(tcpHeader->dest));
}
else {
fprintf(stdout, "%d %s:%u > ", (unsigned int)ipHeader->version, inet_ntoa(source.sin_addr), ntohs(tcpHeader->source));
fprintf(stdout, "%s:%u = TCP Flags [", inet_ntoa(dest.sin_addr), ntohs(tcpHeader->dest));
}
if ((unsigned int)tcpHeader->urg == 1) fprintf(stdout, "U.");
if ((unsigned int)tcpHeader->ack == 1) fprintf(stdout, "A.");
if ((unsigned int)tcpHeader->psh == 1) fprintf(stdout, "P.");
if ((unsigned int)tcpHeader->rst == 1) fprintf(stdout, "R.");
if ((unsigned int)tcpHeader->syn == 1) fprintf(stdout, "S.");
if ((unsigned int)tcpHeader->fin == 1) fprintf(stdout, "F.");
fprintf(stdout, "], seq %u, ack %u, win %d, length %d", ntohl(tcpHeader->seq), ntohl(tcpHeader->ack_seq),
ntohs(tcpHeader->window), Size);
/*print option에 따라 payload 부분 다르게 출력*/
/*
ascill 출력
if (!strcmp(printOption, "a")) {
fprintf(stdout, "\n\033[101methernet\033[0m");
Change_hex_to_ascii(captureData, Buffer, A, ETH_HLEN); // ethernet
fprintf(stdout, "\n\033[101mip\t\033[0m");
Change_hex_to_ascii(captureData, Buffer + ETH_HLEN, A, (ipHeader->ihl * 4)); // ip
fprintf(stdout, "\n\033[101mtcp\t\033[0m");
Change_hex_to_ascii(captureData, Buffer + ETH_HLEN + (ipHeader->ihl * 4), A, sizeof tcpHeader); // tcp
fprintf(stdout, "\n\033[101mpayload\t\033[0m");
Change_hex_to_ascii(captureData, Buffer + ETH_HLEN + (ipHeader->ihl * 4) + sizeof tcpHeader, A,
(Size - sizeof tcpHeader - (ipHeader->ihl * 4) - ETH_HLEN)); // payload
}
hex 출력
else if (!strcmp(printOption, "x")) {
fprintf(stdout, "\n\033[101methernet\033[0m");
Change_hex_to_ascii(captureData, Buffer, X, ETH_HLEN); // ethernet
fprintf(stdout, "\n\033[101mip\t\033[0m");
Change_hex_to_ascii(captureData, Buffer + ETH_HLEN, X, (ipHeader->ihl * 4)); // ip
fprintf(stdout, "\n\033[101mtcp\t\033[0m");
Change_hex_to_ascii(captureData, Buffer + ETH_HLEN + (ipHeader->ihl * 4), X, sizeof tcpHeader); // tcp
fprintf(stdout, "\n\033[101mpayload\t\033[0m");
Change_hex_to_ascii(captureData, Buffer + ETH_HLEN + (ipHeader->ihl * 4) + sizeof tcpHeader, X,
(Size - sizeof tcpHeader - (ipHeader->ihl * 4) - ETH_HLEN)); // payload
}
*/
//http 출력.
Tcp_header_fprint(captureData, Buffer, etherHeader, ipHeader, tcpHeader, source, dest, Size);
if(ntohs(tcpHeader->source) == http || ntohs(tcpHeader->dest) == http){
//DEBUG
//printf("\nHTTP DETECTED\n");
http_header_capture(captureData, Buffer + ETH_HLEN + (ipHeader->ihl * 4) + 20, Size);
}
//https 출력
if(ntohs(tcpHeader->source) == https || ntohs(tcpHeader->dest) == https){
//DEBUG
//printf("\nHTTPS DETECTED\n");
https_header_print(captureData, Buffer + ETH_HLEN + (ipHeader->ihl * 4) + 20, Size);
}
/*file 출력*/
}
}
}
void Tcp_header_fprint(FILE *captureData, unsigned char *Buffer, struct ethhdr *etherHeader, struct iphdr *ipHeader, struct tcphdr *tcpHeader, struct sockaddr_in source, struct sockaddr_in dest, int Size) {
fprintf(captureData, "\n############################## TCP Packet #####################################\n");
Ethernet_header_fprint(captureData, etherHeader); // ethernet 정보 fprint
Ip_header_fprint(captureData, ipHeader, source, dest); // ip 정보 fprint
fprintf(captureData, "\n --------------------------------------------------------\n");
fprintf(captureData, " | TCP Header |\n");
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " Source Port | %u\n", ntohs(tcpHeader->source));
fprintf(captureData, " Dest Port | %u\n", ntohs(tcpHeader->dest));
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " Sequence Number | %u\n", ntohl(tcpHeader->seq));
fprintf(captureData, " Acknowledge Number | %u\n", ntohl(tcpHeader->ack_seq));
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " OFFSET(Header Length) | %d DWORDS (%d BYTES)\n", (unsigned int)tcpHeader->doff,
(unsigned int)tcpHeader->doff * 4);
fprintf(captureData, " -- FLAGS -----------------------------------------------\n");
fprintf(captureData, " |-Urgent Flag | %d\n", (unsigned int)tcpHeader->urg);
fprintf(captureData, " |-Ack Flag | %d\n", (unsigned int)tcpHeader->ack);
fprintf(captureData, " |-Push Flag | %d\n", (unsigned int)tcpHeader->psh);
fprintf(captureData, " |-Reset Flag | %d\n", (unsigned int)tcpHeader->rst);
fprintf(captureData, " |-Synchronise Flag | %d\n", (unsigned int)tcpHeader->syn);
fprintf(captureData, " |-Finish Flag | %d\n", (unsigned int)tcpHeader->fin);
fprintf(captureData, " Window Size (rwnd) | %d\n", ntohs(tcpHeader->window));
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " Checksum | 0x%04x\n", ntohs(tcpHeader->check));
fprintf(captureData, " Urgent Pointer | %d\n", tcpHeader->urg_ptr);
fprintf(captureData, " --------------------------------------------------------\n");
/* 패킷 정보(payload) Hex dump 와 ASCII 변환 데이터 파일에 출력 */
Change_hex_to_ascii(captureData, Buffer + ETH_HLEN + (ipHeader->ihl * 4) + tcpHeader->doff * 4, F, (Size - tcpHeader->doff * 4 - (ipHeader->ihl * 4) - ETH_HLEN));
//fprintf(captureData, "\n===============================================================================\n");
}
#define RESPONSE_SIZE 32768
void http_header_capture(FILE *captureData, unsigned char *response, int Size){
/*http 위치찾기.
for(int i=0;i<30;i++){
printf("%02x ", *(response+i));
}
*/
int getMode = 0;
//if GET -> Response가 필요없음.
if(*response==0x47 && *(response+1)==0x45 && *(response+2) == 0x54){
getMode = 1;
}
char *p = response, *q = 0;
char *end = response + RESPONSE_SIZE;
char *body = 0;
enum {length, chunked, connection};
int encoding = 0;
int remaining = 0;
while(1){
if (!body && (body = strstr(response, "\r\n\r\n"))) {
*body = 0;
body += 4;
fprintf(captureData, "\nReceived Headers:\n%s\n", response);
printf("\nReceived Headers:\n%s\n", response);
q = strstr(response, "\nContent-Length: ");
if (q) {
encoding = length;
q = strchr(q, ' ');
q += 1;
remaining = strtol(q, 0, 10);
} else {
q = strstr(response, "\nTransfer-Encoding: chunked");
if (q) {
encoding = chunked;
remaining = 0;
} else {
encoding = connection;
}
}
if(getMode != 1){
fprintf(captureData, "\nReceived Body:\n");
printf("\nReceived Body:\n");
}
}else{
//if not GET -> Get은 요청이니까 제외
if(getMode != 1){
if(body){
fprintf(captureData, "%s", body);
}
printf("%s", body);
}
break;
}
if (body) {
if (encoding == length) {
if (p - body >= remaining && *body>=0x21 && *body<=0x7e) {
fprintf(captureData, "%.*s", remaining, body);
printf("%.*s", remaining, body);
break;
}
} else if (encoding == chunked) {
do {
if (remaining == 0) {
if ((q = strstr(body, "\r\n"))) {
remaining = strtol(body, 0, 16);
if (!remaining) goto finish;
body = q + 2;
} else {
break;
}
}
if (remaining && p - body >= remaining && *body>=0x21 && *body<=0x7e) {
fprintf(captureData, "%.*s", remaining, body);
printf("%.*s", remaining, body);
body += remaining + 2;
remaining = 0;
}
} while (!remaining);
}
} //if (body)
else{
break;
}
}
finish:
printf("\n");
}
void https_header_print(FILE *captureData, unsigned char *httpsHeader, int Size){
fprintf(captureData, "\n --------------------------------------------------------\n");
fprintf(captureData, " | HTTPS Header |\n");
https_header_capture(captureData, httpsHeader, Size);
}
void https_header_capture(FILE *captureData, unsigned char *httpsHeader, int Size){
int idx = 0;
//for DEBUG
/*
for(int i=0;i<10;i++){
printf("%02X ", httpsHeader[i]);
}
printf("\n");
*/
fprintf(captureData, " --------------------------------------------------------\n");
//Content Type: Handshake(22), ChangeCipherSpec(20), ApplicationData(23), Encrypted Alert(21)
if(httpsHeader[idx]==20){
https_ccs_capture(captureData, httpsHeader, idx);
return;
}
if(httpsHeader[idx]==21){
https_encalert_capture(captureData, httpsHeader, idx);
return;
}
if(httpsHeader[idx]==22){
https_handshake_capture(captureData, httpsHeader, idx);
return;
}
if(httpsHeader[idx]==23){
https_appdata_capture(captureData, httpsHeader, idx);
return;
}
}
void https_encalert_capture(FILE *captureData, unsigned char *httpsHeader, int idx){
fprintf(captureData, " Content Type | Encrypted Alert(21)\n");
idx++;
int alertLen = httpsHeader[idx];
fprintf(captureData, " Version | TLS 1.2\n");
fprintf(captureData, " Length | %d\n", alertLen);
idx++;
fprintf(captureData, " Alert Message | Encrypted Alert\n");
for(int i=0;i<alertLen;i++){
idx++;
}
return;
}
void https_appdata_capture(FILE *captureData, unsigned char *httpsHeader, int idx){
fprintf(stdout, "Content Type: Application Data\n");
fprintf(captureData, " Content Type | Application Data(23)\n");
idx+=2;
int appLength=0;
if(httpsHeader[idx]==1){
fprintf(stdout, "Version: TLS 1.0\n");
fprintf(captureData, " Version | TLS 1.0\n");
}
if(httpsHeader[idx]==2){
fprintf(stdout, "Version: TLS 1.1\n");
fprintf(captureData, " Version | TLS 1.1\n");
}
if(httpsHeader[idx]==3){
fprintf(captureData, " Version | TLS 1.2\n");
fprintf(stdout, "Version: TLS 1.2\n");
}
idx++;
appLength += httpsHeader[idx]*16*16;
idx++;
appLength+=httpsHeader[idx];
fprintf(captureData, " Length | %d\n", appLength);
idx++;
fprintf(captureData, " First Ten Enc AppData | ");
fprintf(stdout, "Length: %d\n", appLength);
fprintf(stdout, "First Ten Encrypted Application Data: ");
if(appLength>10){
for(int i=0;i<10;i++){
fprintf(stdout, "%02x", httpsHeader[idx]);
fprintf(captureData, "%02x", httpsHeader[idx]);
idx++;
}
for(int i=0;i<appLength-10;i++){
idx++;
}
}
if(appLength<10){
for(int i=0;i<appLength;i++){
fprintf(stdout, "%02x", httpsHeader[idx]);
fprintf(captureData, "%02x", httpsHeader[idx]);
idx++;
}
}
fprintf(stdout, "\n");
fprintf(captureData, "\n");
//DEBUG
//fprintf(stdout, "Next: %02x\n", httpsHeader[idx]);
if(httpsHeader[idx]==23 || httpsHeader[idx]==22 || httpsHeader[idx]==20||httpsHeader[idx]==21){
https_header_capture(captureData, httpsHeader+idx, idx);
return;
}else{
return;
}
}
void https_ccs_capture(FILE *captureData, unsigned char *httpsHeader, int idx){
fprintf(stdout, "Content Type: ChangeCipherSpec(20)\n");
fprintf(captureData, " Content Type | ChangeCipherSpec(20)\n");
idx+=2;
if(httpsHeader[idx]==1){
fprintf(stdout, "Version: TLS 1.0\n");
fprintf(captureData, " Version | TLS 1.0\n");
}
if(httpsHeader[idx]==2){
fprintf(stdout, "Version: TLS 1.1\n");
fprintf(captureData, " Version | TLS 1.1\n");
}
if(httpsHeader[idx]==3){
fprintf(captureData, " Version | TLS 1.2\n");
fprintf(stdout, "Version: TLS 1.2\n");
}
idx++;
int length = httpsHeader[idx]*16*16;
//printf("first length: %d", length);
idx++;
length+=httpsHeader[idx];
fprintf(stdout, "Length: %d\n", length);
idx++;
fprintf(stdout, "Change Cipher Spec Message\n");
idx++;
fprintf(stdout, "next: %02x\n", httpsHeader[idx]);
if(httpsHeader[idx]==23 || httpsHeader[idx]==22 || httpsHeader[idx]==20||httpsHeader[idx]==21){
https_header_capture(captureData, httpsHeader+idx, idx);
return;
}else{
return;
}
}
void https_handshake_capture(FILE *captureData, unsigned char *httpsHeader, int idx){
//Server(1) Client(0)
int server = 0;
if(!(httpsHeader[idx]==22 && httpsHeader[idx+1]==3)){
return;
}
fprintf(stdout, "Content Type: Handshake(22)\n");
fprintf(captureData, " Content Type | Handshake(22)\n");
idx+=2;
if(httpsHeader[idx]==1){
fprintf(stdout, "Version: TLS 1.0\n");
fprintf(captureData, " Version | TLS 1.0\n");
}
if(httpsHeader[idx]==2){
fprintf(stdout, "Version: TLS 1.1\n");
fprintf(captureData, " Version | TLS 1.1\n");
}
if(httpsHeader[idx]==3){
fprintf(captureData, " Version | TLS 1.2\n");
fprintf(stdout, "Version: TLS 1.2\n");
}
idx++;
int length = httpsHeader[idx]*16*16;
//printf("first length: %d", length);
idx++;
length+=httpsHeader[idx];
fprintf(stdout, "Length: %d\n", length);
fprintf(captureData, " Length | %d\n", length);
idx++;
//printf("Test value: %d\n", httpsHeader[idx]);
if(httpsHeader[idx]> 5){
return;
}
if(httpsHeader[idx]==1){
fprintf(stdout, "\nHandshake Protocol: Client Hello\n");
fprintf(stdout, "Handshake Type: Client Hello\n");
fprintf(captureData, " Handshake Protocol | Client Hello\n");
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " Handshake Type | Client Hello\n");
}
if(httpsHeader[idx]==2){
server=1;
fprintf(stdout, "Handshake Protocol: Server Hello\n");
fprintf(stdout, "Handshake Type: Server Hello\n");
fprintf(captureData, " Handshake Protocol | Server Hello\n");
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " Handshake Type | Server Hello\n");
}
if(httpsHeader[idx]==4){
fprintf(stdout, "Handshake Protocol: New Session Ticket\n");
fprintf(captureData, " Handshake Protocol | New Session Ticket\n");
fprintf(captureData, " --------------------------------------------------------\n");
idx+=2;
int ticketT = httpsHeader[idx]*256;
idx++;
ticketT +=httpsHeader[idx];
fprintf(stdout, "Session Ticket Lifecycle Hint: %d\n", ticketT);
fprintf(captureData, " Session Ticket Lifecycle Hint | %d\n", ticketT);
idx++;
int ticketLen = httpsHeader[idx]*256;
idx++;
ticketLen += httpsHeader[idx];
fprintf(stdout, "Session Ticket Length: %d\n", ticketLen);
fprintf(captureData, " Session Ticket Length | %d\n", ticketLen);
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " Handshake Type | Server Hello\n");
idx++;
fprintf(stdout, "First Ten Session Ticket: ");
fprintf(captureData, " First Ten Session Ticket | ");
if(ticketLen>10){
for(int i=0;i<10;i++){
fprintf(stdout, "%02x", httpsHeader[idx]);
fprintf(captureData, "%02x", httpsHeader[idx]);
idx++;
}
for(int i=0;i<ticketLen-10;i++){
idx++;
}
}
if(ticketLen<10){
for(int i=0;i<ticketLen;i++){
fprintf(stdout, "%02x", httpsHeader[idx]);
fprintf(captureData, "%02x", httpsHeader[idx]);
idx++;
}
}
fprintf(stdout, "\n");
fprintf(captureData, "\n");
//DEBUG
//fprintf(stdout, "Next: %02x\n", httpsHeader[idx]);
if(httpsHeader[idx]==23 || httpsHeader[idx]==22 || httpsHeader[idx]==20||httpsHeader[idx]==21){
https_header_capture(captureData, httpsHeader+idx, idx);
return;
}else{
return;
}
return;
}
if(httpsHeader[idx]==11){
fprintf(stdout, "Handshake Protocol: Certificate\n");
fprintf(stdout, "Handshake Type: Certificate\n");
fprintf(captureData, " Handshake Protocol | Certificate\n");
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " Handshake Type | Certificate\n");
idx+=2;
int cerLen = httpsHeader[idx]*256;
idx++;
cerLen += httpsHeader[idx];
fprintf(stdout, "Certificate Length: %d\n", cerLen);
fprintf(captureData, " Certificate Length | %d\n", cerLen);
idx+=cerLen+1;
if(httpsHeader[idx]==23 || httpsHeader[idx]==22 || httpsHeader[idx]==20||httpsHeader[idx]==21){
https_header_capture(captureData, httpsHeader+idx, idx);
return;
}else{
return;
}
return;
}
if(httpsHeader[idx]==14){
fprintf(stdout, "Handshake Protocol: Server Hello Done\n");
fprintf(stdout, "Handshake Type: Server Hello Done\n");
fprintf(captureData, " Handshake Protocol | Server Hello Done\n");
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " Handshake Type | Server Hello Done\n");
fprintf(stdout, "Length: 0\n");
fprintf(captureData, " Length | 0\n");
return;
}
if(httpsHeader[idx]==16){
fprintf(captureData, " Handshake Protocol | Client Key Exchange\n");
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(captureData, " Handshake Type | Client Key Exchange\n");
fprintf(stdout, "Handshake Protocol: Client Key Exchange\n");
idx+=3;
fprintf(stdout, "Length: %d\n", httpsHeader[idx]);
fprintf(captureData, " Length | %d\n", httpsHeader[idx]);
int ckLen=httpsHeader[idx];
idx++;
fprintf(captureData, " Params | EC Diffie-Hellman Client \n");
fprintf(captureData, " --------------------------------------------------------\n");
fprintf(stdout, "EC Diffie-Hellman Client Params\n");
fprintf(stdout, "PubKey Length: %d\n", ckLen-1);
fprintf(captureData, " PubKey Length | %d\n", ckLen-1);
fprintf(captureData, " PubKey(10) | ");
for(int i=0;i<10;i++){
fprintf(captureData, "%02x", httpsHeader[idx+i]);
}
for(int i=0;i<ckLen-1;i++){
fprintf(stdout, "%02x", httpsHeader[idx]);
idx++;
}
fprintf(captureData, "\n");
fprintf(stdout, "\n");
if(httpsHeader[idx]==23 || httpsHeader[idx]==22 || httpsHeader[idx]==20||httpsHeader[idx]==21){
https_header_capture(captureData, httpsHeader+idx, idx);
return;
}else{
return;
}
}
idx++;
int hSLength = httpsHeader[idx]*16*16*16*16;
int extLength;
idx++;
hSLength += httpsHeader[idx]*16*16;
idx++;
hSLength += httpsHeader[idx];
fprintf(captureData, " Handshake Length | %d\n", hSLength);
fprintf(captureData, " Version | ");
fprintf(stdout, "Handshake Length: %d\n", hSLength);
idx+=2;
if(httpsHeader[idx]==1){
fprintf(captureData, "TLS 1.0\n");
fprintf(stdout, "Version: TLS 1.0\n");
}
if(httpsHeader[idx]==2){
fprintf(captureData, "TLS 1.1\n");
fprintf(stdout, "Version: TLS 1.1\n");
}
if(httpsHeader[idx]==3){
fprintf(captureData, "TLS 1.2\n");
fprintf(stdout, "Version: TLS 1.2\n");
}
idx++;
fprintf(captureData, " Random Value | ");
fprintf(stdout, "Random value: ");
for(int i=0;i<32;i++){
fprintf(captureData, "%02x", httpsHeader[idx+i]);
fprintf(stdout, "%02x", httpsHeader[idx+i]);
}
fprintf(captureData, "\n");
fprintf(stdout, "\n");
idx+=32;
fprintf(captureData, " Session Length | %d\n", httpsHeader[idx]);
fprintf(stdout, "Session Length: %d\n", httpsHeader[idx]);
int sesLength = httpsHeader[idx];
idx++;
if(sesLength>0){
fprintf(stdout, "Session ID: ");
fprintf(captureData, " Session ID | ");
for(int i=0;i<sesLength;i++){
fprintf(captureData, "%02x", httpsHeader[idx]);
fprintf(stdout, "%02x", httpsHeader[idx]);
idx++;
}
fprintf(captureData, "\n");
fprintf(stdout, "\n");
}
//sleep(1);
if(server == 1){
if(httpsHeader[idx] == 0x13 && httpsHeader[idx+1] == 0x01){
fprintf(captureData, " Cipher Suite | TLS_AES_128_GDM_SHA256\n");
fprintf(stdout, "Cipher Suite: TLS_AES_128_GCM_SHA256\n");
}
if(httpsHeader[idx] == 0x13 && httpsHeader[idx+1] == 0x02){
fprintf(captureData, " Cipher Suite | TLS_AES_256_GCM_SHA384\n");
fprintf(stdout, "Cipher Suite: TLS_AES_256_GCM_SHA384\n");
}
idx+=2;
if(httpsHeader[idx] != 0){
fprintf(captureData, " Compression Method | %d\n", httpsHeader[idx]);
printf("Compression Method: %d\n", httpsHeader[idx]);
}
if(httpsHeader[idx] == 0){
fprintf(captureData, " Compression Method | (null)\n");
printf("Compression Method: (null)\n");
}
idx++;
extLength = httpsHeader[idx]*16*16;
idx++;
extLength+=httpsHeader[idx];
fprintf(captureData, " Extensions Length | %d\n", extLength);
fprintf(stdout, "Extensions Length: %d\n", extLength);
/*
idx+=6;
if(httpsHeader[idx]==4){
fprintf(stdout,"Extension: supported version\n");
fprintf(stdout,"Type: supported version (43)\n");
fprintf(stdout,"Length: 2\n");
fprintf(stdout,"Supported Version: TLS 1.3\n");
}
*/
idx++;
int typeLen;
int extensionCount = 0;
//extension 계속 뽑아먹기.
while(extLength>0){
extensionCount++;
fprintf(captureData, "Extension #%d\n", extensionCount);
fprintf(captureData, "\n");
typeLen=httpsHeader[idx]*256;
idx++;
typeLen+=httpsHeader[idx];
if(typeLen==51){
//for DEBUG
/*
for(int i=0;i<10;i++){
fprintf(captureData, "%02x ", httpsHeader[idx+i]);
}
*/
fprintf(captureData, " Type | Key share(%d)\n", httpsHeader[idx]);
fprintf(stdout,"Type: Key share\n");
idx++;
int klen=httpsHeader[idx]*16*16;
idx++;
klen+=httpsHeader[idx];
fprintf(captureData, " Length | %d\n", klen);
fprintf(stdout, "Length: %d\n", klen);
extLength-=klen;
idx+=2;
if(klen == 2){
if(httpsHeader[idx]==23){
fprintf(captureData, " Selected Group | secp256r1 (%d)\n", httpsHeader[idx]);
}
}else{
fprintf(stdout, "Key share Entry\n");
fprintf(captureData, " Key Share Entry\n");
if(httpsHeader[idx]==0x1d){
fprintf(stdout, "Group: x25519\n");
fprintf(captureData, " Group | x25519 (%d)\n", httpsHeader[idx]);
}
if(httpsHeader[idx]==23){
fprintf(stdout, "Group: secp256r1 (%d) \n", httpsHeader[idx]);
fprintf(captureData, " Group | secp256r1 (%d)\n", httpsHeader[idx]);
}
idx+=2;
fprintf(captureData, " Key Exchange Length | %d\n", httpsHeader[idx]);
int keLen2 = httpsHeader[idx];
idx++;
fprintf(captureData, " First Ten Key Exchange | ");
for(int i=0;i<10;i++){
fprintf(captureData, "%02x", httpsHeader[idx]);
idx++;
}
fprintf(captureData, "\n");
idx+=keLen2-10;
}
}
if(typeLen==43){
fprintf(captureData, " Type | supported_versions (%d)\n", httpsHeader[idx]);
fprintf(stdout,"Type: supported_versions (%d)\n", httpsHeader[idx]);
idx++;
int klen=httpsHeader[idx]*16*16;
idx++;
klen+=httpsHeader[idx];
fprintf(captureData, " Length | %d\n", klen);
fprintf(stdout, "Length: %d\n", klen);
extLength-=klen;
for(int i=0;i<klen/2;i++){
idx+=2;
fprintf(captureData, " Supported Versions | ");
if(httpsHeader[idx]==2){
fprintf(captureData, "TLS 1.1\n");
fprintf(stdout, "Supported Version: TLS 1.1 (0x03%02x\n", httpsHeader[idx]);
}
if(httpsHeader[idx]==3){
fprintf(captureData, "TLS 1.2\n");
fprintf(stdout, "Supported Version: TLS 1.2 (0x03%02x\n", httpsHeader[idx]);
}
if(httpsHeader[idx]==4){
fprintf(captureData, "TLS 1.3\n");
fprintf(stdout, "Supported Version: TLS 1.3 (0x03%02x\n", httpsHeader[idx]);
}
}
idx++;
}
if(typeLen==65281){
fprintf(captureData, " Type | renegotiation_info (%d)\n", httpsHeader[idx]);
fprintf(stdout,"Type: renegotiation_info (%d)\n", httpsHeader[idx]);
idx++;
int klen=httpsHeader[idx]*16*16;
idx++;
klen+=httpsHeader[idx];
fprintf(captureData, " Length | %d\n", klen);
fprintf(stdout, "Length: %d\n", klen);
extLength-=klen;
idx++;
fprintf(captureData, " Renegotiation info extension length | %d\n", httpsHeader[idx]);
idx++;
}