]>
Commit | Line | Data |
---|---|---|
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') | |
9 | ||
10 | ||
11 | example =[[ | |
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 | ]] | |
22 | author = "Iceman" | |
23 | usage = "script run tnp3dump -k <key> -n -p -o <filename>" | |
24 | desc =[[ | |
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. | |
27 | Arguments: | |
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 | ||
35 | local HASHCONSTANT = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20' | |
36 | ||
37 | local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds | |
38 | local DEBUG = false -- the debug flag | |
39 | local numBlocks = 64 | |
40 | local numSectors = 16 | |
41 | --- | |
42 | -- A debug printout-function | |
43 | function 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 | |
57 | end | |
58 | --- | |
59 | -- This is only meant to be used when errors occur | |
60 | function oops(err) | |
61 | print("ERROR: ",err) | |
62 | end | |
63 | --- | |
64 | -- Usage help | |
65 | function help() | |
66 | print(desc) | |
67 | print("Example usage") | |
68 | print(example) | |
69 | end | |
70 | -- | |
71 | -- Exit message | |
72 | function ExitMsg(msg) | |
73 | print( string.rep('--',20) ) | |
74 | print( string.rep('--',20) ) | |
75 | print(msg) | |
76 | print() | |
77 | end | |
78 | ||
79 | local 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 | |
84 | end | |
85 | ||
86 | local 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" | |
98 | end | |
99 | ||
100 | local function computeCrc16(s) | |
101 | local hash = core.crc16(utils.ConvertHexToAscii(s)) | |
102 | return hash | |
103 | end | |
104 | ||
105 | local function reverseCrcBytes(crc) | |
106 | crc2 = crc:sub(3,4)..crc:sub(1,2) | |
107 | return tonumber(crc2,16) | |
108 | end | |
109 | ||
110 | local 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 | if blockNo%4 ~= 3 then | |
219 | if blockNo < 8 then | |
220 | -- Block 0-7 not encrypted | |
221 | blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata) | |
222 | else | |
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) | |
227 | ||
228 | local hex = utils.ConvertAsciiToBytes(aestest) | |
229 | hex = utils.ConvertBytesToHex(hex) | |
230 | ||
231 | -- blocks with zero not encrypted. | |
232 | if string.find(blockdata, '^0+$') then | |
233 | blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata) | |
234 | else | |
235 | blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,hex) | |
236 | io.write( blockNo..',') | |
237 | end | |
238 | end | |
239 | else | |
240 | -- Sectorblocks, not encrypted | |
241 | blocks[blockNo+1] = ('%02d :: %s%s'):format(blockNo,key,blockdata:sub(13,32)) | |
242 | end | |
243 | end | |
244 | io.write('\n') | |
245 | ||
246 | core.clearCommandBuffer() | |
247 | ||
248 | -- Print results | |
249 | local bindata = {} | |
250 | local emldata = '' | |
251 | ||
252 | for _,s in pairs(blocks) do | |
253 | local slice = s:sub(8,#s) | |
254 | local str = utils.ConvertBytesToAscii( | |
255 | utils.ConvertHexToBytes(slice) | |
256 | ) | |
257 | emldata = emldata..slice..'\n' | |
258 | for c in (str):gmatch('.') do | |
259 | bindata[#bindata+1] = c | |
260 | end | |
261 | end | |
262 | ||
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) | |
269 | ||
270 | -- Write dump to files | |
271 | if not DEBUG then | |
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)) | |
276 | end | |
277 | ||
278 | local itemtypename = toyNames[itemtype] | |
279 | if itemtypename == nil then | |
280 | itemtypename = toyNames[utils.SwapEndiannessStr(itemtype,16)] | |
281 | end | |
282 | -- Show info | |
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 ) ) | |
288 | ||
289 | print( string.rep('--',20) ) | |
290 | ||
291 | end | |
292 | main(args) |