2017-08-31 21:43:44 +00:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
#/**
|
|
|
|
# * Software Name : pycrate
|
2018-02-09 21:23:26 +00:00
|
|
|
# * Version : 0.3
|
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
|
|
|
|
|
|
|
NASMODispatcher = {
|
2017-11-12 14:18:50 +00:00
|
|
|
2 : ESMTypeClasses,
|
2017-10-22 12:03:18 +00:00
|
|
|
3 : CCTypeMOClasses,
|
2018-11-21 11:22:25 +00:00
|
|
|
4 : GTTPTypeClasses,
|
2017-08-31 21:43:44 +00:00
|
|
|
5 : MMTypeClasses,
|
2018-11-21 11:22:25 +00:00
|
|
|
6 : RRTypeMOClasses,
|
2017-11-12 14:18:50 +00:00
|
|
|
7 : EMMTypeMOClasses,
|
2017-10-22 12:03:18 +00:00
|
|
|
8 : GMMTypeMOClasses,
|
2017-11-12 14:18:50 +00:00
|
|
|
9 : PPSMSCPTypeClasses,
|
|
|
|
10: SMTypeClasses,
|
|
|
|
11: SSTypeMOClasses
|
2017-08-31 21:43:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NASMTDispatcher = {
|
2017-11-12 14:18:50 +00:00
|
|
|
2 : ESMTypeClasses,
|
2017-10-22 12:03:18 +00:00
|
|
|
3 : CCTypeMTClasses,
|
2018-11-21 11:22:25 +00:00
|
|
|
4 : GTTPTypeClasses,
|
2017-08-31 21:43:44 +00:00
|
|
|
5 : MMTypeClasses,
|
2018-11-21 11:22:25 +00:00
|
|
|
6 : RRTypeMTClasses,
|
2017-11-12 14:18:50 +00:00
|
|
|
7 : EMMTypeMTClasses,
|
2017-10-22 12:03:18 +00:00
|
|
|
8 : GMMTypeMTClasses,
|
2017-11-12 14:18:50 +00:00
|
|
|
9 : PPSMSCPTypeClasses,
|
|
|
|
10: SMTypeClasses,
|
|
|
|
11: SSTypeMTClasses
|
2017-08-31 21:43:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-12 14:18:50 +00:00
|
|
|
def parse_NAS_MO(buf):
|
|
|
|
"""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:
|
|
|
|
pd, type = unpack('>BB', buf[:2])
|
|
|
|
except:
|
|
|
|
# error 111, unspecified protocol error
|
|
|
|
return None, 111
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
pd, type = buf[0], buf[1]
|
|
|
|
except:
|
|
|
|
# error 111, unspecified protocol error
|
|
|
|
return None, 111
|
|
|
|
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)
|
2017-08-31 21:43:44 +00:00
|
|
|
#
|
|
|
|
try:
|
|
|
|
Msg = NASMODispatcher[pd][type]()
|
|
|
|
except:
|
|
|
|
# error 97, message type non-existent or not implemented
|
|
|
|
return None, 97
|
|
|
|
#
|
|
|
|
try:
|
|
|
|
Msg.from_bytes(buf)
|
|
|
|
except:
|
|
|
|
# error 96, invalid mandatory info
|
|
|
|
return None, 96
|
|
|
|
#
|
|
|
|
return Msg, 0
|
|
|
|
|
|
|
|
|
2017-11-12 14:18:50 +00:00
|
|
|
def parse_NAS_MT(buf):
|
|
|
|
"""Parses a Mobile Terminated NAS message bytes' buffer
|
|
|
|
|
|
|
|
Args:
|
|
|
|
buf: downlink 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:
|
|
|
|
pd, type = unpack('>BB', buf[:2])
|
|
|
|
except:
|
|
|
|
# error 111, unspecified protocol error
|
|
|
|
return None, 111
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
pd, type = buf[0], buf[1]
|
|
|
|
except:
|
|
|
|
# error 111, unspecified protocol error
|
|
|
|
return None, 111
|
|
|
|
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)
|
2017-08-31 21:43:44 +00:00
|
|
|
#
|
|
|
|
try:
|
|
|
|
Msg = NASMTDispatcher[pd][type]()
|
|
|
|
except:
|
|
|
|
# error 97, message type non-existent or not implemented
|
|
|
|
return None, 97
|
|
|
|
#
|
|
|
|
try:
|
|
|
|
Msg.from_bytes(buf)
|
|
|
|
except:
|
|
|
|
# error 96, invalid mandatory info
|
|
|
|
return None, 96
|
|
|
|
#
|
|
|
|
return Msg, 0
|