def french_apdu(self, rand=None, autn=None, scope=None, debug=False, trigger=None): if trigger is not None: trigger.disarm() self.cardrequest = CardRequest(timeout=5, cardType=AnyCardType()) self.cardservice = self.cardrequest.waitforcard() if debug: obs = ConsoleCardConnectionObserver() self.cardservice.connection.addObserver(obs) self.cardservice.connection.connect() self.c = self.cardservice.connection r, sw1, sw2 = self.c.transmit( [0x00, 0xa4, 0x08, 0x04, 0x02, 0x2f, 0x00]) r, sw1, sw2 = self.c.transmit([0x00, 0xC0, 0x00, 0x00] + [sw2]) r, sw1, sw2 = self.c.transmit([0x00, 0xB2, 0x01, 0x04] + [r[7]]) r, sw1, sw2 = self.c.transmit([0x00, 0xA4, 0x04, 0x04] + list(r[3:4 + r[3]])) r, sw1, sw2 = self.c.transmit([0x00, 0xC0, 0x00, 0x00] + [sw2]) if rand is None and autn is None: authcmd = [0x00, 0x88, 0x00, 0x81, 0x22, 0x10 ] + [0xaa] * 16 + [0x10] + [0xbb] * 16 else: authcmd = [0x00, 0x88, 0x00, 0x81, 0x22, 0x10] + rand + [0x10 ] + autn if trigger is not None: trigger.arm() scope.arm() r, sw1, sw2 = self.c.transmit(authcmd)
def open(self): self.conn = self.reader.createConnection() self.conn.connect() if DEBUG: observer = ConsoleCardConnectionObserver() self.conn.addObserver(observer)
def connectionObserver(): # request any card type cardtype = AnyCardType() cardrequest = CardRequest(timeout=1, cardType=cardtype) cardservice = cardrequest.waitforcard() observer = ConsoleCardConnectionObserver() cardservice.connection.addObserver(observer) cardservice.connection.connect() print('ATR: ' + toHexString(cardservice.connection.getATR())) # get reader used for the connection # device = cardservice.connection.getReader() GET_RESPONSE = [0XA0, 0XC0, 00, 00] SELECT = [0xA0, 0xA4, 0x00, 0x00, 0x02] DF_TELECOM = [0x7F, 0x10] apdu = SELECT + DF_TELECOM response, sw1, sw2 = cardservice.connection.transmit(apdu) if sw1 == 0x9F: apdu = GET_RESPONSE + [sw2] response, sw1, sw2 = cardservice.connection.transmit(apdu) else: print('no DF_TELECOM')
def update(self, observable, actions): (addedcards, removedcards) = actions for card in addedcards: logger.info("+ Inserted: %s", toHexString(card.atr)) connection = card.createConnection() connection.connect() card.connection = connection.component # This will log raw card traffic to console connection.addObserver(ConsoleCardConnectionObserver()) # connection object itself is CardConnectionDecorator wrapper # and we need to address the underlying connection object # directly logger.debug("Opened connection %s", connection.component) desfire = DESFire(PCSCDevice(connection.component)) applications = desfire.get_applications() if FOOBAR_APP_ID in applications: consumer.attach_card(card) else: logger.warn( "DESFire card doesn't have the required application. Maybe not properly formatted?" ) for card in removedcards: logger.info("- Removed: %s", toHexString(card.atr)) consumer.detach_card(card)
def nextg_apdu(self, rand=None, autn=None, debug=False): self.cardrequest = CardRequest(timeout=5, cardType=AnyCardType()) self.cardservice = self.cardrequest.waitforcard() if debug: obs = ConsoleCardConnectionObserver() self.cardservice.connection.addObserver(obs) self.cardservice.connection.connect() self.c = self.cardservice.connection # print("ATR... : %s" % self.cardservice.connection.getATR()) r, sw1, sw2 = self.c.transmit( [0x00, 0xa4, 0x08, 0x04, 0x02, 0x2f, 0x00]) r, sw1, sw2 = self.c.transmit([0x00, 0xc0, 0x00, 0x00, 0x28]) r, sw1, sw2 = self.c.transmit([0x00, 0xb2, 0x01, 0x04, 0x26]) r, sw1, sw2 = self.c.transmit([ 0x00, 0xa4, 0x04, 0x04, 0x10, 0xa0, 0x00, 0x00, 0x00, 0x87, 0x10, 0x02, 0xff, 0x33, 0xff, 0xff, 0x89, 0x01, 0x01, 0x01, 0x00 ]) r, sw1, sw2 = self.c.transmit([0x00, 0xc0, 0x00, 0x00, 0x3e]) r, sw1, sw2 = self.c.transmit( [0x00, 0xa4, 0x00, 0x04, 0x02, 0x6f, 0x07]) r, sw1, sw2 = self.c.transmit([0x00, 0xc0, 0x00, 0x00, 0x25]) r, sw1, sw2 = self.c.transmit([0x00, 0xb0, 0x00, 0x00, 0x09]) if rand is None and autn is None: authcmd = [0x00, 0x88, 0x00, 0x81, 0x22, 0x10 ] + [0xaa] * 16 + [0x10] + [0xbb] * 16 else: authcmd = [0x00, 0x88, 0x00, 0x81, 0x22, 0x10] + rand + [0x10 ] + autn r, sw1, sw2 = self.c.transmit(authcmd)
def nextg_apdu(self,rand=None,autn=None,debug=False,trigger=None): trigger.disarm() self.cardrequest = CardRequest(timeout=5,cardType=AnyCardType()) self.cardservice = self.cardrequest.waitforcard() if debug: obs = ConsoleCardConnectionObserver() self.cardservice.connection.addObserver(obs) self.cardservice.connection.connect() self.c = self.cardservice.connection # print("ATR... : %s" % self.cardservice.connection.getATR()) r,sw1,sw2 = self.c.transmit([0x00, 0xa4, 0x08, 0x04, 0x02, 0x2f, 0x00]) r,sw1,sw2 = self.c.transmit([0x00, 0xc0, 0x00, 0x00, sw2]) r,sw1,sw2 = self.c.transmit([0x00, 0xb2, 0x01, 0x04, r[7]]) r,sw1,sw2 = self.c.transmit([0x00,0xA4,0x04,0x04] + list(r[3:4 + r[3]])) r,sw1,sw2 = self.c.transmit([0x00,0xC0,0x00,0x00] + [sw2]) r,sw1,sw2 = self.c.transmit([0x00,0xA4,0x00,0x04,0x02,0x6F,0x07]) r,sw1,sw2 = self.c.transmit([0x00, 0xc0, 0x00, 0x00, sw2]) # r,sw1,sw2 = self.c.transmit([0x00, 0xb0, 0x00, 0x00, r[7]]) # r,sw1,sw2 = # r,sw1,sw2 = self.c.transmit([0x00, 0xb2, 0x01, 0x04, r[7]]) if rand is None and autn is None: authcmd = [0x00, 0x88, 0x00, 0x81, 0x22, 0x10] + [0xaa] * 16 + [0x10] + [0xbb] * 16 else: authcmd = [0x00, 0x88, 0x00, 0x81, 0x22, 0x10] + rand + [0x10] + autn trigger.arm() print("Arming") r,sw1,sw2 = self.c.transmit(authcmd)
def _do_connect_and_select(p, args): readers = scsys.readers() if len(readers) == 0: p.error('No readers found.') reader = None if args.reader is None: reader = readers[0] elif isinstance(args.reader, int): if args.reader >= len(reader): p.error(f'Invalid reader index {args.reader}') return None reader = readers[args.reader] else: for r in readers: if str(r).beginswith(args.reader): reader = r break if reader is None: p.error( f'Reader name {repr(args.reader)} does not match any connected readers.' ) assert reader is not None conn = reader.createConnection() if args.debug: observer = ConsoleCardConnectionObserver() conn.addObserver(observer) conn.connect() _select(conn, args.aid) return conn
def fuzzFile(self,observer=False): self.cardrequest = CardRequest(timeout=5,cardType=AnyCardType()) self.cardservice = self.cardrequest.waitforcard() if observer: obs = ConsoleCardConnectionObserver() self.cardservice.connection.addObserver(obs) self.cardservice.connection.connect() self.c = self.cardservice.connection print("ATR... : %s" % self.cardservice.connection.getATR()) print("Brute forcing...") out = "" for i in range(0,0xFF): for x in range(0,0xFF): response, sw1, sw2 = self.cardservice.connection.transmit([0x00,0xA4,0x08,0x04,0x02, i,x]) if sw1 == 0x61: out += "Valid APDU from MF: %02x::%02x\n" % (i,x) return out
def verificar_eID_en_lector(self): ''' Verifica que la tarjeta esté puesta en el lector :return: True si está puesta, False en caso contrario. ''' print('insert a card (SIM card if possible) within 10s') try: cardrequest = CardRequest(timeout=10, cardType=self.cardtype) self.cardservice = cardrequest.waitforcard() # attach the console tracer observer = ConsoleCardConnectionObserver() self.cardservice.connection.addObserver(observer) self.cardservice.connection.connect() return True except CardRequestTimeoutException: print('time-out: no card inserted during last 10s') return False
def __init__(self, reader, extended=False, debug=False, protocol=None, combi=False): self.reader = reader # Begining Of Line import curses curses.setupterm() self.BOL = curses.tigetstr("cr") + curses.tigetstr("cuu1") # connect to the reader self.connection = reader.createConnection() # create an observer to get debug if debug: observer = ConsoleCardConnectionObserver() self.connection.addObserver(observer) self.BOL = "" # connect using the selected protocol (if any) self.connection.connect(protocol=protocol) # get the ATR self.ATR = self.connection.getATR() # display used protocol protocols = { CardConnection.T0_protocol: "T=0", CardConnection.T1_protocol: "T=1" } print "Using protocol:", protocols[self.connection.getProtocol()] # extended APDU self.extended = extended # select APDU if not extended: if combi: SELECT = toBytes("00 A4 04 00 06 A0 00 00 00 18 50") else: SELECT = toBytes("00 A4 04 00 06 A0 00 00 00 18 FF") expected = [[], 0x90, 0x00] self.transmitAndCompare(SELECT, expected)
def __init__(self, logger=None, sam_connection=None, picc_connection=None, mid="1122334455667788", tid="aabbccddeeff0000", debug_mode=True): try: # get setting self._logger = logger self._mid = mid self._tid = tid self.initialize = False # check sam and picc connection object if sam_connection is None or picc_connection is None: self._logger and self._logger.error( "SAM and or PICC connection is unavailable") else: # assign to sam and picc interface self._reader_picc_connection = picc_connection self._reader_sam_connection = sam_connection # add observer if debug_mode: self._reader_observer = ConsoleCardConnectionObserver() self._reader_picc_connection.addObserver( self._reader_observer) self._reader_sam_connection.addObserver( self._reader_observer) # connect to picc only to check the card present or not self.card_open_connection() self.sam_open_connection() self._logger and self._logger.debug( "Initializing picc and samcard reader OK") self.initialize = True except Exception as err: pass self._logger and self._logger.error(err)
def __init__(self): global CHANNEL_KEY, API_URL self.observer = ConsoleCardConnectionObserver() config_file = Path("nfc.config") log_file = Path("log.txt") if not log_file.is_file(): f = open("log.txt", "w+") f.close() if config_file.is_file(): f = open("nfc.config", "r") urlLine = f.readline() split = urlLine.split('=') API_URL = split[1] keyLine = f.readline() split = keyLine.split('=') CHANNEL_KEY = split[1] f.close() else: f = open("nfc.config", "w+") f.write("URL=\nKEY=\n")
def cardConnect(self, time=10, cardtype=None): try: # request card insertion print 'insert a card (SIM card if possible) within 10s' cardrequest = CardRequest(timeout=time, cardType=cardtype) self.cardservice = cardrequest.waitforcard() # attach the console tracer observer = ConsoleCardConnectionObserver() self.cardservice.connection.addObserver(observer) # connect to the card and perform a few transmits self.cardservice.connection.connect() response, sw1, sw2 = self.cardservice.connection.transmit( self.get_tag_apdu) if sw1 == 0x90: self.get_tag(response) else: print "Get Tag Apdu transmit işlemi basarısız" sys.exit() except CardRequestTimeoutException: print 'time-out: no card inserted during last 10s'
def update(self, observable, actions): addedcards, removedcards = actions for card in addedcards: connection = card.createConnection() connection.connect() # This will log raw card traffic to console connection.addObserver(ConsoleCardConnectionObserver()) comp = connection.component if not comp.hcard: raise Exception( "Tried to transit to non-open connection: {}".format(comp)) protocol = comp.getProtocol() pcscprotocolheader = translateprotocolheader(protocol) # http://pyscard.sourceforge.net/epydoc/smartcard.scard.scard-module.html#SCardTransmit msg = [0xff, 0xca, 0x00, 0x00, 0x04] hresult, response = SCardTransmit(comp.hcard, pcscprotocolheader, msg) if hresult != 0: raise Exception('Failed to transmit with protocol ' + str(pcscprotocolheader) + '. ' + SCardGetErrorMessage(hresult)) resp_bytes = ['{0:02x}'.format(r) for r in response] status = ''.join(resp_bytes[-2:]) if status != '9000': return # raise Exception('Could not read card.') serial = ''.join(resp_bytes[0:4]) self.on_card(serial)
def french_apdu(self, rand=None, autn=None, debug=False): self.cardrequest = CardRequest(timeout=5, cardType=AnyCardType()) self.cardservice = self.cardrequest.waitforcard() if debug: obs = ConsoleCardConnectionObserver() self.cardservice.connection.addObserver(obs) self.cardservice.connection.connect() self.c = self.cardservice.connection # print(" !!! USING SHORTER SSTIC2018 PAPER APDU SEQUENCE !!!") r, sw1, sw2 = self.c.transmit( [0x00, 0xa4, 0x08, 0x04, 0x02, 0x2f, 0x00]) r, sw1, sw2 = self.c.transmit([0x00, 0xC0, 0x00, 0x00] + [sw2]) r, sw1, sw2 = self.c.transmit([0x00, 0xB2, 0x01, 0x04] + [r[7]]) r, sw1, sw2 = self.c.transmit([0x00, 0xA4, 0x04, 0x04] + list(r[3:4 + r[3]])) r, sw1, sw2 = self.c.transmit([0x00, 0xC0, 0x00, 0x00] + [sw2]) if rand is None and autn is None: authcmd = [0x00, 0x88, 0x00, 0x81, 0x22, 0x10 ] + [0xaa] * 16 + [0x10] + [0xbb] * 16 else: authcmd = [0x00, 0x88, 0x00, 0x81, 0x22, 0x10] + rand + [0x10 ] + autn r, sw1, sw2 = self.c.transmit(authcmd)
print 'Invalid PIN', args[0] sys.exit(True) else: PIN = args[0] try: # request any card type cardtype = AnyCardType() # request card insertion print 'insert a card within 10s' cardrequest = CardRequest(timeout=10, cardType=cardtype) cardservice = cardrequest.waitforcard() # attach the console tracer if Debug: observer = ConsoleCardConnectionObserver() cardservice.connection.addObserver(observer) # connect to the card cardservice.connection.connect(Protocol) #get_challenge(0) # try to select PSE apdu = SELECT + [len(DF_PSE)] + DF_PSE response, sw1, sw2 = send_apdu(apdu) if check_return(sw1, sw2): # there is a PSE print 'PSE found!' decode_pse(response)
def __init__(self): self.observer = ConsoleCardConnectionObserver()
def __init__(self): self.observer = ConsoleCardConnectionObserver() self.getbaca = "" self.getTap = ""
def update(self, observable, actions): (addedcards, removedcards) = actions for card in addedcards: logger.info("+ Inserted: %s", toHexString(card.atr)) connection = card.createConnection() connection.connect() # This will log raw card traffic to console connection.addObserver(ConsoleCardConnectionObserver()) # connection object itself is CardConnectionDecorator wrapper # and we need to address the underlying connection object # directly self.desfire = DESFire(PCSCDevice(connection.component)) while True: num = int( input(""" 10. Authenticate ---------------------------- 20. Get card information 21. Format card ---------------------------- 30. Create application 31. Select applicatino 32. List application ---------------------------- 40. Change key 41. Get key settings 42. Change key settings ---------------------------- 50. Craete file 51. List files 52. Write file 53. Read file 90. Exit """)) if num == 90: break elif num == 10: self.auth() elif num == 20: self.getCardInfo() elif num == 21: self.formatCard() elif num == 30: self.createApplication() elif num == 31: self.selectApplication() elif num == 32: self.listApplication() elif num == 40: self.changeKey() elif num == 41: self.getKeySettings() elif num == 42: self.changeKeySettings() elif num == 50: self.createFile() elif num == 51: self.listFiles() elif num == 52: self.writeFile() elif num == 53: self.readFile()
def update(self, observable, actions): (addedcards, removedcards) = actions for card in addedcards: logger.info("+ Inserted: %s", toHexString(card.atr)) connection = card.createConnection() connection.connect() # This will log raw card traffic to console connection.addObserver(ConsoleCardConnectionObserver()) # connection object itself is CardConnectionDecorator wrapper # and we need to address the underlying connection object # directly logger.info("Opened connection %s", connection.component) desfire = DESFire(PCSCDevice(connection.component)) key_setting=desfire.getKeySetting() logger.info('Auth Key %d',0) desfire.authenticate(0,key_setting) info=desfire.getCardVersion() logger.info(info) logger.info('Format card') desfire.formatCard() logger.info('Create application with ID 00AE16') desfire.createApplication("00 AE 16",[DESFireKeySettings.KS_ALLOW_CHANGE_MK,DESFireKeySettings.KS_LISTING_WITHOUT_MK,DESFireKeySettings.KS_CONFIGURATION_CHANGEABLE],14,DESFireKeyType.DF_KEY_AES) logger.info('Select application with ID 00AE16') desfire.selectApplication('00 AE 16') default_key=desfire.createKeySetting('00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00',0,DESFireKeyType.DF_KEY_AES,[]) app_key=desfire.createKeySetting('00 10 20 31 40 50 60 70 80 90 A0 B0 B0 A0 90 80',0,DESFireKeyType.DF_KEY_AES,[]) logger.info('Auth Key %d',0) desfire.authenticate(0,default_key) logger.info('Cange Key %d',0) desfire.changeKey(0,app_key,default_key) logger.info('Auth Key %d',0) desfire.authenticate(0,app_key) desfire.changeKeySettings([ DESFireKeySettings.KS_ALLOW_CHANGE_MK, DESFireKeySettings.KS_CONFIGURATION_CHANGEABLE, DESFireKeySettings.KS_CHANGE_KEY_WITH_KEY_1]) app_key_1=desfire.createKeySetting('11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF 00',0,DESFireKeyType.DF_KEY_AES,[]) logger.info('Cange Key %d',1) desfire.changeKey(1,app_key_1,default_key) logger.info('Auth Key %d',1) desfire.authenticate(1,app_key_1) app_key_2=desfire.createKeySetting('22 33 44 55 66 77 88 99 AA BB CC DD EE FF 00 11',0,DESFireKeyType.DF_KEY_AES,[]) logger.info('Cange Key %d',2) desfire.changeKey(2,app_key_2,default_key) app_key_3=desfire.createKeySetting('33 44 55 66 77 88 99 AA BB CC DD EE FF 00 11 22',0,DESFireKeyType.DF_KEY_AES,[]) logger.info('Cange Key %d',3) desfire.changeKey(3,app_key_3,default_key) app_key_4=desfire.createKeySetting('44 55 66 77 88 99 AA BB CC DD EE FF 00 11 22 33',0,DESFireKeyType.DF_KEY_AES,[]) logger.info('Cange Key %d',4) desfire.changeKey(4,app_key_4,default_key) logger.info('Auth Key %d',0) desfire.authenticate(0,app_key) filePerm=DESFireFilePermissions() filePerm.setPerm(0x04,0x03,0x0F,0x02) # key 4 read, key3 write, no key read and write, key2 change permissions logger.info('Creat File with ID %d and %d byte',0,32) desfire.createStdDataFile(0,filePerm,32) # file Id 0, length 32 byte logger.info('Auth Key %d',3) desfire.authenticate(3,app_key_3) write='00 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20' logger.info('Data write %s',write) desfire.writeFileData(0,0,32,write) logger.info('Auth Key %d',4) desfire.authenticate(4,app_key_4) read=desfire.readFileData(0,0,32) logger.info('Data read %s',byte_array_to_human_readable_hex(read))
def __init__(self, incallback, outcallback): CardObserver.__init__(self) self.InCallback = incallback self.outCallback = outcallback self.observer = ConsoleCardConnectionObserver()
def __init__(self, verbose): self.observer = ConsoleCardConnectionObserver() self.reader_name = "Uknown" self.verbose = verbose