From 162ba3af3e08ae4396880629b0686b4e7bc3aa55 Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Thu, 27 Jul 2023 11:46:26 +0200 Subject: [PATCH] pySim-trace: mark card reset in the trace The trace log currently does not contain any information about card resets. This makes the trace difficult to follow. Let's use the CardReset object to display the ATR in the trace. Related: OS#6094 Change-Id: Ia550a8bd2f45d2ad622cb2ac2a2905397db76bce --- pySim-trace.py | 6 ++++++ pySim/apdu/__init__.py | 9 ++++++++- pySim/apdu_source/gsmtap.py | 2 +- pySim/apdu_source/pyshark_gsmtap.py | 2 +- pySim/apdu_source/pyshark_rspro.py | 3 ++- 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pySim-trace.py b/pySim-trace.py index 8a9fad7b..8313c27c 100755 --- a/pySim-trace.py +++ b/pySim-trace.py @@ -93,6 +93,11 @@ class Tracer: print("%02u %-16s %-35s %-8s %s %s" % (inst.lchan_nr, inst._name, inst.path_str, inst.col_id, inst.col_sw, inst.processed)) print("===============================") + def format_reset(self, apdu: CardReset): + """Output a single decoded CardReset.""" + print(apdu) + print("===============================") + def main(self): """Main loop of tracer: Iterates over all Apdu received from source.""" while True: @@ -101,6 +106,7 @@ class Tracer: if isinstance(apdu, CardReset): self.rs.reset() + self.format_reset(apdu) continue # ask ApduDecoder to look-up (INS,CLA) + instantiate an ApduCommand derived diff --git a/pySim/apdu/__init__.py b/pySim/apdu/__init__.py index cc0f7015..b884e230 100644 --- a/pySim/apdu/__init__.py +++ b/pySim/apdu/__init__.py @@ -448,4 +448,11 @@ class ApduDecoder(ApduHandler): class CardReset: - pass + def __init__(self, atr: bytes): + self.atr = atr + + def __str__(self): + if (self.atr): + return '%s(%s)' % (type(self).__name__, b2h(self.atr)) + else: + return '%s' % (type(self).__name__) diff --git a/pySim/apdu_source/gsmtap.py b/pySim/apdu_source/gsmtap.py index fe450e23..aaf97adc 100644 --- a/pySim/apdu_source/gsmtap.py +++ b/pySim/apdu_source/gsmtap.py @@ -49,7 +49,7 @@ class GsmtapApduSource(ApduSource): return ApduCommands.parse_cmd_bytes(gsmtap_msg['body']) elif sub_type == 'atr': # card has been reset - return CardReset() + return CardReset(gsmtap_msg['body']) elif sub_type in ['pps_req', 'pps_rsp']: # simply ignore for now pass diff --git a/pySim/apdu_source/pyshark_gsmtap.py b/pySim/apdu_source/pyshark_gsmtap.py index 8ea9ae7c..aa7b6248 100644 --- a/pySim/apdu_source/pyshark_gsmtap.py +++ b/pySim/apdu_source/pyshark_gsmtap.py @@ -69,7 +69,7 @@ class _PysharkGsmtap(ApduSource): return ApduCommands.parse_cmd_bytes(gsmtap_msg['body']) elif sub_type == 'atr': # card has been reset - return CardReset() + return CardReset(gsmtap_msg['body']) elif sub_type in ['pps_req', 'pps_rsp']: # simply ignore for now pass diff --git a/pySim/apdu_source/pyshark_rspro.py b/pySim/apdu_source/pyshark_rspro.py index 499e9ff7..8c614ffc 100644 --- a/pySim/apdu_source/pyshark_rspro.py +++ b/pySim/apdu_source/pyshark_rspro.py @@ -117,7 +117,8 @@ class _PysharkRspro(ApduSource): vccPresent, resetActive, clkActive = self.get_pstatus(slot_pstatus) if vccPresent and clkActive and not resetActive: logger.debug("RESET") - return CardReset() + #TODO: extract ATR from RSPRO message and use it here + return CardReset(None) else: print("Unhandled msg type %s: %s" % (msg_type, rspro_msg))