2015-03-30 22:15:53 +08:00
|
|
|
local cmds = require('commands')
|
|
|
|
local getopt = require('getopt')
|
|
|
|
local lib14a = require('read14a')
|
|
|
|
local utils = require('utils')
|
|
|
|
local pre = require('precalc')
|
2015-04-08 00:02:29 +08:00
|
|
|
local toys = require('default_toys')
|
2015-03-30 22:15:53 +08:00
|
|
|
|
|
|
|
local lsh = bit32.lshift
|
|
|
|
local rsh = bit32.rshift
|
|
|
|
local bor = bit32.bor
|
|
|
|
local band = bit32.band
|
|
|
|
|
|
|
|
example =[[
|
2015-04-07 00:47:53 +08:00
|
|
|
script run tnp3clone
|
|
|
|
script run tnp3clone -h
|
2015-05-12 01:38:53 +08:00
|
|
|
script run tnp3clone -l
|
2015-04-07 00:47:53 +08:00
|
|
|
script run tnp3clone -t aa00 -s 0030
|
2015-03-30 22:15:53 +08:00
|
|
|
|
|
|
|
]]
|
|
|
|
author = "Iceman"
|
2015-04-07 00:47:53 +08:00
|
|
|
usage = "script run tnp3clone -t <toytype> -s <subtype>"
|
2015-03-30 22:15:53 +08:00
|
|
|
desc =[[
|
|
|
|
This script will try making a barebone clone of a tnp3 tag on to a magic generation1 card.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
-h : this help
|
2015-05-12 01:38:53 +08:00
|
|
|
-l : list all known toy tokens
|
|
|
|
-t <data> : toytype id, 4hex symbols
|
2015-04-07 00:47:53 +08:00
|
|
|
-s <data> : subtype id, 4hex symbols
|
2015-04-10 02:36:12 +08:00
|
|
|
|
|
|
|
For fun, try the following subtype id:
|
|
|
|
0612 - Lightcore
|
|
|
|
0118 - Series 1
|
|
|
|
0138 - Series 2
|
|
|
|
0234 - Special
|
|
|
|
023c - Special
|
2015-04-25 00:35:09 +08:00
|
|
|
0020 - Swapforce
|
2015-03-30 22:15:53 +08:00
|
|
|
]]
|
|
|
|
|
|
|
|
-- This is only meant to be used when errors occur
|
2019-01-21 06:46:03 +08:00
|
|
|
local function oops(err)
|
2015-03-30 22:15:53 +08:00
|
|
|
print("ERROR: ",err)
|
|
|
|
end
|
|
|
|
-- Usage help
|
2019-01-21 06:46:03 +08:00
|
|
|
local function help()
|
2015-03-30 22:15:53 +08:00
|
|
|
print(desc)
|
|
|
|
print("Example usage")
|
|
|
|
print(example)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function waitCmd()
|
|
|
|
local response = core.WaitForResponseTimeout(cmds.CMD_ACK,2000)
|
|
|
|
if response then
|
|
|
|
local count,cmd,arg0 = bin.unpack('LL',response)
|
|
|
|
if(arg0==1) then
|
|
|
|
local count,arg1,arg2,data = bin.unpack('LLH511',response,count)
|
|
|
|
return data:sub(1,32)
|
|
|
|
else
|
|
|
|
return nil, "Couldn't read block."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil, "No response from device"
|
|
|
|
end
|
|
|
|
|
2015-04-02 18:07:51 +08:00
|
|
|
local function readblock( blocknum, keyA )
|
2019-01-21 06:46:03 +08:00
|
|
|
-- Read block N
|
2015-04-02 18:07:51 +08:00
|
|
|
cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = blocknum, arg2 = 0, arg3 = 0, data = keyA}
|
2015-03-30 22:15:53 +08:00
|
|
|
err = core.SendCommand(cmd:getBytes())
|
2015-04-02 18:07:51 +08:00
|
|
|
if err then return nil, err end
|
2015-03-30 22:15:53 +08:00
|
|
|
local block0, err = waitCmd()
|
2015-04-02 18:07:51 +08:00
|
|
|
if err then return nil, err end
|
|
|
|
return block0
|
|
|
|
end
|
2019-01-21 06:46:03 +08:00
|
|
|
|
2015-04-02 18:07:51 +08:00
|
|
|
local function readmagicblock( blocknum )
|
2019-01-21 06:46:03 +08:00
|
|
|
-- Read block N
|
2015-04-02 18:07:51 +08:00
|
|
|
local CSETBLOCK_SINGLE_OPERATION = 0x1F
|
|
|
|
cmd = Command:new{cmd = cmds.CMD_MIFARE_CGETBLOCK, arg1 = CSETBLOCK_SINGLE_OPERATION, arg2 = 0, arg3 = blocknum}
|
2015-05-12 01:38:53 +08:00
|
|
|
err = core.SendCommand(cmd:getBytes())
|
2015-04-02 18:07:51 +08:00
|
|
|
if err then return nil, err end
|
|
|
|
local block0, err = waitCmd()
|
|
|
|
if err then return nil, err end
|
2015-03-30 22:15:53 +08:00
|
|
|
return block0
|
|
|
|
end
|
|
|
|
|
|
|
|
local function main(args)
|
|
|
|
|
2015-04-07 00:47:53 +08:00
|
|
|
print( string.rep('--',20) )
|
|
|
|
print( string.rep('--',20) )
|
|
|
|
|
2015-03-30 22:15:53 +08:00
|
|
|
local numBlocks = 64
|
2019-01-21 06:46:03 +08:00
|
|
|
local cset = 'hf mf csetbl '
|
2015-04-07 00:47:53 +08:00
|
|
|
local csetuid = 'hf mf csetuid '
|
2015-04-02 18:07:51 +08:00
|
|
|
local cget = 'hf mf cgetbl '
|
2015-03-30 22:15:53 +08:00
|
|
|
local empty = '00000000000000000000000000000000'
|
2019-01-22 00:36:58 +08:00
|
|
|
local AccAndKeyB = '7F0F0869000000000000'
|
2019-01-21 06:46:03 +08:00
|
|
|
local atqa = '0F01'
|
|
|
|
local sak = '81'
|
2015-03-30 22:15:53 +08:00
|
|
|
-- Defaults to Gusto
|
|
|
|
local toytype = 'C201'
|
2015-04-07 00:47:53 +08:00
|
|
|
local subtype = '0030'
|
|
|
|
local DEBUG = true
|
2015-03-30 22:15:53 +08:00
|
|
|
|
|
|
|
-- Arguments for the script
|
2015-05-12 01:38:53 +08:00
|
|
|
for o, a in getopt.getopt(args, 'ht:s:l') do
|
2015-03-30 22:15:53 +08:00
|
|
|
if o == "h" then return help() end
|
|
|
|
if o == "t" then toytype = a end
|
2015-04-07 00:47:53 +08:00
|
|
|
if o == "s" then subtype = a end
|
2015-05-12 01:38:53 +08:00
|
|
|
if o == "l" then return toys.List() end
|
2015-03-30 22:15:53 +08:00
|
|
|
end
|
2015-05-12 01:38:53 +08:00
|
|
|
|
2019-01-21 06:46:03 +08:00
|
|
|
if #toytype ~= 4 then return oops('[!] Wrong size - toytype. (4hex symbols)') end
|
|
|
|
if #subtype ~= 4 then return oops('[!] Wrong size - subtype. (4hex symbols)') end
|
2015-04-07 00:47:53 +08:00
|
|
|
|
|
|
|
-- look up type, find & validate types
|
|
|
|
local item = toys.Find( toytype, subtype)
|
|
|
|
if item then
|
2019-01-21 06:46:03 +08:00
|
|
|
print( ('[+] Looking up input: Found %s - %s (%s)'):format(item[6],item[5], item[4]) )
|
2015-04-07 00:47:53 +08:00
|
|
|
else
|
2019-01-21 06:46:03 +08:00
|
|
|
print('[-] Didn\'t find item type. If you are sure about it, post on forum')
|
2015-04-07 00:47:53 +08:00
|
|
|
end
|
|
|
|
--15,16
|
2019-01-21 06:46:03 +08:00
|
|
|
--13-14
|
2015-03-30 22:15:53 +08:00
|
|
|
|
|
|
|
-- find tag
|
2018-01-25 16:51:49 +08:00
|
|
|
result, err = lib14a.read(false, true)
|
2019-01-21 06:46:03 +08:00
|
|
|
if not result then return oops(err) end
|
2015-03-30 22:15:53 +08:00
|
|
|
|
|
|
|
-- load keys
|
|
|
|
local akeys = pre.GetAll(result.uid)
|
2019-01-21 06:46:03 +08:00
|
|
|
local keyA = akeys:sub(1, 12 )
|
2015-03-30 22:15:53 +08:00
|
|
|
|
2019-01-21 06:46:03 +08:00
|
|
|
local b0 = readblock(0, keyA)
|
2015-04-02 18:07:51 +08:00
|
|
|
if not b0 then
|
2019-01-21 06:46:03 +08:00
|
|
|
print('[-] failed reading block with factorydefault key. Trying chinese magic read.')
|
2015-04-02 18:07:51 +08:00
|
|
|
b0, err = readmagicblock(0)
|
|
|
|
if not b0 then
|
2019-01-21 06:46:03 +08:00
|
|
|
oops('[!] '..err)
|
|
|
|
return oops('[!] failed reading block with chinese magic command. Quitting...')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
core.clearCommandBuffer()
|
2019-01-21 06:55:11 +08:00
|
|
|
|
2015-04-05 20:27:12 +08:00
|
|
|
-- wipe card.
|
2019-01-21 06:46:03 +08:00
|
|
|
local cmd = (csetuid..'%s %s %s w'):format(result.uid, atqa, sak)
|
2015-04-05 20:27:12 +08:00
|
|
|
core.console(cmd)
|
2019-01-21 06:46:03 +08:00
|
|
|
core.clearCommandBuffer()
|
|
|
|
|
2015-05-12 01:38:53 +08:00
|
|
|
local b1 = toytype..string.rep('00',10)..subtype
|
2015-04-05 20:27:12 +08:00
|
|
|
|
2015-03-30 22:15:53 +08:00
|
|
|
local calc = utils.Crc16(b0..b1)
|
|
|
|
local calcEndian = bor(rsh(calc,8), lsh(band(calc, 0xff), 8))
|
|
|
|
|
2015-04-02 18:07:51 +08:00
|
|
|
local cmd = (cset..'1 %s%04x'):format( b1, calcEndian)
|
2015-04-05 20:27:12 +08:00
|
|
|
core.console(cmd)
|
2019-01-21 06:46:03 +08:00
|
|
|
core.clearCommandBuffer()
|
2015-03-30 22:15:53 +08:00
|
|
|
|
|
|
|
local pos, key
|
|
|
|
for blockNo = 2, numBlocks-1, 1 do
|
|
|
|
pos = (math.floor( blockNo / 4 ) * 12)+1
|
|
|
|
key = akeys:sub(pos, pos + 11 )
|
2015-04-05 20:27:12 +08:00
|
|
|
if blockNo%4 == 3 then
|
2015-03-30 22:15:53 +08:00
|
|
|
cmd = ('%s %d %s%s'):format(cset,blockNo,key,AccAndKeyB)
|
2015-04-05 20:27:12 +08:00
|
|
|
core.console(cmd)
|
|
|
|
end
|
2015-03-30 22:15:53 +08:00
|
|
|
end
|
2015-04-05 20:27:12 +08:00
|
|
|
core.clearCommandBuffer()
|
2019-01-21 06:46:03 +08:00
|
|
|
|
|
|
|
-- Set sector trailer S0, since it has different access rights
|
2019-01-21 06:53:54 +08:00
|
|
|
cmd = ('%s 3 %s0f0f0f69000000000000'):format(cset, keyA)
|
2019-01-21 06:46:03 +08:00
|
|
|
core.console(cmd)
|
|
|
|
core.clearCommandBuffer()
|
2015-03-30 22:15:53 +08:00
|
|
|
end
|
2019-01-21 06:46:03 +08:00
|
|
|
main(args)
|