Project

General

Profile

Actions

Bug #5575

open

PySim Shell - Change OPC value

Added by Royman 28 days ago. Updated 6 days ago.

Status:
In Progress
Priority:
High
Assignee:
Target version:
-
Start date:
06/01/2022
Due date:
% Done:

30%

Spec Reference:

Description

Dear PySim Team,

either my Syntax is wrong, or the Json Path function is not working correctly for changing the K, OP and OPC values in the USIM_AUTH_KEY folder. If possible, please give me a hint what the correct syntax would look like, I tried multiple syntax' that only produced exception errors.

I tried the JSON Path example from the readme.MD (target: OFM), that is working just fine for me, but it is a boolean value and therefore the syntax is not 100% applicable to the OPC which looks like a string value.

I'm looking forward to hearing from you.

Kind regards,
Christoph Reimann

Card used: SysmocomSJA2
Card Reader: Omnikey CardMan 3121 USB CCID interface
OS: Debian 64 bit

Commands:

user@debian:~/pysim$ ./pySim-shell.py -p0
Using PC/SC reader interface
Waiting for card...
Autodetected card type: sysmoISIM-SJA2
Info: Card is of type: UICC-SIM
AIDs on card:
USIM: a0000000871002ffffffff8907090000 (EF.DIR)
ISIM: a0000000871004ffffffff8907090000 (EF.DIR)
ARA-M: a00000015141434c00
ADF.ISD: a000000003000000
Detected CardModel: SysmocomSJA2
Welcome to pySim-shell!
pySIM-shell (MF)> verify_adm 72523357
pySIM-shell (MF)> select ADF.USIM 
{
"file_descriptor": {
"file_descriptor_byte": {
"shareable": true,
"file_type": "df",
"structure": "no_info_given" 
},
"record_len": null,
"num_of_rec": null
},
"df_name": "a0000000871002ffffffff8907090000",
"proprietary_information": {
"uicc_characteristics": "71",
"available_memory": 96936
},
"life_cycle_status_integer": "operational_activated",
"security_attrib_compact": "00",
"pin_status_template_do": {
"ps_do": "70",
"key_reference": 11
}
}
pySIM-shell (MF/ADF.USIM)> select EF.USIM_AUTH_KEY
{
"file_descriptor": {
"file_descriptor_byte": {
"shareable": true,
"file_type": "working_ef",
"structure": "transparent" 
},
"record_len": null,
"num_of_rec": null
},
"file_identifier": "af20",
"proprietary_information": {
"unknown_ber_tlv_ie_d0": {
"raw": "30" 
}
},
"life_cycle_status_integer": "operational_activated",
"security_attrib_compact": "031a1a",
"file_size": 33,
"short_file_identifier": null
}
pySIM-shell (MF/ADF.USIM/EF.USIM_AUTH_KEY)> read_binary_decoded 
{
"cfg": {
"only_4bytes_res_in_3g": 0,
"use_sres_deriv_func_2_in_3g": 0,
"use_opc_instead_of_op": 1,
"algorithm": "milenage" 
},
"key": "228b74faa1d93a938f2e44789bc4f3aa",
"op": null,
"opc": "55e6b31c4e1ffae3b433736392c1dd83" 
}
pySIM-shell (MF/ADF.USIM/EF.USIM_AUTH_KEY)> update_binary_decoded --json-path opc "55e6b31c4e1ffae3b433736392c1dd83" 
EXCEPTION of type 'JSONDecodeError' occurred with message: 'Extra data: line 1 column 5 (char 4)'
To enable full traceback, run the following command: 'set debug true'
pySIM-shell (MF/ADF.USIM/EF.USIM_AUTH_KEY)> update_binary_decoded --json-path opc 55e6b31c4e1ffae3b433736392c1dd83
EXCEPTION of type 'JSONDecodeError' occurred with message: 'Extra data: line 1 column 5 (char 4)'
To enable full traceback, run the following command: 'set debug true'
pySIM-shell (MF/ADF.USIM/EF.USIM_AUTH_KEY)> update_binary_decoded --json-path "opc": "55e6b31c4e1ffae3b433736392c1dd83" 
usage: update_binary_decoded [-h] [--json-path JSON_PATH] data
update_binary_decoded: error: unrecognized arguments: 55e6b31c4e1ffae3b433736392c1dd83
pySIM-shell (MF/ADF.USIM/EF.USIM_AUTH_KEY)> update_binary_decoded '{"opc": "55e6b31c4e1ffae3b433736392c1dd83"}'
EXCEPTION of type 'KeyError' occurred with message: ''cfg''
To enable full traceback, run the following command: 'set debug true'
pySIM-shell (MF/ADF.USIM/EF.USIM_AUTH_KEY)> 


