-
Notifications
You must be signed in to change notification settings - Fork 3
/
aflnet.c
1464 lines (1236 loc) · 44.7 KB
/
aflnet.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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include "alloc-inl.h"
#include "aflnet.h"
// Protocol-specific functions for extracting requests and responses
region_t* extract_requests_smtp(unsigned char* buf, unsigned int buf_size, unsigned int* region_count_ref)
{
char *mem;
unsigned int byte_count = 0;
unsigned int mem_count = 0;
unsigned int mem_size = 1024;
unsigned int region_count = 0;
region_t *regions = NULL;
char terminator[2] = {0x0D, 0x0A};
mem=(char *)ck_alloc(mem_size);
unsigned int cur_start = 0;
unsigned int cur_end = 0;
while (byte_count < buf_size) {
memcpy(&mem[mem_count], buf + byte_count++, 1);
//Check if the last two bytes are 0x0D0A
if ((mem_count > 1) && (memcmp(&mem[mem_count - 1], terminator, 2) == 0)) {
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
mem_count = 0;
cur_start = cur_end + 1;
cur_end = cur_start;
} else {
mem_count++;
cur_end++;
//Check if the last byte has been reached
if (cur_end == buf_size - 1) {
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
break;
}
if (mem_count == mem_size) {
//enlarge the mem buffer
mem_size = mem_size * 2;
mem=(char *)ck_realloc(mem, mem_size);
}
}
}
if (mem) ck_free(mem);
//in case region_count equals zero, it means that the structure of the buffer is broken
//hence we create one region for the whole buffer
if ((region_count == 0) && (buf_size > 0)) {
regions = (region_t *)ck_realloc(regions, sizeof(region_t));
regions[0].start_byte = 0;
regions[0].end_byte = buf_size - 1;
regions[0].state_sequence = NULL;
regions[0].state_count = 0;
region_count = 1;
}
*region_count_ref = region_count;
return regions;
}
region_t* extract_requests_ssh(unsigned char* buf, unsigned int buf_size, unsigned int* region_count_ref)
{
char *mem;
unsigned int byte_count = 0;
unsigned int mem_count = 0;
unsigned int mem_size = 1024;
unsigned int region_count = 0;
region_t *regions = NULL;
char terminator[2] = {0x0D, 0x0A};
mem=(char *)ck_alloc(mem_size);
unsigned int cur_start = 0;
unsigned int cur_end = 0;
while (byte_count < buf_size) {
memcpy(&mem[mem_count], buf + byte_count++, 1);
//Check if the region buffer length is at least 6 bytes
//Why 6 bytes? It is because both the SSH identification and the normal message are longer than 6 bytes
//For normal message, it starts with message size (4 bytes), #padding_bytes (1 byte) and message code (1 byte)
if (mem_count >= 6) {
if (!strncmp(mem, "SSH-", 4)) {
//It could be an identification message
//Find terminator (0x0D 0x0A)
while ((byte_count < buf_size) && (memcmp(&mem[mem_count - 1], terminator, 2))) {
if (mem_count == mem_size - 1) {
//enlarge the mem buffer
mem_size = mem_size * 2;
mem=(char *)ck_realloc(mem, mem_size);
}
memcpy(&mem[++mem_count], buf + byte_count++, 1);
cur_end++;
}
//Create one region
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
//Check if the last byte has been reached
if (cur_end < buf_size - 1) {
mem_count = 0;
cur_start = cur_end + 1;
cur_end = cur_start;
}
} else {
//It could be a normal message
//Extract the message size stored in the first 4 bytes
unsigned int* size_buf = (unsigned int*)&mem[0];
unsigned int message_size = (unsigned int)ntohl(*size_buf);
unsigned char message_code = (unsigned char)mem[5];
//and skip the payload and the MAC
unsigned int bytes_to_skip = message_size - 2;
if ((message_code >= 20) && (message_code <= 49)) {
//Do nothing
} else {
bytes_to_skip += 8;
}
unsigned int temp_count = 0;
while ((byte_count < buf_size) && (temp_count < bytes_to_skip)) {
byte_count++;
cur_end++;
temp_count++;
}
if (byte_count < buf_size) {
byte_count--;
cur_end--;
}
//Create one region
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
//Check if the last byte has been reached
if (cur_end < buf_size - 1) {
mem_count = 0;
cur_start = cur_end + 1;
cur_end = cur_start;
}
}
} else {
mem_count++;
cur_end++;
//Check if the last byte has been reached
if (cur_end == buf_size - 1) {
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
break;
}
if (mem_count == mem_size) {
//enlarge the mem buffer
mem_size = mem_size * 2;
mem=(char *)ck_realloc(mem, mem_size);
}
}
}
if (mem) ck_free(mem);
//in case region_count equals zero, it means that the structure of the buffer is broken
//hence we create one region for the whole buffer
if ((region_count == 0) && (buf_size > 0)) {
regions = (region_t *)ck_realloc(regions, sizeof(region_t));
regions[0].start_byte = 0;
regions[0].end_byte = buf_size - 1;
regions[0].state_sequence = NULL;
regions[0].state_count = 0;
region_count = 1;
}
*region_count_ref = region_count;
return regions;
}
region_t* extract_requests_tls(unsigned char* buf, unsigned int buf_size, unsigned int* region_count_ref)
{
char *mem;
unsigned int byte_count = 0;
unsigned int mem_count = 0;
unsigned int mem_size = 1024;
unsigned int region_count = 0;
region_t *regions = NULL;
mem=(char *)ck_alloc(mem_size);
unsigned int cur_start = 0;
unsigned int cur_end = 0;
while (byte_count < buf_size) {
memcpy(&mem[mem_count], buf + byte_count++, 1);
//Check if the region buffer length is at least 5 bytes (record header size)
if (mem_count >= 5) {
//1st byte: content type
//2nd and 3rd byte: TLS version
//Extract the message size stored in the 4th and 5th bytes
u16* size_buf = (u16*)&mem[3];
u16 message_size = (u16)ntohs(*size_buf);
//and skip the payload
unsigned int bytes_to_skip = message_size;
unsigned int temp_count = 0;
while ((byte_count < buf_size) && (temp_count < bytes_to_skip)) {
byte_count++;
cur_end++;
temp_count++;
}
if (byte_count < buf_size) {
byte_count--;
cur_end--;
}
//Create one region
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
//Check if the last byte has been reached
if (cur_end < buf_size - 1) {
mem_count = 0;
cur_start = cur_end + 1;
cur_end = cur_start;
}
} else {
mem_count++;
cur_end++;
//Check if the last byte has been reached
if (cur_end == buf_size - 1) {
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
break;
}
if (mem_count == mem_size) {
//enlarge the mem buffer
mem_size = mem_size * 2;
mem=(char *)ck_realloc(mem, mem_size);
}
}
}
if (mem) ck_free(mem);
//in case region_count equals zero, it means that the structure of the buffer is broken
//hence we create one region for the whole buffer
if ((region_count == 0) && (buf_size > 0)) {
regions = (region_t *)ck_realloc(regions, sizeof(region_t));
regions[0].start_byte = 0;
regions[0].end_byte = buf_size - 1;
regions[0].state_sequence = NULL;
regions[0].state_count = 0;
region_count = 1;
}
*region_count_ref = region_count;
return regions;
}
region_t* extract_requests_dicom(unsigned char* buf, unsigned int buf_size, unsigned int* region_count_ref)
{
unsigned int pdu_length = 0;
unsigned int packet_length = 0;
unsigned int region_count = 0;
unsigned int end = 0;
unsigned int start = 0;
region_t *regions = NULL;
unsigned int byte_count = 0;
while (byte_count < buf_size) {
if ((byte_count + 2 >= buf_size) || (byte_count + 5 >= buf_size)) break;
// Bytes from third to sixth encode the PDU length.
pdu_length =
(buf[byte_count + 5]) |
(buf[byte_count + 4] << 8) |
(buf[byte_count + 3] << 16) |
(buf[byte_count + 2] << 24);
// DICOM Header(6 bytes) includes PDU type and PDU length.
packet_length = pdu_length + 6;
start = byte_count;
end = byte_count + packet_length - 1;
if (end < start) break; // it means that int overflow has happened -_0_0_-
if (end >= buf_size) break; // checking boundaries
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = start;
regions[region_count - 1].end_byte = end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
if ( (byte_count + packet_length) < byte_count ) break; // checking int overflow
if ( (byte_count + packet_length) < packet_length ) break; // checking int overflow
byte_count += packet_length;
}
// if bytes is left
if ((byte_count < buf_size) && (buf_size > 0)) {
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = byte_count;
regions[region_count - 1].end_byte = buf_size - 1;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
}
*region_count_ref = region_count;
return regions;
}
region_t* extract_requests_dns(unsigned char* buf, unsigned int buf_size, unsigned int* region_count_ref)
{
char *mem;
unsigned int mem_count = 0;
unsigned int mem_size = 1024;
unsigned int region_count = 0;
region_t *regions = NULL;
mem = (char *)ck_alloc(mem_size);
unsigned int cur_start = 0;
unsigned int cur_end = 0;
for (unsigned int byte_count = 0; byte_count < buf_size; byte_count++) {
memcpy(&mem[mem_count], buf + byte_count, 1);
// A DNS header is 12 bytes long & the 1st null byte after that indicates the end of the query.
if ((mem_count >= 12) && (*(mem+mem_count) == 0)) {
// 4 bytes left of the tail.
cur_end += 4;
byte_count += 4;
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
if (cur_end == buf_size - 1) break;
mem_count = 0;
cur_start = cur_end + 1;
cur_end = cur_start;
} else {
mem_count++;
cur_end++;
// Check if the last byte has been reached
if (cur_end == buf_size - 1) {
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
break;
}
if (mem_count == mem_size) {
// Enlarge the mem buffer
mem_size *= 2;
mem = (char *)ck_realloc(mem, mem_size);
}
}
}
if (mem) ck_free(mem);
// In case region_count equals zero, it means that the structure of the buffer is broken
// hence we create one region for the whole buffer
if ((region_count == 0) && (buf_size > 0)) {
regions = (region_t *)ck_realloc(regions, sizeof(region_t));
regions[0].start_byte = 0;
regions[0].end_byte = buf_size - 1;
regions[0].state_sequence = NULL;
regions[0].state_count = 0;
region_count = 1;
}
*region_count_ref = region_count;
return regions;
}
region_t* extract_requests_rtsp(unsigned char* buf, unsigned int buf_size, unsigned int* region_count_ref)
{
char *mem;
unsigned int byte_count = 0;
unsigned int mem_count = 0;
unsigned int mem_size = 1024;
unsigned int region_count = 0;
region_t *regions = NULL;
char terminator[4] = {0x0D, 0x0A, 0x0D, 0x0A};
mem=(char *)ck_alloc(mem_size);
unsigned int cur_start = 0;
unsigned int cur_end = 0;
while (byte_count < buf_size) {
memcpy(&mem[mem_count], buf + byte_count++, 1);
//Check if the last four bytes are 0x0D0A0D0A
if ((mem_count > 3) && (memcmp(&mem[mem_count - 3], terminator, 4) == 0)) {
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
mem_count = 0;
cur_start = cur_end + 1;
cur_end = cur_start;
} else {
mem_count++;
cur_end++;
//Check if the last byte has been reached
if (cur_end == buf_size - 1) {
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
break;
}
if (mem_count == mem_size) {
//enlarge the mem buffer
mem_size = mem_size * 2;
mem=(char *)ck_realloc(mem, mem_size);
}
}
}
if (mem) ck_free(mem);
//in case region_count equals zero, it means that the structure of the buffer is broken
//hence we create one region for the whole buffer
if ((region_count == 0) && (buf_size > 0)) {
regions = (region_t *)ck_realloc(regions, sizeof(region_t));
regions[0].start_byte = 0;
regions[0].end_byte = buf_size - 1;
regions[0].state_sequence = NULL;
regions[0].state_count = 0;
region_count = 1;
}
*region_count_ref = region_count;
return regions;
}
region_t* extract_requests_ftp(unsigned char* buf, unsigned int buf_size, unsigned int* region_count_ref)
{
char *mem;
unsigned int byte_count = 0;
unsigned int mem_count = 0;
unsigned int mem_size = 1024;
unsigned int region_count = 0;
region_t *regions = NULL;
char terminator[2] = {0x0D, 0x0A};
mem=(char *)ck_alloc(mem_size);
unsigned int cur_start = 0;
unsigned int cur_end = 0;
while (byte_count < buf_size) {
memcpy(&mem[mem_count], buf + byte_count++, 1);
//Check if the last two bytes are 0x0D0A
if ((mem_count > 1) && (memcmp(&mem[mem_count - 1], terminator, 2) == 0)) {
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
mem_count = 0;
cur_start = cur_end + 1;
cur_end = cur_start;
} else {
mem_count++;
cur_end++;
//Check if the last byte has been reached
if (cur_end == buf_size - 1) {
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
break;
}
if (mem_count == mem_size) {
//enlarge the mem buffer
mem_size = mem_size * 2;
mem=(char *)ck_realloc(mem, mem_size);
}
}
}
if (mem) ck_free(mem);
//in case region_count equals zero, it means that the structure of the buffer is broken
//hence we create one region for the whole buffer
if ((region_count == 0) && (buf_size > 0)) {
regions = (region_t *)ck_realloc(regions, sizeof(region_t));
regions[0].start_byte = 0;
regions[0].end_byte = buf_size - 1;
regions[0].state_sequence = NULL;
regions[0].state_count = 0;
region_count = 1;
}
*region_count_ref = region_count;
return regions;
}
unsigned int* extract_response_codes_smtp(unsigned char* buf, unsigned int buf_size, unsigned int* state_count_ref)
{
char *mem;
unsigned int byte_count = 0;
unsigned int mem_count = 0;
unsigned int mem_size = 1024;
unsigned int *state_sequence = NULL;
unsigned int state_count = 0;
char terminator[2] = {0x0D, 0x0A};
mem=(char *)ck_alloc(mem_size);
state_count++;
state_sequence = (unsigned int *)ck_realloc(state_sequence, state_count * sizeof(unsigned int));
state_sequence[state_count - 1] = 0;
while (byte_count < buf_size) {
memcpy(&mem[mem_count], buf + byte_count++, 1);
if ((mem_count > 0) && (memcmp(&mem[mem_count - 1], terminator, 2) == 0)) {
//Extract the response code which is the first 3 bytes
char temp[4];
memcpy(temp, mem, 4);
temp[3] = 0x0;
unsigned int message_code = (unsigned int) atoi(temp);
if (message_code == 0) break;
state_count++;
state_sequence = (unsigned int *)ck_realloc(state_sequence, state_count * sizeof(unsigned int));
state_sequence[state_count - 1] = message_code;
mem_count = 0;
} else {
mem_count++;
if (mem_count == mem_size) {
//enlarge the mem buffer
mem_size = mem_size * 2;
mem=(char *)ck_realloc(mem, mem_size);
}
}
}
if (mem) ck_free(mem);
*state_count_ref = state_count;
return state_sequence;
}
unsigned int* extract_response_codes_ssh(unsigned char* buf, unsigned int buf_size, unsigned int* state_count_ref)
{
char mem[7];
unsigned int byte_count = 0;
unsigned int *state_sequence = NULL;
unsigned int state_count = 0;
//Initial state
state_count++;
state_sequence = (unsigned int *)ck_realloc(state_sequence, state_count * sizeof(unsigned int));
if (state_sequence == NULL) PFATAL("Unable realloc a memory region to store state sequence");
state_sequence[state_count - 1] = 0;
while (byte_count < buf_size) {
memcpy(mem, buf + byte_count, 6);
byte_count += 6;
/* If this is the identification message */
if (strstr(mem, "SSH")) {
//Read until \x0D\x0A
char tmp = 0x00;
while (tmp != 0x0A) {
memcpy(&tmp, buf + byte_count, 1);
byte_count += 1;
}
state_count++;
state_sequence = (unsigned int *)ck_realloc(state_sequence, state_count * sizeof(unsigned int));
if (state_sequence == NULL) PFATAL("Unable realloc a memory region to store state sequence");
state_sequence[state_count - 1] = 256; //Identification
} else {
//Extract the message type and skip the payload and the MAC
unsigned int* size_buf = (unsigned int*)&mem[0];
unsigned int message_size = (unsigned int)ntohl(*size_buf);
//Break if the response does not adhere to the known format(s)
//Normally, it only happens in the last response
if (message_size - 2 > buf_size - byte_count) break;
unsigned char message_code = (unsigned char)mem[5];
state_count++;
state_sequence = (unsigned int *)ck_realloc(state_sequence, state_count * sizeof(unsigned int));
if (state_sequence == NULL) PFATAL("Unable realloc a memory region to store state sequence");
state_sequence[state_count - 1] = message_code;
/* If this is a KEY exchange related message */
if ((message_code >= 20) && (message_code <= 49)) {
//Do nothing
} else {
message_size += 8;
}
byte_count += message_size - 2;
}
}
*state_count_ref = state_count;
return state_sequence;
}
unsigned int* extract_response_codes_tls(unsigned char* buf, unsigned int buf_size, unsigned int* state_count_ref)
{
char *mem;
unsigned int byte_count = 0;
unsigned int mem_count = 0;
unsigned int mem_size = 1024;
unsigned char content_type, message_type;
unsigned int *state_sequence = NULL;
unsigned int state_count = 0;
mem=(char *)ck_alloc(mem_size);
//Add initial state
state_count++;
state_sequence = (unsigned int *)ck_realloc(state_sequence, state_count * sizeof(unsigned int));
state_sequence[state_count - 1] = 0;
while (byte_count < buf_size) {
memcpy(&mem[mem_count], buf + byte_count++, 1);
//Check if the region buffer length is at least 6 bytes (5 bytes for record header size)
//the 6th byte could be message type
if (mem_count >= 6) {
//1st byte: content type
//2nd and 3rd byte: TLS version
//Extract the message size stored in the 4th and 5th bytes
content_type = mem[0];
//Check if this is an application data record
if (content_type != 0x17) {
message_type = mem[5];
} else {
message_type = 0xFF;
}
u16* size_buf = (u16*)&mem[3];
u16 message_size = (u16)ntohs(*size_buf);
//and skip the payload
unsigned int bytes_to_skip = message_size - 1;
unsigned int temp_count = 0;
while ((byte_count < buf_size) && (temp_count < bytes_to_skip)) {
byte_count++;
temp_count++;
}
if (byte_count < buf_size) {
byte_count--;
}
//add a new response code
unsigned int message_code = (content_type << 8) + message_type;
state_count++;
state_sequence = (unsigned int *)ck_realloc(state_sequence, state_count * sizeof(unsigned int));
state_sequence[state_count - 1] = message_code;
mem_count = 0;
} else {
mem_count++;
if (mem_count == mem_size) {
//enlarge the mem buffer
mem_size = mem_size * 2;
mem=(char *)ck_realloc(mem, mem_size);
}
}
}
if (mem) ck_free(mem);
*state_count_ref = state_count;
return state_sequence;
}
unsigned int* extract_response_codes_dicom(unsigned char* buf, unsigned int buf_size, unsigned int* state_count_ref)
{
if (buf_size == 0) {
*state_count_ref = 0;
return NULL;
}
unsigned int *state_sequence = NULL;
unsigned int state_count = 0;
state_count++;
state_sequence = (unsigned int *)ck_realloc(state_sequence, state_count * sizeof(unsigned int));
state_sequence[state_count - 1] = 0; // initial status code is 0
state_count++;
unsigned int message_code = buf[0]; // return PDU type as status code
state_sequence = (unsigned int *)ck_realloc(state_sequence, state_count * sizeof(unsigned int));
state_sequence[state_count - 1] = message_code;
*state_count_ref = state_count;
return state_sequence;
};
unsigned int* extract_response_codes_dns(unsigned char* buf, unsigned int buf_size, unsigned int* state_count_ref)
{
char *mem;
unsigned int mem_count = 0;
unsigned int mem_size = 1024;
unsigned int *state_sequence = NULL;
unsigned int state_count = 0;
mem=(char *)ck_alloc(mem_size);
state_count++;
state_sequence = (unsigned int *)ck_realloc(state_sequence, state_count * sizeof(unsigned int));
state_sequence[state_count - 1] = 0;
for (unsigned int byte_count = 0; byte_count < buf_size; byte_count++) {
memcpy(&mem[mem_count], buf + byte_count, 1);
// The original query will be included with the response.
if ((mem_count >= 12) && (*(mem+mem_count) == 0)) {
// 4 bytes left of the query. Jump to the answer.
byte_count += 5;
mem_count += 5;
// Save the 3rd & 4th bytes as the response code
unsigned int message_code = (unsigned int) ((mem[2] << 8) + mem[3]);
state_count++;
state_sequence = (unsigned int *)ck_realloc(state_sequence, state_count * sizeof(unsigned int));
state_sequence[state_count - 1] = message_code;
mem_count = 0;
} else {
mem_count++;
if (mem_count == mem_size) {
//enlarge the mem buffer
mem_size = mem_size * 2;
mem=(char *)ck_realloc(mem, mem_size);
}
}
}
if (mem) ck_free(mem);
*state_count_ref = state_count;
return state_sequence;
}
static unsigned char dtls12_version[2] = {0xFE, 0xFD};
// (D)TLS known and custom constants
// the known 1-byte (D)TLS content types
#define CCS_CONTENT_TYPE 0x14
#define ALERT_CONTENT_TYPE 0x15
#define HS_CONTENT_TYPE 0x16
#define APPLICATION_CONTENT_TYPE 0x17
#define HEARTBEAT_CONTENT_TYPE 0x18
// custom content types
#define UNKNOWN_CONTENT_TYPE 0xFF // the content type is unrecognized
// custom handshake types (for handshake content)
#define UNKNOWN_MESSAGE_TYPE 0xFF // when the message type cannot be determined because the message is likely encrypted
#define MALFORMED_MESSAGE_TYPE 0xFE // when message type cannot be determined because the message appears to be malformed
region_t *extract_requests_dtls12(unsigned char* buf, unsigned int buf_size, unsigned int* region_count_ref) {
unsigned int byte_count = 0;
unsigned int region_count = 0;
region_t *regions = NULL;
unsigned int cur_start = 0;
while (byte_count < buf_size) {
//Check if the first three bytes are <valid_content_type><dtls-1.2>
if ((byte_count > 3 && buf_size - byte_count > 1) &&
(buf[byte_count] >= CCS_CONTENT_TYPE && buf[byte_count] <= HEARTBEAT_CONTENT_TYPE) &&
(memcmp(&buf[byte_count+1], dtls12_version, 2) == 0)) {
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = byte_count-1;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
cur_start = byte_count;
} else {
//Check if the last byte has been reached
if (byte_count == buf_size - 1) {
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = byte_count;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
break;
}
}
byte_count ++;
}
//in case region_count equals zero, it means that the structure of the buffer is broken
//hence we create one region for the whole buffer
if ((region_count == 0) && (buf_size > 0)) {
regions = (region_t *)ck_realloc(regions, sizeof(region_t));
regions[0].start_byte = 0;
regions[0].end_byte = buf_size - 1;
regions[0].state_sequence = NULL;
regions[0].state_count = 0;
region_count = 1;
}
*region_count_ref = region_count;
return regions;
}
// a status code comprises <content_type, message_type> tuples
// message_type varies depending on content_type (e.g. for handshake content, message_type is the handshake message type...)
//
unsigned int* extract_response_codes_dtls12(unsigned char* buf, unsigned int buf_size, unsigned int* state_count_ref)
{
unsigned int byte_count = 0;
unsigned int *state_sequence = NULL;
unsigned int state_count = 0;
unsigned int status_code = 0;
state_count++;
state_sequence = (unsigned int *)ck_realloc(state_sequence, state_count * sizeof(unsigned int));
state_sequence[state_count - 1] = 0; // initial status code is 0
while (byte_count < buf_size) {
// a DTLS 1.2 record has a 13 bytes header, followed by the contained message
if ( (buf_size - byte_count > 13) &&
(buf[byte_count] >= CCS_CONTENT_TYPE && buf[byte_count] <= HEARTBEAT_CONTENT_TYPE) &&
(memcmp(&buf[byte_count+1], dtls12_version, 2) == 0)) {
unsigned char content_type = buf[byte_count];
unsigned char message_type;
u32 record_length = read_bytes_to_uint32(buf, byte_count+11, 2);
// the record length exceeds buffer boundaries (not expected)
if (buf_size - byte_count - 13 - record_length < 0) {
message_type = MALFORMED_MESSAGE_TYPE;
}
else {
switch(content_type) {
case HS_CONTENT_TYPE: ;
unsigned char hs_msg_type = buf[byte_count+13];
// the minimum size of a correct DTLS 1.2 handshake message is 12 bytes comprising fragment header fields
if (record_length >= 12) {
u32 frag_length = read_bytes_to_uint32(buf, byte_count+22, 3);
// we can check if the handshake record is encrypted by subtracting fragment length from record length
// which should yield 12 if the fragment is not encrypted
// the likelyhood for an encrypted fragment to satisfy this condition is very small
if (record_length - frag_length == 12) {
// not encrypted
message_type = hs_msg_type;
} else {
// encrypted handshake message
message_type = UNKNOWN_MESSAGE_TYPE;
}
} else {
// malformed handshake message
message_type = MALFORMED_MESSAGE_TYPE;
}
break;
case CCS_CONTENT_TYPE:
if (record_length == 1) {
// unencrypted CCS
unsigned char ccs_msg_type = buf[byte_count+13];
message_type = ccs_msg_type;
} else {
if (record_length > 1) {
// encrypted CCS
message_type = UNKNOWN_MESSAGE_TYPE;
} else {
// malformed CCS
message_type = MALFORMED_MESSAGE_TYPE;
}
}
break;
case ALERT_CONTENT_TYPE:
if (record_length == 2) {
// unencrypted alert, the type is sufficient for determining which alert occurred
// unsigned char level = buf[byte_count+13];
unsigned char type = buf[byte_count+14];
message_type = type;
} else {
if (record_length > 2) {
// encrypted alert
message_type = UNKNOWN_MESSAGE_TYPE;
} else {
// malformed alert
message_type = MALFORMED_MESSAGE_TYPE;
}
}
break;
case APPLICATION_CONTENT_TYPE:
// for application messages we cannot determine whether they are encrypted or not
message_type = UNKNOWN_MESSAGE_TYPE;
break;
case HEARTBEAT_CONTENT_TYPE:
// a heartbeat message is at least 3 bytes long (1 byte type, 2 bytes payload length)
// unfortunately, telling an encrypted message from an unencrypted message cannot be done reliably due to the variable length of padding
// hence we just use unknown for either case
if (record_length >= 3) {
// unsigned char hb_msg_type = buf[byte_count+13];
// u32 hb_length = read_bytes_to_uint32(buf, byte_count+14, 2);
// unkown heartbeat message
message_type = UNKNOWN_MESSAGE_TYPE;
} else {
// malformed heartbeat
message_type = MALFORMED_MESSAGE_TYPE;
}
break;
default:
// unknown content and message type, should not be hit
content_type = UNKNOWN_CONTENT_TYPE;
message_type = UNKNOWN_MESSAGE_TYPE;
break;
}
}
status_code = (content_type << 8) + message_type;
state_count++;