-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstructions.c
executable file
·1739 lines (1334 loc) · 67.1 KB
/
instructions.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
/*
This deals with anything to do with the instructions structure type. It either goes from instructions to packet building, or raw packet
analysis to instructions. All IPv4/6 core analysis code will be located here. I attempted to design as modular as possible although developed
the majority of this system in a weekend on the spot. I didn't think it through beyond the overall concept of attacks against anti
surveillance platforms. I can't at the moment find any reasons to change things drastically. I will want to work towards more zero
copy scenarios in the future to increase packets, and sessions per second. (mbufs/etc) At least until the initial instructions
are built for on the wire sessions we want to automatically pull and reproduce..
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
#include <netinet/ip6.h>
#include <string.h>
#include <errno.h>
#include <net/ethernet.h>
#include "network.h"
#include "antisurveillance.h"
#include "packetbuilding.h"
#include "utils.h"
#include "attacks.h"
#include "instructions.h"
#include "adjust.h"
#define MAX_THREAD 16
// Prepare a filter structure using various flags.. call it several times to set different values if required for those flags..
// This is a simple packet filter. I will create a new area which does ProtocolFilters (DNS/HTTP/etc) which will actually
// verify against the packets whether or not it is a legit session of some protocol..
void FilterPrepare(FilterInformation *fptr, int type, uint32_t value) {
if (fptr->init != 1) {
memset((void *)fptr, 0, sizeof(FilterInformation));
fptr->init = 1;
}
// does this filter allow looking for specific packet propertieS? like SYN/ACK/PSH/RST
if (type & FILTER_PACKET_FLAGS) {
fptr->flags |= FILTER_PACKET_FLAGS;
fptr->packet_flags = value;
}
// will it look for both sides of the connection? ie: port 80 would allow stuff from server to client as well
if (type & FILTER_PACKET_FAMILIAR) {
fptr->flags |= FILTER_PACKET_FAMILIAR;
}
// does the IP address match?
if (type & FILTER_CLIENT_IP) {
fptr->flags |= FILTER_CLIENT_IP;
fptr->source_ip = value;
}
// does the server IP match?
if (type & FILTER_SERVER_IP) {
fptr->flags |= FILTER_SERVER_IP;
fptr->destination_ip = value;
}
// does the server port match?
if (type & FILTER_SERVER_PORT) {
fptr->flags |= FILTER_SERVER_PORT;
fptr->destination_port = value;
}
// does the source port (usually random on client) match?
if (type & FILTER_CLIENT_PORT) {
fptr->flags |= FILTER_CLIENT_PORT;
fptr->source_port = value;
}
// filter by TCP packets
if (type & FILTER_PACKET_TCP)
fptr->flags |= FILTER_PACKET_TCP;
// filter by UDP packets
if (type & FILTER_PACKET_UDP)
fptr->flags |= FILTER_PACKET_UDP;
// filter by ICMP packets
if (type & FILTER_PACKET_ICMP)
fptr->flags |= FILTER_PACKET_ICMP;
// filter by IPv4 packets
if (type & FILTER_PACKET_IPV4)
fptr->flags |= FILTER_PACKET_IPV4;
// filter by ipv6 packets
if (type & FILTER_PACKET_IPV6)
fptr->flags |= FILTER_PACKET_IPV6;
if (type & FILTER_OURS)
fptr->flags |= FILTER_OURS;
}
// Filters through packets ensuring that it matches a criteria of something being looked for..
int FilterCheck(AS_context *ctx, FilterInformation *fptr, PacketBuildInstructions *iptr) {
int ret = 0;
OutgoingPacketQueue *optr = NULL;
int cur_packet = 0;
int found = 0;
if (iptr == NULL) return -1;
//return 1;
// if the filter is empty... its allowed
if (fptr->flags == 0 || fptr->init != 1) return 1;
// verify client IP
if (fptr->flags & FILTER_CLIENT_IP) {
if (!fptr->is_source_ipv6) {
if (iptr->source_ip != fptr->source_ip)
if (!(fptr->flags & FILTER_PACKET_FAMILIAR) ||
((fptr->flags & FILTER_PACKET_FAMILIAR) && (iptr->destination_ip != fptr->source_ip)))
goto end;
} else if (fptr->is_source_ipv6) {
if (!CompareIPv6Addresses(&iptr->source_ipv6, &fptr->source_ipv6))
if (!(fptr->flags & FILTER_PACKET_FAMILIAR) ||
((fptr->flags & FILTER_PACKET_FAMILIAR) && (!CompareIPv6Addresses(&iptr->destination_ipv6, &fptr->source_ipv6))))
goto end;
}
}
// verify server IP
if (fptr->flags & FILTER_SERVER_IP) {
if (!fptr->is_destination_ipv6) {
if (iptr->destination_ip != fptr->destination_ip)
if (!(fptr->flags & FILTER_PACKET_FAMILIAR) ||
((fptr->flags & FILTER_PACKET_FAMILIAR) && (iptr->source_ip != fptr->destination_ip)))
goto end;
} else if (fptr->is_destination_ipv6) {
if (!CompareIPv6Addresses(&iptr->destination_ipv6, &fptr->destination_ipv6))
if (!(fptr->flags & FILTER_PACKET_FAMILIAR) ||
((fptr->flags & FILTER_PACKET_FAMILIAR) && (!CompareIPv6Addresses(&iptr->source_ipv6, &fptr->destination_ipv6))))
goto end;
}
}
// verify server port (for instance www 80)
if (fptr->flags & FILTER_SERVER_PORT)
if (iptr->destination_port != fptr->destination_port)
if (!(fptr->flags & FILTER_PACKET_FAMILIAR) ||
((fptr->flags & FILTER_PACKET_FAMILIAR) && (iptr->source_port != fptr->destination_port)))
goto end;
// verify client source port
if (fptr->flags & FILTER_CLIENT_PORT)
if (iptr->source_port != fptr->source_port)
if (!(fptr->flags & FILTER_PACKET_FAMILIAR) ||
((fptr->flags & FILTER_PACKET_FAMILIAR) && (iptr->destination_port != fptr->source_port)))
goto end;
// looking for a specific type of packet by its flags..
if (fptr->flags & FILTER_PACKET_FLAGS) {
if (fptr->packet_flags & TCP_FLAG_SYN)
if (!(iptr->flags & TCP_FLAG_SYN)) goto end;
if (fptr->packet_flags & TCP_FLAG_ACK)
if (!(iptr->flags & TCP_FLAG_ACK)) goto end;
if (fptr->packet_flags & TCP_FLAG_PSH)
if (!(iptr->flags & TCP_FLAG_PSH)) goto end;
if (fptr->packet_flags & TCP_FLAG_FIN)
if (!(iptr->flags & TCP_FLAG_FIN)) goto end;
if (fptr->packet_flags & TCP_FLAG_RST)
if (!(iptr->flags & TCP_FLAG_RST)) goto end;
}
// are we filtering by TCP? If so.. is it either TCP 4, or 6?
if (fptr->flags & FILTER_PACKET_TCP)
if (!(iptr->type & PACKET_TYPE_TCP_4) && !(iptr->type & PACKET_TYPE_TCP_6)) goto end;
// are we filtering by UDP? If so.. is it either IPv4, or IPv6?
if (fptr->flags & FILTER_PACKET_UDP)
if (!(iptr->type & PACKET_TYPE_UDP_4) && !(iptr->type & PACKET_TYPE_UDP_6)) goto end;
// are we looking for ICMP? if so.. check if it matches either IPv4, or IPv6 ICMP
if (fptr->flags & FILTER_PACKET_ICMP)
if (!(iptr->type & PACKET_TYPE_ICMP_4) && !(iptr->type & PACKET_TYPE_ICMP_6)) goto end;
// is this packet IPv4?
if (fptr->flags & FILTER_PACKET_IPV4)
if (!(iptr->type & PACKET_TYPE_IPV4)) goto end;
// is this packet IPv6?
if (fptr->flags & FILTER_PACKET_IPV6)
if (!(iptr->type & PACKET_TYPE_IPV6)) goto end;
// we must verify that this packet doesnt match any of our recent outgoing packet queues...
// this is so we dont continously add our own virtual sessions into the system as new attack
// structures
// this is a bit CPU intensive.. i might decide to just check for ports later (by using an array
// after a session was replayed)
if (fptr->flags & FILTER_OURS) {
pthread_mutex_lock(&ctx->network_pool_mutex);
optr = ctx->outgoing_pool_waiting;
while (optr != NULL && !found) {
while (cur_packet < optr->cur_packet) {
if ((optr->packets[cur_packet].source_port == iptr->source_port) &&
(optr->packets[cur_packet].dest_port == iptr->destination_port)) {
// we need this temporary value since the mutex has been locked...
found = 1;
break;
}
cur_packet++;
}
optr = optr->next;
}
pthread_mutex_unlock(&ctx->network_pool_mutex);
}
ret = 1;
end:;
return ret;
}
// creates the base structure for instruction to build a for the wire packet..
PacketBuildInstructions *BuildInstructionsNew(PacketBuildInstructions **list, ConnectionProperties *cptr, int from_client, int flags) {
PacketBuildInstructions *bptr = NULL;
if ((bptr = (PacketBuildInstructions *)calloc(1, sizeof(PacketBuildInstructions))) == NULL) return NULL;
if (!cptr->is_ipv6) {
bptr->type = PACKET_TYPE_TCP_4 | PACKET_TYPE_IPV4|PACKET_TYPE_TCP;
bptr->source_ip = from_client ? cptr->client_ip : cptr->server_ip;
bptr->destination_ip = from_client ? cptr->server_ip : cptr->client_ip;
} else {
bptr->type = PACKET_TYPE_TCP_6 | PACKET_TYPE_IPV6|PACKET_TYPE_TCP;
if (from_client) {
CopyIPv6Address(&bptr->source_ipv6, &cptr->client_ipv6);
CopyIPv6Address(&bptr->destination_ipv6, &cptr->server_ipv6);
} else {
CopyIPv6Address(&bptr->source_ipv6, &cptr->server_ipv6);
CopyIPv6Address(&bptr->destination_ipv6, &cptr->client_ipv6);
}
}
bptr->source_port = from_client ? cptr->client_port : cptr->server_port;
bptr->destination_port = from_client ? cptr->server_port : cptr->client_port;
bptr->flags = flags;
// OS emulation
bptr->ttl = from_client ? cptr->client_ttl : cptr->server_ttl;
bptr->tcp_window_size = from_client ? cptr->max_packet_size_client : cptr->max_packet_size_server;
if (bptr->tcp_window_size == 0) {
bptr->tcp_window_size = 1500 - ((20*2)+12);
}
bptr->next = NULL;
// FIFO ordering
L_link_ordered((LINK **)list, (LINK *)bptr);
return bptr;
}
// Generates instructions for fabricating a TCP connection being opened between two hosts..
// handles both ipv6, and ipv4
int GenerateTCPConnectionInstructions(ConnectionProperties *cptr, PacketBuildInstructions **final_build_list) {
PacketBuildInstructions *bptr = NULL;
PacketBuildInstructions *build_list = NULL;
int packet_flags = 0;
int packet_ttl = 0;
int ret = -1;
// first we need to generate a connection syn packet..
packet_flags = TCP_FLAG_SYN|TCP_OPTIONS|TCP_OPTIONS_TIMESTAMP|TCP_OPTIONS_WINDOW;
packet_ttl = cptr->client_ttl;
if ((bptr = BuildInstructionsNew(&build_list, cptr, 1, packet_flags)) == NULL) goto err;
bptr->header_identifier = cptr->client_identifier++;
bptr->client = 1; // so it can generate source port again later... for pushing same messages w out full reconstruction
bptr->ack = 0;
bptr->seq = cptr->client_seq++;
bptr->aptr = cptr->aptr;
// then nthe server needs to respond acknowledgng it
packet_flags = TCP_FLAG_SYN|TCP_FLAG_ACK|TCP_OPTIONS|TCP_OPTIONS_TIMESTAMP|TCP_OPTIONS_WINDOW;
packet_ttl = cptr->server_ttl;
if ((bptr = BuildInstructionsNew(&build_list, cptr, 0, packet_flags)) == NULL) goto err;
bptr->header_identifier = cptr->server_identifier++;
bptr->ack = cptr->client_seq;
bptr->seq = cptr->server_seq++;
bptr->aptr = cptr->aptr;
// then the client must respond acknowledging that servers response..
packet_flags = TCP_FLAG_ACK|TCP_OPTIONS|TCP_OPTIONS_TIMESTAMP;
packet_ttl = cptr->client_ttl;
if ((bptr = BuildInstructionsNew(&build_list, cptr, 1, packet_flags)) == NULL) goto err;
bptr->header_identifier = cptr->client_identifier++;
bptr->client = 1;
bptr->ack = cptr->server_seq;
bptr->seq = cptr->client_seq;
bptr->aptr = cptr->aptr;
L_link_ordered((LINK **)final_build_list, (LINK *)build_list);
return 1;
err:;
return ret;
}
// Generates the instructions for the fabrication of TCP data transfer between two hosts
// Its general enough to be used with binary protocols, and supports client or server side to opposite
// notes from old HTTP building function: (i want to support packet loss over a large amount of sessions soon.. even if 1-5%)
// later we can emulate some packet loss in here.. its just random()%100 < some percentage..
// with a loop resending the packet.. super simple to handle. we can also falsify other scenarios
// involving ICMP etc.. some very nasty tricks coming.
// works on ipv4/6 now
int GenerateTCPSendDataInstructions(ConnectionProperties *cptr, PacketBuildInstructions **final_build_list, int from_client, char *data, int size) {
PacketBuildInstructions *bptr = NULL;
PacketBuildInstructions *build_list = NULL;
int packet_flags = 0;
int packet_size;
char *data_ptr = data;
int data_size = size;
int packet_ttl = 0;
uint32_t source_ip;
uint32_t source_port;
uint32_t dest_ip;
uint32_t dest_port;
uint32_t *src_identifier = NULL;
uint32_t *dst_identifier = NULL;
uint32_t *my_seq = NULL;
uint32_t *remote_seq = NULL;
int window_size = 0;
// prepare variables depending on the side of the that the data is going from -> to
if (from_client) {
source_ip = cptr->client_ip;
source_port = cptr->client_port;
dest_ip = cptr->server_ip;
dest_port = cptr->server_port;
src_identifier = &cptr->client_identifier;
dst_identifier = &cptr->server_identifier;
my_seq = &cptr->client_seq;
remote_seq = &cptr->server_seq;
} else {
source_ip = cptr->server_ip;
source_port = cptr->server_port;
dest_ip = cptr->client_ip;
dest_port = cptr->client_port;
src_identifier = &cptr->server_identifier;
dst_identifier = &cptr->client_identifier;
my_seq = &cptr->server_seq;
remote_seq = &cptr->client_seq;
}
//printf("data size: %d\n", data_size);
// now the sending side must loop until it sends all daata
while (data_size > 0) {
packet_size = min(data_size, from_client ? cptr->max_packet_size_client : cptr->max_packet_size_server);
if (packet_size == 0 && data_size) {
packet_size = 1500 - (20*2+12);
if (packet_size > data_size) packet_size = data_size;
}
//if (packet_size > 53) packet_size -=
// if something wasn't handled properly.. (when i turned off OSPick().. i had to search for hours to find this =/)
if (packet_size < 0) return -1;
// the client sends its request... split into packets..
packet_flags = TCP_FLAG_PSH|TCP_FLAG_ACK|TCP_OPTIONS|TCP_OPTIONS_TIMESTAMP;
packet_ttl = from_client ? cptr->client_ttl : cptr->server_ttl;
window_size = from_client ? cptr->max_packet_size_client : cptr->max_packet_size_server;
if ((bptr = BuildInstructionsNew(&build_list, cptr, from_client, packet_flags)) == NULL) goto err;
if (DataPrepare(&bptr->data, data_ptr, packet_size) != 1) goto err;
bptr->data_size = packet_size;
bptr->header_identifier = (*src_identifier)++;
bptr->client = from_client;
bptr->ack = *remote_seq;
bptr->seq = *my_seq;
bptr->aptr = cptr->aptr;
*my_seq += packet_size;
data_size -= packet_size;
data_ptr += packet_size;
// receiver sends ACK packet for this packet
packet_flags = TCP_FLAG_ACK|TCP_OPTIONS|TCP_OPTIONS_TIMESTAMP;
packet_ttl = from_client ? cptr->server_ttl : cptr->client_ttl;
window_size = from_client ? cptr->max_packet_size_server : cptr->max_packet_size_client;
if ((bptr = BuildInstructionsNew(&build_list, cptr, !from_client, packet_flags)) == NULL) goto err;
bptr->header_identifier = (*dst_identifier)++;
bptr->ack = *my_seq;
bptr->seq = *remote_seq;
bptr->client = !from_client;
bptr->aptr = cptr->aptr;
}
L_link_ordered((LINK **)final_build_list, (LINK *)build_list);
return 1;
err:;
return 0;
}
// Generates fabricated packets required to disconnect a TCP session between two hosts.. starting with one side (client or server)
int GenerateTCPCloseConnectionInstructions(ConnectionProperties *cptr, PacketBuildInstructions **final_build_list, int from_client) {
PacketBuildInstructions *bptr = NULL;
PacketBuildInstructions *build_list = NULL;
int packet_flags = 0;
//int packet_size = 0;
uint32_t source_ip=0;
uint32_t source_port=0;
uint32_t dest_ip=0;
uint32_t dest_port=0;
uint32_t *src_identifier = NULL;
uint32_t *dst_identifier = NULL;
uint32_t *my_seq = NULL;
uint32_t *remote_seq = NULL;
int packet_ttl = 0;
int window_size = 0;
// prepare variables depending on the side of the that the data is going from -> to
if (from_client) {
source_ip = cptr->client_ip;
source_port = cptr->client_port;
dest_ip = cptr->server_ip;
dest_port = cptr->server_port;
src_identifier = &cptr->client_identifier;
dst_identifier = &cptr->server_identifier;
my_seq = &cptr->client_seq;
remote_seq = &cptr->server_seq;
} else {
source_ip = cptr->server_ip;
source_port = cptr->server_port;
dest_ip = cptr->client_ip;
dest_port = cptr->client_port;
src_identifier = &cptr->server_identifier;
dst_identifier = &cptr->client_identifier;
my_seq = &cptr->server_seq;
remote_seq = &cptr->client_seq;
}
// source (client or server) sends FIN packet...
packet_flags = TCP_FLAG_FIN|TCP_FLAG_ACK|TCP_OPTIONS|TCP_OPTIONS_TIMESTAMP;
packet_ttl = from_client ? cptr->client_ttl : cptr->server_ttl;
window_size = from_client ? cptr->max_packet_size_client : cptr->max_packet_size_server;
if ((bptr = BuildInstructionsNew(&build_list, cptr, from_client, packet_flags)) == NULL) goto err;
bptr->client = from_client;
bptr->header_identifier = (*src_identifier)++;
bptr->ack = *remote_seq;
bptr->seq = *my_seq;
*my_seq += 1;
bptr->aptr = cptr->aptr;
// other side needs to respond..adds its own FIN with its ACK
packet_flags = TCP_FLAG_FIN|TCP_FLAG_ACK|TCP_OPTIONS|TCP_OPTIONS_TIMESTAMP;
packet_ttl = from_client ? cptr->server_ttl : cptr->client_ttl;
window_size = from_client ? cptr->max_packet_size_server : cptr->max_packet_size_client;
if ((bptr = BuildInstructionsNew(&build_list, cptr, !from_client, packet_flags)) == NULL) goto err;
bptr->client = !from_client;
bptr->header_identifier = (*dst_identifier)++;
bptr->ack = *my_seq;
bptr->seq = *remote_seq;
*remote_seq += 1;
bptr->aptr = cptr->aptr;
// source (client or server) sends the final ACK packet...
packet_flags = TCP_FLAG_ACK|TCP_OPTIONS|TCP_OPTIONS_TIMESTAMP;
packet_ttl = from_client ? cptr->client_ttl : cptr->server_ttl;
window_size = from_client ? cptr->max_packet_size_client : cptr->max_packet_size_server;
if ((bptr = BuildInstructionsNew(&build_list, cptr, from_client, packet_flags)) == NULL) goto err;
bptr->client = from_client;
bptr->header_identifier = (*src_identifier)++;
bptr->ack = *remote_seq;
bptr->seq = *my_seq;
bptr->aptr = cptr->aptr;
L_link_ordered((LINK **)final_build_list, (LINK *)build_list);
return 1;
err:;
return 0;
}
// Process an IPv4 UDP packet from the wire, or a PCAP
PacketBuildInstructions *ProcessUDP4Packet(char *packet, int size) {
PacketBuildInstructions *iptr = NULL;
struct packetudp4 *p = NULL;
int data_size = 0;
char *checkbuf = NULL;
struct pseudo_header_udp4 *udp_chk_hdr = NULL;
uint32_t pkt_chk = 0, our_chk = 0;
//printf("Process UDP4\n");
// sanity checks since we are reading directly from the network (as opposed to pcap when i developed this)
if (size < sizeof(struct packetudp4)) goto end;
p = (struct packetudp4 *)packet;
// Lets do this here.. so we can append it to the list using a jump pointer so its faster
// was taking way too long loading a 4gig pcap (hundreds of millions of packets)
if ((iptr = (PacketBuildInstructions *)calloc(1, sizeof(PacketBuildInstructions))) == NULL) goto end;
// ensure the type is set
iptr->type = PACKET_TYPE_UDP_4 | PACKET_TYPE_IPV4|PACKET_TYPE_UDP;
// start out OK.. might fail it later during checksum
iptr->ok = 1;
PtrDuplicate(packet, size, &iptr->packet, &iptr->packet_size);
// source IP, and port from the IP/TCP headers
iptr->source_ip = p->ip.saddr;
iptr->source_port = ntohs(p->udp.source);
// destination IP, and port from the TCP/IP headers
iptr->destination_ip = p->ip.daddr;
iptr->destination_port = ntohs(p->udp.dest);
// how much data is present in this packet?
// The UDP header portion has the length of everything except IP header or less (ether header), or WIFI...
data_size = ntohs(p->udp.len) - sizeof(struct udphdr);
// sanity checks since we are reading fromm the network
if ((data_size < 0) || (data_size != (size - sizeof(struct packetudp4)))) goto end;
if (data_size > 0 && iptr->packet) {
iptr->data = iptr->packet + sizeof(struct packetudp4);
iptr->data_size = data_size;
}
// Keep note of the packets checksum..
pkt_chk = p->udp.check;
// Set it to 0 now so we can verify ourselves..
p->udp.check = 0;
if ((checkbuf = (char *)calloc(1, sizeof(struct pseudo_header_udp4) + sizeof(struct udphdr) + iptr->data_size)) == NULL) goto end;
// copy udp hdr after the pseudo header for checksum
memcpy((void *)(checkbuf + sizeof(struct pseudo_header_udp4)), &p->udp, sizeof(struct udphdr));
// if there is data then we copy it behind all headers inside of the checkbuf pointer
if (iptr->data_size)
memcpy((void *)(checkbuf + sizeof(struct pseudo_header_udp4) + sizeof(struct udphdr)), iptr->data, iptr->data_size);
// fill out the pseudo header for this UDP checksum
udp_chk_hdr = (struct pseudo_header_udp4 *)checkbuf;
udp_chk_hdr->protocol = IPPROTO_UDP;
udp_chk_hdr->source_address = iptr->source_ip;
udp_chk_hdr->destination_address = iptr->destination_ip;
udp_chk_hdr->placeholder = 0;
udp_chk_hdr->len = htons(sizeof(struct udphdr) + iptr->data_size);
// perform checksum functin on PSEUDO header we just set parameters for, the actual for the wire UDP header, and the data
our_chk = in_cksum((unsigned short *)checkbuf, sizeof(struct pseudo_header_udp4) + sizeof(struct udphdr) + iptr->data_size);
if (pkt_chk != our_chk) iptr->ok = 0;
// put the original checksum back regardless
p->udp.check = pkt_chk;
// free the buffer we allocated for the checksum
free(checkbuf);
end:;
return iptr;
}
// Process an IPv4 ICMP packet from the wire, or a
// Sanity checks added since itll parse live packets
PacketBuildInstructions *ProcessICMP4Packet(char *packet, int size) {
PacketBuildInstructions *iptr = NULL;
struct packeticmp4 *p = (struct packeticmp4 *)packet;
int data_size = 0;
unsigned short pkt_chk = 0, our_chk = 0;
//printf("Process ICMP4\n");
// data coming from network.. so sanity checks required
if (size < sizeof(struct packeticmp4)) goto end;
// allocate space for an instruction structure which analysis of this packet will create
if ((iptr = (PacketBuildInstructions *)calloc(1, sizeof(PacketBuildInstructions))) == NULL) goto end;
// ensure the type is set
iptr->type = PACKET_TYPE_ICMP_4 | PACKET_TYPE_IPV4|PACKET_TYPE_ICMP;
iptr->header_identifier = ntohs(p->ip.id);
// get IP addreses out of the packet
iptr->source_ip = p->ip.saddr;
//printf("SRC: %u DST: %u\n", p->ip.saddr, p->ip.daddr);
iptr->destination_ip = p->ip.daddr;
// how much data is present in this packet?
data_size = ntohs(p->ip.tot_len) - sizeof(struct packeticmp4);
// since we are ready from the network.. we need some sanity checks
if ((data_size < 0) && (data_size != (size - sizeof(struct packeticmp4)))) goto end;
// set packet as OK (can disqualify from checksum)
iptr->ok = 1;
PtrDuplicate(packet, size, &iptr->packet, &iptr->packet_size);
// use packet checksum from the packet
pkt_chk = p->icmp.checksum;
// copy data from the original packet
if (data_size > 0 && iptr->packet) {
iptr->data = iptr->packet + sizeof(struct packeticmp4);
iptr->data_size = data_size;
}
// set to 0 in packet so we can calculate correctly..
p->icmp.checksum = 0;
// ICMP checksum.. it can happen inline without copying to a new buffer.. no pseudo header
our_chk = (unsigned short)in_cksum((unsigned short *)&p->icmp, sizeof(struct icmphdr) + iptr->data_size);
// did the check equal what we expected?
if (pkt_chk != our_chk) {
iptr->ok = 0;
}
// set back the original checksum we used to verify against
p->icmp.checksum = pkt_chk;
end:;
return iptr;
}
// Process an IPv4 TCP/IP packet from wire or PCAP
PacketBuildInstructions *ProcessTCP4Packet(char *packet, int size) {
PacketBuildInstructions *iptr = NULL;
struct packet *p = NULL;
int flags = 0;
int data_size = 0;
char *sptr = NULL;
uint16_t pkt_chk = 0;
struct pseudo_tcp4 *p_tcp = NULL;
char *checkbuf = NULL;
int tcp_header_size = 0;
// sanity since this is coming off of the wire
if (size < sizeof(struct packet)) goto end;
p = (struct packet *)packet;
//if (ntohs(p->tcp.source) != 10000 && ntohs(p->tcp.dest) != 10000) return NULL;
// Determine which TCP flags are set in this packet
flags = 0;
if (p->tcp.syn) flags |= TCP_FLAG_SYN;
if (p->tcp.ack) flags |= TCP_FLAG_ACK;
if (p->tcp.psh) flags |= TCP_FLAG_PSH;
if (p->tcp.fin) flags |= TCP_FLAG_FIN;
if (p->tcp.rst) flags |= TCP_FLAG_RST;
// Lets do this here.. so we can append it to the list using a jump pointer so its faster
// was taking way too long loading a 4gig pcap (hundreds of millions of packets)
if ((iptr = (PacketBuildInstructions *)calloc(1, sizeof(PacketBuildInstructions))) == NULL) goto end;
// ensure the type is set
iptr->type = PACKET_TYPE_TCP_4 | PACKET_TYPE_IPV4|PACKET_TYPE_TCP;
// source IP, and port from the IP/TCP headers
iptr->source_ip = p->ip.saddr;
iptr->source_port = ntohs(p->tcp.source);//ntohs(p->tcp.source);
// destination IP, and port from the TCP/IP headers
iptr->destination_ip = p->ip.daddr;
iptr->destination_port = ntohs(p->tcp.dest);//ntohs(p->tcp.dest);
//printf("packet ports %d -> %d\n", iptr->source_port, iptr->destination_port);
// Ensure this new structure has the proper flags which were set in this packet
iptr->flags = flags;
// IP packet TTL (time to live).. (OS emu)
iptr->ttl = p->ip.ttl;
// TCP window size (OS emu)
iptr->tcp_window_size = ntohs(p->tcp.window);
// start OK.. until checksum.. or disqualify for other reasons
iptr->ok = 1;
PtrDuplicate(packet, size, &iptr->packet, &iptr->packet_size);
// total size from IPv4 header
data_size = ntohs(p->ip.tot_len);
// subtract header size from total packet size to get data size..
data_size -= (p->ip.ihl << 2) + (p->tcp.doff << 2);
// start checksum...
// get tcp header size (so we know if it has options, or not)
tcp_header_size = (p->tcp.doff << 2);
// sanity checks since we are reading fromm the network
// *** finish sanity checks
//if ((data_size < 0) || (data_size != (pptr->size - sizeof(struct icmphdr) - sizeof(struct tcphdr) - sizeof)) goto end;
if (data_size > 0 && iptr->packet) {
// pointer to where the data starts in this packet being analyzed
sptr = (char *)(iptr->packet + ((p->ip.ihl << 2) + (p->tcp.doff << 2)));
iptr->data = sptr;
iptr->data_size = data_size;
}
// header identifier from IP header
iptr->header_identifier = ntohs(p->ip.id);
// ack/seq are important for tcp/ip (used to transmit lost packets, etc)
// in future for quantum insert protection it will be important as well...
iptr->seq = ntohl(p->tcp.seq);
iptr->ack = ntohl(p->tcp.ack_seq);
// if it has more than the tcp header structure size.. the rest is TCP/IP options
// *** finish this: other parts of the code needs to realize we modified tcp_header_size, etc
if (tcp_header_size > sizeof(struct tcphdr) && 1==0) {
// calculate options size by space remaining after the tcphdr structure
iptr->options_size = tcp_header_size - sizeof(struct tcphdr);
// allocate space for the options in its own separate memory space
if ((iptr->options = (char *)malloc(iptr->options_size)) == NULL) goto end;
// copy the options from the packet into the allocated space
memcpy(iptr->options, (void *)(packet + sizeof(struct packet)), iptr->options_size);
// calculate actual TCP header size without options
tcp_header_size = sizeof(struct tcphdr);
}
// start of packet checksum verifications
// set a temporary buffer to the packets IP header checksum
pkt_chk = p->ip.check;
// set packets IP checksum to 0 so we can calculate and verify here
p->ip.check = 0;
// calculate what it should be
p->ip.check = (unsigned short)in_cksum((unsigned short *)&p->ip, sizeof(struct iphdr));
// verify its OK.. if not mark this packet as bad (so we can verify how many bad/good and decide
// on discarding or not)
if (p->ip.check != pkt_chk) iptr->ok = 0;
// lets put the IP header checksum back
p->ip.check = pkt_chk;
// now lets verify TCP header..
// copy the packets header..
pkt_chk = p->tcp.check;
// set the packet header to 0 so we can calculate it like an operatinng system would
p->tcp.check = 0;
// it needs to be calculated with a special pseudo structure..
checkbuf = (char *)calloc(1,sizeof(struct pseudo_tcp4) + tcp_header_size + iptr->data_size + iptr->options_size);
if (checkbuf == NULL) goto end;
// code taken from ipv4 tcp packet building function
p_tcp = (struct pseudo_tcp4 *)checkbuf;
// copy tcp header into the psuedo structure
memcpy(&p_tcp->tcp, &p->tcp, tcp_header_size);
// set psuedo header parameters for calculating checksum
p_tcp->saddr = p->ip.saddr;
p_tcp->daddr = p->ip.daddr;
p_tcp->mbz = 0;
p_tcp->ptcl = IPPROTO_TCP;
p_tcp->tcpl = htons(tcp_header_size + iptr->data_size);
// copy tcp/ip options into its buffer
if (iptr->options_size)
memcpy(checkbuf + sizeof(struct pseudo_tcp4), iptr->options, iptr->options_size);
// now copy the data itself of the packet
if (iptr->data_size)
memcpy(checkbuf + sizeof(struct pseudo_tcp4) + tcp_header_size + iptr->options_size, iptr->data, iptr->data_size);
// put the checksum into the correct location inside of the header
p->tcp.check = (unsigned short)in_cksum((unsigned short *)checkbuf, tcp_header_size + sizeof(struct pseudo_tcp4) + iptr->data_size);
// TCP header verification failed
if (p->tcp.check != pkt_chk) iptr->ok = 0;
// put the original value back
p->tcp.check = pkt_chk;
// done w checksums..
// free that buffer we used for checksum calculations
free(checkbuf);
// moved other analysis to the next function which builds the attack structure
end:;
return iptr;
}
// Process an IPv4 TCP/IP packet from wire or PCAP
// *** finish sanity checks
PacketBuildInstructions *ProcessTCP6Packet(char *packet, int size) {
PacketBuildInstructions *iptr = NULL;
struct packettcp6 *p = NULL;
int flags = 0;
int data_size = 0;
char *sptr = NULL;
int tcp_header_size = 0;
if (size < sizeof(struct packettcp6)) goto end;
p = (struct packettcp6 *)packet;
// Determine which TCP flags are set in this packet
flags = 0;
if (p->tcp.syn) flags |= TCP_FLAG_SYN;
if (p->tcp.ack) flags |= TCP_FLAG_ACK;
if (p->tcp.psh) flags |= TCP_FLAG_PSH;
if (p->tcp.fin) flags |= TCP_FLAG_FIN;
if (p->tcp.rst) flags |= TCP_FLAG_RST;
// Lets do this here.. so we can append it to the list using a jump pointer so its faster
// was taking way too long loading a 4gig pcap (hundreds of millions of packets)
if ((iptr = (PacketBuildInstructions *)calloc(1, sizeof(PacketBuildInstructions))) == NULL) goto end;
// ensure the type is set
iptr->type = PACKET_TYPE_TCP_6 | PACKET_TYPE_IPV6|PACKET_TYPE_TCP;
// source IP, and port from the IP/TCP headers
CopyIPv6Address(&iptr->source_ipv6, &p->ip.ip6_src);
iptr->source_port = ntohs(p->tcp.source);
// destination IP, and port from the TCP/IP headers
CopyIPv6Address(&iptr->destination_ipv6, &p->ip.ip6_dst);
iptr->destination_port = ntohs(p->tcp.dest);
// Ensure this new structure has the proper flags which were set in this packet
iptr->flags = flags;
// IP packet TTL (time to live).. (OS emu)
iptr->ttl = p->ip.ip6_ctlun.ip6_un1.ip6_un1_hlim;
// TCP window size (OS emu)
iptr->tcp_window_size = ntohs(p->tcp.window);
// start OK.. until checksum.. or disqualify for other reasons
iptr->ok = 1;
PtrDuplicate(packet, size, &iptr->packet, &iptr->packet_size);
// total size from IPv6 header
data_size = ntohs(p->ip.ip6_ctlun.ip6_un1.ip6_un1_plen);// - sizeof(struct ip6_hdr);
// get tcp header size (so we know if it has options, or not)
tcp_header_size = (p->tcp.doff << 2);
data_size -= tcp_header_size;
if (data_size > 0 && iptr->packet) {
// pointer to where the data starts in this packet being analyzed
sptr = (char *)(iptr->packet + sizeof(struct ip6_hdr) + tcp_header_size);
iptr->data = sptr;
iptr->data_size = data_size;
}
// ack/seq are important for tcp/ip (used to transmit lost packets, etc)
// in future for quantum insert protection it will be important as well...
iptr->seq = ntohl(p->tcp.seq);
iptr->ack = ntohl(p->tcp.ack_seq);
// if it has more than the tcp header structure size.. the rest is TCP/IP options
// *** finish this: other parts of the code needs to realize we modified tcp_header_size, etc
if (tcp_header_size > sizeof(struct tcphdr) && 1==0) {
// calculate options size by space remaining after the tcphdr structure
iptr->options_size = tcp_header_size - sizeof(struct tcphdr);
// allocate space for the options in its own separate memory space
if ((iptr->options = (char *)malloc(iptr->options_size)) == NULL) goto end;
// copy the options from the packet into the allocated space
memcpy(iptr->options, (void *)(packet + sizeof(struct packet)), iptr->options_size);
// calculate actual TCP header size without options
tcp_header_size = sizeof(struct tcphdr);
}
// moved other analysis to the next function which builds the attack structure
end:;
return iptr;
}
// Process an IPv4 TCP/IP packet from wire or PCAP
// *** finish sanity checks
PacketBuildInstructions *ProcessUDP6Packet(char *packet, int size) {
PacketBuildInstructions *iptr = NULL;
struct packetudp6 *p = NULL;
int data_size = 0;
char *checkbuf = NULL;
struct pseudo_header_udp6 *udp_chk_hdr = NULL;
uint32_t pkt_chk = 0, our_chk = 0;
// sanity since this is coming off of the wire
if (size < sizeof(struct packetudp6)) goto end;
p = (struct packetudp6 *)packet;
// Lets do this here.. so we can append it to the list using a jump pointer so its faster
// was taking way too long loading a 4gig pcap (hundreds of millions of packets)
if ((iptr = (PacketBuildInstructions *)calloc(1, sizeof(PacketBuildInstructions))) == NULL) goto end;
// ensure the type is set
iptr->type = PACKET_TYPE_UDP_6 | PACKET_TYPE_IPV6|PACKET_TYPE_UDP;
// source IP, and port from the IP/TCP headers
CopyIPv6Address(&iptr->source_ipv6, &p->ip.ip6_src);
iptr->source_port = ntohs(p->udp.source);
// destination IP, and port from the TCP/IP headers
CopyIPv6Address(&iptr->destination_ipv6, &p->ip.ip6_dst);
iptr->destination_port = ntohs(p->udp.dest);
//inet_ntop(AF_INET6, &p->ip.ip6_src, &Aip_src, sizeof(Aip_src));
//inet_ntop(AF_INET6, &p->ip.ip6_dst, &Aip_dst, sizeof(Aip_dst));
//printf("src: %s dst: %s addr %s\n", Aip_src, Aip_dst, addr);
// start OK.. until checksum.. or disqualify for other reasons
iptr->ok = 1;
PtrDuplicate(packet, size, &iptr->packet, &iptr->packet_size);
// calculate data size from udp header
data_size = ntohs(p->udp.len);// - sizeof(struct udphdr);
if (data_size > 0 && iptr->packet) {
// ensure the structure will always have access to this data
iptr->data = iptr->packet + sizeof(struct packetudp6);
iptr->data_size = data_size;
}
// Keep note of the packets checksum..
pkt_chk = p->udp.check;
// Set it to 0 now so we can verify ourselves..
p->udp.check = 0;
if ((checkbuf = (char *)calloc(1, sizeof(struct pseudo_header_udp6) + sizeof(struct udphdr) + iptr->data_size)) == NULL) goto end;
// copy udp hdr after the pseudo header for checksum
memcpy((void *)(checkbuf + sizeof(struct pseudo_header_udp6)), &p->udp, sizeof(struct udphdr));