def get_tag(): context = nfc.init() pnd = nfc.open(context) if pnd is None: print('ERROR: Unable to open NFC device.') exit() if nfc.initiator_init(pnd) < 0: nfc.perror(pnd, "nfc_initiator_init") print('ERROR: Unable to init NFC device.') exit() # Declare modulations nmMifare = nfc.modulation() nmMifare.nmt = nfc.NMT_ISO14443A nmMifare.nbr = nfc.NBR_106 nt = nfc.target() # Wait for tag ret = nfc.initiator_select_passive_target(pnd, nmMifare, 0, 0, nt) # Convert raw uid to hex, then trim to length (2*nt.nti.nai.szUidLen) tag = ''.join(format(x, '02x') for x in nt.nti.nai.abtUid)[:2*nt.nti.nai.szUidLen] nfc.close(pnd) nfc.exit(context) # Return UID return tag
def try_authenticate(block): keys = [(MC_AUTH_B, keyB)] + \ [(MC_AUTH_A,k) for k in defaultKeys] + [(MC_AUTH_B,k) for k in defaultKeys] + [(MC_AUTH_A, keyA)] for key, key_data in keys: res = authenticate(block, key, key_data) if res: break else: ret = nfc.initiator_select_passive_target(pnd, nmMifare, 0, 0, nt) print(ret) print("reopen")
def block_for_card(device, target_type=None, nbr=None): """Block (wait) until a card is detected, then return its info. """ modul = nfc.modulation() modul.nmt = target_type if target_type is not None else _target_type modul.nbr = nbr if nbr is not None else _nbr target = nfc.target() # this blocks retval = nfc.initiator_select_passive_target(device, modul, 0, 0, target) if not retval: raise NFCError('Couldn\'t establish initial connection with card', { 'retval': retval, 'target_type': modul.nmt, 'nbr': modul.nbr }) return target
def try_read(): if nfc.initiator_select_passive_target(pnd, nmMifare, 0, 0, nt) > -1: return read_sector(7)
def wait_card(): while True: ret = nfc.initiator_select_passive_target(pnd, nmMifare, 0, 0, nt) if ret > -1: return ret
def nfcProtocol(): """ NFC Protocol """ global pnd, nt, context nmMifare = nfc.modulation() nmMifare.nmt = nfc.NMT_ISO14443A nmMifare.nbr = nfc.NBR_106 print("Polling for target...\n"); nt = nfc.target() ret = nfc.initiator_select_passive_target(pnd, nmMifare, 0, 0, nt) print("Target detected!\n"); # Select application pbtTx = DoorProtocol['APDU'] szTx = len(pbtTx) szRx = len(DoorProtocol['DOOR_HELLO']) res, rapdu = cardTransmit(pnd, pbtTx, szTx, szRx) print("Application selected!"); print("REC: " + rapdu) if rapdu != DoorProtocol['DOOR_HELLO']: print("** Opss ** I'm expecting HELLO msg, but card sent to me: %s. len: %d\n" % (rapdu, len(rapdu))) sys.exit(1) # FIDO Auth Request Message print("Doing AuthRequest to FIDO UAF Server\n"); UAFurl = fido_server['AUTH_REQUEST_MSG'] % (fido_server['SCHEME'], fido_server['HOSTNAME'], fido_server['PORT'], fido_server['AUTH_REQUEST_ENDPOINT']) try: r = requests.get(UAFurl) r.raise_for_status() except requests.exceptions.RequestException as e: # This is the correct syntax print(e) sys.exit(1) if (r.status_code != 200): print("** Opss ** Error to connect to FIDO Server") pbtTx = DoorProtocol['ERROR'] szTx = len(pbtTx) szRx = len(DoorProtocol['ERROR']) res, rapdu = cardTransmit(pnd, pbtTx, szTx, szRx) sys.exit(1) content = r.content blocks = (len(content) / BLOCK_SIZE) + 1 pbtTx = "BLOCK:%s" % blocks print("Sending number of blocks: %s " % pbtTx); szTx = len(pbtTx) szRx = len(DoorProtocol['DOOR_NEXT']) res, rapdu = cardTransmit(pnd, pbtTx, szTx, szRx) print("REC: " + rapdu) if rapdu != DoorProtocol['DOOR_NEXT']: print("Error to send number of blocks") sys.exit(1) # Sending UAFRequestMessage to card chunks = len(content) msg_packages = ([ content[i:i + BLOCK_SIZE] for i in range(0, chunks, BLOCK_SIZE) ]) for pack, index in zip(msg_packages, range(1, chunks+1)): print("Seding package %s..." % index) pbtTx = pack szTx = len(pbtTx) szRx = len(DoorProtocol['DOOR_OK']) res, rapdu = cardTransmit(pnd, pbtTx, szTx, szRx) if rapdu != DoorProtocol['DOOR_OK']: print("** Opss ** I'm expecting OK msg, but card sent to me: %s. len: %s" % (rapdu, len(rapdu))) sys.exit(1) print("REC: " + rapdu) print("\nSending READY!") pbtTx = DoorProtocol['DOOR_READY'] szTx = len(pbtTx) szRx = len(DoorProtocol['DOOR_WAIT']) res, rapdu = cardTransmit(pnd, pbtTx, szTx, szRx) if rapdu != DoorProtocol['DOOR_WAIT']: print("** Opss ** I'm expecting WAIT msg, but card sent to me: %s. len: %s" % (rapdu, len(rapdu))) sys.exit(1) print("REC: " + rapdu) print("\nWaiting...\n") time.sleep(5) while rapdu == DoorProtocol['DOOR_WAIT']: szTx = len(pbtTx) szRx = len(DoorProtocol['DOOR_DONE']) res, rapdu = cardTransmit(pnd, pbtTx, szTx, szRx) print("SEND: " + pbtTx) if rapdu != DoorProtocol['DOOR_DONE'] and rapdu != DoorProtocol['DOOR_WAIT']: print("** Opss ** I'm expecting DONE or WAIT msg, but card sent to me: %s. len: %s" % (rapdu, len(rapdu))) sys.exit(1) print("REC: " + rapdu) if (rapdu == DoorProtocol['DOOR_DONE']): print("Sending RESPONSE!") pbtTx = DoorProtocol['DOOR_RESPONSE'] szTx = len(pbtTx) szRx = len("BLOCK: ") res, rapdu = cardTransmit(pnd, pbtTx, szTx, szRx) if not "BLOCK" in rapdu: print("** Opss ** I'm expecting RESPONSE msg, but card sent to me: %s. len: %s" % (rapdu, len(rapdu))) sys.exit(1) print("REC: " + rapdu) r_decode = rapdu.decode('utf-8') blocks = re.search(r'\d+', r_decode) blocks = blocks.group() blocks = int(blocks) print("\nBLOCKS:%s" % blocks) UAFmsg = '\0' for block in range(0, blocks): print("receiving block --> %s" % block) pbtTx = DoorProtocol['DOOR_NEXT'] szTx = len(pbtTx) szRx = BLOCK_SIZE res, rapdu = cardTransmit(pnd, pbtTx, szTx, szRx) UAFmsg += rapdu UAFmsg = "".join(map(chr, UAFmsg)) UAFmsg = bytearry2json(UAFmsg) # FIDO Auth Request Message print("Forwarding card response to FIDO UAF Server: \n") UAFurl = fido_server['AUTH_REQUEST_MSG'] % (fido_server['SCHEME'], fido_server['HOSTNAME'], fido_server['PORT'], fido_server['AUTH_RESPONSE_ENDPOINT']) headers = { 'Accept': 'application/json', 'Content-Type': 'application/json'} r = requests.post(UAFurl, data=UAFmsg, headers=headers) response = json.loads(r.text) if response[0]["status"] == "SUCCESS": pbtTx = DoorProtocol['DOOR_GRANTED'] szTx = len(pbtTx) szRx = len(DoorProtocol['DOOR_BYE']) res, rapdu = cardTransmit(pnd, pbtTx, szTx, szRx) print("Access granted!"); # ligar led verde response = saml_request() print(etree.tostring(response)) else: pbtTx = DoorProtocol['DOOR_DENY'] szTx = len(pbtTx) szRx = len(DoorProtocol['DOOR_BYE']) res, rapdu = cardTransmit(pnd, pbtTx, szTx, szRx) print("Access denied!"); # ligar led vermelho if (rapdu == DoorProtocol['DOOR_BYE']): print("bye!") else: print(":-(") return
nmMifare = nfc.modulation() nmMifare.nmt = nfc.NMT_ISO14443A nmMifare.nbr = nfc.NBR_106 nt = nfc.target() admin_switch = False while True: if not admin_switch: GPIO.cleanup() # Wait for tag ret = nfc.initiator_select_passive_target(pnd, nmMifare, 0, 0, nt) # Convert raw uid to hex, then trim to length (2*nt.nti.nai.szUidLen) tag = ''.join(format(x, '02x') for x in nt.nti.nai.abtUid)[:2*nt.nti.nai.szUidLen] output('Found tag with UID of ' + tag) cursor = conn.cursor() cursor.execute("SELECT UID, ADMIN, ACCESS_ROOM1, INSIDE_ROOM1 FROM MAIN WHERE UID = ?", (tag,)) details = cursor.fetchone() if details is None: output("Tag is unrecognised") light_on('red') if admin_switch: conn.execute("INSERT INTO MAIN(UID, ADMIN, ACCESS_ROOM1, INSIDE_ROOM1) VALUES(?, 0, 1, 0)", (tag,)) conn.commit() admin_switch = False
print('ERROR: Unable to open NFC device.') sys.exit(1) if nfc.initiator_init(pnd) < 0: nfc.perror(pnd, "nfc_initiator_init") print('ERROR: Unable to init NFC device.') sys.exit(1) print('NFC reader: %s opened' % nfc.device_get_name(pnd)) nmMifare = nfc.modulation() nmMifare.nmt = nfc.NMT_ISO14443A nmMifare.nbr = nfc.NBR_106 nt = nfc.target() ret = nfc.initiator_select_passive_target(pnd, nmMifare, 0, 0, nt) print('The following (NFC) ISO14443A tag was found:') print(' ATQA (SENS_RES): ', end='') nfc.print_hex(nt.nti.nai.abtAtqa, 2) id = 1 if nt.nti.nai.abtUid[0] == 8: id = 3 print(' UID (NFCID%d): ' % id, end='') nfc.print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen) print(' SAK (SEL_RES): ', end='') print(nt.nti.nai.btSak) if nt.nti.nai.szAtsLen: print(' ATS (ATR): ', end='') nfc.print_hex(nt.nti.nai.abtAts, nt.nti.nai.szAtsLen)