Files

Actions #1

Updated by laforge 21 days ago

  • Assignee set to dexter
  • Priority changed from Normal to High
Actions #2

Updated by dexter 15 days ago

I would have expected that putting '' around the parameter would help. At least with EF.IMSI I get a similar behavior:

pySIM-shell (MF/ADF.USIM/EF.IMSI)> update_binary_decoded --json-path imsi "001010000000102" 
Traceback (most recent call last):
  File "/home/owner/.local/lib/python3.7/site-packages/cmd2/cmd2.py", line 2129, in onecmd_plus_hooks
    stop = self.onecmd(statement, add_to_history=add_to_history)
  File "/home/owner/.local/lib/python3.7/site-packages/cmd2/cmd2.py", line 2559, in onecmd
    stop = func(statement)
  File "/home/owner/.local/lib/python3.7/site-packages/cmd2/decorators.py", line 336, in cmd_wrapper
    return func(*args_list, **kwargs)
  File "/home/owner/work/git_master/pysim/pySim/filesystem.py", line 587, in do_update_binary_decoded
    json.loads(opts.data))
  File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.7/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 2 (char 1)
EXCEPTION of type 'JSONDecodeError' occurred with message: 'Extra data: line 1 column 2 (char 1)'
pySIM-shell (MF/ADF.USIM/EF.IMSI)> update_binary_decoded --json-path imsi '"001010000000102"'
pySIM-shell (MF/ADF.USIM/EF.IMSI)> 

When I try the same with EF.USIM_AUTH_KEY I get the following:

pySIM-shell (MF/ADF.USIM/EF.USIM_AUTH_KEY)> update_binary_decoded '{"cfg": { "only_4bytes_res_in_3g": 0, "use_sres_deriv_func_2_in_3g": 0, "use_opc_instead_of_op": 1, "algorithm": "milenage" }, "key": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "op": null, "opc": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" }'
Traceback (most recent call last):
  File "/home/owner/.local/lib/python3.7/site-packages/cmd2/cmd2.py", line 2129, in onecmd_plus_hooks
    stop = self.onecmd(statement, add_to_history=add_to_history)
  File "/home/owner/.local/lib/python3.7/site-packages/cmd2/cmd2.py", line 2559, in onecmd
    stop = func(statement)
  File "/home/owner/.local/lib/python3.7/site-packages/cmd2/decorators.py", line 336, in cmd_wrapper
    return func(*args_list, **kwargs)
  File "/home/owner/work/git_master/pysim/pySim/filesystem.py", line 590, in do_update_binary_decoded
    (data, sw) = self._cmd.rs.update_binary_dec(data_json)
  File "/home/owner/work/git_master/pysim/pySim/filesystem.py", line 1559, in update_binary_dec
    data_hex = self.selected_file.encode_hex(data)
  File "/home/owner/work/git_master/pysim/pySim/filesystem.py", line 730, in encode_hex
    return b2h(self._construct.build(abstract_data))
  File "/home/owner/.local/lib/python3.7/site-packages/construct/core.py", line 336, in build
    self.build_stream(obj, stream, **contextkw)
  File "/home/owner/.local/lib/python3.7/site-packages/construct/core.py", line 348, in build_stream
    self._build(obj, stream, context, "(building)")
  File "/home/owner/.local/lib/python3.7/site-packages/construct/core.py", line 2144, in _build
    buildret = sc._build(subobj, stream, context, path)
  File "/home/owner/.local/lib/python3.7/site-packages/construct/core.py", line 2657, in _build
    return self.subcon._build(obj, stream, context, path)
  File "/home/owner/.local/lib/python3.7/site-packages/construct/core.py", line 5111, in _build
    buildret = self.subcon._build(obj, stream2, context, path)
  File "/home/owner/.local/lib/python3.7/site-packages/construct/core.py", line 2139, in _build
    subobj = obj[sc.name] # raises KeyError
