Skip to content

Instantly share code, notes, and snippets.

@DrGenetik
Created March 13, 2022 20:12
Show Gist options
  • Save DrGenetik/5c057f51cdc12f6d995051adb0531ddb to your computer and use it in GitHub Desktop.
Save DrGenetik/5c057f51cdc12f6d995051adb0531ddb to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from binascii import hexlify
from itertools import zip_longest
from secrets import token_bytes
import sys
def grouper(iterable, n, *, incomplete="fill", fillvalue=None):
"Collect data into non-overlapping fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, fillvalue='x') --> ABC DEF Gxx
# grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError
# grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF
args = [iter(iterable)] * n
if incomplete == "fill":
return zip_longest(*args, fillvalue=fillvalue)
if incomplete == "strict":
return zip(*args, strict=True)
if incomplete == "ignore":
return zip(*args)
else:
raise ValueError("Expected fill, strict, or ignore")
def read_bin(infile):
with open(infile, "rb") as fi:
return fi.read(540)
def write_nfc(outfile, data):
uid = hexlify(data[0:3] + data[4:8], " ", 1).decode("UTF-8").upper()
signature = hexlify(token_bytes(32), " ", 1).decode("UTF-8").upper()
with open(outfile, "w") as fo:
fo.write("Filetype: Flipper NFC device\n")
fo.write("Version: 2\n")
fo.write("# Nfc device type can be UID, Mifare Ultralight, Bank card\n")
fo.write("Device type: NTAG215\n")
fo.write("# UID, ATQA and SAK are common for all formats\n")
fo.write(f"UID: {uid}\n")
fo.write("ATQA: 44 00\n")
fo.write("SAK: 00\n")
fo.write("# Mifare Ultralight specific data\n")
fo.write(f"Signature: {signature}\n")
fo.write("Mifare version: 00 04 04 02 01 00 11 03\n")
fo.write("Counter 0: 0\n")
fo.write("Tearing 0: 00\n")
fo.write("Counter 1: 0\n")
fo.write("Tearing 1: 00\n")
fo.write("Counter 2: 0\n")
fo.write("Tearing 2: 00\n")
fo.write("Pages total: 135\n")
for page_num, page_data in enumerate(grouper(data, 4)):
page_data = " ".join("{:02X}".format(c) for c in page_data)
fo.write(f"Page {page_num}: {page_data}\n")
def convert(infile, outfile):
data = read_bin(infile)
write_nfc(outfile, data)
if __name__ == "__main__":
convert(*sys.argv[1:3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment