1 local cmds = require('commands')
2 local getopt = require('getopt')
3 local bin = require('bin')
4 local lib14a = require('read14a')
5 local utils = require('utils')
6 local md5 = require('md5')
7 local toyNames = require('default_toys')
12 3. script run tnp3 -k aabbccddeeff
13 4. script run tnp3 -k aabbccddeeff -n
16 usage = "script run tnp3 -k <key> -n"
18 This script will try to dump the contents of a Mifare TNP3xxx card.
19 It will need a valid KeyA in order to find the other keys and decode the card.
22 -k <key> : Sector 0 Key A.
23 -n : Use the nested cmd to find all keys
26 -- AES konstant? LEN 0x24 36,
27 -- I dekompilen är det för internal static array = 0x36 54
28 local hashconstant = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20'
30 local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds
31 local DEBUG = true -- the debug flag
35 -- A debug printout-function
41 if type(args) == "table" then
52 -- This is only meant to be used when errors occur
60 print("Example usage")
66 print( string.rep('--',20) )
67 print( string.rep('--',20) )
72 local function readdumpkeys(infile)
73 t = infile:read("*all")
75 local len,hex = bin.unpack(("H%d"):format(len),t)
79 local function waitCmd()
80 local response = core.WaitForResponseTimeout(cmds.CMD_ACK,TIMEOUT)
82 local count,cmd,arg0 = bin.unpack('LL',response)
84 local count,arg1,arg2,data = bin.unpack('LLH511',response,count)
87 return nil, "Couldn't read block.."
90 return nil, "No response from device"
93 local function main(args)
95 print( string.rep('--',20) )
96 --print( string.rep('--',20) )
102 local useNested = false
103 local cmdReadBlockString = 'hf mf rdbl %d A %s'
104 local input = "dumpkeys.bin"
106 -- Arguments for the script
107 for o, a in getopt.getopt(args, 'hk:n') do
108 if o == "h" then return help() end
109 if o == "k" then keyA = a end
110 if o == "n" then useNested = true end
113 -- validate input args.
114 keyA = keyA or '4b0b20107ccb'
115 if #(keyA) ~= 12 then
116 return oops( string.format('Wrong length of write key (was %d) expected 12', #keyA))
120 local cmdSetDbgOff = "hf mf dbg 0"
121 core.console( cmdSetDbgOff)
123 result, err = lib14a.read1443a(false)
128 core.clearCommandBuffer()
130 if 0x01 ~= result.sak then -- NXP MIFARE TNP3xxx
131 return oops('This is not a TNP3xxx tag. aborting.')
134 print((' Found tag : %s'):format(result.name))
137 print(('Using keyA : %s'):format(keyA))
138 print( string.rep('--',20) )
140 --Trying to find the other keys
142 core.console( ('hf mf nested 1 0 A %s d'):format(keyA) )
146 print('Loading dumpkeys.bin')
147 local infile = io.open(input, "rb")
148 if infile == nil then
149 return oops('Could not read file ', input)
151 local akeys = readdumpkeys(infile):sub(0,12*16)
154 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 0,arg2 = 0,arg3 = 0, data = keyA}
155 err = core.SendCommand(cmd:getBytes())
156 if err then return oops(err) end
157 local block0, err = waitCmd()
158 if err then return oops(err) end
161 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 1,arg2 = 0,arg3 = 0, data = keyA}
162 err = core.SendCommand(cmd:getBytes())
163 if err then return oops(err) end
164 local block1, err = waitCmd()
165 if err then return oops(err) end
172 print('Reading card data')
175 for blockNo = 0, numBlocks-1, 1 do
177 if core.ukbhit() then
178 print("aborted by user")
182 pos = (math.floor( blockNo / 4 ) * 12)+1
183 key = akeys:sub(pos, pos + 11 )
184 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = blockNo ,arg2 = 0,arg3 = 0, data = key}
185 local err = core.SendCommand(cmd:getBytes())
186 if err then return oops(err) end
187 local blockdata, err = waitCmd()
188 if err then return oops(err) end
194 -- Block 0-7 not encrypted
195 blocks[blockNo+1] = ('%02d :: %s :: %s'):format(blockNo,blockdata,blockdata)
197 local base = ('%s%s%d%s'):format(block0, block1, blockNo, hashconstant) local md5hash = md5.sumhexa(base)
198 local aestest = core.aes(md5hash, blockdata)
200 local _,hex = bin.unpack(("H%d"):format(16),aestest)
202 -- local hexascii = string.gsub(hex, '(%x%x)',
204 -- return string.char(tonumber(value, 16))
208 if string.find(blockdata, '^0+$') then
209 blocks[blockNo+1] = ('%02d :: %s :: %s'):format(blockNo,blockdata,blockdata)
211 --blocks[blockNo+1] = ('%02d :: %s :: %s :: %s '):format(blockNo,key,md5hash,hex)
212 blocks[blockNo+1] = ('%02d :: %s :: %s'):format(blockNo,blockdata,hex)
217 -- Sectorblocks, not encrypted
218 blocks[blockNo+1] = ('%02d :: %s :: %s'):format(blockNo,blockdata,blockdata)
223 local uid = block0:sub(1,8)
224 local itemtype = block1:sub(1,4)
225 local cardid = block1:sub(9,24)
226 print( (' UID : %s'):format(uid) )
227 print( (' ITEM TYPE : %s - %s'):format(itemtype, toyNames[itemtype]) )
228 print( (' CARDID : %s'):format(cardid ) )
229 print('BLK :: DATA DECRYPTED' )
230 print( string.rep('--',36) )
231 for _,s in pairs(blocks) do