]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/scripts/tnp3dump.lua
ADD: `analyse nuid` - generates NUID 4byte from a UID 7byte. Mifare Classic Ev1...
[proxmark3-svn] / client / scripts / tnp3dump.lua
CommitLineData
b915fda3 1local cmds = require('commands')
2local getopt = require('getopt')
3local bin = require('bin')
4local lib14a = require('read14a')
5local utils = require('utils')
6local md5 = require('md5')
7local dumplib = require('html_dumplib')
cff17e78 8local toys = require('default_toys')
04a6113f 9
b915fda3 10example =[[
04a6113f 11 script run tnp3dump
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
b915fda3 20]]
21author = "Iceman"
04a6113f 22usage = "script run tnp3dump -k <key> -n -p -o <filename>"
b915fda3 23desc =[[
24This script will try to dump the contents of a Mifare TNP3xxx card.
25It will need a valid KeyA in order to find the other keys and decode the card.
26Arguments:
27 -h : this help
28 -k <key> : Sector 0 Key A.
29 -n : Use the nested cmd to find all keys
04a6113f 30 -p : Use the precalc to find all keys
b915fda3 31 -o : filename for the saved dumps
32]]
9c09e006 33local RANDOM = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20'
b9534ca0 34local TIMEOUT = 2500 -- Shouldn't take longer than 2 seconds
b915fda3 35local DEBUG = false -- the debug flag
36local numBlocks = 64
37local numSectors = 16
38---
39-- A debug printout-function
40function dbg(args)
a826cb0d 41 if not DEBUG then return end
b915fda3 42
43 if type(args) == "table" then
44 local i = 1
45 while result[i] do
46 dbg(result[i])
47 i = i+1
48 end
49 else
50 print("###", args)
51 end
52end
53---
54-- This is only meant to be used when errors occur
55function oops(err)
56 print("ERROR: ",err)
a826cb0d 57 return nil,err
b915fda3 58end
59---
60-- Usage help
61function help()
62 print(desc)
63 print("Example usage")
64 print(example)
65end
66--
67-- Exit message
68function ExitMsg(msg)
69 print( string.rep('--',20) )
70 print( string.rep('--',20) )
71 print(msg)
72 print()
73end
74
75local function readdumpkeys(infile)
76 t = infile:read("*all")
77 len = string.len(t)
78 local len,hex = bin.unpack(("H%d"):format(len),t)
79 return hex
80end
81
82local function waitCmd()
b9534ca0 83 local response = core.WaitForResponseTimeout(cmds.CMD_ACK, TIMEOUT)
b915fda3 84 if response then
b9534ca0 85 local count, cmd, arg0 = bin.unpack('LL',response)
b915fda3 86 if(arg0==1) then
87 local count,arg1,arg2,data = bin.unpack('LLH511',response,count)
88 return data:sub(1,32)
89 else
b9534ca0 90 return nil, "Couldn't read block.. ["..arg0.."]"
b915fda3 91 end
92 end
b9534ca0 93 return nil, 'No response from device'
b915fda3 94end
95
b915fda3 96local function main(args)
97
98 print( string.rep('--',20) )
99 print( string.rep('--',20) )
100
101 local keyA
102 local cmd
103 local err
104 local useNested = false
04a6113f 105 local usePreCalc = false
b915fda3 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");
109
110 -- Arguments for the script
04a6113f 111 for o, a in getopt.getopt(args, 'hk:npo:') do
b915fda3 112 if o == "h" then return help() end
113 if o == "k" then keyA = a end
114 if o == "n" then useNested = true end
04a6113f 115 if o == "p" then usePreCalc = true end
b915fda3 116 if o == "o" then outputTemplate = a end
117 end
c3fe354b 118
b915fda3 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))
123 end
124
125 -- Turn off Debug
126 local cmdSetDbgOff = "hf mf dbg 0"
127 core.console( cmdSetDbgOff)
128
129 result, err = lib14a.read1443a(false)
130 if not result then
131 return oops(err)
132 end
133
134 core.clearCommandBuffer()
135
b915fda3 136 -- Show tag info
04a6113f 137 print((' Found tag %s'):format(result.name))
138
139 dbg(('Using keyA : %s'):format(keyA))
b915fda3 140
141 --Trying to find the other keys
142 if useNested then
143 core.console( ('hf mf nested 1 0 A %s d'):format(keyA) )
144 end
04a6113f 145
b915fda3 146 core.clearCommandBuffer()
147
04a6113f 148 local akeys = ''
149 if usePreCalc then
150 local pre = require('precalc')
151 akeys = pre.GetAll(result.uid)
b9534ca0 152 dbg(akeys)
04a6113f 153 else
154 print('Loading dumpkeys.bin')
155 local hex, err = utils.ReadDumpFile(input)
156 if not hex then
157 return oops(err)
158 end
159 akeys = hex:sub(0,12*16)
b915fda3 160 end
04a6113f 161
b915fda3 162 -- Read block 0
b9534ca0 163 dbg('Reading block 0')
164 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 0, arg2 = 0, arg3 = 0, data = keyA}
b915fda3 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
169
a826cb0d 170 core.clearCommandBuffer()
171
b915fda3 172 -- Read block 1
b9534ca0 173 dbg('Reading block 1')
174 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 1, arg2 = 0, arg3 = 0, data = keyA}
b915fda3 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
179
a826cb0d 180 core.clearCommandBuffer()
181
9c09e006 182 local tmpHash = block0..block1..'%02x'..RANDOM
183
b915fda3 184 local key
185 local pos = 0
186 local blockNo
187 local blocks = {}
a826cb0d 188
b915fda3 189 -- main loop
04a6113f 190 io.write('Reading blocks > ')
b915fda3 191 for blockNo = 0, numBlocks-1, 1 do
192
b9534ca0 193 io.flush()
194
b915fda3 195 if core.ukbhit() then
196 print("aborted by user")
197 break
198 end
199
a826cb0d 200 core.clearCommandBuffer()
201
b915fda3 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
209
40762506 210
b915fda3 211 if blockNo%4 ~= 3 then
40762506 212
b915fda3 213 if blockNo < 8 then
214 -- Block 0-7 not encrypted
0beb94e6 215 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
b915fda3 216 else
b915fda3 217 -- blocks with zero not encrypted.
218 if string.find(blockdata, '^0+$') then
0beb94e6 219 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
b915fda3 220 else
9c09e006 221 local baseStr = utils.ConvertHexToAscii(tmpHash:format(blockNo))
222 local key = md5.sumhexa(baseStr)
223 local aestest = core.aes128_decrypt(key, blockdata)
8977988f 224 local hex = utils.ConvertAsciiToHex(aestest)
a826cb0d 225
0beb94e6 226 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,hex)
227 io.write(blockNo..',')
b915fda3 228 end
229 end
230 else
231 -- Sectorblocks, not encrypted
232 blocks[blockNo+1] = ('%02d :: %s%s'):format(blockNo,key,blockdata:sub(13,32))
233 end
234 end
235 io.write('\n')
236
237 core.clearCommandBuffer()
238
239 -- Print results
240 local bindata = {}
241 local emldata = ''
242
243 for _,s in pairs(blocks) do
244 local slice = s:sub(8,#s)
a826cb0d 245 local str = utils.ConvertHexToAscii(slice)
b915fda3 246 emldata = emldata..slice..'\n'
247 for c in (str):gmatch('.') do
248 bindata[#bindata+1] = c
9c09e006 249 end
b915fda3 250 end
c3fe354b 251
252 print( string.rep('--',20) )
b915fda3 253
5149e37e 254 local uid = block0:sub(1,8)
c3fe354b 255 local toytype = block1:sub(1,4)
04a6113f 256 local cardidLsw = block1:sub(9,16)
257 local cardidMsw = block1:sub(16,24)
5149e37e 258 local cardid = block1:sub(9,24)
c3fe354b 259 local subtype = block1:sub(25,28)
5149e37e 260
b915fda3 261 -- Write dump to files
262 if not DEBUG then
9c09e006 263 local foo = dumplib.SaveAsBinary(bindata, outputTemplate..'-'..uid..'.bin')
04a6113f 264 print(("Wrote a BIN dump to: %s"):format(foo))
9c09e006 265 local bar = dumplib.SaveAsText(emldata, outputTemplate..'-'..uid..'.eml')
04a6113f 266 print(("Wrote a EML dump to: %s"):format(bar))
b915fda3 267 end
9c09e006 268
269 print( string.rep('--',20) )
270 -- Show info
b915fda3 271
c3fe354b 272 local item = toys.Find(toytype, subtype)
273 if item then
9c09e006 274 print((' ITEM TYPE : %s - %s (%s)'):format(item[6],item[5], item[4]) )
c3fe354b 275 else
276 print((' ITEM TYPE : 0x%s 0x%s'):format(toytype, subtype))
04a6113f 277 end
9c09e006 278
a8d4906b 279 print( (' UID : 0x%s'):format(uid) )
280 print( (' CARDID : 0x%s'):format(cardid ) )
b915fda3 281 print( string.rep('--',20) )
0beb94e6 282
283 core.clearCommandBuffer()
b915fda3 284end
285main(args)
Impressum, Datenschutz