LudovicRousseau / pyscard

pyscard smartcard library for python
http://pyscard.sourceforge.net/
GNU Lesser General Public License v2.1
379 stars 108 forks source link

When trying to read Mifare classic card, there is an IndexError #101

Closed cheriimoya closed 3 years ago

cheriimoya commented 3 years ago

I tried to send [0x60, 0x05, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1, 0x2, 0x3, 0x4] to a mifare classic card and got an exception i did not expect.

Your system information

Please describe your issue in as much detail as possible:

I would have expected a clear error message.

But there was an exception thrown.

The script i wrote:

from smartcard.CardRequest import CardRequest

READBLOCK = [0x60, 0x05]
KEY = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
CARDID = [1, 2, 3, 4]

CARDREQUEST = CardRequest()
CARDSERVICE = CARDREQUEST.waitforcard()
CARDSERVICE.connection.connect()

COMMAND = READBLOCK + KEY + CARDID
RES = CARDSERVICE.connection.transmit(COMMAND)
print(RES)

The exception:

Traceback (most recent call last):
  File "test.py", line 17, in <module>
    res = CARDSERVICE.connection.transmit(COMMAND, CARDSERVICE.connection.T1_protocol)
  File "/nix/store/n4c9kwr2471qkqqd8bw33rnkrk28agf1-python3.8-pyscard-1.9.9/lib/python3.8/site-packages/smartcard/CardConnectionDecorator.py", line 82, in transmit
    return self.component.transmit(bytes, protocol)
  File "/nix/store/n4c9kwr2471qkqqd8bw33rnkrk28agf1-python3.8-pyscard-1.9.9/lib/python3.8/site-packages/smartcard/CardConnection.py", line 146, in transmit
    data, sw1, sw2 = self.doTransmit(bytes, protocol)
  File "/nix/store/n4c9kwr2471qkqqd8bw33rnkrk28agf1-python3.8-pyscard-1.9.9/lib/python3.8/site-packages/smartcard/pcsc/PCSCCardConnection.py", line 205, in doTransmit
    sw1 = (response[-2] + 256) % 256
IndexError: list index out of range

Steps for reproducing this issue:

  1. connection.transmit(<some command that mifare classic doesn't understand, i guess>)
  2. exception happens without further information
LudovicRousseau commented 3 years ago

Can you generate a pcscd trace as documented in https://pcsclite.apdu.fr/#support

cheriimoya commented 3 years ago

here you go @LudovicRousseau https://gist.github.com/cheriimoya/30bf82f0d66e4e34399eda9c2c5e1c29

LudovicRousseau commented 3 years ago
00000003 APDU: 00 A4 04 00 08 A0 00 00 05 27 47 11 17 
00000002 ifdhandler.c:1302:IFDHTransmitToICC() usb:072f/2200:libudev:0:/dev/bus/usb/001/054 (lun: 0)
00000002 commands.c:1755:CmdXfrBlockTPDU_T0() T=0: 13 bytes
00000004 -> 000000 6F 0D 00 00 00 00 08 00 00 00 00 A4 04 00 08 A0 00 00 05 27 47 11 17 
00006627 <- 000000 80 00 00 00 00 00 08 00 FE 00 
00000017 SW: 
00000005 winscard.c:1640:SCardTransmit() UnrefReader() count was: 2
00000004 winscard_svc.c:685:ContextThread() TRANSMIT rv=0x0 for client 15

The reader reports an error (0xFE) not card present. But the error is NOT logged and NOT reported to pcsc-lite. That is very strange.

Have you modified libccid or pcsc-lite source code?

cheriimoya commented 3 years ago

i have not modified any software in any way. unfortunately, i do not have access to the reader anymore, so i guess it's not relevant for me now

LudovicRousseau commented 3 years ago

The problem is a reader firmware bug. The reader reports 0xFE in Error byte but does not indicate the frame is an error frame in the Status byte.

I suggest to use another reader model.