-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
2200 lines (1839 loc) · 69.7 KB
/
main.cpp
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
/* Stark Erich 6041
Program na generovanie zadanych paketov
Zadanie 1 - KS2 FEI 2014
Navrhnite a implementujte program, ktory generuje pakety podla zadanych poziadaviek
a uklada ich do suboru. Pakety musia byt spravne rozpoznane programom Wireshark.
Spresnenie zadania:
Pri vytvarani paketu, si program nacita konfiguracny XML subor kde budu potrebne informacie
na zostavenie paketov v zavislosti na type protokolu.
Je nutne nastavit vsetky nastavenia z konfiguracneho suboru.
Vyznam tagov v sablone:
<packet_summary> uvodny tag pre zaciatok celeho konfiguracneho nastavenia
<item> zaciatok noveho paketu
<index> poradove cislo paketu
<frame_type> typ ramca (Ethernet, 802.3 )
<local_mac_address> zdrojova MAC adresa (vzor formatu : 00-1d-60-45-59-07)
<remote_mac_address> cielová MAC adresa (vzor formatu : 00-1d-60-45-59-07)
<protocol> typ vnorenych protokolov zo sietovej a transportnej (IP,IPX,TCP,UDP)
<version> verzia protokolu (4,6)
<local_address> zdrojova IP adresa pri protokole IP v dekadickom tvare
<remote_address> cielova IP adresa pri protokole IP v dekadickom tvare
<local_net_address> zdrojova sietova adresa pri IPX protokole v hexa tvare
<local_socket_address> zdrojovy port pri IPX protokole v hexa tvare
<remote_net_address> cielova sietová adresa pri IPX protokole v hexa tvare
<remote_socket_address> cielovy port pri IPX protokole v hexa tvare
<protocol_type> typ vnoreneho protokolu
<local_port> zdrojovy port v dekadickom tvare
<remote_port> cielovy port v dekadickom tvare
<packets> pocet generovanych paketov daneho typu
Blizsie poziadavky:
- Ak je vnoreny protokol IP treba nastavit dalsie parametre potrebne ku spravnej identifikacii
paketu (staci default hodnoty- ide napr. o TTL, flags, total length, atd.). Kontrolny sucet
sa hodnoti ako bonus (header checksum).
- Ak je vnoreny protokol TCP, UDP treba nastavit dalsie parametre potrebne ku spravnej identifikacii
paketu (napr. priznak, okno, velkost...stací default hodnoty ). Vypocet kontrolneho suctu je povinny.
- Pri vnorenom IPX protokole nastavit default hodnoty pre dlzku paketu, transport kontrol, aby bol paket
spravne identifikovany.
- Pri type ramca 802.3 neriesit SNAP a 802.2 iba IPX.
- Program musi ukladat pakety jednotlivo, alebo viac naraz.
Kazda cast aplikacie musi po spusteni svoju cinnost zdokumentovat pomocou sprav o prave vykonanych ulohach
napr. Nacitam obsah suboru s nazvom ..., Nacitanie suboru ... prebehlo v poriadku, Analyzujem hlavicku,
Vypocitam CRC. Porovnavam CRC. Paket podla CRC dorazil neporusene, atd. texty sprav kazdy student upravi
podla potreby vlastnej implementacie!!!).
Riesenie:
Zoznam pouzitych premennych a ich komentovany vyznam (vyplnit podla vasho kodu!!! napr. SA - zdrojova adresa paketu):
vsetky premenne mam pochopitelne pomenovane
*/
/*
* Author: Erich Stark
*
* 2014
*
*
* Licence: GPLv3
*/
#include <stdio.h> // to get "printf" function
#include <stdlib.h> // to get "free" function
#include <iostream>
#include <fstream>
#include <string.h>
#include <string>
#include "xmlparser.h"
#include <regex>
#include <iterator>
#include <unistd.h>
#include <pcap.h>
#include <array>
#include <vector>
using namespace std;
// setup
int const IPv4(0x45);
string const UDP("UDP");
string const TCP("TCP");
string const PEP("PEP");
// colors
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
int str_to_int(string text) {
return stoi(text, NULL, 16);
}
int str_to_int(string text, int base) {
return stoi(text, NULL, base);
}
string parse_mac_address(string mac_address) {
auto it = std::remove_if(std::begin(mac_address), std::end(mac_address), [](char c) {
return (c == '-');
});
mac_address.erase(it, std::end(mac_address));
return mac_address;
}
string parse_ipx_items(string ipx_item) {
auto it = std::remove_if(std::begin(ipx_item), std::end(ipx_item), [](char c) {
return (c == ' ');
});
ipx_item.erase(it, std::end(ipx_item));
return ipx_item;
}
u_char* parse_ip_addr(string ip_address) {
char ip1[3] = "";
char ip2[3] = "";
char ip3[3] = "";
char ip4[3] = "";
u_char* ip;
ip = (u_char*) malloc((4)*1);
int count = 0;
int j = 0;
for (int i = 0; i < ip_address.length(); i++) {
if (ip_address[i] == '.') {
count++;
j = -1;
} else {
if (count == 0)ip1[j] = ip_address[i];
else if (count == 1) ip2[j] = ip_address[i];
else if (count == 2) ip3[j] = ip_address[i];
else if (count == 3) ip4[j] = ip_address[i];
}
j++;
}
//nastavenie cielovej adresy
ip[0] = atoi(ip1);
ip[1] = atoi(ip2);
ip[2] = atoi(ip3);
ip[3] = atoi(ip4);
return ip;
}
string dec_to_hexstr(string dec_str) {
string result;
long int decimalNumber = str_to_int(dec_str, 10);
long int quotient;
int temp;
quotient = decimalNumber;
while (quotient != 0) {
temp = quotient % 16;
//To convert integer into character
if (temp < 10)
temp = temp + 48;
else
temp = temp + 55;
result += temp;
quotient = quotient / 16;
}
if (result.length() == 1) {
result += "000";
} else if (result.length() == 2) {
result += "00";
} else if (result.length() == 3) {
result += "0";
}
return string(result.rbegin(), result.rend());
}
u_char* setup_ipx_packet(int size_of_packet,
string local_mac_address,
string remote_mac_address,
string local_net_address,
string remote_net_address,
string local_socket_address,
string remote_socket_address) {
u_char* packet;
packet = (u_char*) malloc((size_of_packet)*1);
// 802.3 header
// destination mac 00-03-ba-9a-15-63
string remote_mac_address_new = parse_mac_address(remote_mac_address);
int j = 0;
for (int i = 0; i <= 5; i++) {
packet[i] = str_to_int(remote_mac_address_new.substr(j, 2)); // destination node 802.3
packet[i + 24] = str_to_int(remote_mac_address_new.substr(j, 2)); // destination node IPX
j += 2;
}
// source mac 00-1d-60-45-59-07
string local_mac_address_new = parse_mac_address(local_mac_address);
j = 0;
for (int i = 6; i <= 11; i++) {
packet[i] = str_to_int(local_mac_address_new.substr(j, 2)); // source node 802.3
packet[i + 30] = str_to_int(local_mac_address_new.substr(j, 2)); // source node IPX
j += 2;
}
// lenght 0x01b0 kvoli identifikacii
packet[12] = 0x00;
packet[13] = 0x20; // dlzka dat (ipx cele 32 dec)
//end header
// checksum musi byt 0xffff
packet[14] = 0xff;
packet[15] = 0xff;
// ipx packet lenght max 0x01b0
packet[16] = 0x00;
packet[17] = 0x4e; // 3e
// transport control
packet[18] = 0x03; // 3 hops
// packet type pep 0x04
packet[19] = 0x04;
// destination network 30 09 80 00
string remote_net_address_new = parse_ipx_items(remote_net_address);
j = 0;
for (int i = 20; i <= 23; i++) {
packet[i] = str_to_int(remote_net_address_new.substr(j, 2));
j += 2;
}
// destination socket 04 53
string remote_socket_address_new = parse_ipx_items(remote_socket_address);
packet[30] = str_to_int(remote_socket_address_new.substr(0, 2));
packet[31] = str_to_int(remote_socket_address_new.substr(2, 2));
// source network 30 09 80 00
string local_net_address_new = parse_ipx_items(local_net_address);
j = 0;
for (int i = 32; i <= 35; i++) {
packet[i] = str_to_int(local_net_address_new.substr(j, 2));
j += 2;
}
// source socket 04 53
string local_socket_address_new = parse_ipx_items(local_socket_address);
packet[42] = str_to_int(local_socket_address_new.substr(0, 2));
packet[43] = str_to_int(local_socket_address_new.substr(2, 2));
for (int i = 44; i <= 60; i++) {
packet[i] = 0x00;
}
// set request or response 0x0002
packet[44] = 0x00;
packet[45] = 0x01;
return packet;
}
u_char* setup_udp_packet(int size_of_packet,
string local_mac_address,
string remote_mac_address,
string version,
string local_address,
string remote_address,
string protocol_type,
string local_port,
string remote_port,
string service_name) {
u_char* packet;
packet = (u_char*) malloc((size_of_packet)*1);
// set destination MAC
string remote_mac_address_new = parse_mac_address(remote_mac_address);
int j = 0;
for (int i = 0; i <= 5; i++) {
packet[i] = str_to_int(remote_mac_address_new.substr(j, 2));
j += 2;
}
// set source MAC
j = 0;
string local_mac_address_new = parse_mac_address(local_mac_address);
for (int i = 6; i <= 11; i++) {
packet[i] = str_to_int(local_mac_address_new.substr(j, 2));
j += 2;
}
// set IP protocol 0x0800
packet[12] = 0x08;
packet[13] = 0x00;
// IPv4 = 0x45
packet[14] = IPv4;
// diff services field
packet[15] = 0x00;
// length IP
packet[16] = 0x00;
packet[17] = 0x28; // 40 bytes
// identification
packet[18] = 0x00;
packet[19] = 0x00;
// flags
packet[20] = 0x40; // dont fragment
// fragment offset
packet[21] = 0x00;
// Time To Live - TTL 128?
packet[22] = 0x80;
// protocol UDP
packet[23] = 0x11;
// IP header checksum
packet[24] = 0x00;
packet[25] = 0x00;
// IP source address
u_char* ipcko_local = parse_ip_addr(local_address);
for (int i = 26; i <= 29; i++) {
packet[i] = ipcko_local[i - 26];
}
// IP destination address
u_char* ipcko_remote = parse_ip_addr(remote_address);
for (int i = 30; i <= 33; i++) {
packet[i] = ipcko_remote[i - 30];
}
// check sum
uint32_t sum_ip = 0;
uint16_t word_ip;
for (int i = 14; i <= 33; i += 2) {
word_ip = ((packet[i] << 8) & 0xff00) + (packet[i + 1] & 0xff);
sum_ip += (uint32_t) word_ip;
}
while (sum_ip >> 16)
sum_ip = (sum_ip & 0xffff) + (sum_ip >> 16);
sum_ip = ~sum_ip;
packet[24] = (sum_ip & 0xff00) >> 8;
packet[25] = sum_ip & 0xff;
cout << GREEN << "[OK]" << RESET << " Vypocitany checksum pre IP hlavicku UDP" << endl;
// UDP information
// udp source port
string local_port_new = dec_to_hexstr(local_port); // 44 5c
packet[34] = str_to_int(local_port_new.substr(0, 2)); // D8E6
packet[35] = str_to_int(local_port_new.substr(2, 2));
string remote_port_new = dec_to_hexstr(remote_port);
// udp destination port
packet[36] = str_to_int(remote_port_new.substr(0, 2)); // 35
packet[37] = str_to_int(remote_port_new.substr(2, 2));
// udp length
packet[38] = 0x00;
packet[39] = 0x14; // 20 bytes
// check sum
packet[40] = 0x5a;
packet[41] = 0x11;
// data 50:69:76:61:72:6e:69:6b:20:6a:65:20:68:6f:6d:6f:73:20:3a:44
packet[42] = 0x00;
packet[43] = 0x00;
packet[44] = 0x00;
packet[45] = 0x00;
packet[46] = 0x00;
packet[47] = 0x00;
packet[48] = 0x00;
packet[49] = 0x00;
packet[50] = 0x00;
packet[51] = 0x00;
packet[52] = 0x00;
packet[53] = 0x00;
packet[54] = 0x00;
packet[55] = 0x00;
packet[56] = 0x00;
packet[57] = 0x00;
packet[58] = 0x00;
packet[59] = 0x00;
packet[60] = 0x00;
packet[61] = 0x00;
uint32_t sum = 0;
uint16_t word;
int i;
for (i = 26; i <= 39; i += 2) {
word = ((packet[i] << 8) & 0xff00) + (packet[i + 1] & 0xff);
sum += (uint32_t) word;
}
sum += 17 + 40 - 20;
while (sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
sum = ~sum;
packet[40] = (sum & 0xff00) >> 8;
packet[41] = sum & 0xff;
cout << GREEN << "[OK]" << RESET << " Vypocitany checksum pre UDP" << endl;
return packet;
}
u_char* setup_tcp_packet(int size_of_packet,
string local_mac_address,
string remote_mac_address,
string version,
string local_address,
string remote_address,
string protocol_type,
string local_port,
string remote_port,
string service_name) {
u_char* packet;
packet = (u_char*) malloc((size_of_packet)*1);
// mac destination address to 00-00-00-00-00-00
string remote_mac_address_new = parse_mac_address(remote_mac_address);
int j = 0;
for (int i = 0; i <= 5; i++) {
packet[i] = str_to_int(remote_mac_address_new.substr(j, 2));
j += 2;
}
// set mac source address to 00-21-85-11-29-1b
string local_mac_address_new = parse_mac_address(local_mac_address);
j = 0;
for (int i = 6; i <= 11; i++) {
packet[i] = str_to_int(local_mac_address_new.substr(j, 2));
j += 2;
}
// set IP protocol 0x0800
packet[12] = 0x08;
packet[13] = 0x00;
// IPv4 = 0x45
packet[14] = IPv4;
// differentiated services field
packet[15] = 0x00;
// length IP
packet[16] = 0x00;
packet[17] = 0x28; // 40 bytes
// identification 3a50
packet[18] = 0x00;
packet[19] = 0x00;
// flags
packet[20] = 0x40; // dont fragment
// fragment offset
packet[21] = 0x00;
// Time To Live - TTL 128?
packet[22] = 0x80;
// protocol TCP
packet[23] = 0x06;
// ip header checksum // TODO
packet[24] = 0x00;
packet[25] = 0x00;
// IP source address
u_char* ipcko_local = parse_ip_addr(local_address);
for (int i = 26; i <= 29; i++) {
packet[i] = ipcko_local[i - 26];
}
// IP destination address
u_char* ipcko_remote = parse_ip_addr(remote_address);
for (int i = 30; i <= 33; i++) {
packet[i] = ipcko_remote[i - 30];
}
uint32_t sum_ip = 0;
uint16_t word_ip;
for (int i = 14; i <= 33; i += 2) {
word_ip = ((packet[i] << 8) & 0xff00) + (packet[i + 1] & 0xff);
sum_ip += (uint32_t) word_ip;
}
while (sum_ip >> 16)
sum_ip = (sum_ip & 0xffff) + (sum_ip >> 16);
sum_ip = ~sum_ip;
packet[24] = (sum_ip & 0xff00) >> 8;
packet[25] = sum_ip & 0xff;
cout << GREEN << "[OK]" << RESET << " Vypocitany checksum pre IP hlavicku TCP" << endl;
// TCP information
// TCP source port
string local_port_new = dec_to_hexstr(local_port);
packet[34] = str_to_int(local_port_new.substr(0, 2));
packet[35] = str_to_int(local_port_new.substr(2, 2));
// TCP destination port
string remote_port_new = dec_to_hexstr(remote_port);
packet[36] = str_to_int(remote_port_new.substr(0, 2));
packet[37] = str_to_int(remote_port_new.substr(2, 2));
// TCP sequence number: 0 (relative) example: 0x1626d405
packet[38] = 0x00;
packet[39] = 0x00;
packet[40] = 0x00;
packet[41] = 0x00;
// acknowledgment number : 1 example: 0x59042021
packet[42] = 0x00;
packet[43] = 0x00;
packet[44] = 0x00;
packet[45] = 0x00;
// tcp header lenght
packet[46] = 0x50; // 20 bytes
// flags 0x01f - prenastavene na 0x00 lebo mi nesedelo FTP vo wiresharku
packet[47] = 0x00; // flags
// window size
packet[48] = 0x01; // example number
packet[49] = 0x00;
// checksum
packet[50] = 0x00;
packet[51] = 0x00;
// urgent pointer
packet[52] = 0x00;
packet[53] = 0x00;
// 54-65 options
// no operation
packet[54] = 0x00;
packet[55] = 0x00;
packet[56] = 0x00;
packet[57] = 0x00;
packet[58] = 0x00;
packet[59] = 0x00;
packet[60] = 0x00;
packet[61] = 0x00;
packet[62] = 0x00;
packet[63] = 0x00;
//packet[64] = 0x00;
//packet[65] = 0x00;
uint32_t sum = 0;
uint16_t word;
int i;
for (i = 26; i <= 49; i += 2) {
word = ((packet[i] << 8) & 0xff00) + (packet[i + 1] & 0xff);
sum += (uint32_t) word;
}
sum += 6 + 40 - 20;
while (sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
sum = ~sum;
packet[50] = (sum & 0xff00) >> 8;
packet[51] = sum & 0xff;
cout << GREEN << "[OK]" << RESET << " Vypocitany checksum pre TCP" << endl;
return packet;
}
u_char* setup_ike_packet(int size_of_packet,
string local_mac_address,
string remote_mac_address,
string version_ike,
string exchange_type,
string local_address,
string remote_address,
string local_port,
string remote_port,
string initiator_cookie,
string responder_cookie
) {
u_char* packet;
packet = (u_char*) malloc(size_of_packet);
// set destination MAC
string remote_mac_address_new = parse_mac_address(remote_mac_address);
int j = 0;
for (int i = 0; i <= 5; i++) {
packet[i] = str_to_int(remote_mac_address_new.substr(j, 2));
j += 2;
}
// set source MAC
j = 0;
string local_mac_address_new = parse_mac_address(local_mac_address);
for (int i = 6; i <= 11; i++) {
packet[i] = str_to_int(local_mac_address_new.substr(j, 2));
j += 2;
}
// set IP protocol 0x0800
packet[12] = 0x08;
packet[13] = 0x00;
// IPv4 = 0x45
packet[14] = IPv4;
// diff services field
packet[15] = 0x00;
// length IP
packet[16] = 0x01;
packet[17] = 0x10; // 272 bytes
// identification
packet[18] = 0x00;
packet[19] = 0x01;
// flags
packet[20] = 0x00;
// fragment offset
packet[21] = 0x00;
// Time To Live - TTL 64
packet[22] = 0x40;
// protocol UDP - IKE je pri UDP
packet[23] = 0x11;
// IP header checksum
packet[24] = 0x00;
packet[25] = 0x00;
// IP source address
u_char* ipcko_local = parse_ip_addr(local_address);
for (int i = 26; i <= 29; i++) {
packet[i] = ipcko_local[i - 26];
}
// IP destination address
u_char* ipcko_remote = parse_ip_addr(remote_address);
for (int i = 30; i <= 33; i++) {
packet[i] = ipcko_remote[i - 30];
}
// check sum
uint32_t sum_ip = 0;
uint16_t word_ip;
for (int i = 14; i <= 33; i += 2) {
word_ip = ((packet[i] << 8) & 0xff00) + (packet[i + 1] & 0xff);
sum_ip += (uint32_t) word_ip;
}
while (sum_ip >> 16)
sum_ip = (sum_ip & 0xffff) + (sum_ip >> 16);
sum_ip = ~sum_ip;
packet[24] = (sum_ip & 0xff00) >> 8;
packet[25] = sum_ip & 0xff;
cout << GREEN << "[OK]" << RESET << " Vypocitany checksum pre IP hlavicku UDP" << endl;
// UDP information
// udp source port
string local_port_new = dec_to_hexstr(local_port); // 44 5c
packet[34] = str_to_int(local_port_new.substr(0, 2)); // D8E6
packet[35] = str_to_int(local_port_new.substr(2, 2));
string remote_port_new = dec_to_hexstr(remote_port);
// udp destination port
packet[36] = str_to_int(remote_port_new.substr(0, 2)); // 35
packet[37] = str_to_int(remote_port_new.substr(2, 2));
// udp length
packet[38] = 0x00;
packet[39] = 0xfc; // 252 bytes
// check sum
packet[40] = 0x00;
packet[41] = 0x00;
// start of IKE (ISAKMP)
// // initiator cookie
// packet[42] = 0xdd;
// packet[43] = 0xe8;
// packet[44] = 0x90;
// packet[45] = 0xdb;
// packet[46] = 0x1f;
// packet[47] = 0x62;
// packet[48] = 0xef;
// packet[49] = 0x70;
//
// // responder cookie
// for (int i = 0; i < 8; i++) {
// packet[i + 50] = 0x00;
// }
//
// // next payload
// packet[58] = 0x21; // security association dec 33
//
// // version
// packet[59] = 0x20;
//
// // exchange type
// packet[60] = 0x22; // IKE_SA_INIT dec 34
//
// // flags
// packet[61] = 0x08;
//
// // message ID
// for (int i = 0; i < 4; i++) {
// packet[i + 62] = 0x00;
// }
//
// // length 244
// packet[66] = 0x00;
// packet[67] = 0x00;
// packet[68] = 0x00;
// packet[69] = 0xf4;
//
// // type payload: security association
//
// // next payload
// packet[70] = 0x22; // key exchange dec 34
//
// // critical bit
// packet[71] = 0x00;
//
// // payload length 44
// packet[72] = 0x00;
// packet[73] = 0x2c;
//
// packet[74] = 0x00;
//
// // type payload: proposal (2) #1
//
// // next payload: NONE / No next payload
// packet[75] = 0x00;
//
// // critical bit
// packet[76] = 0x00;
//
// // payload length 40
// packet[77] = 0x00;
// packet[78] = 0x28;
//
// // proposal number 1
// packet[79] = 0x01;
//
// // protocol ID: IKE 1
// packet[80] = 0x01;
//
// // SPI size 0
// packet[81] = 0x00;
//
// // proposal transforms
// packet[82] = 0x04;
//
// // type payload: transform
//
// // next payload: transform
// packet[83] = 0x03;
//
// // critical bit
// packet[84] = 0x00;
//
// // payload length
// packet[85] = 0x00;
// packet[86] = 0x08;
//
// // transform type: encryption algorithm (ENCR)
// packet[87] = 0x01;
//
// packet[88] = 0x00;
//
// // transform ID: (ENCR) ENCR_3DES
// packet[89] = 0x00;
// packet[90] = 0x03;
//
// // type payload transform
//
// // next payload: transform
// packet[91] = 0x03;
//
// // critical bit
// packet[92] = 0x00;
//
// // payload length
// packet[93] = 0x00;
// packet[94] = 0x08;
//
// // Transform Type: Pseudo-random Function (PRF) (2)
// packet[95] = 0x02;
//
// packet[96] = 0x00;
//
// // Transform ID (PRF): PRF_HMAC_MD5 (1)
// packet[97] = 0x00;
// packet[98] = 0x01;
//
// // next payload: transform
// packet[99] = 0x03;
//
// // critical bit
// packet[100] = 0x00;
//
// // payload length
// packet[101] = 0x00;
// packet[102] = 0x08;
//
// // Transform Type: Integrity Algorithm (INTEG) (3)
// packet[103] = 0x03;
//
// packet[104] = 0x00;
//
// // Transform ID (INTEG): AUTH_HMAC_MD5_96 (1)
// packet[105] = 0x00;
// packet[106] = 0x01;
//
//
// packet[107] = 0x00;
// packet[108] = 0x00;
// packet[109] = 0x00;
//
// packet[107] = 0x08;
// packet[108] = 0x04;
// packet[109] = 0x00;
// packet[110] = 0x00;
// packet[111] = 0x02;
//
// packet[112] = 0x28;
// packet[113] = 0x00;
// packet[114] = 0x00;
// packet[115] = 0x88;
// packet[116] = 0x00;
// packet[117] = 0x02;
//
// for
// packet[0] = 0x00;
//packet[1] = 0x01;
//packet[2] = 0x01;
//packet[3] = 0x00;
//packet[4] = 0x00;
//packet[5] = 0x02;
//packet[6] = 0x00;
//packet[7] = 0x01;
//packet[8] = 0x01;
//packet[9] = 0x00;
//packet[10] = 0x00;
//packet[11] = 0x01;
//packet[12] = 0x08;
//packet[13] = 0x00;
//packet[14] = 0x45;
//packet[15] = 0x00;
//packet[16] = 0x01;
//packet[17] = 0x10;
//packet[18] = 0x00;
//packet[19] = 0x01;
//packet[20] = 0x00;
//packet[21] = 0x00;
//packet[22] = 0x40;
//packet[23] = 0x11;
//packet[24] = 0x7b;
//packet[25] = 0xda;
//packet[26] = 0x7f;
//packet[27] = 0x00;
//packet[28] = 0x00;
//packet[29] = 0x01;
//packet[30] = 0x7f;
//packet[31] = 0x00;
//packet[32] = 0x00;
//packet[33] = 0x01;
//packet[34] = 0x01;
//packet[35] = 0xf4;
//packet[36] = 0x01;
//packet[37] = 0xf4;
//packet[38] = 0x00;
//packet[39] = 0xfc;
//packet[40] = 0x3d;
//packet[41] = 0x64;
// start of IKE (ISAKMP)
// initiator cookie
// Initiator cookie: dde890db1f62ef70
j = 0;
for (int i = 0; i < 8; i++) {
packet[i + 42] = str_to_int(initiator_cookie.substr(j, 2));
j += 2;
}
// Responder cookie: 0000000000000000
j = 0;
for (int i = 0; i < 8; i++) {
packet[i + 50] = str_to_int(responder_cookie.substr(j, 2));
j += 2;
}
// Next payload: Security Association (33)
packet[58] = 0x21;
// IKE version 20 - 2 major 0 minor
packet[59] = str_to_int(version_ike);
// Exchange type: INFORMATIONAL (37)
packet[60] = str_to_int(dec_to_hexstr(exchange_type));
// Flags
packet[61] = 0x08;
// Message ID: 0x00000000
packet[62] = 0x00;
packet[63] = 0x00;
packet[64] = 0x00;
packet[65] = 0x00;
// length
packet[66] = 0x00;
packet[67] = 0x00;