Esempio n. 1
0
def activate(clf, target):
    log.debug("check if authenticate command is available")
    try:
        rsp = clf.exchange(b'\x1A\x00', timeout=0.01)
        if clf.sense(target) is None: return
        if rsp.startswith(b"\xAF"):
            return MifareUltralightC(clf, target)
    except nfc.clf.TimeoutError:
        if clf.sense(target) is None: return
    except nfc.clf.CommunicationError as error:
        log.debug(repr(error))
        return

    log.debug("check if version command is available")
    try:
        rsp = bytes(clf.exchange(b'\x60', timeout=0.01))
        if rsp in VERSION_MAP:
            return VERSION_MAP[rsp](clf, target)
        if rsp == b"\x00":
            if clf.sense(target) is None: return None
            else: return NTAG203(clf, target)
        log.debug("no match for version %s", hexlify(rsp).upper())
        return
    except nfc.clf.TimeoutError:
        if clf.sense(target) is None: return
    except nfc.clf.CommunicationError as error:
        log.debug(repr(error))
        return

    return MifareUltralight(clf, target)
Esempio n. 2
0
def activate(clf, target):
    log.debug("check if authenticate command is available")
    try:
        rsp = clf.exchange(b'\x1A\x00', timeout=0.01)
        if clf.sense(target) is None:
            return
        if rsp.startswith(b"\xAF"):
            return MifareUltralightC(clf, target)
    except nfc.clf.TimeoutError:
        if clf.sense(target) is None:
            return
    except nfc.clf.CommunicationError as error:
        log.debug(repr(error))
        return

    log.debug("check if version command is available")
    try:
        rsp = bytes(clf.exchange(b'\x60', timeout=0.01))
        if rsp in VERSION_MAP:
            return VERSION_MAP[rsp](clf, target)
        if rsp == b"\x00":
            if clf.sense(target) is None:
                return None
            else:
                return NTAG203(clf, target)
        log.debug("no match for version %s", hexlify(rsp).upper())
        return
    except nfc.clf.TimeoutError:
        if clf.sense(target) is None:
            return
    except nfc.clf.CommunicationError as error:
        log.debug(repr(error))
        return

    return MifareUltralight(clf, target)
Esempio n. 3
0
 def test_sense_with_unknown_technology(self, clf):
     valid_target = nfc.clf.RemoteTarget('106A')
     wrong_target = nfc.clf.RemoteTarget('106X')
     with pytest.raises(nfc.clf.UnsupportedTargetError) as excinfo:
         clf.sense(wrong_target)
     assert str(excinfo.value) == "unknown technology type in '106X'"
     assert clf.sense(wrong_target, valid_target) is None
     clf.device.sense_tta.assert_called_once_with(valid_target)
Esempio n. 4
0
 def test_sense_with_unknown_technology(self, clf):
     valid_target = nfc.clf.RemoteTarget('106A')
     wrong_target = nfc.clf.RemoteTarget('106X')
     with pytest.raises(nfc.clf.UnsupportedTargetError) as excinfo:
         clf.sense(wrong_target)
     assert str(excinfo.value) == "unknown technology type in '106X'"
     assert clf.sense(wrong_target, valid_target) is None
     clf.device.sense_tta.assert_called_once_with(valid_target)
Esempio n. 5
0
 def test_sense_dep_invalid_atr_req(self, clf):
     target = nfc.clf.RemoteTarget('106A')
     target.atr_req = bytearray(15)
     with pytest.raises(ValueError) as excinfo:
         clf.sense(target)
     assert str(excinfo.value) == "minimum atr_req length is 16 byte"
     target.atr_req = bytearray(65)
     with pytest.raises(ValueError) as excinfo:
         clf.sense(target)
     assert str(excinfo.value) == "maximum atr_req length is 64 byte"
Esempio n. 6
0
 def test_sense_dep_invalid_atr_req(self, clf):
     target = nfc.clf.RemoteTarget('106A')
     target.atr_req = bytearray(15)
     with pytest.raises(ValueError) as excinfo:
         clf.sense(target)
     assert str(excinfo.value) == "minimum atr_req length is 16 byte"
     target.atr_req = bytearray(65)
     with pytest.raises(ValueError) as excinfo:
         clf.sense(target)
     assert str(excinfo.value) == "maximum atr_req length is 64 byte"
