1 local cmds = require('commands')
2 local getopt = require('getopt')
3 local lib14a = require('read14a')
4 local utils = require('utils')
5 local pre = require('precalc')
6 local toys = require('default_toys')
8 local lsh = bit32.lshift
9 local rsh = bit32.rshift
11 local band = bit32.band
15 script run tnp3clone -h
16 script run tnp3clone -t aa00 -s 0030
20 usage = "script run tnp3clone -t <toytype> -s <subtype>"
22 This script will try making a barebone clone of a tnp3 tag on to a magic generation1 card.
26 -t <data> : toytype id, 4hex symbols.
27 -s <data> : subtype id, 4hex symbols
29 For fun, try the following subtype id:
39 -- This is only meant to be used when errors occur
46 print("Example usage")
50 local function waitCmd()
51 local response = core.WaitForResponseTimeout(cmds.CMD_ACK,2000)
53 local count,cmd,arg0 = bin.unpack('LL',response)
55 local count,arg1,arg2,data = bin.unpack('LLH511',response,count)
58 return nil, "Couldn't read block."
61 return nil, "No response from device"
64 local function readblock( blocknum, keyA )
66 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = blocknum, arg2 = 0, arg3 = 0, data = keyA}
67 err = core.SendCommand(cmd:getBytes())
68 if err then return nil, err end
69 local block0, err = waitCmd()
70 if err then return nil, err end
73 local function readmagicblock( blocknum )
75 local CSETBLOCK_SINGLE_OPERATION = 0x1F
76 cmd = Command:new{cmd = cmds.CMD_MIFARE_CGETBLOCK, arg1 = CSETBLOCK_SINGLE_OPERATION, arg2 = 0, arg3 = blocknum}
77 err = core.SendCommand(cmd:getBytes())
78 if err then return nil, err end
79 local block0, err = waitCmd()
80 if err then return nil, err end
84 local function main(args)
86 print( string.rep('--',20) )
87 print( string.rep('--',20) )
90 local cset = 'hf mf csetbl '
91 local csetuid = 'hf mf csetuid '
92 local cget = 'hf mf cgetbl '
93 local empty = '00000000000000000000000000000000'
94 local AccAndKeyB = '7F078869000000000000'
96 local toytype = 'C201'
97 local subtype = '0030'
100 -- Arguments for the script
101 for o, a in getopt.getopt(args, 'ht:s:') do
102 if o == "h" then return help() end
103 if o == "t" then toytype = a end
104 if o == "s" then subtype = a end
107 if #toytype ~= 4 then return oops('Wrong size - toytype. (4hex symbols)') end
108 if #subtype ~= 4 then return oops('Wrong size - subtype. (4hex symbols)') end
110 -- look up type, find & validate types
111 local item = toys.Find( toytype, subtype)
113 print( (' Looking up input: Found %s - %s (%s)'):format(item[6],item[5], item[4]) )
115 print('Didn\'t find item type. If you are sure about it, report it in')
122 result, err = lib14a.read1443a(false)
123 if not result then return oops(err) end
126 local akeys = pre.GetAll(result.uid)
127 local keyA = akeys:sub(1, 12 )
129 local b0 = readblock(0,keyA)
131 print('failed reading block with factorydefault key. Trying chinese magic read.')
132 b0, err = readmagicblock(0)
135 return oops('failed reading block with chinese magic command. quitting...')
140 local cmd = (csetuid..'%s 0004 08 w'):format(result.uid)
143 local b1 = toytype..'00000000000000000000'..subtype
144 local calc = utils.Crc16(b0..b1)
145 local calcEndian = bor(rsh(calc,8), lsh(band(calc, 0xff), 8))
147 local cmd = (cset..'1 %s%04x'):format( b1, calcEndian)
151 for blockNo = 2, numBlocks-1, 1 do
152 pos = (math.floor( blockNo / 4 ) * 12)+1
153 key = akeys:sub(pos, pos + 11 )
154 if blockNo%4 == 3 then
155 cmd = ('%s %d %s%s'):format(cset,blockNo,key,AccAndKeyB)
159 core.clearCommandBuffer()