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
34 local HASHCONSTANT = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20'
36 local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds
37 local DEBUG = false -- the debug flag
41 -- A debug printout-function
47 if type(args) == "table" then
58 -- This is only meant to be used when errors occur
66 print("Example usage")
72 print( string.rep('--',20) )
73 print( string.rep('--',20) )
78 local function readdumpkeys(infile)
79 t = infile:read("*all")
81 local len,hex = bin.unpack(("H%d"):format(len),t)
85 local function waitCmd()
86 local response = core.WaitForResponseTimeout(cmds.CMD_ACK,TIMEOUT)
88 local count,cmd,arg0 = bin.unpack('LL',response)
90 local count,arg1,arg2,data = bin.unpack('LLH511',response,count)
93 return nil, "Couldn't read block.."
96 return nil, "No response from device"
99 local function computeCrc16(s)
100 local hash = core.crc16(utils.ConvertHexToAscii(s))
104 local function reverseCrcBytes(crc)
105 crc2 = crc:sub(3,4)..crc:sub(1,2)
106 return tonumber(crc2,16)
109 local function main(args)
111 print( string.rep('--',20) )
112 print( string.rep('--',20) )
117 local useNested = false
118 local usePreCalc = false
119 local cmdReadBlockString = 'hf mf rdbl %d A %s'
120 local input = "dumpkeys.bin"
121 local outputTemplate = os.date("toydump_%Y-%m-%d_%H%M%S");
123 -- Arguments for the script
124 for o, a in getopt.getopt(args, 'hk:npo:') do
125 if o == "h" then return help() end
126 if o == "k" then keyA = a end
127 if o == "n" then useNested = true end
128 if o == "p" then usePreCalc = true end
129 if o == "o" then outputTemplate = a end
132 -- validate input args.
133 keyA = keyA or '4b0b20107ccb'
134 if #(keyA) ~= 12 then
135 return oops( string.format('Wrong length of write key (was %d) expected 12', #keyA))
139 local cmdSetDbgOff = "hf mf dbg 0"
140 core.console( cmdSetDbgOff)
142 result, err = lib14a.read1443a(false)
147 core.clearCommandBuffer()
149 if 0x01 ~= result.sak then -- NXP MIFARE TNP3xxx
150 -- return oops('This is not a TNP3xxx tag. aborting.')
154 print((' Found tag %s'):format(result.name))
156 dbg(('Using keyA : %s'):format(keyA))
158 --Trying to find the other keys
160 core.console( ('hf mf nested 1 0 A %s d'):format(keyA) )
163 core.clearCommandBuffer()
167 local pre = require('precalc')
168 akeys = pre.GetAll(result.uid)
170 print('Loading dumpkeys.bin')
171 local hex, err = utils.ReadDumpFile(input)
175 akeys = hex:sub(0,12*16)
179 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 0,arg2 = 0,arg3 = 0, data = keyA}
180 err = core.SendCommand(cmd:getBytes())
181 if err then return oops(err) end
182 local block0, err = waitCmd()
183 if err then return oops(err) end
186 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 1,arg2 = 0,arg3 = 0, data = keyA}
187 err = core.SendCommand(cmd:getBytes())
188 if err then return oops(err) end
189 local block1, err = waitCmd()
190 if err then return oops(err) end
197 print('Reading card data')
198 core.clearCommandBuffer()
201 io.write('Reading blocks > ')
202 for blockNo = 0, numBlocks-1, 1 do
204 if core.ukbhit() then
205 print("aborted by user")
209 pos = (math.floor( blockNo / 4 ) * 12)+1
210 key = akeys:sub(pos, pos + 11 )
211 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = blockNo ,arg2 = 0,arg3 = 0, data = key}
212 local err = core.SendCommand(cmd:getBytes())
213 if err then return oops(err) end
214 local blockdata, err = waitCmd()
215 if err then return oops(err) end
218 if blockNo%4 ~= 3 then
221 -- Block 0-7 not encrypted
222 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
224 local base = ('%s%s%02x%s'):format(block0, block1, blockNo, HASHCONSTANT)
225 local baseStr = utils.ConvertHexToAscii(base)
226 local md5hash = md5.sumhexa(baseStr)
227 local aestest = core.aes(md5hash, blockdata)
229 local hex = utils.ConvertAsciiToBytes(aestest)
230 hex = utils.ConvertBytesToHex(hex)
232 -- blocks with zero not encrypted.
233 if string.find(blockdata, '^0+$') then
234 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
236 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,hex)
237 io.write( blockNo..',')
241 -- Sectorblocks, not encrypted
242 blocks[blockNo+1] = ('%02d :: %s%s'):format(blockNo,key,blockdata:sub(13,32))
247 core.clearCommandBuffer()
253 for _,s in pairs(blocks) do
254 local slice = s:sub(8,#s)
255 local str = utils.ConvertBytesToAscii(
256 utils.ConvertHexToBytes(slice)
258 emldata = emldata..slice..'\n'
259 for c in (str):gmatch('.') do
260 bindata[#bindata+1] = c
264 print( string.rep('--',20) )
267 local uid = block0:sub(1,8)
268 local toytype = block1:sub(1,4)
269 local cardidLsw = block1:sub(9,16)
270 local cardidMsw = block1:sub(16,24)
271 local cardid = block1:sub(9,24)
272 local subtype = block1:sub(25,28)
274 -- Write dump to files
276 local foo = dumplib.SaveAsBinary(bindata, outputTemplate..'_uid_'..uid..'.bin')
277 print(("Wrote a BIN dump to: %s"):format(foo))
278 local bar = dumplib.SaveAsText(emldata, outputTemplate..'_uid_'..uid..'.eml')
279 print(("Wrote a EML dump to: %s"):format(bar))
282 local item = toys.Find(toytype, subtype)
284 local itemStr = ('%s - %s (%s)'):format(item[6],item[5], item[4])
285 print(' ITEM TYPE : '..itemStr )
287 print((' ITEM TYPE : 0x%s 0x%s'):format(toytype, subtype))
291 print( (' Alter ego / traptype : 0x%s'):format(traptype) )
292 print( (' UID : 0x%s'):format(uid) )
293 print( (' CARDID : 0x%s'):format(cardid ) )
295 print( string.rep('--',20) )