-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathpcapng.c
1986 lines (1594 loc) · 74.2 KB
/
pcapng.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
/*******************************************************************************
*
* Copyright (c) 2012-2021 Robert Krause ([email protected])
*
* This file is part of Pcapfix.
*
* Pcapfix is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or any later version.
*
* Pcapfix is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Pcapfix. If not, see http://www.gnu.org/licenses/.
*
******************************************************************************/
#include "pcapfix.h"
#include "pcapng.h"
/* Header of all pcapng blocks */
struct block_header {
u_int32_t block_type; /* block type */
u_int32_t total_length; /* block length */
};
/* Header of all pcapng options */
struct option_header {
u_int16_t option_code; /* option code - depending of block (0 - end of opts, 1 - comment are in common) */
u_int16_t option_length; /* option length - length of option in bytes (will be padded to 32bit) */
};
/* Section Header Block (SHB) - ID 0x0A0D0D0A */
struct section_header_block {
u_int32_t byte_order_magic; /* byte order magic - indicates swapped data */
u_int16_t major_version; /* major version of pcapng (1 atm) */
u_int16_t minor_version; /* minor version of pcapng (0 atm) */
int64_t section_length; /* length of section - can be -1 (parsing necessary) */
};
/* Interface Description Block (IDB) - ID 0x00000001 */
struct interface_description_block {
u_int16_t linktype; /* the link layer type (was -network- in classic pcap global header) */
u_int16_t reserved; /* 2 bytes of reserved data */
u_int32_t snaplen; /* maximum number of bytes dumped from each packet (was -snaplen- in classic pcap global header */
};
/* Packet Block (PB) - ID 0x00000002 (OBSOLETE - EPB should be used instead) */
struct packet_block {
u_int16_t interface_id; /* the interface the packet was captured from - identified by interface description block in current section */
u_int16_t drops_count; /* packet dropped by IF and OS since prior packet */
u_int32_t timestamp_high; /* high bytes of timestamp */
u_int32_t timestamp_low; /* low bytes of timestamp */
u_int32_t caplen; /* length of packet in the capture file (was -incl_len- in classic pcap packet header) */
u_int32_t len; /* length of packet when transmitted (was -orig_len- in classic pcap packet header) */
};
/* Simple Packet Block (SPB) - ID 0x00000003 */
struct simple_packet_block {
u_int32_t len; /* length of packet when transmitted (was -orig_len- in classic pcap packet header) */
};
/* Name Resolution Block (NRB) - ID 0x00000004 */
struct name_resolution_block {
u_int16_t record_type; /* type of record (ipv4 / ipv6) */
u_int16_t record_length; /* length of record value */
};
/* Interface Statistics Block - ID 0x00000005 */
struct interface_statistics_block {
u_int32_t interface_id; /* the interface the stats refer to - identified by interface description block in current section */
u_int32_t timestamp_high; /* high bytes of timestamp */
u_int32_t timestamp_low; /* low bytes of timestamp */
};
/* Enhanced Packet Block (EPB) - ID 0x00000006 */
struct enhanced_packet_block {
u_int32_t interface_id; /* the interface the packet was captured from - identified by interface description block in current section */
u_int32_t timestamp_high; /* high bytes of timestamp */
u_int32_t timestamp_low; /* low bytes of timestamp */
u_int32_t caplen; /* length of packet in the capture file (was -incl_len- in classic pcap packet header) */
u_int32_t len; /* length of packet when transmitted (was -orig_len- in classic pcap packet header) */
};
/*
* Function: fix_pcapng
* ---------------------
* tries to fix a pcapng file
*
* pcap: file pointer to input file
* pcap_fix: file pointer to output file
*
* returns: >0 success (number of corruptions fixed)
* 0 success (nothing to fix)
* -1 error (not a pcap file)
* -2 error (unable to repair)
* -3 error (EOF while reading input file)
*
*/
int fix_pcapng(FILE *pcap, FILE *pcap_fix) {
struct block_header bh; /* Block Header */
struct option_header oh; /* Option Header */
struct section_header_block shb; /* Section Header Block */
struct interface_description_block idb; /* Interface Description Block */
struct packet_block pb; /* Packet Block */
struct simple_packet_block spb; /* Simple Packet Block */
struct name_resolution_block nrb; /* Name Resolution Block */
struct interface_statistics_block isb; /* Interface Statistics Block */
struct enhanced_packet_block epb; /* Enhanced Packet Block */
char *data; /* Storage for packet data */
char *new_block; /* Storage for new (maybe repaired) block to finally write into ouput file */
// we use a buffer to cache 1mb of writing... this way writing is faster and
// we can read and write the file at the same time
char *writebuffer, *tmpbuf;
off_t writepos = 0;
off_t bytes; /* written bytes/blocks counter */
off_t padding; /* calculation for padding bytes */
off_t pos; /* current block position in input file */
off_t filesize; /* size of input file */
unsigned int block_pos; /* current position inside -new_block- to write further data to */
unsigned int check; /* variable to check end of blocks sizes */
unsigned int count; /* option / record counter to create EOO/EOR if necessary */
unsigned int shb_num; /* number of SHB counter */
unsigned int idb_num; /* number of IDB counter */
unsigned int step; /* step counter for progress bar */
unsigned int packets;
off_t left; /* bytes left to proceed until current blocks end is reached */
int fixes; /* corruptions counter */
int res; /* return values */
/* init write buffer */
writebuffer = malloc(1024000);
/* get file size of input file */
fseeko(pcap, 0, SEEK_END);
filesize = ftello(pcap);
fseeko(pcap, 0, SEEK_SET);
/* init variables */
pos = 0; /* begin file check at position 0 */
packets = 0;
fixes = 0; /* no corruptions fixed yet */
shb_num = 0; /* no SHBs progressed yet */
idb_num = 0; /* no IDBs progressed yet */
step = 1; /* progress bar starts at 0 steps */
/* loop every block inside pcapng file until end of file is reached */
while ( pos < (off_t) (filesize-sizeof(bh))) {
/* print out progress bar if in non-verbose mode */
if ((verbose == 0) && (5*(float)pos/(float)filesize > step)) {
print_progress(pos, filesize);
step++;
}
/* read the header of the current block */
bytes = fread(&bh, sizeof(bh), 1, pcap);
if (bytes != 1) return -3;
/* check for invalid block length / file cut off OR block size < 12 bytes */
if (bh.total_length > filesize-pos || bh.total_length < 12) {
/* block size is invalid (too small / larger than bytes in input file) */
if (verbose >= 1) {
if (bh.total_length > filesize-pos) printf("[-] Block Length (%" PRIu16 ") exceeds file size (%" FMT_OFF_T ").\n", bh.total_length, filesize);
else printf("[-] Block Length (%" PRIu16 ") is too small.\n", bh.total_length);
}
/* search for next valid block */
if (verbose >= 1) printf("[*] Trying to align next block...\n");
res = find_valid_block(pcap, filesize);
/* block found? */
if (res == 0) {
/* another valid block has been found in the file */
if (verbose >= 1) printf("[+] GOT Next Block at Position %" FMT_OFF_T "\n", ftello(pcap));
/* adjust total blocks length to match next block */
bh.total_length = ftello(pcap)-pos;
} else {
/* there are no more blocks inside the file */
if (verbose >= 1) printf("[*] No more valid Blocks found inside file! (maybe it was the last one)\n");
/* adjust total blocks length to end of file */
bh.total_length = filesize-pos;
}
if (bh.total_length < 12) {
printf("[-] Block too small ==> SKIPPING.\n");
/* reset input file pointer to next block */
fseeko(pcap, pos+bh.total_length, SEEK_SET);
continue;
}
if (verbose >= 1) printf("[*] Assuming this blocks size as %" PRIu32 " bytes.\n", bh.total_length);
else printf("[-] Invalid Block size => CORRECTED.\n");
/* reset input file pointer behind block header */
fseeko(pcap, pos+sizeof(struct block_header), SEEK_SET);
/* increase corruptions counter */
fixes++;
}
/* how many bytes are left until the final block size (end of block) is reached */
left = bh.total_length-sizeof(bh)-sizeof(check);
/* allocate memory for the new block - that will be written to repaired output file finally */
new_block = malloc(bh.total_length);
/* copy the current blocks header into repaired block */
memcpy(new_block, &bh, sizeof(bh));
block_pos = sizeof(bh);
/* what is the type of block at current position ? */
switch (bh.block_type) {
/* Section Header Block */
case TYPE_SHB:
if (verbose >= 1) printf("[*] FOUND: Section Header Block at position %" FMT_OFF_T " (%" PRIu16 " bytes)\n", pos, bh.total_length);
/* read section header block into struct */
bytes = fread(&shb, sizeof(shb), 1, pcap);
if (bytes != 1) return -3;
left -= sizeof(shb);
/* check for pcap's magic bytes () */
if (shb.byte_order_magic == BYTE_ORDER_MAGIC) {
if (verbose >= 1) printf("[+] Byte Order Magic: 0x%x\n", shb.byte_order_magic);
} else if (shb.byte_order_magic == htonl(BYTE_ORDER_MAGIC)) {
if (verbose >= 1) printf("[+] Byte Order Magic: 0x%x (SWAPPED)\n", shb.byte_order_magic);
swapped = 1;
} else {
printf("[-] Unknown Byte Order Magic: 0x%x ==> CORRECTED.\n", shb.byte_order_magic);
shb.byte_order_magic = BYTE_ORDER_MAGIC;
fixes++;
}
/* check for major version number (2) */
if (conshort(shb.major_version) == 1) { /* current major version is 2 */
if (verbose >= 1) printf("[+] Major version number: %hu\n", conshort(shb.major_version));
} else {
printf("[-] Major version number: %hu ==> CORRECTED.\n", conshort(shb.major_version));
shb.major_version = conshort(1);
fixes++;
}
/* check for minor version number */
if (conshort(shb.minor_version) == 0) { /* current minor version is 4 */
if (verbose >= 1) printf("[+] Minor version number: %hu\n", conshort(shb.minor_version));
} else {
printf("[-] Minor version number: %hu ==> CORRECTED.\n", conshort(shb.minor_version));
shb.minor_version = conshort(0);
fixes++;
}
/* section length */
if (shb.section_length == -1) {
if (verbose >= 1) printf("[*] Section length: %" PRId64 "\n", shb.section_length);
} else {
if (verbose >= 1) printf("[*] Section length: %" PRId64 " ==> SETTING TO -1\n", shb.section_length);
shb.section_length = -1;
}
/* check if enough space is aligned in memory block */
if (bh.total_length < block_pos+sizeof(shb)) {
bh.total_length = block_pos+sizeof(shb);
printf("[-] Given block total length is too small ==> FIXED (%" PRIu32 ")\n", bh.total_length);
new_block = realloc(new_block, bh.total_length);
memcpy(new_block, &bh, sizeof(bh));
fixes++;
}
/* copy section header block into repaired block */
memcpy(new_block+block_pos, &shb, sizeof(shb));
block_pos += sizeof(shb);
/* options */
count = 0 ;
while (left > 0) {
/* read option header into struct */
bytes = fread(&oh, sizeof(oh), 1, pcap);
if (bytes != 1) return -3;
left -= sizeof(oh);
/* which option did we get ? */
switch (oh.option_code) {
/* End of Options */
case 0x00:
if (verbose >= 2) printf("[+] OPTION: End of Options... (%" PRIu16 " bytes)\n", oh.option_length);
break;
/* Comment Option */
case 0x01:
if (verbose >= 2) printf("[+] OPTION: Comment... (%" PRIu16 " bytes)\n", oh.option_length);
break;
/* Hardware Information */
case 0x02:
if (verbose >= 2) printf("[+] OPTION: Hardware... (%" PRIu16 " bytes)\n", oh.option_length);
break;
/* Operating System Information */
case 0x03:
if (verbose >= 2) printf("[+] OPTION: Operating System... (%" PRIu16 " bytes)\n", oh.option_length);
break;
/* User Application Information */
case 0x04:
if (verbose >= 2) printf("[+] OPTION: Userappl... (%" PRIu16 " bytes)\n", oh.option_length);
break;
}
/* Invalid Option? */
if (oh.option_code > 0x04) {
printf("[-] Unknown option code: 0x%04x (%" PRIu16 " bytes) ==> SKIPPING.\n", oh.option_code, oh.option_length);
/* increase corruptions counter */
fixes++;
/* is this the first option we check? */
if (count == 0) {
/* there are NO options inside this block; skipping EOO option */
if (verbose >= 1) printf("[*] No Options inside -> no need for End of Options...\n");
break;
}
/* there have been other options before this corruption, we need EOO option */
if (verbose >= 1) printf("[*] %u Options inside -> Finishing with End of Options...\n", count);
/* adjust option header to end of options */
oh.option_code = 0x00;
oh.option_length = 0x00;
}
/* option is valid */
/* calculate padding for current option value */
padding = oh.option_length;
if (oh.option_length%4 != 0) padding += (4-oh.option_length%4);
/* check oversize */
if (padding > (unsigned)left) {
printf("[-] Option size (%" FMT_OFF_T ") exceeds remaining block space (%" FMT_OFF_T "). ==> SKIPPING OPTION.\n", padding, left);
fixes++;
/* because this block oversizes, there should not be any further option */
/* is this the first option we check? */
if (count == 0) {
/* there are NO options inside this block; skipping EOO option */
if (verbose >= 1) printf("[*] No Options inside -> no need for End of Options...\n");
break;
}
/* there have been other options before this corruption, we need EOO option */
if (verbose >= 1) printf("[*] %u Options inside -> Finishing with End of Options...\n", count);
/* adjust option header to end of options */
oh.option_code = 0x00;
oh.option_length = 0x00;
}
/* check if enough space is aligned in memory block */
if (bh.total_length < block_pos+sizeof(oh)) {
bh.total_length = block_pos+sizeof(oh);
printf("[-] Given block total length is too small ==> FIXED (%" PRIu32 ")\n", bh.total_length);
new_block = realloc(new_block, bh.total_length);
memcpy(new_block, &bh, sizeof(bh));
fixes++;
}
/* copy option header into repaired block */
memcpy(new_block+block_pos, &oh, sizeof(oh));
block_pos += sizeof(oh);
/* end of options? -> do not write any further */
if (oh.option_code == 0x00 && oh.option_length == 0x00) break;
/* read data of current option */
data = malloc(padding);
bytes = fread(data, padding, 1, pcap);
left -= padding;
/* check if enough space is aligned in memory block */
if (bh.total_length < block_pos+padding) {
bh.total_length = block_pos+padding;
printf("[-] Given block total length is too small ==> FIXED (%" PRIu32 ")\n", bh.total_length);
new_block = realloc(new_block, bh.total_length);
memcpy(new_block, &bh, sizeof(bh));
fixes++;
}
/* write option data to repaired block */
memcpy(new_block+block_pos, data, padding);
block_pos += padding;
/* clean up memory */
free(data);
count++;
}
break;
/* Packet Block */
case TYPE_PB:
packets++;
/* check oversize */
if (sizeof(pb) > (unsigned)left) {
printf("[-] Packet #%u exceeds size of block header (%zu > %" FMT_OFF_T ") ==> SKIPPING.\n", packets, sizeof(pb), left);
/* set to "invalid block" */
bh.block_type = -1;
break;
}
/* check for the mandatory SBH that MUST be before any packet! */
if (shb_num == 0) {
/* no SBH before this packet, we NEED to create one */
printf("[-] No Section Block header found ==> CREATING.\n");
write_shb(pcap_fix, writebuffer, &writepos);
/* increase counters */
shb_num++;
fixes++;
}
if (verbose >= 1) printf("[*] FOUND Packet #%u: Packet Block at position %" FMT_OFF_T " (%" PRIu16 " bytes)\n", packets, pos, bh.total_length);
/* read packet block into struct */
bytes = fread(&pb, sizeof(pb), 1, pcap);
if (bytes != 1) return -3;
left -= sizeof(pb);
/* pre-check too large interface number (1024) */
if (pb.interface_id > 1024) {
/* interface id is unusal high --> this field is probably corrupted */
printf("[-] Probably corrupted Interface ID #%" PRIu32 " (too high?) ==> CORRECTED.\n", pb.interface_id);
pb.interface_id = 1;
fixes++;
}
/* check for the mandatory IDB that MUST identify every packets interface_id */
while (pb.interface_id >= idb_num) {
/* no IDB is identifying this packet, we need to create one - until the ID has been reached */
printf("[-] Missing IDB for Interface #%" PRIu32 " ==> CREATING (#%u).\n", pb.interface_id, idb_num);
write_idb(pcap_fix, writebuffer, &writepos);
/* increase counters */
idb_num++;
fixes++;
}
/* check if enough space is aligned in memory block */
if (bh.total_length < block_pos+sizeof(pb)) {
bh.total_length = block_pos+sizeof(pb);
printf("[-] Given block total length is too small ==> FIXED (%" PRIu32 ")\n", bh.total_length);
new_block = realloc(new_block, bh.total_length);
memcpy(new_block, &bh, sizeof(bh));
fixes++;
}
/* copy packet block into repaired block */
memcpy(new_block+block_pos, &pb, sizeof(pb));
block_pos += sizeof(pb);
/* check for oversized caplen */
if (pb.caplen > (unsigned)left) {
printf("[-] Capture length (%" PRIu32 ") exceeds block size (%" FMT_OFF_T ") ==> CORRECTED.\n", pb.caplen, left);
pb.caplen = left;
}
/* calculate padding for packet data */
padding = pb.caplen;
if (pb.caplen % 4 != 0) padding += (4 - pb.caplen % 4);
/* read packet data from input file */
data = malloc(padding);
bytes = fread(data, padding, 1, pcap);
left -= padding;
/* check if enough space is aligned in memory block */
if (bh.total_length < block_pos+padding) {
bh.total_length = block_pos+padding;
printf("[-] Given block total length is too small ==> FIXED (%" PRIu32 ")\n", bh.total_length);
new_block = realloc(new_block, bh.total_length);
memcpy(new_block, &bh, sizeof(bh));
fixes++;
}
/* copy packet data into repaired block */
memcpy(new_block+block_pos, data, padding);
block_pos += padding;
/* clean up memory */
free(data);
/* options */
count = 0;
while (left > 0) {
/* read options header */
bytes = fread(&oh, sizeof(oh), 1, pcap);
if (bytes != 1) return -3;
left -= sizeof(oh);
/* which option did we get ? */
switch (oh.option_code) {
/* End of Options */
case 0x00:
if (verbose >= 2) printf("[+] OPTION: End of Options... (%" PRIu16 " bytes)\n", oh.option_length);
break;
/* Comment Option */
case 0x01:
if (verbose >= 2) printf("[+] OPTION: Comment... (%" PRIu16 " bytes)\n", oh.option_length);
break;
/* Link Layer Flags */
case 0x02:
if (verbose >= 2) printf("[+] OPTION: Link Layer Flags... (%" PRIu16 " bytes)\n", oh.option_length);
break;
/* Packet Hash */
case 0x03:
if (verbose >= 2) printf("[+] OPTION: Packet Hash... (%" PRIu16 " bytes)\n", oh.option_length);
break;
}
/* Invalid Option? */
if (oh.option_code > 0x03) {
printf("[-] Unknown option code: 0x%04" PRIx16 " (%" PRIu16 " bytes) ==> SKIPPING.\n", oh.option_code, oh.option_length);
/* increase corruptions counter */
fixes++;
/* is this the first option we check? */
if (count == 0) {
/* there are NO options inside this block; skipping EOO option */
if (verbose >= 1) printf("[*] No Options inside -> no need for End of Options...\n");
break;
}
/* there have been other options before this corruption, we need EOO option */
if (verbose >= 1) printf("[*] %u Options inside -> Finishing with End of Options...\n", count);
/* adjust option header to end of options */
oh.option_code = 0x00;
oh.option_length = 0x00;
}
/* option is valid */
/* calculate padding for current option value */
padding = oh.option_length;
if (oh.option_length%4 != 0) padding += (4-oh.option_length%4);
/* check oversize */
if (padding > (unsigned)left) {
printf("[-] Option size (%" FMT_OFF_T ") exceeds remaining block space (%" FMT_OFF_T "). ==> SKIPPING OPTION.\n", padding, left);
fixes++;
/* because this block oversizes, there should not be any further option */
/* is this the first option we check? */
if (count == 0) {
/* there are NO options inside this block; skipping EOO option */
if (verbose >= 1) printf("[*] No Options inside -> no need for End of Options...\n");
break;
}
/* there have been other options before this corruption, we need EOO option */
if (verbose >= 1) printf("[*] %u Options inside -> Finishing with End of Options...\n", count);
/* adjust option header to end of options */
oh.option_code = 0x00;
oh.option_length = 0x00;
}
/* check if enough space is aligned in memory block */
if (bh.total_length < block_pos+sizeof(oh)) {
bh.total_length = block_pos+sizeof(oh);
printf("[-] Given block total length is too small ==> FIXED (%" PRIu32 ")\n", bh.total_length);
new_block = realloc(new_block, bh.total_length);
memcpy(new_block, &bh, sizeof(bh));
fixes++;
}
/* copy options header into repaired block */
memcpy(new_block+block_pos, &oh, sizeof(oh));
block_pos += sizeof(oh);
/* end of options? -> do not write any further */
if (oh.option_code == 0x00 && oh.option_length == 0x00) break;
/* read data of current option */
data = malloc(padding);
bytes = fread(data, padding, 1, pcap);
left -= padding;
/* check if enough space is aligned in memory block */
if (bh.total_length < block_pos+padding) {
bh.total_length = block_pos+padding;
printf("[-] Given block total length is too small ==> FIXED (%" PRIu32 ")\n", bh.total_length);
new_block = realloc(new_block, bh.total_length);
memcpy(new_block, &bh, sizeof(bh));
fixes++;
}
/* copy option data to repaired block */
memcpy(new_block+block_pos, data, padding);
block_pos += padding;
/* clean up memory */
free(data);
count++;
}
break;
/* Simple Packet Block */
case TYPE_SPB:
packets++;
/* check for the mandatory SBH that MUST be before any packet! */
if (shb_num == 0) {
/* no SBH before this packet, we NEED to create one */
printf("[-] No Section Block header found ==> CREATING.\n");
write_shb(pcap_fix, writebuffer, &writepos);
/* increase counters */
shb_num++;
fixes++;
}
if (verbose >= 1) printf("[*] FOUND Packet #%u: Simple Packet Block at position %" FMT_OFF_T " (%" PRIu16 " bytes)\n", packets, pos, bh.total_length);
/* read simple packet block */
bytes = fread(&spb, sizeof(spb), 1, pcap);
if (bytes != 1) return -3;
left -= sizeof(spb);
/* output data */
if (verbose >= 1) printf("[*] SPB original packet length: %u bytes\n", spb.len);
/* check if enough space is aligned in memory block */
if (bh.total_length < block_pos+sizeof(spb)) {
bh.total_length = block_pos+sizeof(spb);
printf("[-] Given block total length is too small ==> FIXED (%" PRIu32 ")\n", bh.total_length);
new_block = realloc(new_block, bh.total_length);
memcpy(new_block, &bh, sizeof(bh));
fixes++;
}
/* copy simple packet block into repaired file */
memcpy(new_block+block_pos, &spb, sizeof(spb));
block_pos += sizeof(spb);
/* calculate padding for packet data */
/* spb.len is NOT the length of packet inside file (origlen != caplen); we need to calculate caplen using block length (left) */
/* decrease by two to avoid oversize padding */
padding = left-2;
if (padding % 4 != 0) padding += (4 - left % 4);
/* read packet data from input file */
data = malloc(padding);
bytes = fread(data, padding, 1, pcap);
left -= padding;
/* check if enough space is aligned in memory block */
if (bh.total_length < block_pos+padding) {
bh.total_length = block_pos+padding;
printf("[-] Given block total length is too small ==> FIXED (%" PRIu32 ")\n", bh.total_length);
new_block = realloc(new_block, bh.total_length);
memcpy(new_block, &bh, sizeof(bh));
fixes++;
}
/* copy packet data into repaired block */
memcpy(new_block+block_pos, data, padding);
block_pos += padding;
/* clean up memory */
free(data);
break;
/* Interface Description Block */
case TYPE_IDB:
/* check for the mandatory SBH that MUST be before any packet! */
if (shb_num == 0) {
/* no SBH before this packet, we NEED to create one */
printf("[-] No Section Block header found ==> CREATING.\n");
write_shb(pcap_fix, writebuffer, &writepos);
/* increase counters */
shb_num++;
fixes++;
}
if (verbose >= 1) printf("[*] FOUND: Interface Description Block (%u bytes)\n", bh.total_length);
/* read interface description block */
bytes = fread(&idb, sizeof(idb), 1, pcap); /* read first bytes of input file into struct */
if (bytes != 1) return -3;
left -= sizeof(idb);
/* check if enough space is aligned in memory block */
if (bh.total_length < block_pos+sizeof(idb)) {
bh.total_length = block_pos+sizeof(idb);
printf("[-] Given block total length is too small ==> FIXED (%" PRIu32 ")\n", bh.total_length);
new_block = realloc(new_block, bh.total_length);
memcpy(new_block, &bh, sizeof(bh));
fixes++;
}
/* copy interface description block into repaired block */
memcpy(new_block+block_pos, &idb, sizeof(idb));
block_pos += sizeof(idb);
/* options */
count = 0;
while (left > 0) {
/* read options header */
bytes = fread(&oh, sizeof(oh), 1, pcap);
if (bytes != 1) return -3;
left -= sizeof(oh);
/* which option did we get? */
switch (oh.option_code) {
/* End of Options */
case 0x00:
if (verbose >= 2) printf("[+] OPTION: End of Options... (%u bytes)\n", oh.option_length);
break;
/* Comment Option */
case 0x01:
if (verbose >= 2) printf("[+] OPTION: Comment... (%u bytes)\n", oh.option_length);
break;
/* Interface Name */
case 0x02:
if (verbose >= 2) printf("[+] OPTION: Interface Name... (%u bytes)\n", oh.option_length);
break;
/* Interface Description */
case 0x03:
if (verbose >= 2) printf("[+] OPTION: Interface Description... (%u bytes)\n", oh.option_length);
break;
/* IPv4 Address of Interface */
case 0x04:
if (verbose >= 2) printf("[+] OPTION: IPv4 Address... (%u bytes)\n", oh.option_length);
break;
/* IPv6 Address of Interface */
case 0x05:
if (verbose >= 2) printf("[+] OPTION: IPv6 Address... (%u bytes)\n", oh.option_length);
break;
/* MAC Address of Interface */
case 0x06:
if (verbose >= 2) printf("[+] OPTION: MAC Address... (%u bytes)\n", oh.option_length);
break;
/* EUI Address of Interface */
case 0x07:
if (verbose >= 2) printf("[+] OPTION: EUI Address... (%u bytes)\n", oh.option_length);
break;
/* Interface Speed */
case 0x08:
if (verbose >= 2) printf("[+] OPTION: Interface Speed... (%u bytes)\n", oh.option_length);
break;
/* Resolution of Timestamps */
case 0x09:
if (verbose >= 2) printf("[+] OPTION: Resolution of Timestamps... (%u bytes)\n", oh.option_length);
break;
/* Timezone */
case 0x0a:
if (verbose >= 2) printf("[+] OPTION: Timezone... (%u bytes)\n", oh.option_length);
break;
/* Filter expression used */
case 0x0b:
if (verbose >= 2) printf("[+] OPTION: Filter expression... (%u bytes)\n", oh.option_length);
break;
/* Operating System */
case 0x0c:
if (verbose >= 2) printf("[+] OPTION: Operating System... (%u bytes)\n", oh.option_length);
break;
/* Frame Check Sequence Length */
case 0x0d:
if (verbose >= 2) printf("[+] OPTION: Frame Check Sequence Length... (%u bytes)\n", oh.option_length);
break;
/* Timestamp Offset */
case 0x0e:
if (verbose >= 2) printf("[+] OPTION: Timestamp Offset... (%u bytes)\n", oh.option_length);
break;
}
/* Invalid Option? */
if (oh.option_code > 0x0e) {
printf("[-] Unknown option code: 0x%04x (%u bytes) ==> SKIPPING.\n", oh.option_code, oh.option_length);
/* increase corruptions counter */
fixes++;
/* is this the first option we check? */
if (count == 0) {
/* there are NO options inside this block; skipping EOO option */
if (verbose >= 1) printf("[*] No Options inside -> no need for End of Options...\n");
break;
}
/* there have been other options before this corruption, we need EOO option */
if (verbose >= 1) printf("[*] %u Options inside -> Finishing with End of Options...\n", count);
/* adjust option header to end of options */
oh.option_code = 0x00;
oh.option_length = 0x00;
}
/* option is valid */
/* calculate padding for current option value */
padding = oh.option_length;
if (oh.option_length%4 != 0) padding += (4-oh.option_length%4);
/* check oversize */
if (padding > (unsigned)left) {
printf("[-] Option size (%" FMT_OFF_T ") exceeds remaining block space (%" FMT_OFF_T "). ==> SKIPPING OPTION.\n", padding, left);
fixes++;
/* because this block oversizes, there should not be any further option */
/* is this the first option we check? */
if (count == 0) {
/* there are NO options inside this block; skipping EOO option */
if (verbose >= 1) printf("[*] No Options inside -> no need for End of Options...\n");
break;
}
/* there have been other options before this corruption, we need EOO option */
if (verbose >= 1) printf("[*] %u Options inside -> Finishing with End of Options...\n", count);
/* adjust option header to end of options */
oh.option_code = 0x00;
oh.option_length = 0x00;
}
/* check if enough space is aligned in memory block */
if (bh.total_length < block_pos+sizeof(oh)) {
bh.total_length = block_pos+sizeof(oh);
printf("[-] Given block total length is too small ==> FIXED (%" PRIu32 ")\n", bh.total_length);
new_block = realloc(new_block, bh.total_length);
memcpy(new_block, &bh, sizeof(bh));
fixes++;
}
/* copy options header into repaired block */
memcpy(new_block+block_pos, &oh, sizeof(oh));
block_pos += sizeof(oh);
/* end of options? -> do not write any further*/
if (oh.option_code == 0x00 && oh.option_length == 0x00) break;
/* read option data */
data = malloc(padding);
bytes = fread(data, padding, 1, pcap);
left -= padding;
/* check if enough space is aligned in memory block */
if (bh.total_length < block_pos+padding) {
bh.total_length = block_pos+padding;
printf("[-] Given block total length is too small ==> FIXED (%" PRIu32 ")\n", bh.total_length);
new_block = realloc(new_block, bh.total_length);
memcpy(new_block, &bh, sizeof(bh));
fixes++;
}
/* write option data into repaired block */
memcpy(new_block+block_pos, data, padding);
block_pos += padding;
/* clean up memory */
free(data);
count++;
}
break;
/* Name Resolution Block */
case TYPE_NRB:
/* check for the mandatory SBH that MUST be before any packet! */
if (shb_num == 0) {
/* no SBH before this packet, we NEED to create one */
printf("[-] No Section Block header found ==> CREATING.\n");
write_shb(pcap_fix, writebuffer, &writepos);
/* increase counters */
shb_num++;
fixes++;
}
if (verbose >= 1) printf("[*] FOUND: Name Resolution Block at position %" FMT_OFF_T " (%" PRIu16 " bytes)\n", pos, bh.total_length);
/* process records */
count = 0;
while (left > 0) {
/* read name resolution block */
bytes = fread(&nrb, sizeof(nrb), 1, pcap); /* read first bytes of input file into struct */
if (bytes != 1) return -3;
left -= sizeof(nrb);
/* which type of record did we get? */
switch (nrb.record_type) {
/* End of Records */
case 0x00:
if (verbose >= 2) printf("[+] RECORD: End of Records... (%u bytes)\n", nrb.record_length);
break;
/* IPv4 Record */
case 0x01:
if (verbose >= 2) printf("[+] RECORD: IPv4 Record... (%u bytes)\n", nrb.record_length);
break;
/* IPv6 Record */
case 0x02:
if (verbose >= 2) printf("[+] RECORD: IPv6 Record... (%u bytes)\n", nrb.record_length);
break;
}
/* Invalid Record? */
if (nrb.record_type > 0x02) {
printf("[-] Unknown record type: 0x%04x (%u bytes) ==> SKIPPING.\n", nrb.record_type, nrb.record_length);
/* increase corruptions counter */
fixes++;
/* is this the first record we check? */
if (count == 0) {
/* there are NO records inside this block; skipping EOR record */
if (verbose >= 1) printf("[*] No Records inside -> no need for End of Records...\n");
break;
}
/* there have been other records before this corruption, we need EOR record */
if (verbose >= 1) printf("[*] %u Records inside -> Finishing with End of Records...\n", count);
/* adjust option header to end of options */
nrb.record_type = 0x00;
nrb.record_length = 0x00;
}
/* record is valid */
/* check if enough space is aligned in memory block */
if (bh.total_length < block_pos+sizeof(nrb)) {
bh.total_length = block_pos+sizeof(nrb);
printf("[-] Given block total length is too small ==> FIXED (%" PRIu32 ")\n", bh.total_length);
new_block = realloc(new_block, bh.total_length);
memcpy(new_block, &bh, sizeof(bh));
fixes++;
}
/* write name resolution block into repaired block */
memcpy(new_block+block_pos, &nrb, sizeof(nrb));
block_pos += sizeof(nrb);
/* end of records? -> do not write any further */
if (nrb.record_type == 0x00 && nrb.record_length == 0x00) break;
/* calculate padding for current record value */
padding = nrb.record_length;
if (nrb.record_length % 4 != 0) padding += (4 - nrb.record_length % 4);
/* read record value from input file */
data = malloc(padding);
bytes = fread(data, padding, 1, pcap);
left -= padding;
/* check if enough space is aligned in memory block */
if (bh.total_length < block_pos+padding) {
bh.total_length = block_pos+padding;
printf("[-] Given block total length is too small ==> FIXED (%" PRIu32 ")\n", bh.total_length);
new_block = realloc(new_block, bh.total_length);
memcpy(new_block, &bh, sizeof(bh));
fixes++;
}
/* copy record value into repaired buffer */