pycrate/pycrate_mobile/NAS.py

196 lines
5.3 KiB
Python
Raw Normal View History

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
# *
# * Copyright 2017. Benoit Michau. ANSSI. P1sec.
2017-08-31 21:43:44 +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
# * 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
from .TS24008_IE import *
from .TS24008_MM import *
from .TS24008_CC import *
from .TS24080_SS import *
from .TS44018_RR import *
from .TS44018_GTTP import *
2017-08-31 21:43:44 +00:00
from .TS24011_PPSMS import *
from .TS23040_SMS import *
2018-03-09 07:16:10 +00:00
from .TS23041_CBS import *
from .TS23038 import *
from .TS24008_GMM import *
from .TS24008_SM import *
2017-08-31 21:43:44 +00:00
from .TS24301_IE import *
from .TS24301_EMM import *
from .TS24301_ESM import *
from .NASLTE import *
2017-08-31 21:43:44 +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
from .TS24501_IE import FGSID
from .TS24501_FGMM import *
#from .TS24501_FGSM import *
from .NAS5G import *
2017-08-31 21:43:44 +00:00
NASMODispatcher = {
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 = {
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
}
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 Exception:
2017-08-31 21:43:44 +00:00
# error 111, unspecified protocol error
return None, 111
else:
try:
pd, type = buf[0], buf[1]
except Exception:
2017-08-31 21:43:44 +00:00
# error 111, unspecified protocol error
return None, 111
if pd & 0xf != 0xe:
# 4-bit protocol discriminator
pd &= 0xf
if pd in (3, 5, 11):
2017-08-31 21:43:44 +00:00
type &= 0x3f
elif pd in (2, 7):
return parse_NASLTE_MO(buf, inner=True)
elif pd in (46, 126):
return parse_NAS5G(buf, inner=True)
2017-08-31 21:43:44 +00:00
#
try:
Msg = NASMODispatcher[pd][type]()
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)
except Exception:
2017-08-31 21:43:44 +00:00
# error 96, invalid mandatory info
return None, 96
#
return Msg, 0
def parse_NAS_MT(buf, wl2=False):
"""Parses a Mobile Terminated NAS message bytes' buffer
Args:
buf: downlink NAS message bytes' buffer
wl2: bool, True if the signalling message is a GSM RR with a
L2PseudoLength prefix
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:
if wl2:
pd, type = unpack('>BB', buf[1:3])
else:
pd, type = unpack('>BB', buf[:2])
except Exception:
2017-08-31 21:43:44 +00:00
# error 111, unspecified protocol error
return None, 111
else:
try:
if wl2:
pd, type = buf[1], buf[2]
else:
pd, type = buf[0], buf[1]
except Exception:
2017-08-31 21:43:44 +00:00
# error 111, unspecified protocol error
return None, 111
if pd & 0xf != 0xe:
# 4-bit protocol discriminator
pd &= 0xf
if pd in (3, 5, 11):
2017-08-31 21:43:44 +00:00
type &= 0x3f
elif pd in (2, 7):
return parse_NASLTE_MT(buf, inner=True)
elif pd in (46, 126):
return parse_NAS5G(buf, inner=True)
2017-08-31 21:43:44 +00:00
#
try:
Msg = NASMTDispatcher[pd][type]()
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)
except Exception:
2017-08-31 21:43:44 +00:00
# error 96, invalid mandatory info
return None, 96
#
return Msg, 0