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 dumplib = require('html_dumplib')
8 local toys = require('default_toys')
12 script run tnp3dump -n
13 script run tnp3dump -p
14 script run tnp3dump -k aabbccddeeff
15 script run tnp3dump -k aabbccddeeff -n
16 script run tnp3dump -o myfile
17 script run tnp3dump -n -o myfile
18 script run tnp3dump -p -o myfile
19 script run tnp3dump -k aabbccddeeff -n -o myfile
22 usage = "script run tnp3dump -k <key> -n -p -o <filename>"
24 This script will try to dump the contents of a Mifare TNP3xxx card.
25 It will need a valid KeyA in order to find the other keys and decode the card.
28 -k <key> : Sector 0 Key A.
29 -n : Use the nested cmd to find all keys
30 -p : Use the precalc to find all keys
31 -o : filename for the saved dumps
33 local RANDOM = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20'
34 local TIMEOUT = 2500 -- Shouldn't take longer than 2 seconds
35 local DEBUG = false -- the debug flag
39 -- A debug printout-function
41 if not DEBUG then return end
43 if type(args) == "table" then
54 -- This is only meant to be used when errors occur
63 print("Example usage")
69 print( string.rep('--',20) )
70 print( string.rep('--',20) )
75 local function readdumpkeys(infile)
76 t = infile:read("*all")
78 local len,hex = bin.unpack(("H%d"):format(len),t)
82 local function waitCmd()
83 local response = core.WaitForResponseTimeout(cmds.CMD_ACK, TIMEOUT)
85 local count, cmd, arg0 = bin.unpack('LL',response)
87 local count,arg1,arg2,data = bin.unpack('LLH511',response,count)
90 return nil, "Couldn't read block.. ["..arg0.."]"
93 return nil, 'No response from device'
96 local function main(args)
98 print( string.rep('--',20) )
99 print( string.rep('--',20) )
104 local useNested = false
105 local usePreCalc = false
106 local cmdReadBlockString = 'hf mf rdbl %d A %s'
107 local input = "dumpkeys.bin"
108 local outputTemplate = os.date("toydump_%Y-%m-%d_%H%M%S");
110 -- Arguments for the script
111 for o, a in getopt.getopt(args, 'hk:npo:') do
112 if o == "h" then return help() end
113 if o == "k" then keyA = a end
114 if o == "n" then useNested = true end
115 if o == "p" then usePreCalc = true end
116 if o == "o" then outputTemplate = a end
119 -- validate input args.
120 keyA = keyA or '4b0b20107ccb'
121 if #(keyA) ~= 12 then
122 return oops( string.format('Wrong length of write key (was %d) expected 12', #keyA))
126 local cmdSetDbgOff = "hf mf dbg 0"
127 core.console( cmdSetDbgOff)
129 result, err = lib14a.read1443a(false)
134 core.clearCommandBuffer()
137 print((' Found tag %s'):format(result.name))
139 dbg(('Using keyA : %s'):format(keyA))
141 --Trying to find the other keys
143 core.console( ('hf mf nested 1 0 A %s d'):format(keyA) )
146 core.clearCommandBuffer()
150 local pre = require('precalc')
151 akeys = pre.GetAll(result.uid)
154 print('Loading dumpkeys.bin')
155 local hex, err = utils.ReadDumpFile(input)
159 akeys = hex:sub(0,12*16)
163 dbg('Reading block 0')
164 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 0, arg2 = 0, arg3 = 0, data = keyA}
165 err = core.SendCommand(cmd:getBytes())
166 if err then return oops(err) end
167 local block0, err = waitCmd()
168 if err then return oops(err) end
170 core.clearCommandBuffer()
173 dbg('Reading block 1')
174 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 1, arg2 = 0, arg3 = 0, data = keyA}
175 err = core.SendCommand(cmd:getBytes())
176 if err then return oops(err) end
177 local block1, err = waitCmd()
178 if err then return oops(err) end
180 core.clearCommandBuffer()
182 local tmpHash = block0..block1..'%02x'..RANDOM
190 io.write('Reading blocks > ')
191 for blockNo = 0, numBlocks-1, 1 do
195 if core.ukbhit() then
196 print("aborted by user")
200 core.clearCommandBuffer()
202 pos = (math.floor( blockNo / 4 ) * 12)+1
203 key = akeys:sub(pos, pos + 11 )
204 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = blockNo ,arg2 = 0,arg3 = 0, data = key}
205 local err = core.SendCommand(cmd:getBytes())
206 if err then return oops(err) end
207 local blockdata, err = waitCmd()
208 if err then return oops(err) end
211 if blockNo%4 ~= 3 then
214 -- Block 0-7 not encrypted
215 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
217 -- blocks with zero not encrypted.
218 if string.find(blockdata, '^0+$') then
219 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
221 local baseStr = utils.ConvertHexToAscii(tmpHash:format(blockNo))
222 local key = md5.sumhexa(baseStr)
223 local aestest = core.aes128_decrypt(key, blockdata)
224 local hex = utils.ConvertAsciiToHex(aestest)
226 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,hex)
227 io.write(blockNo..',')
231 -- Sectorblocks, not encrypted
232 blocks[blockNo+1] = ('%02d :: %s%s'):format(blockNo,key,blockdata:sub(13,32))
237 core.clearCommandBuffer()
243 for _,s in pairs(blocks) do
244 local slice = s:sub(8,#s)
245 local str = utils.ConvertHexToAscii(slice)
246 emldata = emldata..slice..'\n'
247 for c in (str):gmatch('.') do
248 bindata[#bindata+1] = c
252 print( string.rep('--',20) )
254 local uid = block0:sub(1,8)
255 local toytype = block1:sub(1,4)
256 local cardidLsw = block1:sub(9,16)
257 local cardidMsw = block1:sub(16,24)
258 local cardid = block1:sub(9,24)
259 local subtype = block1:sub(25,28)
261 -- Write dump to files
263 local foo = dumplib.SaveAsBinary(bindata, outputTemplate..'-'..uid..'.bin')
264 print(("Wrote a BIN dump to: %s"):format(foo))
265 local bar = dumplib.SaveAsText(emldata, outputTemplate..'-'..uid..'.eml')
266 print(("Wrote a EML dump to: %s"):format(bar))
269 print( string.rep('--',20) )
272 local item = toys.Find(toytype, subtype)
274 print((' ITEM TYPE : %s - %s (%s)'):format(item[6],item[5], item[4]) )
276 print((' ITEM TYPE : 0x%s 0x%s'):format(toytype, subtype))
279 print( (' UID : 0x%s'):format(uid) )
280 print( (' CARDID : 0x%s'):format(cardid ) )
281 print( string.rep('--',20) )
283 core.clearCommandBuffer()