proxmark3/client/scripts/tnp3clone.lua

189 lines
5.1 KiB
Lua
Raw Normal View History

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