2017-02-23 13:47:38 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
Commandline interface for sysmoUSIM-SJS1
|
|
|
|
|
|
|
|
(C) 2017 by Sysmocom s.f.m.c. GmbH
|
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
Author: Philipp Maier
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys, getopt
|
2017-08-16 13:18:09 +00:00
|
|
|
from utils import *
|
2017-02-23 13:47:38 +00:00
|
|
|
from simcard import *
|
|
|
|
from sysmo_usimsjs1 import *
|
|
|
|
|
|
|
|
|
|
|
|
def banner():
|
|
|
|
print "sysmoUSIM-SJS1 parameterization tool"
|
|
|
|
print "Copyright (c)2017 Sysmocom s.f.m.c. GmbH"
|
|
|
|
print ""
|
|
|
|
|
|
|
|
|
|
|
|
def helptext():
|
|
|
|
print " * Commandline options:"
|
|
|
|
print " -a, --adm1 CHV ................. Administrator PIN (e.g 55538407)"
|
|
|
|
print " -u, --usim ..................... Enable USIM mode"
|
|
|
|
print " -c, --classic .................. Disable USIM mode (make classic-sim)"
|
|
|
|
print " -m, --mode ..................... Display mode (classic-sim or USIM?)"
|
|
|
|
print " -t, --auth ..................... Show Authentication algorithms"
|
2017-12-14 04:25:28 +00:00
|
|
|
print " -T, --set-auth list ............ List available algorithms"
|
2018-07-04 11:38:48 +00:00
|
|
|
print " -T, --set-auth 2G:3G ........... Set 2G/3G Auth algo (e.g. COMP128v1:COMP128v1)"
|
2017-02-23 13:47:38 +00:00
|
|
|
print " -l, --milenage ................. Show milenage parameters"
|
|
|
|
print " -L, --set-milenage HEXSTRING ... Set milenage parameters"
|
|
|
|
print " -o, --opc ...................... Show OP/c configuration"
|
|
|
|
print " -O, --set-op HEXSTRING ......... Set OP value"
|
|
|
|
print " -C, --set-opc HEXSTRING ........ Set OPc value"
|
2017-03-05 00:00:13 +00:00
|
|
|
print " -k, --ki ....................... Show KI value"
|
|
|
|
print " -K, --set-ki ................... Set KI value"
|
2017-08-16 15:22:00 +00:00
|
|
|
print " -s --seq-parameters ........... Show MILENAGE SEQ/SQN parameters"
|
2017-08-17 12:36:19 +00:00
|
|
|
print " -S --reset-seq-parameters...... Reset MILENAGE SEQ/SQN parameters to default"
|
2018-08-07 14:20:49 +00:00
|
|
|
print " -I, --set-iccid ................ Set ICCID value"
|
2018-08-08 08:49:26 +00:00
|
|
|
print " -J, --set-imsi ................. Set IMSI value"
|
2019-03-21 16:05:42 +00:00
|
|
|
print " -n, --mnclen ................... Show MNC length value"
|
|
|
|
print " -N, --set-mnclen ............... Set MNC length value"
|
2017-02-23 13:47:38 +00:00
|
|
|
print ""
|
|
|
|
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
|
|
|
|
banner()
|
|
|
|
getopt_adm1 = None
|
|
|
|
getopt_write_sim_mode = None # True = USIM, False = classic SIM
|
|
|
|
getopt_show_sim_mode = False
|
|
|
|
getopt_show_auth = False
|
|
|
|
getopt_write_auth = None
|
|
|
|
getopt_show_milenage = False
|
|
|
|
getopt_write_milenage = None
|
|
|
|
getopt_show_opc = False
|
|
|
|
getopt_write_op = None
|
|
|
|
getopt_write_opc = None
|
2017-03-05 00:00:13 +00:00
|
|
|
getopt_show_ki = None
|
|
|
|
getopt_write_ki = None
|
2017-03-17 15:43:35 +00:00
|
|
|
getopt_force = False
|
2017-03-20 23:30:34 +00:00
|
|
|
getopt_write_iccid = None
|
2017-08-16 15:22:00 +00:00
|
|
|
getopt_seq_par = False
|
2017-08-17 12:36:19 +00:00
|
|
|
getopt_reset_seq_par = False
|
2018-08-08 08:49:26 +00:00
|
|
|
getopt_write_imsi = None
|
2019-03-21 16:05:42 +00:00
|
|
|
getopt_show_mnclen = None
|
|
|
|
getopt_write_mnclen = None
|
2017-02-23 13:47:38 +00:00
|
|
|
|
|
|
|
# Analyze commandline options
|
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(argv,
|
2019-03-21 16:05:42 +00:00
|
|
|
"ha:ucmtT:lL:oO:C:kK:fiI:sSJ:nN:",
|
2018-07-04 10:11:29 +00:00
|
|
|
["help","adm1=","usim","classic",
|
2017-02-23 13:47:38 +00:00
|
|
|
"mode","auth","set-auth=","milenage",
|
2017-03-05 00:00:13 +00:00
|
|
|
"set-milenage","opc","set-op=","set-opc=",
|
2017-08-16 15:22:00 +00:00
|
|
|
"ki","set-ki=","force","iccid","set-iccid=",
|
2018-08-08 08:49:26 +00:00
|
|
|
"seq-parameters", "reset-seq-parameters",
|
2019-03-21 16:05:42 +00:00
|
|
|
"set-imsi", "set-mnclen", "mnclen"])
|
2017-02-23 13:47:38 +00:00
|
|
|
except getopt.GetoptError:
|
|
|
|
print " * Error: Invalid commandline options"
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
for opt, arg in opts:
|
|
|
|
if opt in ("-h", "--help"):
|
|
|
|
helptext()
|
|
|
|
sys.exit()
|
|
|
|
elif opt in ("-a", "--adm1"):
|
|
|
|
getopt_adm1 = ascii_to_list(arg)
|
|
|
|
elif opt in ("-u", "--usim"):
|
|
|
|
getopt_write_sim_mode = True
|
|
|
|
elif opt in ("-c", "--classic"):
|
|
|
|
getopt_write_sim_mode = False
|
|
|
|
elif opt in ("-m", "--mode"):
|
|
|
|
getopt_show_sim_mode = True
|
|
|
|
elif opt in ("-t", "--auth"):
|
|
|
|
getopt_show_auth = True
|
|
|
|
elif opt in ("-T", "--set-auth"):
|
2017-12-14 04:25:28 +00:00
|
|
|
if arg.upper() == 'LIST':
|
|
|
|
print 'Valid -T arguments are algorithm number or string.'
|
|
|
|
print 'Available:'
|
|
|
|
print '\n'.join([' %d %s' % entry for entry in sysmo_usim_algorithms])
|
|
|
|
sys.exit()
|
2017-02-23 13:47:38 +00:00
|
|
|
getopt_write_auth = arg.split(':',1)
|
|
|
|
elif opt in ("-l", "--milenage"):
|
|
|
|
getopt_show_milenage = True
|
|
|
|
elif opt in ("-L", "--set-milenage"):
|
|
|
|
getopt_write_milenage = asciihex_to_list(arg)
|
|
|
|
elif opt in ("-o", "--opc"):
|
|
|
|
getopt_show_opc = True
|
|
|
|
elif opt in ("-O", "--set-op"):
|
|
|
|
getopt_write_op = asciihex_to_list(arg)
|
|
|
|
elif opt in ("-C", "--set-opc"):
|
|
|
|
getopt_write_opc = asciihex_to_list(arg)
|
2017-03-05 00:00:13 +00:00
|
|
|
elif opt in ("-k", "--ki"):
|
|
|
|
getopt_show_ki = True
|
|
|
|
elif opt in ("-K", "--set-ki"):
|
|
|
|
getopt_write_ki = asciihex_to_list(arg)
|
2017-03-17 15:43:35 +00:00
|
|
|
elif opt in ("-f", "--force"):
|
|
|
|
getopt_force = True
|
2017-03-20 23:30:34 +00:00
|
|
|
elif opt in ("-I", "--set-iccid"):
|
|
|
|
getopt_write_iccid = asciihex_to_list(pad_asciihex(arg))
|
2017-08-16 15:22:00 +00:00
|
|
|
elif opt in ("-s", "--sqe-parameters"):
|
|
|
|
getopt_seq_par = True
|
2017-08-17 12:36:19 +00:00
|
|
|
elif opt in ("-S", "--reset-sqe-parameters"):
|
|
|
|
getopt_reset_seq_par = True
|
2018-08-08 08:49:26 +00:00
|
|
|
elif opt in ("-J", "--set-imsi"):
|
|
|
|
getopt_write_imsi = asciihex_to_list(pad_asciihex(arg, True, '9'))
|
2019-03-21 16:05:42 +00:00
|
|
|
elif opt in ("-n", "--mnclen"):
|
|
|
|
getopt_show_mnclen = True
|
|
|
|
elif opt in ("-N", "--set-mnclen"):
|
|
|
|
getopt_write_mnclen = asciihex_to_list(arg)
|
2017-02-23 13:47:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
if not getopt_adm1:
|
|
|
|
print " * Error: adm1 parameter missing -- exiting..."
|
|
|
|
print ""
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
# Claim terminal
|
2017-02-23 14:09:03 +00:00
|
|
|
print "Initializing smartcard terminal..."
|
2018-07-04 13:10:23 +00:00
|
|
|
sim = Simcard(GSM_USIM, toBytes("3B 9F 96 80 1F C7 80 31 A0 73 BE 21 13 67 43 20 07 18 00 00 01 A5"))
|
2017-02-23 13:47:38 +00:00
|
|
|
print("")
|
|
|
|
|
2017-08-16 14:23:12 +00:00
|
|
|
print "Detected Card ICCID: ", sim.card.get_ICCID()
|
|
|
|
print ""
|
|
|
|
|
2017-03-17 15:43:35 +00:00
|
|
|
# Authenticate
|
|
|
|
print "Authenticating..."
|
|
|
|
if sysmo_usim_admin_auth(sim, getopt_adm1, getopt_force) == False:
|
|
|
|
print ""
|
|
|
|
print " === Authentication problem! The Card will permanently ==="
|
|
|
|
print " === lock down after 3 failed attemts! Double check ADM1! ==="
|
|
|
|
print ""
|
|
|
|
exit(1)
|
|
|
|
print("")
|
2017-02-23 13:47:38 +00:00
|
|
|
|
2017-08-16 14:23:12 +00:00
|
|
|
sim.card.SELECT_ADF_USIM()
|
|
|
|
print "Detected Card IMSI: ", sim.card.get_imsi()
|
|
|
|
print ""
|
|
|
|
|
2017-02-23 13:47:38 +00:00
|
|
|
# Execute tasks
|
|
|
|
if getopt_write_sim_mode != None:
|
|
|
|
print "Programming SIM-Mode..."
|
2017-08-16 08:04:39 +00:00
|
|
|
sysmo_usim_write_sim_mode(sim, getopt_write_sim_mode)
|
2017-02-23 13:47:38 +00:00
|
|
|
print("")
|
|
|
|
|
|
|
|
if getopt_show_sim_mode:
|
|
|
|
print "Reading SIM-Mode..."
|
2017-08-16 08:04:39 +00:00
|
|
|
sysmo_usim_show_sim_mode(sim)
|
2017-02-23 13:47:38 +00:00
|
|
|
print("")
|
|
|
|
|
|
|
|
if getopt_write_auth:
|
|
|
|
print "Programming Authentication parameters..."
|
2017-08-16 20:38:38 +00:00
|
|
|
sysmo_usim_write_auth_params(sim, getopt_write_auth[0], getopt_write_auth[1])
|
2017-02-23 13:47:38 +00:00
|
|
|
print("")
|
|
|
|
|
|
|
|
if getopt_show_auth:
|
|
|
|
print "Reading Authentication parameters..."
|
2017-08-16 08:04:39 +00:00
|
|
|
sysmo_usim_show_auth_params(sim)
|
2017-02-23 13:47:38 +00:00
|
|
|
print("")
|
|
|
|
|
|
|
|
if getopt_write_milenage:
|
|
|
|
print "Programming Milenage parameters..."
|
|
|
|
ef_mlngc = SYSMO_USIMSJS1_FILE_EF_MLNGC(getopt_write_milenage)
|
2017-08-16 08:04:39 +00:00
|
|
|
sysmo_usim_write_milenage_params(sim, ef_mlngc)
|
2017-02-23 13:47:38 +00:00
|
|
|
print("")
|
|
|
|
|
|
|
|
if getopt_show_milenage:
|
|
|
|
print "Reading Milenage parameters..."
|
2017-08-16 08:04:39 +00:00
|
|
|
sysmo_usim_show_milenage_params(sim)
|
2017-02-23 13:47:38 +00:00
|
|
|
print("")
|
|
|
|
|
2017-08-16 15:22:00 +00:00
|
|
|
if getopt_seq_par:
|
|
|
|
print "Reading Milenage Sequence parameters..."
|
|
|
|
sysmo_usim_read_milenage_sqn_params(sim)
|
|
|
|
|
2017-02-23 13:47:38 +00:00
|
|
|
if getopt_write_op:
|
|
|
|
print "Writing OP value..."
|
2017-08-16 08:04:39 +00:00
|
|
|
sysmo_usim_write_opc_params(sim, 0, getopt_write_op)
|
2017-02-23 13:47:38 +00:00
|
|
|
print("")
|
|
|
|
|
|
|
|
if getopt_write_opc:
|
2017-03-05 00:01:03 +00:00
|
|
|
print "Writing OPC value..."
|
2017-08-16 08:04:39 +00:00
|
|
|
sysmo_usim_write_opc_params(sim, 1, getopt_write_opc)
|
2017-02-23 13:47:38 +00:00
|
|
|
print("")
|
|
|
|
|
|
|
|
if getopt_show_opc:
|
2017-03-05 00:01:03 +00:00
|
|
|
print "Reading OP/C value..."
|
2017-08-16 08:04:39 +00:00
|
|
|
sysmo_usim_show_opc_params(sim)
|
2017-02-23 13:47:38 +00:00
|
|
|
print("")
|
|
|
|
|
2017-03-05 00:00:13 +00:00
|
|
|
if getopt_write_ki:
|
|
|
|
print "Writing KI value..."
|
2017-08-16 08:04:39 +00:00
|
|
|
sysmo_usim_write_ki_params(sim, getopt_write_ki)
|
2017-03-05 00:00:13 +00:00
|
|
|
print("")
|
|
|
|
|
|
|
|
if getopt_show_ki:
|
|
|
|
print "Reading KI value..."
|
2017-08-16 08:04:39 +00:00
|
|
|
sysmo_usim_show_ki_params(sim)
|
2017-03-05 00:00:13 +00:00
|
|
|
print("")
|
|
|
|
|
2017-03-20 23:30:34 +00:00
|
|
|
if getopt_write_iccid:
|
|
|
|
print "Writing ICCID value..."
|
2017-08-16 08:04:39 +00:00
|
|
|
sysmo_usim_write_iccid(sim, getopt_write_iccid)
|
2017-03-20 23:30:34 +00:00
|
|
|
print("")
|
|
|
|
|
2017-08-17 12:36:19 +00:00
|
|
|
if getopt_reset_seq_par:
|
|
|
|
print "Resetting MILENAGE Sequence Parameters..."
|
|
|
|
sysmo_usim_reset_milenage_sqn_params(sim)
|
|
|
|
print("")
|
|
|
|
|
2018-08-08 08:49:26 +00:00
|
|
|
if getopt_write_imsi:
|
|
|
|
print "Writing IMSI value..."
|
|
|
|
sysmo_usim_write_imsi(sim, getopt_write_imsi)
|
|
|
|
print("")
|
2017-08-17 12:36:19 +00:00
|
|
|
|
2019-03-21 16:05:42 +00:00
|
|
|
if getopt_show_mnclen:
|
|
|
|
print "Reading MNCLEN value..."
|
|
|
|
sysmo_usim_show_mnclen(sim)
|
|
|
|
print("")
|
|
|
|
|
|
|
|
if getopt_write_mnclen:
|
|
|
|
print "Writing MNCLEN value..."
|
|
|
|
sysmo_usim_write_mnclen(sim, getopt_write_mnclen)
|
|
|
|
print("")
|
|
|
|
|
2017-02-23 13:47:38 +00:00
|
|
|
print "Done!"
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv[1:])
|
|
|
|
|
|
|
|
|
|
|
|
|