Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. from sys import stdin, exc_info
  4. from time import sleep
  5.  
  6. from smartcard.CardType import AnyCardType
  7. from smartcard.CardRequest import CardRequest
  8. from smartcard.CardConnectionObserver import ConsoleCardConnectionObserver
  9. from smartcard.util import toHexString
  10.  
  11.  
  12. APDUlist = [
  13. #("RST", ""),
  14. ("8010000011FF9FFFFFFF0F1FFF7F0300002008200000", "9145"),
  15. ("8012000045", "9000"),
  16. ("801400000C810301250002028281030100", "9000"),
  17. #("RST", "")
  18. ]
  19.  
  20. print "Please insert card into a PC/SC reader or press Ctrl+C to terminate.\n"
  21. try:
  22.     cardrequest = CardRequest( timeout=10,  cardType=AnyCardType() )
  23.     cardservice = cardrequest.waitforcard()
  24.     cardservice.connection.connect()
  25.     print "Card found. ATR: " + toHexString(cardservice.connection.getATR())
  26.     print
  27.     for Apdu in APDUlist:
  28.         if Apdu[0] == "RST":
  29.             cardservice.connection.disconnect()
  30.             cardservice.connection.connect()
  31.         else:
  32.             ToSend = []
  33.             for iLoop in range(len(Apdu[0]) / 2):
  34.                 ToSend = ToSend +  [int(Apdu[0][(iLoop * 2):((iLoop + 1) * 2)], 16)]
  35.             print "Sending APDU command: ", toHexString(ToSend)
  36.             response, sw1, sw2 = cardservice.connection.transmit( ToSend )
  37.             if len(Apdu[1]) == 2:
  38.                 if (sw1, sw2) != (hex(int(Apdu[1][:2], 16)), hex(int(Apdu[1][2:], 16))):
  39.                     print "Bad Status Word, exiting...\n"
  40.                     exit()
  41.             print "Got response: ", toHexString(response), hex(sw1), hex(sw2)
  42.     cardservice.connection.disconnect()
  43.    
  44. except KeyboardInterrupt:
  45.     print "Terminating."
  46.     exit()
  47. except:
  48.     print "Card connection has failed. Terminating."
  49.     print exc_info()[0], ':', exc_info()[1]
  50.     exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement