forked from fasferraz/SWu-IKEv2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswu_emulator.py
3382 lines (2632 loc) · 153 KB
/
swu_emulator.py
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
import serial
import struct
import socket
import random
import time
import select
import sys
import os
import fcntl
import subprocess
import multiprocessing
import requests
from os.path import expanduser
from optparse import OptionParser
from binascii import hexlify, unhexlify
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import dh
from cryptography.hazmat.primitives import hashes, hmac
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from Crypto.Cipher import AES
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from smartcard.System import readers
from smartcard.util import toHexString,toBytes
from CryptoMobile.Milenage import Milenage
requests.packages.urllib3.disable_warnings()
'''
Ike Process
IPsec_encoder (receives data from tunnel interface -> encrypts and sends it towards the server/epdg)
IPsec_decoder (receives encrypted data from server/epdg -> decypts it and sends it to the tunnel interface)
'''
INTER_PROCESS_CREATE_SA = 1
INTER_PROCESS_UPDATE_SA = 2
INTER_PROCESS_DELETE_SA = 3
INTER_PROCESS_IKE = 4
INTER_PROCESS_IE_ENCR_ALG = 1
INTER_PROCESS_IE_INTEG_ALG = 2
INTER_PROCESS_IE_ENCR_KEY = 3
INTER_PROCESS_IE_INTEG_KEY = 4
INTER_PROCESS_IE_SPI_INIT = 5
INTER_PROCESS_IE_SPI_RESP = 6
INTER_PROCESS_IE_IKE_MESSAGE = 7
#DEFAULTs
DEFAULT_IKE_PORT = 500
DEFAULT_IKE_NAT_TRAVERSAL_PORT = 4500
DEFAULT_SERVER = '1.2.3.4'
DEFAULT_COM = '/dev/ttyUSB2'
DEFAULT_IMSI = '123456012345678'
DEFAULT_MCC = '123'
DEFAULT_MNC = '456'
DEFAULT_APN = 'internet'
DEFAULT_TIMEOUT_UDP = 2
#DEFAULT_TIMEOUT_UDP_NAT_TRANSVERSAL = 2
DEFAULT_CK = '0123456789ABCDEF0123456789ABCDEF'
DEFAULT_IK = '0123456789ABCDEF0123456789ABCDEF'
DEFAULT_RES = '0123456789ABCDEF'
NONE = 0
#IKEv2 Payload Types
SA = 33
KE = 34
IDI = 35
IDR = 36
CERT = 37
CERTREQ = 38
AUTH = 39
NINR = 40
N = 41
D = 42
V = 43
TSI = 44
TSR = 45
SK = 46
CP = 47
EAP = 48
#IKEv2 Exchange Types
IKE_SA_INIT = 34
IKE_AUTH = 35
CREATE_CHILD_SA = 36
INFORMATIONAL = 37
IKE = 1
AH = 2
ESP = 3
#Transform Type Values
ENCR = 1
PRF = 2
INTEG = 3
D_H = 4
ESN = 5
#Transform Type 1 - Encryption Algorithm Transform IDs
ENCR_DES_IV64 = 1
ENCR_DES= 2
ENCR_3DES = 3
ENCR_RC5 = 4
ENCR_IDEA = 5
ENCR_CAST = 6
ENCR_BLOWFISH = 7
ENCR_3IDEA = 8
ENCR_DES_IV32 = 9
ENCR_NULL = 11 #Not allowed
ENCR_AES_CBC = 12
ENCR_AES_CTR = 13
ENCR_AES_CCM_8 = 14
ENCR_AES_CCM_12 = 15
ENCR_AES_CCM_16 = 16
ENCR_AES_GCM_8 = 18
ENCR_AES_GCM_12 = 19
ENCR_AES_GCM_16 = 20
#Transform Type 2 - Pseudorandom Function Transform IDs
PRF_HMAC_MD5 = 1
PRF_HMAC_SHA1 = 2
PRF_HMAC_TIGER = 3
PRF_AES128_XCBC = 4
PRF_HMAC_SHA2_256 = 5
PRF_HMAC_SHA2_384 = 6
PRF_HMAC_SHA2_512 = 7
PRF_AES128_CMAC = 8
#Transform Type 3 - Integrity Algorithm Transform IDs
NONE = 0
AUTH_HMAC_MD5_96 = 1
AUTH_HMAC_SHA1_96 = 2
AUTH_DES_MAC = 3
AUTH_KPDK_MD5 = 4
AUTH_AES_XCBC_96 = 5
AUTH_HMAC_MD5_128 = 6
AUTH_HMAC_SHA1_160 = 7
AUTH_AES_CMAC_96 = 8
AUTH_AES_128_GMAC = 9
AUTH_AES_192_GMAC = 10
AUTH_AES_256_GMAC = 11
AUTH_HMAC_SHA2_256_128 = 12
AUTH_HMAC_SHA2_384_192 = 13
AUTH_HMAC_SHA2_512_256 = 14
#Transform Type 4 - Diffie-Hellman Group Transform IDs
MODP_768_bit = 1
MODP_1024_bit = 2
MODP_1536_bit = 5
MODP_2048_bit = 14
MODP_3072_bit = 15
MODP_4096_bit = 16
MODP_6144_bit = 17
MODP_8192_bit = 18
ESN_NO_ESN = 0
ESN_ESN = 1
TLV = 0
TV = 1
#IKEv2 Transform Attribute Types
KEY_LENGTH = (14, TV)
#states
OK = 0
TIMEOUT = 1
REPEAT_STATE = 2
DECODING_ERROR = 3
MANDATORY_INFORMATION_MISSING = 4
OTHER_ERROR = 5
#IKEv2 Notify Message Types - Error Types
UNSUPPORTED_CRITICAL_PAYLOAD = 1
INVALID_IKE_SPI = 4
INVALID_MAJOR_VERSION = 5
INVALID_SYNTAX = 7
INVALID_MESSAGE_ID = 9
INVALID_SPI = 11
NO_PROPOSAL_CHOSEN = 14
INVALID_KE_PAYLOAD = 17
AUTHENTICATION_FAILED = 24
SINGLE_PAIR_REQUIRED = 34
NO_ADDITIONAL_SAS = 35
INTERNAL_ADDRESS_FAILURE = 36
FAILED_CP_REQUIRED = 37
TS_UNACCEPTABLE = 38
INVALID_SELECTORS = 39
TEMPORARY_FAILURE = 43
CHILD_SA_NOT_FOUND = 44
# from 24.302
PDN_CONNECTION_REJECTION = 8192
MAX_CONNECTION_REACHED = 8193
SEMANTIC_ERROR_IN_THE_TFT_OPERATION = 8241
SYNTACTICAL_ERROR_IN_THE_TFT_OPERATION = 8242
SEMANTIC_ERRORS_IN_PACKET_FILTERS = 8244
SYNTACTICAL_ERRORS_IN_PACKET_FILTERS = 8245
NON_3GPP_ACCESS_TO_EPC_NOT_ALLOWED = 9000
USER_UNKNOWN = 9001
NO_APN_SUBSCRIPTION = 9002
AUTHORIZATION_REJECTED = 9003
ILLEGAL_ME = 9006
NETWORK_FAILURE = 10500
RAT_TYPE_NOT_ALLOWED = 11001
IMEI_NOT_ACCEPTED = 11005
PLMN_NOT_ALLOWED = 11011
UNAUTHENTICATED_EMERGENCY_NOT_SUPPORTED = 11055
#IKEv2 Notify Message Types - Status Types
INITIAL_CONTACT = 16384
SET_WINDOW_SIZE = 16385
ADDITIONAL_TS_POSSIBLE = 16386
IPCOMP_SUPPORTED = 16387
NAT_DETECTION_SOURCE_IP = 16388
NAT_DETECTION_DESTINATION_IP = 16389
COOKIE = 16390
USE_TRANSPORT_MODE = 16391
HTTP_CERT_LOOKUP_SUPPORTED = 16392
REKEY_SA = 16393
ESP_TFC_PADDING_NOT_SUPPORTED = 16394
NON_FIRST_FRAGMENTS_ALSO = 16395
# from 24.302
REACTIVATION_REQUESTED_CAUSE = 40961
BACKOFF_TIMER = 41041
PDN_TYPE_IPv4_ONLY_ALLOWED = 41050
PDN_TYPE_IPv6_ONLY_ALLOWED = 41051
DEVICE_IDENTITY = 41101
EMERGENCY_SUPPORT = 41112
EMERGENCY_CALL_NUMBERS = 41134
NBIFOM_GENERIC_CONTAINER = 41288
P_CSCF_RESELECTION_SUPPORT = 41304
PTI = 41501
IKEV2_MULTIPLE_BEARER_PDN_CONNECTIVITY = 42011
EPS_QOS = 42014
EXTENDED_EPS_QOS = 42015
TFT = 42017
MODIFIED_BEARER = 42020
APN_AMBR = 42094
EXTENDED_APN_AMBR = 42095
N1_MODE_CAPABILITY = 51015
#IKEv2 Authenticaton Method
RSA_DIGITAL_SIGNATURE = 1
SHARED_KEY_MESSAGE_INTEGRITY_CODE = 2
DSS_DIGITAL_SIGNATURE = 3
#IKEv2 Traffic Selector Types
TS_IPV4_ADDR_RANGE = 7
TS_IPV6_ADDR_RANGE = 8
#IP protocol_id
ANY = 0
TCP = 6
UDP = 17
ICMP = 1
ESP_PROTOCOL = 50
NAT_TRAVERSAL = 4500
#IKEv2 Configuration Payload CFG Types
CFG_REQUEST = 1
CFG_REPLY = 2
CFG_SET = 3
CFG_ACK = 4
# IKEv2 Configuration Payload Attribute Types (num, length) None = more
INTERNAL_IP4_ADDRESS = 1
INTERNAL_IP4_NETMASK = 2
INTERNAL_IP4_DNS = 3
INTERNAL_IP4_NBNS = 4
INTERNAL_IP4_DHCP = 6
APPLICATION_VERSION = 7
INTERNAL_IP6_ADDRESS = 8
INTERNAL_IP6_DNS = 10
INTERNAL_IP6_DHCP = 12
INTERNAL_IP4_SUBNET = 13
SUPPORTED_ATTRIBUTES = 14
INTERNAL_IP6_SUBNET = 15
MIP6_HOME_PREFIX = 16
INTERNAL_IP6_LINK = 17
INTERNAL_IP6_PREFIX = 18
HOME_AGENT_ADDRESS = 19
P_CSCF_IP4_ADDRESS = 20
P_CSCF_IP6_ADDRESS = 21
FTT_KAT = 22
EXTERNAL_SOURCE_IP4_NAT_INFO = 23
TIMEOUT_PERIOD_FOR_LIVENESS_CHECK = 24
INTERNAL_DNS_DOMAIN = 25
INTERNAL_DNSSEC_TA = 26
#IKEv2 Identification Payload ID Types
ID_IPV4_ADDR = 1
ID_FQDN = 2
ID_RFC822_ADDR = 3
ID_IPV6_ADDR = 5
ID_DER_ASN1_DN = 9
ID_DER_ASN1_GN = 10
ID_KEY_ID = 11
ID_FC_NAME = 12
ID_NULL = 13
#EAP COde type
EAP_REQUEST = 1
EAP_RESPONSE = 2
EAP_SUCCESS = 3
EAP_FAILURE = 4
#IANA EAP Type
EAP_AKA = 23
#EAP-AKA/EAP-SIM Subtypes:
AKA_Challenge = 1
AKA_Authentication_Reject = 2
AKA_Synchronization_Failure = 4
AKA_Identity = 5
SIM_Start = 10
SIM_Challenge = 11
AKA_Notification = 12
SIM_Notification = 12
AKA_Reauthentication = 13
SIM_Reauthentication = 13
AKA_Client_Error = 14
SIM_Client_Error = 14
#EAP-AKA/EAP-SIM Atrributes:
AT_RAND = 1
AT_AUTN = 2
AT_RES = 3
AT_AUTS = 4
AT_PADDING = 6
AT_NONCE_MT = 7
AT_PERMANENT_ID_REQ = 10
AT_MAC = 11
AT_NOTIFICATION = 12
AT_ANY_ID_REQ = 13
AT_IDENTITY = 14
AT_VERSION_LIST = 15
AT_SELECTED_VERSION = 16
AT_FULLAUTH_ID_REQ = 17
AT_COUNTER = 19
AT_COUNTER_TOO_SMALL = 20
AT_NONCE_S = 21
AT_CLIENT_ERROR_CODE = 22
AT_IV = 129
AT_ENCR_DATA = 130
AT_NEXT_PSEUDONYM = 132
AT_NEXT_REAUTH_ID = 133
AT_CHECKCODE = 134
AT_RESULT_IND = 135
# Role
ROLE_INITIATOR = 1
ROLE_RESPONDER = 0
class swu():
def __init__(self, source_address,epdg_address,apn,modem,default_gateway,mcc,mnc,imsi,ki,op,opc,netns):
self.source_address = source_address
self.epdg_address = epdg_address
self.apn = apn
self.com_port = modem
self.default_gateway = default_gateway
self.mcc = mcc
self.mnc = mnc
self.imsi = imsi
self.ki = ki
self.op = op
self.opc = opc
self.netns_name = netns
self.set_variables()
self.set_udp() # default
self.create_socket(self.client_address)
self.create_socket_nat(self.client_address_nat)
self.create_socket_esp(self.client_address_esp)
self.userplane_mode = ESP_PROTOCOL
def set_variables(self):
self.port = DEFAULT_IKE_PORT
self.port_nat = DEFAULT_IKE_NAT_TRAVERSAL_PORT
self.client_address = (self.source_address,self.port)
self.client_address_nat = (self.source_address,self.port_nat)
self.client_address_esp = (self.source_address,0)
self.timeout = DEFAULT_TIMEOUT_UDP
self.state = 0
self.server_address = (self.epdg_address, self.port)
self.server_address_nat = (self.epdg_address, self.port_nat)
self.server_address_esp = (self.epdg_address, 0)
self.message_id_request = 0
self.message_id_responses = 0
self.role = ROLE_INITIATOR
self.old_ike_message_received = False
self.ike_spi_initiator_old = None
self.ike_spi_responder_old = None
self.next_reauth_id = None
self.check_nat = True
self.set_identification(IDI,ID_RFC822_ADDR,'0' + self.imsi + '@nai.epc.mnc' + self.mnc + '.mcc' + self.mcc + '.3gppnetwork.org')
#self.set_identification(IDR,ID_FQDN, self.apn + '.apn.epc.mnc' + self.mnc + '.mcc' + self.mcc + '.3gppnetwork.org')
self.set_identification(IDR,ID_FQDN, self.apn)
self.ike_decoded_header = {}
self.decodable_payloads = [
SA,
KE,
IDI,
IDR,
CERT,
CERTREQ,
AUTH,
NINR,
N,
D,
V,
TSI,
TSR,
SK,
CP,
EAP
]
self.iana_diffie_hellman = {
MODP_768_bit: 768,
MODP_1024_bit: 1024,
MODP_1536_bit: 1536,
MODP_2048_bit: 2048,
MODP_3072_bit: 3072,
MODP_4096_bit: 4096,
MODP_6144_bit: 6144,
MODP_8192_bit: 8192
}
self.prf_function = {
PRF_HMAC_MD5 : hashes.MD5(),
PRF_HMAC_SHA1 : hashes.SHA1(),
#PRF_HMAC_TIGER : 3
#PRF_AES128_XCBC : 4
PRF_HMAC_SHA2_256 : hashes.SHA256(),
PRF_HMAC_SHA2_384 : hashes.SHA384(),
PRF_HMAC_SHA2_512 : hashes.SHA512()
#PRF_AES128_CMAC : 8
}
self.prf_key_len_bytes = {
PRF_HMAC_MD5 : 16,
PRF_HMAC_SHA1 : 20,
#PRF_HMAC_TIGER : -,
PRF_AES128_XCBC : 16,
PRF_HMAC_SHA2_256 : 32,
PRF_HMAC_SHA2_384 : 48,
PRF_HMAC_SHA2_512 : 64,
PRF_AES128_CMAC : 16,
}
self.integ_function = {
NONE : None,
AUTH_HMAC_MD5_96 : hashes.MD5(),
AUTH_HMAC_SHA1_96 : hashes.SHA1(),
#AUTH_DES_MAC : -,
#AUTH_KPDK_MD5 : -,
#AUTH_AES_XCBC_96 : 16,
#AUTH_HMAC_MD5_128 : -,
#AUTH_HMAC_SHA1_160 : -,
#AUTH_AES_CMAC_96 : -,
#AUTH_AES_128_GMAC : 16,
#AUTH_AES_192_GMAC : 24,
#AUTH_AES_256_GMAC : 32,
AUTH_HMAC_SHA2_256_128 : hashes.SHA256(),
AUTH_HMAC_SHA2_384_192 : hashes.SHA384(),
AUTH_HMAC_SHA2_512_256 : hashes.SHA512()
}
self.integ_key_len_bytes = {
NONE : 0,
AUTH_HMAC_MD5_96 : 16,
AUTH_HMAC_SHA1_96 : 20,
#AUTH_DES_MAC : -,
#AUTH_KPDK_MD5 : -,
#AUTH_AES_XCBC_96 : 16,
#AUTH_HMAC_MD5_128 : -,
#AUTH_HMAC_SHA1_160 : -,
#AUTH_AES_CMAC_96 : -,
#AUTH_AES_128_GMAC : 16,
#AUTH_AES_192_GMAC : 24,
#AUTH_AES_256_GMAC : 32,
AUTH_HMAC_SHA2_256_128 : 32,
AUTH_HMAC_SHA2_384_192 : 48,
AUTH_HMAC_SHA2_512_256 : 64
}
self.integ_key_truncated_len_bytes = {
NONE : 0,
AUTH_HMAC_MD5_96 : 12,
AUTH_HMAC_SHA1_96 : 12,
#AUTH_DES_MAC : -,
#AUTH_KPDK_MD5 : -,
#AUTH_AES_XCBC_96 : 12,
#AUTH_HMAC_MD5_128 : -,
#AUTH_HMAC_SHA1_160 : -,
#AUTH_AES_CMAC_96 : -,
#AUTH_AES_128_GMAC : 16?,
#AUTH_AES_192_GMAC : 24?,
#AUTH_AES_256_GMAC : 32?,
AUTH_HMAC_SHA2_256_128 : 16,
AUTH_HMAC_SHA2_384_192 : 24,
AUTH_HMAC_SHA2_512_256 : 32
}
self.configuration_payload_len_bytes = {
INTERNAL_IP4_ADDRESS : 4,
INTERNAL_IP4_NETMASK : 4,
INTERNAL_IP4_DNS : 4,
INTERNAL_IP4_NBNS : 4,
INTERNAL_IP4_DHCP : 4,
APPLICATION_VERSION : None,
INTERNAL_IP6_ADDRESS : 16,
INTERNAL_IP6_DNS : 16,
INTERNAL_IP6_DHCP : 16,
INTERNAL_IP4_SUBNET : 8,
SUPPORTED_ATTRIBUTES : None,
INTERNAL_IP6_SUBNET : 17,
MIP6_HOME_PREFIX : 21,
INTERNAL_IP6_LINK : None,
INTERNAL_IP6_PREFIX : 17,
HOME_AGENT_ADDRESS : None, #16 or 20
P_CSCF_IP4_ADDRESS : 4,
P_CSCF_IP6_ADDRESS : 16,
FTT_KAT : 2,
EXTERNAL_SOURCE_IP4_NAT_INFO : 6,
TIMEOUT_PERIOD_FOR_LIVENESS_CHECK : 4,
INTERNAL_DNS_DOMAIN : None,
INTERNAL_DNSSEC_TA : None
}
self.errors = {
OK : 'OK',
TIMEOUT : 'TIMEOUT',
REPEAT_STATE : 'REPEAT_STATE',
DECODING_ERROR : 'DECODING_ERROR',
MANDATORY_INFORMATION_MISSING : 'MANDATORY_INFORMATION_MISSING',
OTHER_ERROR : 'OTHER_ERROR'
}
def set_timeout(self,value):
self.timeout = value
def set_udp(self):
self.socket_type = UDP
def create_socket(self,client_address):
if self.socket_type == UDP:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
else:
exit()
self.socket.bind(client_address)
self.socket.settimeout(self.timeout)
def create_socket_nat(self,client_address):
if self.socket_type == UDP:
self.socket_nat = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
else:
exit()
self.socket_nat.bind(client_address)
self.socket_nat.settimeout(self.timeout)
def create_socket_esp(self,client_address):
self.socket_esp = socket.socket(socket.AF_INET, socket.SOCK_RAW, ESP_PROTOCOL)
self.socket_esp.bind(client_address)
def set_server(self,address):
self.server_address = (address,self.port)
def set_server_nat(self,address):
self.server_address_nat = (address,self.port_nat)
def set_server_esp(self,address):
self.server_address_esp = (address,0)
def send_data(self, data):
if self.userplane_mode == ESP_PROTOCOL:
self.socket.sendto(data, self.server_address)
else:
self.socket_nat.sendto(b'\x00'*4 + data, self.server_address_nat)
def return_random_bytes(self,size):
if size == 0: return b''
if size == 4: return struct.pack('!I', random.randrange(pow(2,32)-1))
if size == 8: return struct.pack('!Q', random.randrange(pow(2,64)-1))
if size == 16: return struct.pack('!Q', random.randrange(pow(2,64)-1)) + struct.pack('!Q', random.randrange(pow(2,64)-1))
def return_random_int(self,size):
if size == 4: return random.randrange(pow(2,32)-1)
if size == 8: return random.randrange(pow(2,64)-1)
if size == 16: return random.randrange(pow(2,128)-1)
def return_flags(self,value): #works with value or tuple
if type(value) is int:
rvi = (value//8)%8
return (rvi // 4, (rvi//2)%2, rvi%2)
else: #is a tuple with (r,v,i)
return 32*value[0]+16*value[1]+8*value[2]
def dh_create_private_key_and_public_bytes(self,key_size):
prime = {
768: 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF,
1024: 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF,
1536: 0xffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca237327ffffffffffffffff,
2048: 0xffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aacaa68ffffffffffffffff,
3072: 0xffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a93ad2caffffffffffffffff,
4096: 0xffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c934063199ffffffffffffffff,
6144: 0xffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c93402849236c3fab4d27c7026c1d4dcb2602646dec9751e763dba37bdf8ff9406ad9e530ee5db382f413001aeb06a53ed9027d831179727b0865a8918da3edbebcf9b14ed44ce6cbaced4bb1bdb7f1447e6cc254b332051512bd7af426fb8f401378cd2bf5983ca01c64b92ecf032ea15d1721d03f482d7ce6e74fef6d55e702f46980c82b5a84031900b1c9e59e7c97fbec7e8f323a97a7e36cc88be0f1d45b7ff585ac54bd407b22b4154aacc8f6d7ebf48e1d814cc5ed20f8037e0a79715eef29be32806a1d58bb7c5da76f550aa3d8a1fbff0eb19ccb1a313d55cda56c9ec2ef29632387fe8d76e3c0468043e8f663f4860ee12bf2d5b0b7474d6e694f91e6dcc4024ffffffffffffffff,
8192: 0xffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c93402849236c3fab4d27c7026c1d4dcb2602646dec9751e763dba37bdf8ff9406ad9e530ee5db382f413001aeb06a53ed9027d831179727b0865a8918da3edbebcf9b14ed44ce6cbaced4bb1bdb7f1447e6cc254b332051512bd7af426fb8f401378cd2bf5983ca01c64b92ecf032ea15d1721d03f482d7ce6e74fef6d55e702f46980c82b5a84031900b1c9e59e7c97fbec7e8f323a97a7e36cc88be0f1d45b7ff585ac54bd407b22b4154aacc8f6d7ebf48e1d814cc5ed20f8037e0a79715eef29be32806a1d58bb7c5da76f550aa3d8a1fbff0eb19ccb1a313d55cda56c9ec2ef29632387fe8d76e3c0468043e8f663f4860ee12bf2d5b0b7474d6e694f91e6dbe115974a3926f12fee5e438777cb6a932df8cd8bec4d073b931ba3bc832b68d9dd300741fa7bf8afc47ed2576f6936ba424663aab639c5ae4f5683423b4742bf1c978238f16cbe39d652de3fdb8befc848ad922222e04a4037c0713eb57a81a23f0c73473fc646cea306b4bcbc8862f8385ddfa9d4b7fa2c087e879683303ed5bdd3a062b3cf5b3a278a66d2a13f83f44f82ddf310ee074ab6a364597e899a0255dc164f31cc50846851df9ab48195ded7ea1b1d510bd7ee74d73faf36bc31ecfa268359046f4eb879f924009438b481c6cd7889a002ed5ee382bc9190da6fc026e479558e4475677e9aa9e3050e2765694dfc81f56e880b96e7160c980dd98edd3dfffffffffffffffff
}
g = 2
self.pn = dh.DHParameterNumbers(prime.get(key_size),g)
parameters = self.pn.parameters()
self.dh_private_key = parameters.generate_private_key()
self.dh_public_key_bytes = self.dh_private_key.public_key().public_numbers().y.to_bytes(key_size//8,'big')
def dh_calculate_shared_key(self,peer_public_key_bytes):
peer_public_numbers = dh.DHPublicNumbers(int.from_bytes(peer_public_key_bytes, byteorder='big'), self.pn)
peer_public_key = peer_public_numbers.public_key()
self.dh_shared_key = self.dh_private_key.exchange(peer_public_key)
print('DIFFIE-HELLMAN KEY',toHex(self.dh_shared_key))
def get_identity(self):
imsi = return_imsi(self.com_port)
self.imsi = imsi
self.set_identification(IDI,ID_RFC822_ADDR,'0' + self.imsi + '@nai.epc.mnc' + self.mnc + '.mcc' + self.mcc + '.3gppnetwork.org')
def eap_keys_calculation(self,ck, ik):
identity = self.identification_initiator[1].encode('utf-8') #idi value
digest = hashes.Hash(hashes.SHA1())
digest.update(identity + ik + ck)
MK = digest.finalize()
print('MK',toHex(MK))
result = b''
xval = MK
modulus = pow(2,160)
for i in range(4):
w0 = sha1_dss(xval)
xval = ((int.from_bytes(xval,'big') + int.from_bytes(w0, 'big') + 1 ) % modulus).to_bytes(20,'big')
w1 = sha1_dss(xval)
xval = ((int.from_bytes(xval,'big') + int.from_bytes(w1, 'big') + 1 ) % modulus).to_bytes(20,'big')
result += w0 + w1
# return
return result[0:16],result[16:32],result[32:96],result[96:160],MK
def eap_keys_calculation_fast_reauth(self,counter, nonce_s):
identity = self.identification_initiator[1].encode('utf-8') #idi value
digest = hashes.Hash(hashes.SHA1())
digest.update(identity + struct.pack('!H',counter) + nonce_s + self.MK)
XKEY = digest.finalize()
print('XKEY',toHex(XKEY))
result = b''
xval = XKEY
modulus = pow(2,160)
for i in range(4):
w0 = sha1_dss(xval)
xval = ((int.from_bytes(xval,'big') + int.from_bytes(w0, 'big') + 1 ) % modulus).to_bytes(20,'big')
w1 = sha1_dss(xval)
xval = ((int.from_bytes(xval,'big') + int.from_bytes(w1, 'big') + 1 ) % modulus).to_bytes(20,'big')
result += w0 + w1
# return
return result[0:64],result[64:128],XKEY
#######################################################################################################################
#######################################################################################################################
################ D E C O D E F U N C T I O N S ################
#######################################################################################################################
#######################################################################################################################
def decode_header(self, data):
try:
#if True:
self.ike_decoded_header['initiator_spi'] = data[0:8]
self.ike_decoded_header['responder_spi'] = data[8:16]
self.ike_decoded_header['next_payload'] = data[16]
self.ike_decoded_header['major_version'] = data[17] // 16
self.ike_decoded_header['minor_version'] = data[17] % 16
self.ike_decoded_header['exchange_type'] = data[18]
self.ike_decoded_header['flags'] = self.return_flags(data[19])
self.ike_decoded_header['message_id'] = struct.unpack("!I",data[20:24])[0]
self.ike_decoded_header['length'] = struct.unpack("!I", data[24:28])[0] #header + payloads
if self.ike_spi_responder == (0).to_bytes(8,'big') and self.ike_spi_initiator == self.ike_decoded_header['initiator_spi'] :
self.ike_spi_responder = self.ike_decoded_header['responder_spi']
self.ike_decoded_header_ok = True
self.old_ike_message_received = False
return
if self.ike_spi_initiator == self.ike_decoded_header['initiator_spi'] and self.ike_spi_responder == self.ike_decoded_header['responder_spi']:
self.ike_decoded_header_ok = True
self.old_ike_message_received = False
return
if self.ike_spi_initiator_old == self.ike_decoded_header['initiator_spi'] and self.ike_spi_responder_old == self.ike_decoded_header['responder_spi']:
self.ike_decoded_header_ok = True
self.old_ike_message_received = True
return
self.ike_decoded_header_ok = False
return
except:
self.ike_decoded_header_ok = False
def decode_generic_payload_header(self,data, position, payload_type):
ike_decoded_payload_header = {}
ike_decoded_payload_header['next_payload'] = data[position]
ike_decoded_payload_header['C'] = data[position+1] // 128
ike_decoded_payload_header['length'] = struct.unpack("!H", data[position+2:position+4])[0]
ike_decoded_payload_header['data'] = data[position+4:position+ike_decoded_payload_header['length']]
#to be used for SK decryption
self.current_next_payload = ike_decoded_payload_header['next_payload']
if payload_type in self.decodable_payloads:
ike_decoded_payload_header['decoded'] = [payload_type, self.decode_payload_type(payload_type, ike_decoded_payload_header['data'])]
else:
ike_decoded_payload_header['decoded'] = [payload_type, None]
position += ike_decoded_payload_header['length']
return position, ike_decoded_payload_header['decoded'], ike_decoded_payload_header['next_payload']
def decode_payload(self, data, next_payload, position=28): #by default it uses position 28 for normal
decoded_payload = []
while position < len(data):
position, payload_decoded, next_payload = self.decode_generic_payload_header(data, position, next_payload)
decoded_payload.append(payload_decoded)
return (True, decoded_payload)
def decode_ike(self, data):
self.current_packet_received = data
try:
#if True:
self.decode_header(data)
if self.ike_decoded_header_ok == False:
self.ike_decoded_ok = False
else:
(self.decoded_payload_ok, self.decoded_payload) = self.decode_payload(data, self.ike_decoded_header['next_payload'])
if self.decoded_payload_ok == False:
self.ike_decoded_ok = False
else:
self.ike_decoded_ok = True
print('received decoded message:')
print(self.decoded_payload)
except:
self.ike_decoded_ok = False
def decode_payload_type(self, type, data):
payload_type = {
SA: self.decode_payload_type_sa ,
KE: self.decode_payload_type_ke ,
IDI: self.decode_payload_type_idi ,
IDR: self.decode_payload_type_idr ,
CERT: self.decode_payload_type_cert ,
CERTREQ: self.decode_payload_type_certreq ,
AUTH: self.decode_payload_type_auth ,
NINR: self.decode_payload_type_ninr ,
N: self.decode_payload_type_n ,
D: self.decode_payload_type_d ,
V: self.decode_payload_type_v ,
TSI: self.decode_payload_type_tsi_tsr ,
TSR: self.decode_payload_type_tsi_tsr ,
SK: self.decode_payload_type_sk ,
CP: self.decode_payload_type_cp ,
EAP: self.decode_payload_type_eap
}
func = payload_type.get(type, self.unsupported_payload_type)
return func(data)
def decode_payload_type_sa(self, data):
spi = b''
if data[5]!= 0:
spi = data[8:8+data[6]]
return [data[4],data[5],spi] # proposal number, protocol_id, spi
def decode_payload_type_ke(self, data):
return [struct.unpack("!H", data[0:2])[0], data[4:]] # diffie-hellman group, key
def decode_payload_type_idi(self, data):
return [data[0],data[4:]]
def decode_payload_type_idr(self, data):
return [data[0],data[4:]]
def decode_payload_type_cert(self, data):
return [data[0],data[1:]]
def decode_payload_type_certreq(self, data):
return [data[0],data[1:]]
def decode_payload_type_auth(self, data):
return [data[0],data[4:]]
def decode_payload_type_ninr(self, data):
return [data] # nounce_received
def decode_payload_type_n(self, data):
spi = b''
notification_data = b''
if data[1]!= 0: #spi present
spi = data[4:4+data[1]]
if len(data)>4 +data[1]: #notification data present
notification_data = data[4+data[1]:]
return [data[0],struct.unpack("!H", data[2:4])[0],spi,notification_data] # protocol_id, notify_message_type, spi, notification_data
def decode_payload_type_d(self, data):
spi = b''
spi_list = []
num_of_spi = 0
if data[1]!= 0: #spi present
num_of_spi = struct.unpack("!H", data[2:4])[0]
for i in range(num_of_spi):
spi_list.append(data[4+i*data[1]:4+(i+1)*data[1]])
return [data[0],num_of_spi, spi_list] # [protocol_id, number of spi, [spi1, spi2, ... spi n]]
def decode_payload_type_v(self, data):
return [data]
def decode_payload_type_tsi_tsr(self, data):
num_of_ts = data[0]
ts_list = []
position = 4
for i in range(num_of_ts):
ts_type = data[position]
protocol_id = data[position+1]
start_port, end_port = struct.unpack("!H", data[position+4:position+6])[0], struct.unpack("!H", data[position+6:position+8])[0]
if ts_type == TS_IPV4_ADDR_RANGE:
starting_address = socket.inet_ntop(socket.AF_INET,data[position+8:position+12])
ending_address = socket.inet_ntop(socket.AF_INET,data[position+12:position+16])
position += 16
elif ts_type == TS_IPV6_ADDR_RANGE:
starting_address = socket.inet_ntop(socket.AF_INET6,data[position+8:position+24])
ending_address = socket.inet_ntop(socket.AF_INET6,data[position+24:position+40])
position += 40
ts_list.append((ts_type,protocol_id,start_port,end_port,starting_address,ending_address))
return [num_of_ts,ts_list]
######### CIPHERED PAYLOAD ######
######### CIPHERED PAYLOAD ######
######### CIPHERED PAYLOAD ######
def decode_payload_type_sk(self, data):
if self.negotiated_encryption_algorithm in (ENCR_AES_CBC,):
vector = data[0:16]
hash_size = self.integ_key_truncated_len_bytes.get(self.negotiated_integrity_algorithm)
hash_data = data[-hash_size:]
encrypted_data = data[16:len(data)-hash_size]
if self.ike_decoded_header['flags'][2] == ROLE_RESPONDER:
if self.old_ike_message_received == True:
cipher = Cipher(algorithms.AES(self.SK_ER_old), modes.CBC(vector))
else:
cipher = Cipher(algorithms.AES(self.SK_ER), modes.CBC(vector))
else:
if self.old_ike_message_received == True:
cipher = Cipher(algorithms.AES(self.SK_EI_old), modes.CBC(vector))
else:
cipher = Cipher(algorithms.AES(self.SK_EI), modes.CBC(vector))
decryptor = cipher.decryptor()
uncipher_data = decryptor.update(encrypted_data) + decryptor.finalize()
padding_length = uncipher_data[-1]
ike_payload = uncipher_data[0:-padding_length-1]
(result_ok, decoded_payload) = self.decode_payload(ike_payload, self.current_next_payload,0)
if result_ok == True:
return decoded_payload
elif self.negotiated_encryption_algorithm in (ENCR_NULL,):
hash_size = self.integ_key_truncated_len_bytes.get(self.negotiated_integrity_algorithm)
hash_data = data[-hash_size:]
ike_payload = data[0:len(data)-hash_size]
(result_ok, decoded_payload) = self.decode_payload(ike_payload, self.current_next_payload,0)
if result_ok == True:
return decoded_payload
def decode_payload_type_cp(self, data):
cfg_type = data[0]
attribute_list = []
position=4
while position < len(data):
attribute_type = struct.unpack("!H", data[position:position+2])[0]
length = struct.unpack("!H", data[position+2:position+4])[0]
attribute_value = b''
if length > 0:
att_len = self.configuration_payload_len_bytes.get(attribute_type)
if att_len == 4: #ip
attribute_value = socket.inet_ntop(socket.AF_INET,data[position+4:position+8])
attribute_list.append((attribute_type,attribute_value))
elif att_len == 8: #ip /netmask
attribute_value_1 = socket.inet_ntop(socket.AF_INET,data[position+4:position+8])
attribute_value_2 = socket.inet_ntop(socket.AF_INET,data[position+8:position+12])
attribute_list.append((attribute_type,attribute_value_1,attribute_value_2))
elif att_len == 16: #ipv6
attribute_value = socket.inet_ntop(socket.AF_INET6,data[position+4:position+20])
attribute_list.append((attribute_type,attribute_value))
elif att_len == 17: #ipv6 + prefix
attribute_value_1 = socket.inet_ntop(socket.AF_INET6,data[position+4:position+20])
attribute_value_2 = data[position+21]
attribute_list.append((attribute_type,attribute_value_1, attribute_value_2))
else:
attribute_value = data[position+4:position+4+length]
attribute_list.append((attribute_type,attribute_value))
else:
attribute_list.append((attribute_type,attribute_value))
position += length + 4
return [cfg_type,attribute_list]
def decode_payload_type_eap(self, data):
code = data[0] #1- request, 2-response, 3-success, 4-failure
identifier = data[1]
if code in (EAP_SUCCESS,EAP_FAILURE):
return [code,identifier]
elif code in (EAP_REQUEST,EAP_RESPONSE):
if data[4] == EAP_AKA:
return [code,identifier,data[4],data[5],self.decode_eap_attributes(data[8:])] #code, identifier, type, sub type, [attributes list]
else:
return [code,identifier,data[4],data[5:]]
else:
return []
def unsupported_payload_type(self, data):