Hi n4n5,
thanks for the pach. Can you please explain in more detail which specific command was not
working anymore?
I'm not sure how tolerante we need to be when it comes to parsing invalid data.
The user can always use pySim-shell update_binary to write eithre some valid data, or
simply set everything to
ff.
Also, pySim-shell update_binary_decded should also work, as it doesn't try to decode
the previous data.
I think the only operation affected is edit_binary_decoded - and here I'm not sure we
are able to adjust our
code for hundreds of different EF decodrs to always support whatever malformed data may be
present in a file.
Regards,
Harald
On Wed, Feb 28, 2024 at 03:52:41PM +0100, n4n5 wrote:
Hello,
Here is a patch for handling malformed IMSI
If you misswrited your IMSI, it will be impossible to read or edit it again :(
This patch fix that
Thanks
Goodbye
From 76e18254499aff27dea07c93e14eb7f74426240f Mon Sep
17 00:00:00 2001
From: n4n5 <56606507+Its-Just-Nans(a)users.noreply.github.com>
Date: Wed, 28 Feb 2024 14:57:33 +0100
Subject: [PATCH] add try-except for handling malformed imsi
---
pySim/utils.py | 32 ++++++++++++++++++--------------
1 file changed, 18 insertions(+), 14 deletions(-)
diff --git a/pySim/utils.py b/pySim/utils.py
index afa476b..c758ed0 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -422,21 +422,25 @@ def enc_imsi(imsi: str):
def dec_imsi(ef: Hexstr) -> Optional[str]:
"""Converts an EF value to the IMSI string
representation"""
- if len(ef) < 4:
- return None
- l = int(ef[0:2], 16) * 2 # Length of the IMSI string
- l = l - 1 # Encoded length byte includes oe nibble
- swapped = swap_nibbles(ef[2:]).rstrip('f')
- if len(swapped) < 1:
- return None
- oe = (int(swapped[0]) >> 3) & 1 # Odd (1) / Even (0)
- if not oe:
- # if even, only half of last byte was used
- l = l-1
- if l != len(swapped) - 1:
+ try:
+ if len(ef) < 4:
+ return None
+ l = int(ef[0:2], 16) * 2 # Length of the IMSI string
+ l = l - 1 # Encoded length byte includes oe nibble
+ swapped = swap_nibbles(ef[2:]).rstrip('f')
+ if len(swapped) < 1:
+ return None
+ oe = (int(swapped[0]) >> 3) & 1 # Odd (1) / Even (0)
+ if not oe:
+ # if even, only half of last byte was used
+ l = l-1
+ if l != len(swapped) - 1:
+ return None
+ imsi = swapped[1:]
+ return imsi
+ except ValueError as err:
+ # print("IMSI is present but malformed")
return None
- imsi = swapped[1:]
- return imsi
def dec_iccid(ef: Hexstr) -> str:
--
- Harald Welte <laforge(a)osmocom.org>
https://laforge.gnumonks.org/
============================================================================
"Privacy in residential applications is a desirable marketing option."
(ETSI EN 300 175-7 Ch. A6)