-
Notifications
You must be signed in to change notification settings - Fork 0
/
nfc.py
74 lines (58 loc) · 2.11 KB
/
nfc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#! /usr/bin/env python
from smartcard.System import readers
from smartcard.CardConnectionObserver import ConsoleCardConnectionObserver
from smartcard.util import toBytes, toHexString
import logging
# Initialize logging.
logging.basicConfig(filename='nfc.log', level=logging.INFO)
# Define the APDUs used in this script
CMD_SERIAL_ID = toBytes("80 14 00 00 08")
CMD_CARD_ID = toBytes("80 14 04 00 06")
CMD_NFC_SCAN = toBytes("ff 00 00 00 06 D4 60 01 01 00 20")
GET_RESPONSE = toBytes("FF C0 00 00 FF")
debug = False
# get all the available readers
readers = readers()
print "Available readers:", readers
connections = list()
for reader in readers:
print "Initializing reader:", reader
connection = reader.createConnection()
connection.connect()
connections.append(connection)
if debug:
observer = ConsoleCardConnectionObserver()
connection.addObserver(observer)
for connection in connections:
print connection
# Get the reader's serial id
data, sw1, sw2 = connection.transmit(CMD_SERIAL_ID)
serialId = toHexString(data)
print "Reader's serial id: {}".format(serialId)
logging.info("Reader serial id: %s", serialId)
logging.info("APDU Command result: %02X %02X", sw1, sw2)
# Get the reader's card id
data, sw1, sw2 = connection.transmit(CMD_CARD_ID)
cardId = toHexString(data)
print "Reader's card id: {}".format(cardId)
logging.info("Reader card id: %s", cardId)
logging.info("APDU Command result: %02X %02X", sw1, sw2)
print connections
old_tag = []
while True:
for connection in connections:
data, sw1, sw2 = connection.transmit(CMD_NFC_SCAN)
if debug:
print data
print "Command: %02X %02X" % (sw1, sw2)
if sw2 == 5:
old_tag = []
continue
GET_RESPONSE[4] = sw2
tag, sw1, sw2 = connection.transmit(GET_RESPONSE)
if debug:
print tag
print "Response: %02X %02X" % (sw1, sw2)
if tag != old_tag:
old_tag = tag
print toHexString(tag)