nfcpy / ndeflib

Python package for parsing and generating NFC Data Exchange Format messages.
ISC License
63 stars 12 forks source link

parse wifi settings ndef record #2

Closed prattom closed 5 years ago

prattom commented 7 years ago

How can I parse following wifi ndef record? I old library we could do it following manner data = bytearray("\x10J\x00\x01\x10\x10\x0e\x002\x10&\x00\x01\x01\x10E\x00\x04Wxyz\x10\x03\x00\x02\x00\x01\x10\x0f\x00\x02\x00\x01\x10'\x00\x0bAbcdefg1234\x10 \x00\x06\xff\xff\xff\xff\xff\xff") record = nfc.ndef.Record('application/vnd.wfa.wsc', '1', data) print nfc.ndef.WifiConfigRecord(record).pretty()

nehpetsde commented 7 years ago

This will be a few more steps:

>>> data = bytearray("\x10J\x00\x01\x10\x10\x0e\x002\x10&\x00\x01\x01\x10E\x00\x04Wxyz\x10\x03\x00\x02\x00\x01\x10\x0f\x00\x02\x00\x01\x10'\x00\x0bAbcdefg1234\x10 \x00\x06\xff\xff\xff\xff\xff\xff")
>>> octets = b''.join(ndef.message_encoder([ndef.Record('application/vnd.wfa.wsc', '1', data)]))
>>> record = list(ndef.message_decoder(octets))[0]
>>> str(record)
"NDEF Wifi Simple Config Record ID '1' Attributes 0x104A 0x100E"
>>> [name for name in record.attribute_names if name in record]
['credential', 'version-1']
>>> credential = record.get_attribute('credential')
>>> [name for name in credential.attribute_names if name in credential]
['network-index', 'authentication-type', 'encryption-type', 'ssid', 'network-key', 'mac-address']
>>> str(credential.get_attribute('ssid'))
'SSID 57:78:79:7A'

Usually you'll get octets from somewhere and not data, so it all starts with decoding. The pretty print is completely gone as it had become impossible to maintain and was subjective anyway.

prattom commented 7 years ago

Thanks Stephen for your quick response

jctlmm commented 7 years ago

Is there an "easier" (i.e. readable) way to parse these records? In the past I used to have something like this:

if record.type == 'application/vnd.wfa.wsc': settings['network-name'] = nfc.ndef.WifiConfigRecord(record).credential['network-name'] settings['network-key'] = nfc.ndef.WifiConfigRecord(record).credential['network-key'] settings['authentication'] = nfc.ndef.WifiConfigRecord(record).credential['authentication'] settings['encryption'] = nfc.ndef.WifiConfigRecord(record).credential['encryption']

nehpetsde commented 7 years ago

Here's the equivalent with ndeflib:

if record.type == 'application/vnd.wfa.wsc:
    credential = record.get_attribute('credential')
    settings['network-name'] = credential.get_attribute('ssid')
    settings['network-key'] = credential.get_attribute('network-key')
    settings['authentication'] = credential.get_attribute('authentication-type')
    settings['encryption'] = credential.get_attribute('encryption-type')
jctlmm commented 7 years ago

Thank you!