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')
11 3. script run tnp3 -k aabbccddeeff
12 4. script run tnp3 -k aabbccddeeff -n
15 usage = "script run tnp3 -k <key> -n"
17 This script will try to dump the contents of a Mifare TNP3xxx card.
18 It will need a valid KeyA in order to find the other keys and decode the card.
21 -k <key> : Sector 0 Key A.
22 -n : Use the nested cmd to find all keys
25 -- AES konstant? LEN 0x24 36,
26 -- I dekompilen är det för internal static array = 0x36 54
27 local hashconstant = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20'
29 local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds
30 local DEBUG = true -- the debug flag
34 -- A debug printout-function
40 if type(args) == "table" then
51 -- This is only meant to be used when errors occur
59 print("Example usage")
65 print( string.rep('--',20) )
66 print( string.rep('--',20) )
71 local function readdumpkeys(infile)
72 t = infile:read("*all")
74 local len,hex = bin.unpack(("H%d"):format(len),t)
78 local function waitCmd()
79 local response = core.WaitForResponseTimeout(cmds.CMD_ACK,TIMEOUT)
81 local count,cmd,arg0 = bin.unpack('LL',response)
83 local count,arg1,arg2,data = bin.unpack('LLH511',response,count)
86 return nil, "Couldn't read block.."
89 return nil, "No response from device"
92 local function main(args)
94 print( string.rep('--',20) )
95 print( string.rep('--',20) )
101 local useNested = false
102 local cmdReadBlockString = 'hf mf rdbl %d A %s'
103 local input = "dumpkeys.bin"
105 -- Arguments for the script
106 for o, a in getopt.getopt(args, 'hk:n') do
107 if o == "h" then return help() end
108 if o == "k" then keyA = a end
109 if o == "n" then useNested = true end
112 -- validate input args.
113 keyA = keyA or '4b0b20107ccb'
114 if #(keyA) ~= 12 then
115 return oops( string.format('Wrong length of write key (was %d) expected 12', #keyA))
118 result, err = lib14a.read1443a(false)
123 print((' Found tag : %s'):format(result.name))
125 core.clearCommandBuffer()
127 if 0x01 ~= result.sak then -- NXP MIFARE TNP3xxx
128 return oops('This is not a TNP3xxx tag. aborting.')
132 print(('Using keyA : %s'):format(keyA))
133 print( string.rep('--',20) )
135 print('Trying to find other keys.')
137 core.console( ('hf mf nested 1 0 A %s d'):format(keyA) )
141 local infile = io.open(input, "rb")
142 if infile == nil then
143 return oops('Could not read file ', input)
145 local akeys = readdumpkeys(infile):sub(0,12*16)
148 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 0,arg2 = 0,arg3 = 0, data = keyA}
149 err = core.SendCommand(cmd:getBytes())
150 if err then return oops(err) end
151 local block0, err = waitCmd()
152 if err then return oops(err) end
155 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 1,arg2 = 0,arg3 = 0, data = keyA}
156 err = core.SendCommand(cmd:getBytes())
157 if err then return oops(err) end
158 local block1, err = waitCmd()
159 if err then return oops(err) end
167 for blockNo = 8, numBlocks-1, 1 do
170 pos = (math.floor( blockNo / 4 ) * 12)+1
171 key = akeys:sub(pos, pos + 12 )
172 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = blockNo ,arg2 = 0,arg3 = 0, data = key}
173 local err = core.SendCommand(cmd:getBytes())
174 if err then return oops(err) end
175 local blockdata, err = waitCmd()
176 if err then return oops(err) end
178 local base = ('%s%s%d%s'):format(block0, block1, blockNo, hashconstant)
179 local md5hash = md5.sumhexa(base)
180 local aestest = core.aes(md5hash, blockdata)
182 local _,hex = bin.unpack(("H%d"):format(16),aestest)
184 -- local hexascii = string.gsub(hex, '(%x%x)',
186 -- return string.char(tonumber(value, 16))
190 if string.find(blockdata, '^0+$') then
191 blocks[blockNo] = ('%02d :: %s :: %s'):format(blockNo,blockdata,blockdata)
193 --blocks[blockNo] = ('%02d :: %s :: %s :: %s '):format(blockNo,key,md5hash,hex)
194 blocks[blockNo] = ('%02d :: %s :: %s'):format(blockNo,blockdata,blockdata)
197 if core.ukbhit() then
198 print("aborted by user")
205 print('BLK :: DATA DECRYPTED' )
206 print( string.rep('--',36) )
207 for _,s in pairs(blocks) do