]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/scripts/tnp3dump.lua
CHG: Added the parameters -fdata-sections -ffunction-sections to common/Makefile...
[proxmark3-svn] / client / scripts / tnp3dump.lua
... / ...
CommitLineData
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')
8local toyNames = require('default_toys')
9
10
11example =[[
12 script run tnp3dump
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
21]]
22author = "Iceman"
23usage = "script run tnp3dump -k <key> -n -p -o <filename>"
24desc =[[
25This script will try to dump the contents of a Mifare TNP3xxx card.
26It will need a valid KeyA in order to find the other keys and decode the card.
27Arguments:
28 -h : this help
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
33]]
34
35local HASHCONSTANT = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20'
36
37local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds
38local DEBUG = false -- the debug flag
39local numBlocks = 64
40local numSectors = 16
41---
42-- A debug printout-function
43function dbg(args)
44 if not DEBUG then
45 return
46 end
47
48 if type(args) == "table" then
49 local i = 1
50 while result[i] do
51 dbg(result[i])
52 i = i+1
53 end
54 else
55 print("###", args)
56 end
57end
58---
59-- This is only meant to be used when errors occur
60function oops(err)
61 print("ERROR: ",err)
62end
63---
64-- Usage help
65function help()
66 print(desc)
67 print("Example usage")
68 print(example)
69end
70--
71-- Exit message
72function ExitMsg(msg)
73 print( string.rep('--',20) )
74 print( string.rep('--',20) )
75 print(msg)
76 print()
77end
78
79local function readdumpkeys(infile)
80 t = infile:read("*all")
81 len = string.len(t)
82 local len,hex = bin.unpack(("H%d"):format(len),t)
83 return hex
84end
85
86local function waitCmd()
87 local response = core.WaitForResponseTimeout(cmds.CMD_ACK,TIMEOUT)
88 if response then
89 local count,cmd,arg0 = bin.unpack('LL',response)
90 if(arg0==1) then
91 local count,arg1,arg2,data = bin.unpack('LLH511',response,count)
92 return data:sub(1,32)
93 else
94 return nil, "Couldn't read block.."
95 end
96 end
97 return nil, "No response from device"
98end
99
100local function computeCrc16(s)
101 local hash = core.crc16(utils.ConvertHexToAscii(s))
102 return hash
103end
104
105local function reverseCrcBytes(crc)
106 crc2 = crc:sub(3,4)..crc:sub(1,2)
107 return tonumber(crc2,16)
108end
109
110local function main(args)
111
112 print( string.rep('--',20) )
113 print( string.rep('--',20) )
114
115 local keyA
116 local cmd
117 local err
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");
123
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
131 end
132
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))
137 end
138
139 -- Turn off Debug
140 local cmdSetDbgOff = "hf mf dbg 0"
141 core.console( cmdSetDbgOff)
142
143 result, err = lib14a.read1443a(false)
144 if not result then
145 return oops(err)
146 end
147
148 core.clearCommandBuffer()
149
150 if 0x01 ~= result.sak then -- NXP MIFARE TNP3xxx
151 -- return oops('This is not a TNP3xxx tag. aborting.')
152 end
153
154 -- Show tag info
155 print((' Found tag %s'):format(result.name))
156
157 dbg(('Using keyA : %s'):format(keyA))
158
159 --Trying to find the other keys
160 if useNested then
161 core.console( ('hf mf nested 1 0 A %s d'):format(keyA) )
162 end
163
164 core.clearCommandBuffer()
165
166 local akeys = ''
167 if usePreCalc then
168 local pre = require('precalc')
169 akeys = pre.GetAll(result.uid)
170 else
171 print('Loading dumpkeys.bin')
172 local hex, err = utils.ReadDumpFile(input)
173 if not hex then
174 return oops(err)
175 end
176 akeys = hex:sub(0,12*16)
177 end
178
179 -- Read block 0
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
185
186 -- Read block 1
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
192
193 local key
194 local pos = 0
195 local blockNo
196 local blocks = {}
197
198 print('Reading card data')
199 core.clearCommandBuffer()
200
201 -- main loop
202 io.write('Reading blocks > ')
203 for blockNo = 0, numBlocks-1, 1 do
204
205 if core.ukbhit() then
206 print("aborted by user")
207 break
208 end
209
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
217
218
219 if blockNo%4 ~= 3 then
220
221 if blockNo < 8 then
222 -- Block 0-7 not encrypted
223 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
224 else
225 local base = ('%s%s%02x%s'):format(block0, block1, blockNo, HASHCONSTANT)
226 local baseStr = utils.ConvertHexToAscii(base)
227 local md5hash = md5.sumhexa(baseStr)
228 local aestest = core.aes(md5hash, blockdata)
229
230 local hex = utils.ConvertAsciiToBytes(aestest)
231 hex = utils.ConvertBytesToHex(hex)
232
233 -- blocks with zero not encrypted.
234 if string.find(blockdata, '^0+$') then
235 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
236 else
237 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,hex)
238 io.write( blockNo..',')
239 end
240 end
241 else
242 -- Sectorblocks, not encrypted
243 blocks[blockNo+1] = ('%02d :: %s%s'):format(blockNo,key,blockdata:sub(13,32))
244 end
245 end
246 io.write('\n')
247
248 core.clearCommandBuffer()
249
250 -- Print results
251 local bindata = {}
252 local emldata = ''
253
254 for _,s in pairs(blocks) do
255 local slice = s:sub(8,#s)
256 local str = utils.ConvertBytesToAscii(
257 utils.ConvertHexToBytes(slice)
258 )
259 emldata = emldata..slice..'\n'
260 for c in (str):gmatch('.') do
261 bindata[#bindata+1] = c
262 end
263 end
264
265 local uid = block0:sub(1,8)
266 local itemtype = block1:sub(1,4)
267 local cardidLsw = block1:sub(9,16)
268 local cardidMsw = block1:sub(16,24)
269 local cardid = block1:sub(9,24)
270 local traptype = block1:sub(25,28)
271
272 -- Write dump to files
273 if not DEBUG then
274 local foo = dumplib.SaveAsBinary(bindata, outputTemplate..'_uid_'..uid..'.bin')
275 print(("Wrote a BIN dump to: %s"):format(foo))
276 local bar = dumplib.SaveAsText(emldata, outputTemplate..'_uid_'..uid..'.eml')
277 print(("Wrote a EML dump to: %s"):format(bar))
278 end
279
280 local itemtypename = toyNames[itemtype]
281 if itemtypename == nil then
282 itemtypename = toyNames[utils.SwapEndiannessStr(itemtype,16)]
283 end
284 -- Show info
285 print( string.rep('--',20) )
286 print( (' ITEM TYPE : 0x%s - %s'):format(itemtype, itemtypename) )
287 print( (' Alter ego / traptype : 0x%s'):format(traptype) )
288 print( (' UID : 0x%s'):format(uid) )
289 print( (' CARDID : 0x%s'):format(cardid ) )
290
291 print( string.rep('--',20) )
292
293end
294main(args)
Impressum, Datenschutz