From 899f0fecb83e079c5aa69d9a0b34396d650fd0a0 Mon Sep 17 00:00:00 2001 From: scourge411 Date: Fri, 2 Jan 2026 22:19:33 -0700 Subject: [PATCH 1/2] add python script to dump tags using libnfc --- libnfc/libnfc_dump.py | 133 +++++++++++++++++++++++++++++++++++ libnfc/requirements.txt | 6 ++ libnfc/requirements.txt.orig | 6 ++ 3 files changed, 145 insertions(+) create mode 100755 libnfc/libnfc_dump.py create mode 100644 libnfc/requirements.txt create mode 100644 libnfc/requirements.txt.orig diff --git a/libnfc/libnfc_dump.py b/libnfc/libnfc_dump.py new file mode 100755 index 0000000..4b16fe7 --- /dev/null +++ b/libnfc/libnfc_dump.py @@ -0,0 +1,133 @@ +#!/usr/bin/env python3 +import argparse +import pynfc.nfc as nfcmod + +from pynfc import Nfc, TimeoutException +from Crypto.Protocol.KDF import HKDF +from Crypto.Hash import SHA256 + +SALT = bytes([0x9a,0x75,0x9c,0xf2,0xc4,0xf7,0xca,0xff,0x22,0x2c,0xb9,0x76,0x9b,0x41,0xbc,0x96]) + +SECNUM = 16 +BPS = 4 + +dump = {} +dumped_ids = [] +warned_ids = [] + +def generate_keys(uid): + keys_a=HKDF(uid, 6, SALT, SHA256, 16, context=b"RFID-A\0") + keys_b=HKDF(uid, 6, SALT, SHA256, 16, context=b"RFID-B\0") + + return {'A':keys_a,'B':keys_b} + + +def build_auth_tag(sector, keys): + tag_struct = nfcmod.mifare_classic_tag() + + last_block = (sector + 1) * BPS - 1 + + tag_struct.amb[last_block].mbt.abtKeyA = (nfcmod.uint8_t * 6)(*keys['A'][sector]) + tag_struct.amb[last_block].mbt.abtKeyB = (nfcmod.uint8_t * 6)(*keys['B'][sector]) + + return tag_struct + +def read_tag(tag, sector, keys): + + data = bytearray() + + print(f"\t--- Reading sector {sector} ---") + first_block = sector * BPS + last_block = first_block + BPS - 1 + + authenticated = False + + try: + auth_tag = build_auth_tag(sector, keys) + key = auth_tag.amb[last_block].mbt.abtKeyA + if tag.auth(auth_tag,sector,True): + # print(f"Authenticated sector {sector} with Key A: {list(map(hex,key[:6]))} ") + authenticated = True + except Exception as e: + pass + + if not authenticated: + print("Authentication failed for this sector") + return data + + for block in range(first_block, first_block + BPS): + try: + buf = (nfcmod.uint8_t* 16)() + ret = nfcmod.mifare_classic_read(tag.target, block, buf) + + if ret != 0: + raise RuntimeError("read failed") + block_data = bytearray(buf) + + #print(f"Block {block:02d}: {block_data}") + + if block == last_block: + block_data[0:6]=key[:6] + block_data[-6:]=auth_tag.amb[last_block].mbt.abtKeyB[:6] + + data += block_data + + except Exception as e: + print(f"Error reading block {block}: {e}") + + return data + +def main(): + + parser = argparse.ArgumentParser(description="Use libnfc to dump a tag") + parser.add_argument('-d', '--device', required=True, help='libnfc device to open. e.g "pn532_uart:/dev/ttyUSB0"') + + args = parser.parse_args() + try: + n = Nfc(args.device) + except Exception as e: + print("Could not open NFC device:", e) + return + + print("Waiting for Bambu tag... (Ctrl+C to exit)") + + try: + for tag in n.poll(): + + if tag.__class__.__name__ != "Mifare": + print("Non-MIFARE tag detected, skipping") + continue + + if not tag.uid: + print("No UID found; skipping") + continue + + if tag.uid in dumped_ids and not tag.uid in warned_ids: + print("Already read tag:", tag.uid.decode()) + warned_ids.append(tag.uid) + continue + + if tag.uid in warned_ids: + continue + + if not dump.get(tag.uid): + print(f"Tag UID: {tag.uid.decode()}") + dump[tag.uid]={'sector':0,'data':list(), 'keys': generate_keys(bytes.fromhex(tag.uid.decode()))} + + if not tag.uid in dumped_ids and dump[tag.uid]['sector']==SECNUM: + print(f'Tag {tag.uid.decode()} fully dumped; remove tag.') + with open(f'hf-tag-{tag.uid.decode().upper()}.bin','wb') as fp: + for data in dump[tag.uid]['data']: + fp.write(data) + dumped_ids.append(tag.uid) + continue + + dump[tag.uid]['data'].append(read_tag(tag,dump[tag.uid]['sector'], dump[tag.uid]['keys'])) + dump[tag.uid]['sector']+=1 + + except (TimeoutException, KeyboardInterrupt): + print("\nExiting.") + +if __name__ == "__main__": + main() + diff --git a/libnfc/requirements.txt b/libnfc/requirements.txt new file mode 100644 index 0000000..8c0d7f0 --- /dev/null +++ b/libnfc/requirements.txt @@ -0,0 +1,6 @@ +libusb1 +ndeflib +pyDes +pynfc +pyserial +pycryptodome diff --git a/libnfc/requirements.txt.orig b/libnfc/requirements.txt.orig new file mode 100644 index 0000000..cdaa5b2 --- /dev/null +++ b/libnfc/requirements.txt.orig @@ -0,0 +1,6 @@ +wheel==0.45.1 +libusb1==3.3.1 +ndeflib==0.3.3 +pyDes==2.0.1 +pynfc==0.1.1 +pyserial==3.5 From ccd95ec0a2919cddd855c15265215eea90c33949 Mon Sep 17 00:00:00 2001 From: scourge411 Date: Sat, 3 Jan 2026 12:36:10 -0700 Subject: [PATCH 2/2] removed extra requirement file --- libnfc/requirements.txt.orig | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 libnfc/requirements.txt.orig diff --git a/libnfc/requirements.txt.orig b/libnfc/requirements.txt.orig deleted file mode 100644 index cdaa5b2..0000000 --- a/libnfc/requirements.txt.orig +++ /dev/null @@ -1,6 +0,0 @@ -wheel==0.45.1 -libusb1==3.3.1 -ndeflib==0.3.3 -pyDes==2.0.1 -pynfc==0.1.1 -pyserial==3.5