struttura base per leggere una carta tramite lettore nfc

This commit is contained in:
2026-02-19 19:09:35 +01:00
commit 04cd7309ec

81
read_nfc.py Normal file
View File

@@ -0,0 +1,81 @@
from smartcard.System import readers
from smartcard.util import toHexString
# Chiavi comuni da testare
KEYS = {
"FFFFFFFFFFFF": [0xFF]*6,
"A0A1A2A3A4A5": [0xA0,0xA1,0xA2,0xA3,0xA4,0xA5],
"D3F7D3F7D3F7": [0xD3,0xF7,0xD3,0xF7,0xD3,0xF7]
}
def authenticate(connection, block):
for key_name, key_value in KEYS.items():
# Carica chiave nello slot 0
load_key = [0xFF, 0x82, 0x00, 0x00, 0x06] + key_value
connection.transmit(load_key)
# Prova con Key A (0x60)
authA = [0xFF, 0x86, 0x00, 0x00, 0x05,
0x01, 0x00, block, 0x60, 0x00]
response, sw1, sw2 = connection.transmit(authA)
if sw1 == 0x90:
return key_name, "A"
# Prova con Key B (0x61)
authB = [0xFF, 0x86, 0x00, 0x00, 0x05,
0x01, 0x00, block, 0x61, 0x00]
response, sw1, sw2 = connection.transmit(authB)
if sw1 == 0x90:
return key_name, "B"
return None, None
def read_block(connection, block):
read = [0xFF, 0xB0, 0x00, block, 0x10]
data, sw1, sw2 = connection.transmit(read)
if sw1 == 0x90:
return data
return None
r = readers()
if not r:
print("Nessun lettore trovato")
exit()
connection = r[0].createConnection()
connection.connect()
# UID
uid_cmd = [0xFF, 0xCA, 0x00, 0x00, 0x00]
uid, sw1, sw2 = connection.transmit(uid_cmd)
print("UID:", toHexString(uid))
print("\n==============================")
TOTAL_BLOCKS = 64
BLOCKS_PER_SECTOR = 4
for sector in range(TOTAL_BLOCKS // BLOCKS_PER_SECTOR):
print(f"\n=== Settore {sector} ===")
base_block = sector * BLOCKS_PER_SECTOR
for i in range(BLOCKS_PER_SECTOR):
block = base_block + i
if i == 3:
print(f"Blocco {block} (Trailer) -> saltato")
continue
key_used, key_type = authenticate(connection, block)
if key_used:
data = read_block(connection, block)
print(f"Blocco {block} | Key {key_type}={key_used} | Data: {toHexString(data)}")
else:
print(f"Blocco {block} | Nessuna chiave valida")