Retrieve smart cards PAN with Python and pyscard

2024/11/18 21:48:09

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!

Answer

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.

https://en.xdnf.cn/q/120027.html

Related Q&A

How to stop a specific key from working in Python

My laptop keyboard has a bug and it sometimes presses the number 5 randomly so i tried many things and they didnt work, I tried programming a code that can stop it but i couldnt because i am a beginner…

How do i sort a 2D array or multiple arrays by the length of the array using bubble sort

trying to write a Python function: def compare_lengths(x, y, z) which takes as arguments three arrays and checks their lengths and returns them as a triple in order of length. For example, if the funct…

How to split a string in Python by 2 or 3, etc [duplicate]

This question already has answers here:Split string every nth character(21 answers)How to iterate over a list in chunks(40 answers)Closed 10 years ago.Does anyone know if its possible in python to spli…

.LAS into a .CSV file using python

How to change a .las file into a .csv file? Have been trying myself but no luck no far. I am just looking for something decently short that will save some time when I have to convert big .olas files i…

using pandas read_excel to read from stdin

Note: I have solve this problem as per below:I can use to_csv to write to stdout in python / pandas. Something like this works fine:final_df.to_csv(sys.stdout, index=False)I would like to read in an a…

How to print a string x times based on user input [duplicate]

This question already has answers here:How can I read inputs as numbers?(10 answers)Understanding for loops in Python(4 answers)Closed 1 year ago.I am trying to make a simple application that will pri…

How does .join work in Python?

I want to display each row of a SQL query result on a webpage. I found some code, but I dont understand what this line does.u"<br>".join([u"{0}".format(row.combination) for r…

Login, Navigate and Retrieve data behind a proxy with Python

I want, with a python script, to be able to login a website and retrieve some data. This behind my companys proxy.I know that this question seems a duplicate of others that you can find searching, but …

overflow in exp, python

cant really figure out why this error RuntimeWarning: overflow encountered in exp is showing up. The function Im trying to implement is:Id = lambda t_u, yp: Is * (np.exp((Vin(t_u) - L*yp)/(n_Ut*Ut)) - …

Getting a matlab codes results on python

I have code in matlab whose results I would like to use in python code (as a matrix or as a .dat file). Could anyone tell me how this could be done?