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-n...@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:

Reply via email to