2017-08-31 21:43:44 +00:00
|
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
|
#/**
|
|
|
|
|
# * Software Name : pycrate
|
2019-02-25 10:26:10 +00:00
|
|
|
|
# * Version : 0.4
|
2017-08-31 21:43:44 +00:00
|
|
|
|
# *
|
2018-11-21 11:22:25 +00:00
|
|
|
|
# * Copyright 2017. Benoit Michau. ANSSI. P1sec.
|
2017-08-31 21:43:44 +00:00
|
|
|
|
# *
|
2018-04-15 19:47:21 +00:00
|
|
|
|
# * This library is free software; you can redistribute it and/or
|
|
|
|
|
# * modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
# * License as published by the Free Software Foundation; either
|
|
|
|
|
# * version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
# *
|
|
|
|
|
# * This library is distributed in the hope that it will be useful,
|
2017-08-31 21:43:44 +00:00
|
|
|
|
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2018-04-15 19:47:21 +00:00
|
|
|
|
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
# * Lesser General Public License for more details.
|
|
|
|
|
# *
|
|
|
|
|
# * You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
# * License along with this library; if not, write to the Free Software
|
|
|
|
|
# * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
|
# * MA 02110-1301 USA
|
2017-08-31 21:43:44 +00:00
|
|
|
|
# *
|
|
|
|
|
# *--------------------------------------------------------
|
|
|
|
|
# * File Name : pycrate_mobile/NAS.py
|
|
|
|
|
# * Created : 2017-07-17
|
|
|
|
|
# * Authors : Benoit Michau
|
|
|
|
|
# *--------------------------------------------------------
|
|
|
|
|
#*/
|
|
|
|
|
|
|
|
|
|
from pycrate_core.utils import *
|
|
|
|
|
if python_version < 3:
|
|
|
|
|
from struct import unpack
|
|
|
|
|
|
2018-02-09 20:58:36 +00:00
|
|
|
|
from .TS24008_IE import *
|
2017-11-12 14:18:50 +00:00
|
|
|
|
from .TS24008_MM import *
|
|
|
|
|
from .TS24008_CC import *
|
|
|
|
|
from .TS24080_SS import *
|
|
|
|
|
from .TS44018_RR import *
|
2018-11-21 11:22:25 +00:00
|
|
|
|
from .TS44018_GTTP import *
|
2017-08-31 21:43:44 +00:00
|
|
|
|
|
2018-02-09 20:58:36 +00:00
|
|
|
|
from .TS24011_PPSMS import *
|
|
|
|
|
from .TS23040_SMS import *
|
2018-03-09 07:16:10 +00:00
|
|
|
|
from .TS23041_CBS import *
|
2018-02-09 20:58:36 +00:00
|
|
|
|
from .TS23038 import *
|
|
|
|
|
|
2017-11-12 14:18:50 +00:00
|
|
|
|
from .TS24008_GMM import *
|
|
|
|
|
from .TS24008_SM import *
|
2017-08-31 21:43:44 +00:00
|
|
|
|
|
2018-02-09 20:58:36 +00:00
|
|
|
|
from .TS24301_IE import *
|
2017-11-12 14:18:50 +00:00
|
|
|
|
from .TS24301_EMM import *
|
|
|
|
|
from .TS24301_ESM import *
|
|
|
|
|
from .NASLTE import *
|
2017-08-31 21:43:44 +00:00
|
|
|
|
|
2019-12-06 10:40:33 +00:00
|
|
|
|
# WNG if trying to import 24501_IE, as some IE in 5G may have same name but not
|
|
|
|
|
# same structure as in 4G
|
2021-01-25 15:29:07 +00:00
|
|
|
|
from .TS24501_IE import FGSID
|
|
|
|
|
from .TS24501_FGMM import *
|
|
|
|
|
#from .TS24501_FGSM import *
|
2019-12-06 10:40:33 +00:00
|
|
|
|
from .NAS5G import *
|
|
|
|
|
|
|
|
|
|
|
2017-08-31 21:43:44 +00:00
|
|
|
|
NASMODispatcher = {
|
2019-12-06 10:40:33 +00:00
|
|
|
|
2 : ESMTypeClasses,
|
|
|
|
|
3 : CCTypeMOClasses,
|
|
|
|
|
4 : GTTPTypeClasses,
|
|
|
|
|
5 : MMTypeClasses,
|
|
|
|
|
6 : RRTypeMOClasses,
|
|
|
|
|
7 : EMMTypeMOClasses,
|
|
|
|
|
8 : GMMTypeMOClasses,
|
|
|
|
|
9 : PPSMSCPTypeClasses,
|
|
|
|
|
10 : SMTypeClasses,
|
|
|
|
|
11 : SSTypeMOClasses,
|
|
|
|
|
46 : FGSMTypeClasses,
|
|
|
|
|
126 : FGMMTypeClasses
|
2017-08-31 21:43:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NASMTDispatcher = {
|
2019-12-06 10:40:33 +00:00
|
|
|
|
2 : ESMTypeClasses,
|
|
|
|
|
3 : CCTypeMTClasses,
|
|
|
|
|
4 : GTTPTypeClasses,
|
|
|
|
|
5 : MMTypeClasses,
|
|
|
|
|
6 : RRTypeMTClasses,
|
|
|
|
|
7 : EMMTypeMTClasses,
|
|
|
|
|
8 : GMMTypeMTClasses,
|
|
|
|
|
9 : PPSMSCPTypeClasses,
|
|
|
|
|
10 : SMTypeClasses,
|
|
|
|
|
11 : SSTypeMTClasses,
|
|
|
|
|
46 : FGSMTypeClasses,
|
|
|
|
|
126 : FGMMTypeClasses
|
2017-08-31 21:43:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-11-27 15:10:35 +00:00
|
|
|
|
def parse_NAS_MO(buf):
|
2017-11-12 14:18:50 +00:00
|
|
|
|
"""Parses a Mobile Originated NAS message bytes' buffer
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
buf: uplink NAS message bytes' buffer
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
element, err: 2-tuple
|
|
|
|
|
element: Element instance, if err is null (no error)
|
|
|
|
|
element: None, if err is not null (standard NAS error code)
|
|
|
|
|
"""
|
2017-08-31 21:43:44 +00:00
|
|
|
|
if python_version < 3:
|
|
|
|
|
try:
|
2018-11-27 15:10:35 +00:00
|
|
|
|
pd, type = unpack('>BB', buf[:2])
|
2019-07-10 15:04:20 +00:00
|
|
|
|
except Exception:
|
2017-08-31 21:43:44 +00:00
|
|
|
|
# error 111, unspecified protocol error
|
|
|
|
|
return None, 111
|
|
|
|
|
else:
|
|
|
|
|
try:
|
2018-11-27 15:10:35 +00:00
|
|
|
|
pd, type = buf[0], buf[1]
|
2019-07-10 15:04:20 +00:00
|
|
|
|
except Exception:
|
2017-08-31 21:43:44 +00:00
|
|
|
|
# error 111, unspecified protocol error
|
|
|
|
|
return None, 111
|
2019-12-06 10:40:33 +00:00
|
|
|
|
if pd & 0xf != 0xe:
|
|
|
|
|
# 4-bit protocol discriminator
|
|
|
|
|
pd &= 0xf
|
2017-11-12 14:18:50 +00:00
|
|
|
|
if pd in (3, 5, 11):
|
2017-08-31 21:43:44 +00:00
|
|
|
|
type &= 0x3f
|
2017-11-12 14:18:50 +00:00
|
|
|
|
elif pd in (2, 7):
|
|
|
|
|
return parse_NASLTE_MO(buf, inner=True)
|
2019-12-06 10:40:33 +00:00
|
|
|
|
elif pd in (46, 126):
|
|
|
|
|
return parse_NAS5G(buf, inner=True)
|
2017-08-31 21:43:44 +00:00
|
|
|
|
#
|
|
|
|
|
try:
|
|
|
|
|
Msg = NASMODispatcher[pd][type]()
|
2019-07-10 15:04:20 +00:00
|
|
|
|
except KeyError:
|
2017-08-31 21:43:44 +00:00
|
|
|
|
# error 97, message type non-existent or not implemented
|
|
|
|
|
return None, 97
|
|
|
|
|
#
|
|
|
|
|
try:
|
|
|
|
|
Msg.from_bytes(buf)
|
2019-07-10 15:04:20 +00:00
|
|
|
|
except Exception:
|
2017-08-31 21:43:44 +00:00
|
|
|
|
# error 96, invalid mandatory info
|
|
|
|
|
return None, 96
|
|
|
|
|
#
|
|
|
|
|
return Msg, 0
|
|
|
|
|
|
|
|
|
|
|
2018-11-23 13:57:24 +00:00
|
|
|
|
def parse_NAS_MT(buf, wl2=False):
|
2017-11-12 14:18:50 +00:00
|
|
|
|
"""Parses a Mobile Terminated NAS message bytes' buffer
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
buf: downlink NAS message bytes' buffer
|
2018-11-23 13:57:24 +00:00
|
|
|
|
wl2: bool, True if the signalling message is a GSM RR with a
|
|
|
|
|
L2PseudoLength prefix
|
2017-11-12 14:18:50 +00:00
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
element, err: 2-tuple
|
|
|
|
|
element: Element instance, if err is null (no error)
|
|
|
|
|
element: None, if err is not null (standard NAS error code)
|
|
|
|
|
"""
|
2017-08-31 21:43:44 +00:00
|
|
|
|
if python_version < 3:
|
|
|
|
|
try:
|
2018-11-23 13:57:24 +00:00
|
|
|
|
if wl2:
|
|
|
|
|
pd, type = unpack('>BB', buf[1:3])
|
|
|
|
|
else:
|
|
|
|
|
pd, type = unpack('>BB', buf[:2])
|
2019-07-10 15:04:20 +00:00
|
|
|
|
except Exception:
|
2017-08-31 21:43:44 +00:00
|
|
|
|
# error 111, unspecified protocol error
|
|
|
|
|
return None, 111
|
|
|
|
|
else:
|
|
|
|
|
try:
|
2018-11-23 13:57:24 +00:00
|
|
|
|
if wl2:
|
|
|
|
|
pd, type = buf[1], buf[2]
|
|
|
|
|
else:
|
|
|
|
|
pd, type = buf[0], buf[1]
|
2019-07-10 15:04:20 +00:00
|
|
|
|
except Exception:
|
2017-08-31 21:43:44 +00:00
|
|
|
|
# error 111, unspecified protocol error
|
|
|
|
|
return None, 111
|
2019-12-06 10:40:33 +00:00
|
|
|
|
if pd & 0xf != 0xe:
|
|
|
|
|
# 4-bit protocol discriminator
|
|
|
|
|
pd &= 0xf
|
2017-11-12 14:18:50 +00:00
|
|
|
|
if pd in (3, 5, 11):
|
2017-08-31 21:43:44 +00:00
|
|
|
|
type &= 0x3f
|
2017-11-12 14:18:50 +00:00
|
|
|
|
elif pd in (2, 7):
|
|
|
|
|
return parse_NASLTE_MT(buf, inner=True)
|
2019-12-06 10:40:33 +00:00
|
|
|
|
elif pd in (46, 126):
|
|
|
|
|
return parse_NAS5G(buf, inner=True)
|
2017-08-31 21:43:44 +00:00
|
|
|
|
#
|
|
|
|
|
try:
|
|
|
|
|
Msg = NASMTDispatcher[pd][type]()
|
2019-07-10 15:04:20 +00:00
|
|
|
|
except KeyError:
|
2017-08-31 21:43:44 +00:00
|
|
|
|
# error 97, message type non-existent or not implemented
|
|
|
|
|
return None, 97
|
|
|
|
|
#
|
|
|
|
|
try:
|
|
|
|
|
Msg.from_bytes(buf)
|
2019-07-10 15:04:20 +00:00
|
|
|
|
except Exception:
|
2017-08-31 21:43:44 +00:00
|
|
|
|
# error 96, invalid mandatory info
|
|
|
|
|
return None, 96
|
|
|
|
|
#
|
|
|
|
|
return Msg, 0
|
2019-12-06 10:40:33 +00:00
|
|
|
|
|