KeyError: None
EXCEPTION of type 'KeyError' occurred with message: 'None'
pySIM-shell (MF/ADF.USIM/EF.USIM_AUTH_KEY)> update_binary_decoded --json-path opc "55e6b31c4e1ffae3b433736392c1dd83" 
Traceback (most recent call last):
  File "/home/owner/.local/lib/python3.7/site-packages/cmd2/cmd2.py", line 2129, in onecmd_plus_hooks
    stop = self.onecmd(statement, add_to_history=add_to_history)
  File "/home/owner/.local/lib/python3.7/site-packages/cmd2/cmd2.py", line 2559, in onecmd
    stop = func(statement)
  File "/home/owner/.local/lib/python3.7/site-packages/cmd2/decorators.py", line 336, in cmd_wrapper
    return func(*args_list, **kwargs)
  File "/home/owner/work/git_master/pysim/pySim/filesystem.py", line 587, in do_update_binary_decoded
    json.loads(opts.data))
  File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.7/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 5 (char 4)
EXCEPTION of type 'JSONDecodeError' occurred with message: 'Extra data: line 1 column 5 (char 4)'
pySIM-shell (MF/ADF.USIM/EF.USIM_AUTH_KEY)> update_binary_decoded --json-path opc '"55e6b31c4e1ffae3b433736392c1dd83"' 
Traceback (most recent call last):
  File "/home/owner/.local/lib/python3.7/site-packages/cmd2/cmd2.py", line 2129, in onecmd_plus_hooks
    stop = self.onecmd(statement, add_to_history=add_to_history)
  File "/home/owner/.local/lib/python3.7/site-packages/cmd2/cmd2.py", line 2559, in onecmd
    stop = func(statement)
  File "/home/owner/.local/lib/python3.7/site-packages/cmd2/decorators.py", line 336, in cmd_wrapper
    return func(*args_list, **kwargs)
  File "/home/owner/work/git_master/pysim/pySim/filesystem.py", line 590, in do_update_binary_decoded
    (data, sw) = self._cmd.rs.update_binary_dec(data_json)
  File "/home/owner/work/git_master/pysim/pySim/filesystem.py", line 1559, in update_binary_dec
    data_hex = self.selected_file.encode_hex(data)
  File "/home/owner/work/git_master/pysim/pySim/filesystem.py", line 730, in encode_hex
    return b2h(self._construct.build(abstract_data))
  File "/home/owner/.local/lib/python3.7/site-packages/construct/core.py", line 336, in build
    self.build_stream(obj, stream, **contextkw)
  File "/home/owner/.local/lib/python3.7/site-packages/construct/core.py", line 348, in build_stream
    self._build(obj, stream, context, "(building)")
  File "/home/owner/.local/lib/python3.7/site-packages/construct/core.py", line 2144, in _build
    buildret = sc._build(subobj, stream, context, path)
  File "/home/owner/.local/lib/python3.7/site-packages/construct/core.py", line 2657, in _build
    return self.subcon._build(obj, stream, context, path)
  File "/home/owner/.local/lib/python3.7/site-packages/construct/core.py", line 5111, in _build
    buildret = self.subcon._build(obj, stream2, context, path)
  File "/home/owner/.local/lib/python3.7/site-packages/construct/core.py", line 2139, in _build
    subobj = obj[sc.name] # raises KeyError