Esempio n. 7
0
def activate(clf, target):
    try:
        log.debug("check if authenticate command is available")
        rsp = clf.exchange('\x1A\x00', timeout=0.01)
        if clf.sense(target) is None: return None
        if rsp.startswith("\xAF"):
            return MifareUltralightC(clf, target)
        if rsp == "\x00":
            return NTAG203(clf, target)
    except nfc.clf.TimeoutError:
        log.debug("nope, authenticate command is not supported")
        if clf.sense(target) is None: return None
    except nfc.clf.CommunicationError as error:
        log.debug(repr(error))
        return
    
    try:
        log.debug("check if version command is available")
        version = clf.exchange('\x60', timeout=0.01)
    except nfc.clf.TimeoutError:
        log.debug("nope, version command is not supported")
        if clf.sense(target) is None: return None
        version = None
    except nfc.clf.CommunicationError as error:
        log.debug(repr(error))
        return
    
    if version is not None:
        log.debug("version = " + ' '.join(["%02X" % x for x in version]))
        if version == "\x00\x04\x03\x01\x01\x00\x0B\x03":
            return MifareUltralightEV1(clf, target, "MF0UL11", 16)
        if version == "\x00\x04\x03\x02\x01\x00\x0B\x03":
            return MifareUltralightEV1(clf, target, "MF0ULH11", 16)
        if version == "\x00\x04\x03\x01\x01\x00\x0E\x03":
            return MifareUltralightEV1(clf, target, "MF0UL21", 37)
        if version == "\x00\x04\x03\x02\x01\x00\x0E\x03":
            return MifareUltralightEV1(clf, target, "MF0ULH21", 37)
        if version == "\x00\x04\x04\x01\x01\x00\x0B\x03":
            return NTAG210(clf, target)
        if version == "\x00\x04\x04\x01\x01\x00\x0E\x03":
            return NTAG212(clf, target)
        if version == "\x00\x04\x04\x02\x01\x00\x0F\x03":
            return NTAG213(clf, target)
        if version == "\x00\x04\x04\x02\x01\x00\x11\x03":
            return NTAG215(clf, target)
        if version == "\x00\x04\x04\x02\x01\x00\x13\x03":
            return NTAG216(clf, target)
        log.debug("no match for this version number")
        return
    else:
        return MifareUltralight(clf, target)
Esempio n. 8
0
def activate(clf, target):
    try:
        log.debug("check if authenticate command is available")
        rsp = clf.exchange('\x1A\x00', timeout=0.01)
        if clf.sense(target) is None: return None
        if rsp.startswith("\xAF"):
            return MifareUltralightC(clf, target)
        if rsp == "\x00":
            return NTAG203(clf, target)
    except nfc.clf.TimeoutError:
        log.debug("nope, authenticate command is not supported")
        if clf.sense(target) is None: return None
    except nfc.clf.CommunicationError as error:
        log.debug(repr(error))
        return

    try:
        log.debug("check if version command is available")
        version = clf.exchange('\x60', timeout=0.01)
    except nfc.clf.TimeoutError:
        log.debug("nope, version command is not supported")
        if clf.sense(target) is None: return None
        version = None
    except nfc.clf.CommunicationError as error:
        log.debug(repr(error))
        return

    if version is not None:
        log.debug("version = " + ' '.join(["%02X" % x for x in version]))
        if version == "\x00\x04\x03\x01\x01\x00\x0B\x03":
            return MifareUltralightEV1(clf, target, "MF0UL11", 16)
        if version == "\x00\x04\x03\x02\x01\x00\x0B\x03":
            return MifareUltralightEV1(clf, target, "MF0ULH11", 16)
        if version == "\x00\x04\x03\x01\x01\x00\x0E\x03":
            return MifareUltralightEV1(clf, target, "MF0UL21", 37)
        if version == "\x00\x04\x03\x02\x01\x00\x0E\x03":
            return MifareUltralightEV1(clf, target, "MF0ULH21", 37)
        if version == "\x00\x04\x04\x01\x01\x00\x0B\x03":
            return NTAG210(clf, target)
        if version == "\x00\x04\x04\x01\x01\x00\x0E\x03":
            return NTAG212(clf, target)
        if version == "\x00\x04\x04\x02\x01\x00\x0F\x03":
            return NTAG213(clf, target)
        if version == "\x00\x04\x04\x02\x01\x00\x11\x03":
            return NTAG215(clf, target)
        if version == "\x00\x04\x04\x02\x01\x00\x13\x03":
            return NTAG216(clf, target)
        log.debug("no match for this version number")
        return
    else:
        return MifareUltralight(clf, target)
Esempio n. 9
0
 def test_sense_ttb_found_tt4_target(self, clf):
     req_target = nfc.clf.RemoteTarget('106B')
     res_target = nfc.clf.RemoteTarget('106B')
     res_target.sensb_res = HEX('50E8253EEC00000011008185')
     clf.device.sense_ttb.return_value = res_target
     res_target = clf.sense(req_target)
     assert isinstance(res_target, nfc.clf.RemoteTarget)
     clf.device.sense_ttb.assert_called_once_with(req_target)
Esempio n. 10
0
 def test_sense_ttb_found_tt4_target(self, clf):
     req_target = nfc.clf.RemoteTarget('106B')
     res_target = nfc.clf.RemoteTarget('106B')
     res_target.sensb_res = HEX('50E8253EEC00000011008185')
     clf.device.sense_ttb.return_value = res_target
     res_target = clf.sense(req_target)
     assert isinstance(res_target, nfc.clf.RemoteTarget)
     clf.device.sense_ttb.assert_called_once_with(req_target)
Esempio n. 11
0
	def read(self,web):
        # Seleccion de la interfaz con la que se leera la tarjeta
		clf = nfc.ContactlessFrontend("usb")                     
        # Seleccion del objetivo de la lectura
		target1 = nfc.clf.RemoteTarget("106A")          
		str2 = str(clf.sense(target1, iterations=100, interval=0.5))[13:21]
        # Request al servidor web de los datos del alumno
		response = urllib2.urlopen(web+str2+'&lugar=LabPBE')
	   	clf.close()
		return (response, str2)
Esempio n. 12
0
 def test_sense_tta_found_error_target(self, clf, sens, sel, sdd, rid):
     req_target = nfc.clf.RemoteTarget('106A')
     res_target = nfc.clf.RemoteTarget('106A')
     res_target.sens_res = HEX(sens)
     res_target.sel_res = HEX(sel)
     res_target.sdd_res = HEX(sdd)
     res_target.rid_res = HEX(rid)
     clf.device.sense_tta.return_value = res_target
     assert clf.sense(req_target) is None
     clf.device.sense_tta.assert_called_once_with(req_target)
Esempio n. 13
0
 def test_sense_tta_found_error_target(self, clf, sens, sel, sdd, rid):
     req_target = nfc.clf.RemoteTarget('106A')
     res_target = nfc.clf.RemoteTarget('106A')
     res_target.sens_res = HEX(sens)
     res_target.sel_res = HEX(sel)
     res_target.sdd_res = HEX(sdd)
     res_target.rid_res = HEX(rid)
     clf.device.sense_tta.return_value = res_target
     assert clf.sense(req_target) is None
     clf.device.sense_tta.assert_called_once_with(req_target)
Esempio n. 14
0
 def test_sense_tta_found_valid_target(self, clf, sens, sel, sdd, rid):
     req_target = nfc.clf.RemoteTarget('106A')
     res_target = nfc.clf.RemoteTarget('106A')
     res_target.sens_res = HEX(sens)
     res_target.sel_res = HEX(sel)
     res_target.sdd_res = HEX(sdd)
     res_target.rid_res = HEX(rid)
     clf.device.sense_tta.return_value = res_target
     res_target = clf.sense(req_target)
     assert isinstance(res_target, nfc.clf.RemoteTarget)
     clf.device.sense_tta.assert_called_once_with(req_target)
Esempio n. 15
0
 def test_sense_tta_found_valid_target(self, clf, sens, sel, sdd, rid):
     req_target = nfc.clf.RemoteTarget('106A')
     res_target = nfc.clf.RemoteTarget('106A')
     res_target.sens_res = HEX(sens)
     res_target.sel_res = HEX(sel)
     res_target.sdd_res = HEX(sdd)
     res_target.rid_res = HEX(rid)
     clf.device.sense_tta.return_value = res_target
     res_target = clf.sense(req_target)
     assert isinstance(res_target, nfc.clf.RemoteTarget)
     clf.device.sense_tta.assert_called_once_with(req_target)
Esempio n. 16
0
def activate(clf, target):
    # Type 2 Tags go mute when they receive an unsupported command. It
    # is then necessary to sense again and by copying sdd_res to
    # sel_req we ensure that only the same tag will be found.
    target.sel_req = target.sdd_res[:]
    if target.sdd_res[0] == 0x04: # NXP
        import nfc.tag.tt2_nxp
        tag = nfc.tag.tt2_nxp.activate(clf, target)
        if tag is not None: return tag
        # make sure the tag is still alive
        target = clf.sense(target)
    if target:
        return Type2Tag(clf, target)
Esempio n. 17
0
def activate(clf, target):
    # Type 2 Tags go mute when they receive an unsupported command. It
    # is then necessary to sense again and by copying sdd_res to
    # sel_req we ensure that only the same tag will be found.
    target.sel_req = target.sdd_res[:]
    if target.sdd_res[0] == 0x04:  # NXP
        import nfc.tag.tt2_nxp
        tag = nfc.tag.tt2_nxp.activate(clf, target)
        if tag is not None: return tag
        # make sure the tag is still alive
        target = clf.sense(target)
    if target:
        return Type2Tag(clf, target)
Esempio n. 18
0
 def test_sense_without_targets(self, clf):
     assert clf.sense() is None
Esempio n. 19
0
 def test_sense_without_targets(self, clf):
     assert clf.sense() is None
Esempio n. 20
0
	def read(self,web):
		clf = nfc.ContactlessFrontend("usb")                                       
		target1 = nfc.clf.RemoteTarget("106A")          
		str = str(clf.sense(target1, iterations=20, interval=0.4))
		str2 = str[13:21]                                                         
		response = urllib2.urlopen(web+str2)
Esempio n. 21
0
def main(args):
    if args.debug:
        loglevel = logging.DEBUG - (1 if args.verbose else 0)
        logging.getLogger("nfc.clf").setLevel(loglevel)
        logging.getLogger().setLevel(loglevel)

    if args.atr and len(args.atr) < 16:
        print("--atr must supply at least 16 byte")

    clf = nfc.ContactlessFrontend()
    if clf.open(args.device):
        targets = list()
        for target in args.targets:
            target_pattern_match = target_pattern.match(target)
            if not target_pattern_match:
                logging.error("invalid target pattern {!r}".format(target))
            else:
                brty, attributes = target_pattern_match.groups()
                target = nfc.clf.RemoteTarget(brty)
                if attributes:
                    for attr in map(str.strip, attributes.split(' ')):
                        name, value = map(str.strip, attr.split('='))
                        value = bytearray.fromhex(value)
                        setattr(target, name, value)
                logging.debug("add to target list: %s", target)
                targets.append(target)

        try:
            while True:
                target = clf.sense(*targets,
                                   iterations=args.iterations,
                                   interval=args.interval)
                print("{0} {1}".format(time.strftime("%X"), target))

                if (target and args.atr and target.brty in brty_for_dep and (
                    (target.sel_res and target.sel_res[0] & 0x40) or
                    (target.sensf_res and target.sensf_res[1:3] == '\1\xFE'))):
                    atr_req = args.atr[:]
                    if atr_req[0] == 0xFF: atr_req[0] = 0xD4
                    for i in (1, 12, 13, 14):
                        if atr_req[i] == 0xFF: atr_req[i] = 0x00
                    if target.sensf_res:
                        for i in range(2, 10):
                            if atr_req[i] == 0xFF:
                                atr_req[i] = target.sensf_res[i - 1]
                    if atr_req[15] == 0xFF:
                        atr_req[15] = 0x30 | (len(atr_req) > 16) << 1
                    try:
                        data = chr(len(atr_req) + 1) + atr_req
                        if target.brty == "106A": data.insert(0, 0xF0)
                        data = clf.exchange(data, 1.0)
                        if target.brty == "106A": assert data.pop(0) == 0xF0
                        assert len(data) == data.pop(0)
                        target.atr_res = data
                        target.atr_req = atr_req
                    except nfc.clf.CommunicationError as error:
                        print(repr(error) + " for NFC-DEP ATR_REQ")
                    except AssertionError:
                        print("invalid ATR_RES: %r" % str(data.encode("hex")))

                if target and target.atr_res:
                    did = target.atr_req[12]
                    psl = "06D404%02x1203" % did  # PSL_REQ
                    rls = ("04D40A%02x" % did) if did else "03D40A"
                    if target.brty == "106A": psl = "F0" + psl
                    psl, rls = map(bytearray.fromhex, (psl, rls))
                    try:
                        clf.exchange(psl, 1.0)
                    except nfc.clf.CommunicationError as error:
                        print(repr(error) + " for NFC-DEP PSL_REQ")
                    else:
                        target.brty = "424F"
                        try:
                            clf.exchange(rls, 1.0)
                        except nfc.clf.CommunicationError as error:
                            print(repr(error) + " for NFC-DEP RLS_REQ")

                if (target and target.sensf_res
                        and target.sensf_res[1:3] != '\x01\xFE'):
                    request_system_code = "\x0A\x0C" + target.sensf_res[1:9]
                    try:
                        clf.exchange(request_system_code, timeout=1.0)
                    except nfc.clf.CommunicationError as error:
                        print(repr(error) + " for Request System Code Command")

                if not args.repeat: break
                time.sleep(args.waittime)
        except IOError as error:
            if error.errno == errno.EIO:
                print("lost connection to local device")
            else:
                print(error)
        except nfc.clf.UnsupportedTargetError as error:
            print error
        except KeyboardInterrupt:
            pass
        finally:
            clf.close()
Esempio n. 22
0
 def test_sense_with_invalid_targets(self, clf):
     with pytest.raises(ValueError) as excinfo:
         clf.sense(nfc.clf.RemoteTarget('106A'), nfc.clf.LocalTarget())
     assert str(excinfo.value).startswith("invalid target argument type")
Esempio n. 23
0
 def test_sense_without_device(self, clf):
     clf.device = None
     with pytest.raises(IOError) as excinfo:
         clf.sense(nfc.clf.RemoteTarget('106A'))
     assert excinfo.value.errno == errno.ENODEV
Esempio n. 24
0
 def test_sense_tta_invalid_sel_req(self, clf):
     target = nfc.clf.RemoteTarget('106A')
     target.sel_req = HEX('0011')
     with pytest.raises(ValueError) as excinfo:
         clf.sense(target)
     assert str(excinfo.value) == "sel_req must be 4, 7, or 10 byte"
Esempio n. 25
0
 def test_sense_with_communication_error(self, clf):
     clf.device.sense_tta.side_effect = nfc.clf.CommunicationError
     target = nfc.clf.RemoteTarget('106A')
     assert clf.sense(target) is None
     clf.device.sense_tta.assert_called_once_with(target)
Esempio n. 26
0
 def test_sense_tta_invalid_sel_req(self, clf):
     target = nfc.clf.RemoteTarget('106A')
     target.sel_req = HEX('0011')
     with pytest.raises(ValueError) as excinfo:
         clf.sense(target)
     assert str(excinfo.value) == "sel_req must be 4, 7, or 10 byte"
