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 toyNames = require('default_toys')
13 script run tnp3dump -n
14 script run tnp3dump -p
15 script run tnp3dump -k aabbccddeeff
16 script run tnp3dump -k aabbccddeeff -n
17 script run tnp3dump -o myfile
18 script run tnp3dump -n -o myfile
19 script run tnp3dump -p -o myfile
20 script run tnp3dump -k aabbccddeeff -n -o myfile
23 usage = "script run tnp3dump -k <key> -n -p -o <filename>"
25 This script will try to dump the contents of a Mifare TNP3xxx card.
26 It will need a valid KeyA in order to find the other keys and decode the card.
29 -k <key> : Sector 0 Key A.
30 -n : Use the nested cmd to find all keys
31 -p : Use the precalc to find all keys
32 -o : filename for the saved dumps
35 local HASHCONSTANT = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20'
37 local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds
38 local DEBUG = false -- the debug flag
42 -- A debug printout-function
48 if type(args) == "table" then
59 -- This is only meant to be used when errors occur
67 print("Example usage")
73 print( string.rep('--',20) )
74 print( string.rep('--',20) )
79 local function readdumpkeys(infile)
80 t = infile:read("*all")
82 local len,hex = bin.unpack(("H%d"):format(len),t)
86 local function waitCmd()
87 local response = core.WaitForResponseTimeout(cmds.CMD_ACK,TIMEOUT)
89 local count,cmd,arg0 = bin.unpack('LL',response)
91 local count,arg1,arg2,data = bin.unpack('LLH511',response,count)
94 return nil, "Couldn't read block.."
97 return nil, "No response from device"
100 local function computeCrc16(s)
101 local hash = core.crc16(utils.ConvertHexToAscii(s))
105 local function reverseCrcBytes(crc)
106 crc2 = crc:sub(3,4)..crc:sub(1,2)
107 return tonumber(crc2,16)
110 local function main(args)
112 print( string.rep('--',20) )
113 print( string.rep('--',20) )
118 local useNested = false
119 local usePreCalc = false
120 local cmdReadBlockString = 'hf mf rdbl %d A %s'
121 local input = "dumpkeys.bin"
122 local outputTemplate = os.date("toydump_%Y-%m-%d_%H%M%S");
124 -- Arguments for the script
125 for o, a in getopt.getopt(args, 'hk:npo:') do
126 if o == "h" then return help() end
127 if o == "k" then keyA = a end
128 if o == "n" then useNested = true end
129 if o == "p" then usePreCalc = true end
130 if o == "o" then outputTemplate = a end
133 -- validate input args.
134 keyA = keyA or '4b0b20107ccb'
135 if #(keyA) ~= 12 then
136 return oops( string.format('Wrong length of write key (was %d) expected 12', #keyA))
140 local cmdSetDbgOff = "hf mf dbg 0"
141 core.console( cmdSetDbgOff)
143 result, err = lib14a.read1443a(false)
148 core.clearCommandBuffer()
150 if 0x01 ~= result.sak then -- NXP MIFARE TNP3xxx
151 return oops('This is not a TNP3xxx tag. aborting.')
155 print((' Found tag %s'):format(result.name))
157 dbg(('Using keyA : %s'):format(keyA))
159 --Trying to find the other keys
161 core.console( ('hf mf nested 1 0 A %s d'):format(keyA) )
164 core.clearCommandBuffer()
168 local pre = require('precalc')
169 akeys = pre.GetAll(result.uid)
171 print('Loading dumpkeys.bin')
172 local hex, err = utils.ReadDumpFile(input)
176 akeys = hex:sub(0,12*16)
180 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 0,arg2 = 0,arg3 = 0, data = keyA}
181 err = core.SendCommand(cmd:getBytes())
182 if err then return oops(err) end
183 local block0, err = waitCmd()
184 if err then return oops(err) end
187 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 1,arg2 = 0,arg3 = 0, data = keyA}
188 err = core.SendCommand(cmd:getBytes())
189 if err then return oops(err) end
190 local block1, err = waitCmd()
191 if err then return oops(err) end
198 print('Reading card data')
199 core.clearCommandBuffer()
202 io.write('Reading blocks > ')
203 for blockNo = 0, numBlocks-1, 1 do
205 if core.ukbhit() then
206 print("aborted by user")
210 pos = (math.floor( blockNo / 4 ) * 12)+1
211 key = akeys:sub(pos, pos + 11 )
212 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = blockNo ,arg2 = 0,arg3 = 0, data = key}
213 local err = core.SendCommand(cmd:getBytes())
214 if err then return oops(err) end
215 local blockdata, err = waitCmd()
216 if err then return oops(err) end
218 if blockNo%4 ~= 3 then
220 -- Block 0-7 not encrypted
221 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
223 local base = ('%s%s%02x%s'):format(block0, block1, blockNo, HASHCONSTANT)
224 local baseStr = utils.ConvertHexToAscii(base)
225 local md5hash = md5.sumhexa(baseStr)
226 local aestest = core.aes(md5hash, blockdata)
228 local hex = utils.ConvertAsciiToBytes(aestest)
229 hex = utils.ConvertBytesToHex(hex)
231 -- blocks with zero not encrypted.
232 if string.find(blockdata, '^0+$') then
233 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
235 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,hex)
236 io.write( blockNo..',')
240 -- Sectorblocks, not encrypted
241 blocks[blockNo+1] = ('%02d :: %s%s'):format(blockNo,key,blockdata:sub(13,32))
246 core.clearCommandBuffer()
252 for _,s in pairs(blocks) do
253 local slice = s:sub(8,#s)
254 local str = utils.ConvertBytesToAscii(
255 utils.ConvertHexToBytes(slice)
257 emldata = emldata..slice..'\n'
258 for c in (str):gmatch('.') do
259 bindata[#bindata+1] = c
263 local uid = block0:sub(1,8)
264 local itemtype = block1:sub(1,4)
265 local cardidLsw = block1:sub(9,16)
266 local cardidMsw = block1:sub(16,24)
267 local cardid = block1:sub(9,24)
268 local traptype = block1:sub(25,28)
270 -- Write dump to files
272 local foo = dumplib.SaveAsBinary(bindata, outputTemplate..'_uid_'..uid..'.bin')
273 print(("Wrote a BIN dump to: %s"):format(foo))
274 local bar = dumplib.SaveAsText(emldata, outputTemplate..'_uid_'..uid..'.eml')
275 print(("Wrote a EML dump to: %s"):format(bar))
278 local itemtypename = toyNames[itemtype]
279 if itemtypename == nil then
280 itemtypename = toyNames[utils.SwapEndiannessStr(itemtype,16)]
283 print( string.rep('--',20) )
284 print( (' ITEM TYPE : 0x%s - %s'):format(itemtype, itemtypename) )
285 print( (' Alter ego / traptype : 0x%s'):format(traptype) )
286 print( (' UID : 0x%s'):format(uid) )
287 print( (' CARDID : 0x%s'):format(cardid ) )
289 print( string.rep('--',20) )