KeyError: None
EXCEPTION of type 'KeyError' occurred with message: 'None'
pySIM-shell (MF/ADF.USIM/EF.USIM_AUTH_KEY)> 

Apparantly there is a problem somewhere on a lower level. At the moment I don't know what this could be.

Actions #3

Updated by laforge 6 days ago

  • Description updated (diff)
  • Assignee changed from dexter to laforge
Actions #4

Updated by laforge 6 days ago

So the general "problem" seems to be that we are expecting the data part (the argument like "55e6b31c4e1ffae3b433736392c1dd83" in the example) to be something that can be parsed as JSON.

The general approach is:
  • take the input JSON (decoded file data)
  • use the JSON path to navigate to a element/object in the JSON document
  • replace the element/object with the data/argument provided by the user

This makes sense, as it means the user can specify more complex JSON objects.

Surprisingly, this was not caught so far as it seems to work with substituting integer parameters, like in the below example to disable the SQN check:

pySIM-shell (MF/ADF.USIM/EF.USIM_AUTH_KEY)> select ADF.USIM/EF.USIM_SQN
pySIM-shell (MF/ADF.USIM/EF.USIM_SQN)> update_binary_decoded --json-path $.flag1.sqn_check 0
pySIM-shell (MF/ADF.USIM/EF.USIM_SQN)> 
"4503000200000000000200000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" 
pySIM-shell (MF/ADF.USIM/EF.USIM_SQN)> read_binary_decoded 
{
    "flag1": {
        "skip_next_sqn_check": 0,
        "delta_max_check": 1,
        "age_limit_check": 0,
        "sqn_check": 0,
...

So why is a single integer something that works? Trying to reproduce this in the python3 shell:

>>> import json
>>> json.loads('1')
1
>>> json.loads('foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.10/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
>>> json.loads('"foo"')
'foo'
>>> json.loads('{"foo": "bar"}')
{'foo': 'bar'}

So quoting does matter... will further investigate.

Actions #5

Updated by laforge 6 days ago

  • Status changed from New to In Progress
  • % Done changed from 0 to 30

The correct syntax would be:

pySIM-shell (MF/ADF.USIM/EF.USIM_AUTH_KEY)> update_binary_decoded --json-path $.opc '"831afd01ef48692ec6fd18aeab6cf381"'

This still raises an exception, but one that's unrelated to the JSON parser, but somewhere within the construct:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/cmd2/cmd2.py", line 2129, in onecmd_plus_hooks
    stop = self.onecmd(statement, add_to_history=add_to_history)
  File "/usr/local/lib/python3.10/dist-packages/cmd2/cmd2.py", line 2559, in onecmd
    stop = func(statement)
  File "/usr/local/lib/python3.10/dist-packages/cmd2/decorators.py", line 336, in cmd_wrapper
    return func(*args_list, **kwargs)
  File "/space/home/laforge/projects/git/pysim/pySim/filesystem.py", line 590, in do_update_binary_decoded
    (data, sw) = self._cmd.rs.update_binary_dec(data_json)
  File "/space/home/laforge/projects/git/pysim/pySim/filesystem.py", line 1559, in update_binary_dec
    data_hex = self.selected_file.encode_hex(data)
  File "/space/home/laforge/projects/git/pysim/pySim/filesystem.py", line 730, in encode_hex
    return b2h(self._construct.build(abstract_data))
  File "/usr/lib/python3/dist-packages/construct/core.py", line 336, in build
    self.build_stream(obj, stream, **contextkw)
  File "/usr/lib/python3/dist-packages/construct/core.py", line 348, in build_stream
    self._build(obj, stream, context, "(building)")
  File "/usr/lib/python3/dist-packages/construct/core.py", line 2144, in _build
    buildret = sc._build(subobj, stream, context, path)
  File "/usr/lib/python3/dist-packages/construct/core.py", line 2657, in _build
    return self.subcon._build(obj, stream, context, path)
  File "/usr/lib/python3/dist-packages/construct/core.py", line 5111, in _build
    buildret = self.subcon._build(obj, stream2, context, path)
  File "/usr/lib/python3/dist-packages/construct/core.py", line 2139, in _build
    subobj = obj[sc.name] # raises KeyError
KeyError: None

The same problem also happens unrelated to json-path:

pySIM-shell (MF/ADF.USIM/EF.USIM_AUTH_KEY)> edit_binary_decoded
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/cmd2/cmd2.py", line 2129, in onecmd_plus_hooks
    stop = self.onecmd(statement, add_to_history=add_to_history)
  File "/usr/local/lib/python3.10/dist-packages/cmd2/cmd2.py", line 2559, in onecmd
    stop = func(statement)
  File "/space/home/laforge/projects/git/pysim/pySim/filesystem.py", line 601, in do_edit_binary_decoded
    json.dump(orig_json, text_file, indent=4)
  File "/usr/lib/python3.10/json/__init__.py", line 179, in dump
    for chunk in iterable:
  File "/usr/lib/python3.10/json/encoder.py", line 431, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/usr/lib/python3.10/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/usr/lib/python3.10/json/encoder.py", line 438, in _iterencode
    o = _default(o)
  File "/usr/lib/python3.10/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable

or

pySIM-shell (MF/ADF.USIM/EF.USIM_AUTH_KEY)> update_binary_decoded '{ "cfg": { "only_4bytes_res_in_3g": 0, "use_sres_deriv_func_2_in_3g": 0, "use_opc_instead_of_op": 1, "algorithm": "milenage" }, "key": "77291f1e17132add86dc23a3af601c89", "op": null, "opc": "831afd01ef48692ec6fd18aeab6cf381" }'
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/cmd2/cmd2.py", line 2129, in onecmd_plus_hooks
    stop = self.onecmd(statement, add_to_history=add_to_history)
  File "/usr/local/lib/python3.10/dist-packages/cmd2/cmd2.py", line 2559, in onecmd
    stop = func(statement)
  File "/usr/local/lib/python3.10/dist-packages/cmd2/decorators.py", line 336, in cmd_wrapper
    return func(*args_list, **kwargs)
  File "/space/home/laforge/projects/git/pysim/pySim/filesystem.py", line 590, in do_update_binary_decoded
    (data, sw) = self._cmd.rs.update_binary_dec(data_json)
  File "/space/home/laforge/projects/git/pysim/pySim/filesystem.py", line 1559, in update_binary_dec
    data_hex = self.selected_file.encode_hex(data)
  File "/space/home/laforge/projects/git/pysim/pySim/filesystem.py", line 730, in encode_hex
    return b2h(self._construct.build(abstract_data))
  File "/usr/lib/python3/dist-packages/construct/core.py", line 336, in build
    self.build_stream(obj, stream, **contextkw)
  File "/usr/lib/python3/dist-packages/construct/core.py", line 348, in build_stream
    self._build(obj, stream, context, "(building)")
  File "/usr/lib/python3/dist-packages/construct/core.py", line 2144, in _build
    buildret = sc._build(subobj, stream, context, path)
  File "/usr/lib/python3/dist-packages/construct/core.py", line 2657, in _build
    return self.subcon._build(obj, stream, context, path)
  File "/usr/lib/python3/dist-packages/construct/core.py", line 5111, in _build
    buildret = self.subcon._build(obj, stream2, context, path)
  File "/usr/lib/python3/dist-packages/construct/core.py", line 2139, in _build
    subobj = obj[sc.name] # raises KeyError
KeyError: None
EXCEPTION of type 'KeyError' occurred with message: 'None'

Actions

Also available in: Atom PDF

Add picture from clipboard (Maximum size: 48.8 MB)