-2

I'm trying to retrieve the PAN of a smart card using pyscard in Python. What I have done so far is to connect to the reader and to retrieve various information about the reader and the card... but I cannot find the way to get the serial number...

Using pyscard, the first thing to do is to create a communication context between PC and Smart Card:

hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)

Once the context has been established, let's try to get the list of active smart card readers:

hresult, readers = SCardListReaders(hcontext, [])

readers is a list, readers[0] will contain the reader, should you possess only one. At this point, what I did is to get the ATR of the card:

hresult, hcard, dwActiveProtocol = SCardConnect(hcontext,
current_reader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1)

And it works. Then I tried to communicate with the card: here I write the way to get a random number, using the APDU command and the 0x84 hex in the second position (INS).

hresult, response = SCardTransmit(hcard,dwActiveProtocol,[0x00, 0x84, 0x00, 0x00, 0x00])

As you can see an APDU is composed by 5 different hex digits: CLA, INS, P1, P2, P3.

Ok, still not serial number, but I'm fighting at least :-)

By the way, I'm reading the pyscard documentation and the ISO7816 doc.

Thank you in advance!

Bya
  • 55
  • 1
  • 12

1 Answers1

0

You should specify which smartcard you are using.

If I am remembering right, not all the cards had the serial number accessible and if it is accessible, I don't think there is a standard command.

Anyway, you should take a look at the iso 7816-4 standand, which defines the APDU commands. Additionally, you should check the documentation of your card to see if it has some non-standard APDU command that can help you.

Gianluca
  • 3,227
  • 2
  • 34
  • 35
  • Thank you for your answer. I will update this question when I'll discover something new. A the moment I have been able to receive the ATR. My users do not have all the same type of smart card, so I was looking for a general way to get the serial number (at least I know that every card they possess has a serial number). Pyscard has *many* different commands and I am exploring them... – Bya Mar 17 '17 at 11:36
  • Ok, one step forward: I realize that I can send APDU commands to the smart card. Now I am looking for the mysterious command which will return the serial number... Thanks for mentioning ISO 7816-4. – Bya Mar 17 '17 at 14:11