Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- from sys import stdin, exc_info
- from time import sleep
- from smartcard.CardType import AnyCardType
- from smartcard.CardRequest import CardRequest
- from smartcard.CardConnectionObserver import ConsoleCardConnectionObserver
- from smartcard.util import toHexString
- APDUlist = [
- #("RST", ""),
- ("8010000011FF9FFFFFFF0F1FFF7F0300002008200000", "9145"),
- ("8012000045", "9000"),
- ("801400000C810301250002028281030100", "9000"),
- #("RST", "")
- ]
- print "Please insert card into a PC/SC reader or press Ctrl+C to terminate.\n"
- try:
- cardrequest = CardRequest( timeout=10, cardType=AnyCardType() )
- cardservice = cardrequest.waitforcard()
- cardservice.connection.connect()
- print "Card found. ATR: " + toHexString(cardservice.connection.getATR())
- print
- for Apdu in APDUlist:
- if Apdu[0] == "RST":
- cardservice.connection.disconnect()
- cardservice.connection.connect()
- else:
- ToSend = []
- for iLoop in range(len(Apdu[0]) / 2):
- ToSend = ToSend + [int(Apdu[0][(iLoop * 2):((iLoop + 1) * 2)], 16)]
- print "Sending APDU command: ", toHexString(ToSend)
- response, sw1, sw2 = cardservice.connection.transmit( ToSend )
- if len(Apdu[1]) == 2:
- if (sw1, sw2) != (hex(int(Apdu[1][:2], 16)), hex(int(Apdu[1][2:], 16))):
- print "Bad Status Word, exiting...\n"
- exit()
- print "Got response: ", toHexString(response), hex(sw1), hex(sw2)
- cardservice.connection.disconnect()
- except KeyboardInterrupt:
- print "Terminating."
- exit()
- except:
- print "Card connection has failed. Terminating."
- print exc_info()[0], ':', exc_info()[1]
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement