Protect Tag

Asked by Rafael on 2016-09-22

Hello! I followed your steps to put a password on my NTAG213, but it returns this error and I blocked my NTAG213.7

/usr/bin/python2.7 /home/rafael/PycharmProjects/NFC/probando.py
Traceback (most recent call last):
  File "/home/rafael/PycharmProjects/NFC/probando.py", line 11, in <module>
    clf.connect(rdwr={"on-connect": connected})
  File "/home/rafael/PycharmProjects/NFC/nfc/clf/__init__.py", line 542, in connect
    result = self._rdwr_connect(rdwr_options, terminate)
  File "/home/rafael/PycharmProjects/NFC/nfc/clf/__init__.py", line 568, in _rdwr_connect
    if options['on-connect'](tag):
  File "/home/rafael/PycharmProjects/NFC/probando.py", line 4, in connected
    clean = tag.protect(b"123456")
  File "/home/rafael/PycharmProjects/NFC/nfc/tag/tt2_nxp.py", line 364, in protect
    return super(NTAG21x, self).protect(*args)
  File "/home/rafael/PycharmProjects/NFC/nfc/tag/tt2.py", line 381, in protect
    password, read_protect, protect_from)
  File "/home/rafael/PycharmProjects/NFC/nfc/tag/__init__.py", line 320, in protect
    status = self._protect(password, read_protect, protect_from)
  File "/home/rafael/PycharmProjects/NFC/nfc/tag/tt2_nxp.py", line 371, in _protect
    return self._protect_with_password(*args)
  File "/home/rafael/PycharmProjects/NFC/nfc/tag/tt2_nxp.py", line 424, in _protect_with_password
    self.target = self.clf.sense(self.target)
AttributeError: can't set attribute

This is my program:

import nfc

def connected(tag):
    clean = tag.protect(b"123456")
    if clean:
        print ("Clean correct")
    else:
        print ("clean incorrect")

clf = nfc.ContactlessFrontend("usb")
clf.connect(rdwr={"on-connect": connected})
clf.close()

Question information

Language:
English Edit question
Status:
Solved
For:
nfcpy Edit question
Assignee:
No assignee Edit question
Solved by:
Stephen Tiedemann
Solved:
2016-09-23
Last query:
2016-09-23
Last reply:
2016-09-23
Rafael (hibara94) said : #1

And wing when you delete the password gives me this error:

/usr/bin/python2.7 /home/rafael/PycharmProjects/NFC/probando.py
Traceback (most recent call last):
  File "/home/rafael/PycharmProjects/NFC/probando.py", line 8, in <module>
    clf.connect(rdwr={"on-connect": connected})
  File "/home/rafael/PycharmProjects/NFC/nfc/clf/__init__.py", line 542, in connect
    result = self._rdwr_connect(rdwr_options, terminate)
  File "/home/rafael/PycharmProjects/NFC/nfc/clf/__init__.py", line 568, in _rdwr_connect
    if options['on-connect'](tag):
  File "/home/rafael/PycharmProjects/NFC/probando.py", line 4, in connected
    tag.authenticate("123456")
  File "/home/rafael/PycharmProjects/NFC/nfc/tag/tt2_nxp.py", line 452, in authenticate
    return super(NTAG21x, self).authenticate(password)
  File "/home/rafael/PycharmProjects/NFC/nfc/tag/__init__.py", line 342, in authenticate
    self._authenticated = self._authenticate(password)
  File "/home/rafael/PycharmProjects/NFC/nfc/tag/tt2_nxp.py", line 462, in _authenticate
    rsp = self.transceive(bytearray("\x1B") + key[0:4], rlen=2)
TypeError: transceive() got an unexpected keyword argument 'rlen'

This is my other program:

import nfc

def connected(tag):
    tag.authenticate(b"123456")
    tag.protect("")

clf = nfc.ContactlessFrontend("usb")
clf.connect(rdwr={"on-connect": connected})
clf.close()

This looks like a bug that was fixed with release 0.11.1 (https://github.com/nfcpy/nfcpy/releases/tag/v0.11.1) and should thus as well be fixed in the version on PyPI. It looks like you're working with an earlier version.

Rafael (hibara94) said : #3

OK thank you very much. But once you assign a password to my NTAG213, it is read-only, as it could do to write or format your content??

thanks !!

Rafael (hibara94) said : #4

And I can remove the password so you can be writeable. Thanks

I hope you remember the password you've used as it has been correctly set and the failure was only afterwards. You should be able to use that password to authenticate and then change to whatever else.

Rafael (hibara94) said : #6

Yes, but my question is how to remove the password, because if I set tag.protect ( "") establishes me as an empty string password, but not me delete the password. How could it be?

Thanks.

Sorry, my fault. Protect with an empty string sets the default password (which is b"\xFF\xFF\xFF\xFF\x00\x00"). To completely remove protection use the protect_from argument with value 255:

tag.protect("", protect_from=255)

Rafael (hibara94) said : #8

Ok, but still does not completely remove the password, I show my program:

import nfc
import nfc.ndef

def connected(tag):
    correct = tag.authenticate("000000")
    if correct:
        print "correct"
        correct2 = tag.protect("", protect_from=255)
        if correct2:
            print "correct 2"
        else:
            print "incorrect 2"
    else:
        print "incorrect"

clf = nfc.ContactlessFrontend("usb")
clf.connect(rdwr={"on-connect": connected})
clf.close()

The results:

correct
correct 2

Process finished with exit code 0

Until here all right, but is this happening now:

My program:

import nfc
import nfc.ndef

def connected(tag):
        print(" readable = %s" % ("no", "yes")[tag.ndef.is_readable])
        print(" writeable = %s" % ("no", "yes")[tag.ndef.is_writeable])
        print(" capacity = %d byte" % tag.ndef.capacity)
        print(" message = %d byte" % tag.ndef.length)
        print tag.ndef.message.pretty()

clf = nfc.ContactlessFrontend("usb")
clf.connect(rdwr={"on-connect": connected})
clf.close()

The results:

  readable = yes
  writeable = no
  capacity = 142 byte
  message = 13 byte
record 1
  type = 'urn:nfc:wkt:U'
  name = ''
  data = '\x00probando'

We can see that is still ¬°¬°¬°writable = no!!! As I can do to make it writable?

Thanks

This should do the trick:

tag.authenticate("")
tag.format()

Rafael (hibara94) said : #10

No, if I format TAG, I delete the content, but writeable = no. I want to be writeable = yes

 readable = yes
 writeable = no
 capacity = 142 byte
 message = 0 byte
record 1
  type = ''
  name = ''
  data = ''

Uups, I forgot that Type 3 Tags store the write protection flags in one-time-programmable memory. So it can't be reverted. The user memory is indeed now unprotected and can be written but the flag is set for eternity. You'll have to authenticate before writing new ndef.

Rafael (hibara94) said : #12

Thanks Stephen Tiedemann, that solved my question.