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) )
137 print('Trying to find keys.')
138 core.console( ('hf mf nested 1 0 A %s d'):format(keyA) )
142 print('Loading dumpkeys.bin')
143 local infile = io.open(input, "rb")
144 if infile == nil then
145 return oops('Could not read file ', input)
147 local akeys = readdumpkeys(infile):sub(0,12*16)
150 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 0,arg2 = 0,arg3 = 0, data = keyA}
151 err = core.SendCommand(cmd:getBytes())
152 if err then return oops(err) end
153 local block0, err = waitCmd()
154 if err then return oops(err) end
157 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 1,arg2 = 0,arg3 = 0, data = keyA}
158 err = core.SendCommand(cmd:getBytes())
159 if err then return oops(err) end
160 local block1, err = waitCmd()
161 if err then return oops(err) end
169 for blockNo = 0, numBlocks-1, 1 do
171 if core.ukbhit() then
172 print("aborted by user")
176 pos = (math.floor( blockNo / 4 ) * 12)+1
177 key = akeys:sub(pos, pos + 12 )
178 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = blockNo ,arg2 = 0,arg3 = 0, data = key}
179 local err = core.SendCommand(cmd:getBytes())
180 if err then return oops(err) end
181 local blockdata, err = waitCmd()
182 if err then return oops(err) end
188 -- Block 0-7 not encrypted
189 blocks[blockNo+1] = ('%02d :: %s :: %s'):format(blockNo,blockdata,blockdata)
191 local base = ('%s%s%d%s'):format(block0, block1, blockNo, hashconstant)
192 local md5hash = md5.sumhexa(base)
193 local aestest = core.aes(md5hash, blockdata)
195 local _,hex = bin.unpack(("H%d"):format(16),aestest)
197 -- local hexascii = string.gsub(hex, '(%x%x)',
199 -- return string.char(tonumber(value, 16))
203 if string.find(blockdata, '^0+$') then
204 blocks[blockNo+1] = ('%02d :: %s :: %s'):format(blockNo,blockdata,blockdata)
206 --blocks[blockNo+1] = ('%02d :: %s :: %s :: %s '):format(blockNo,key,md5hash,hex)
207 blocks[blockNo+1] = ('%02d :: %s :: %s'):format(blockNo,blockdata,hex)
212 -- Sectorblocks, not encrypted
213 blocks[blockNo+1] = ('%02d :: %s :: %s'):format(blockNo,blockdata,blockdata)
218 print('BLK :: DATA DECRYPTED' )
219 print( string.rep('--',36) )
220 for _,s in pairs(blocks) do