Esempio n. 27
0
def main(args):
    if args.debug:
        loglevel = logging.DEBUG - (1 if args.verbose else 0)
        logging.getLogger("nfc.clf").setLevel(loglevel)

    if args.atr and len(args.atr) < 16:
        print("--atr must supply at least 16 byte")

    clf = nfc.ContactlessFrontend()
    if clf.open(args.device):
        targets = list()
        for target in args.targets:
            target_pattern_match = target_pattern.match(target)
            if not target_pattern_match:
                logging.error("invalid target pattern {!r}".format(target))
            else:
                brty, attributes = target_pattern_match.groups()
                target = nfc.clf.RemoteTarget(brty)
                if attributes:
                    for attr in map(str.strip, attributes.split(' ')):
                        name, value = map(str.strip, attr.split('='))
                        value = bytearray.fromhex(value)
                        setattr(target, name, value)
                targets.append(target)

        try:
            while True:
                target = clf.sense(*targets, iterations=args.iterations,
                                   interval=args.interval)
                print("{0} {1}".format(time.strftime("%X"), target))
                
                if (target and args.atr and target.brty in brty_for_dep and
                    ((target.sel_res and target.sel_res[0] & 0x40) or
                     (target.sensf_res and target.sensf_res[1:3]=='\1\xFE'))):
                    atr_req = args.atr[:]
                    if atr_req[0] == 0xFF: atr_req[0] = 0xD4
                    for i in (1, 12, 13, 14):
                        if atr_req[i] == 0xFF: atr_req[i] = 0x00
                    if target.sensf_res:
                        for i in range(2, 10):
                            if atr_req[i] == 0xFF:
                                atr_req[i] = target.sensf_res[i-1]
                    if atr_req[15] == 0xFF:
                        atr_req[15] = 0x30 | (len(atr_req)>16)<<1
                    try:
                        data = chr(len(atr_req)+1) + atr_req
                        if target.brty == "106A": data.insert(0, 0xF0)
                        data = clf.exchange(data, 1.0)
                        if target.brty == "106A": assert data.pop(0) == 0xF0
                        assert len(data) == data.pop(0)
                        target.atr_res = data
                        target.atr_req = atr_req
                    except nfc.clf.CommunicationError as error:
                        print(repr(error) + " for NFC-DEP ATR_REQ")
                    except AssertionError:
                        print("invalid ATR_RES: %r" % str(data.encode("hex")))
                
                if target and target.atr_res:
                    did = target.atr_req[12]
                    psl = "06D404%02x1203" % did # PSL_REQ
                    rls = ("04D40A%02x"%did) if did else "03D40A"
                    if target.brty == "106A": psl = "F0" + psl
                    psl, rls = map(bytearray.fromhex, (psl, rls))
                    try: clf.exchange(psl, 1.0)
                    except nfc.clf.CommunicationError as error:
                        print(repr(error) + " for NFC-DEP PSL_REQ")
                    else:
                        target.brty = "424F"
                        try: clf.exchange(rls, 1.0)
                        except nfc.clf.CommunicationError as error:
                            print(repr(error) + " for NFC-DEP RLS_REQ")

                if (target and target.sensf_res and
                    target.sensf_res[1:3] != '\x01\xFE'):
                    request_system_code = "\x0A\x0C"+target.sensf_res[1:9]
                    try: clf.exchange(request_system_code, timeout=1.0)
                    except nfc.clf.CommunicationError as error:
                        print(repr(error) + " for Request System Code Command")
                
                if not args.repeat: break
                time.sleep(args.waittime)
        except IOError as error:
            if error.errno == errno.EIO:
                print("lost connection to local device")
            else: print(error)
        except nfc.clf.UnsupportedTargetError as error:
            print error
        except KeyboardInterrupt:
            pass
        finally:
            clf.close()
Esempio n. 28
0
 def test_sense_with_communication_error(self, clf):
     clf.device.sense_tta.side_effect = nfc.clf.CommunicationError
     target = nfc.clf.RemoteTarget('106A')
     assert clf.sense(target) is None
     clf.device.sense_tta.assert_called_once_with(target)
Esempio n. 29
0
 def test_sense_without_device(self, clf):
     clf.device = None
     with pytest.raises(IOError) as excinfo:
         clf.sense(nfc.clf.RemoteTarget('106A'))
     assert excinfo.value.errno == errno.ENODEV
Esempio n. 30
0
 def test_sense_with_invalid_targets(self, clf):
     with pytest.raises(ValueError) as excinfo:
         clf.sense(nfc.clf.RemoteTarget('106A'), nfc.clf.LocalTarget())
     assert str(excinfo.value).startswith("invalid target argument type")