]>
Commit | Line | Data |
---|---|---|
1 | //-----------------------------------------------------------------------------\r | |
2 | // Copyright (C) 2011,2012 Merlok\r | |
3 | //\r | |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or,\r | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of\r | |
6 | // the license.\r | |
7 | //-----------------------------------------------------------------------------\r | |
8 | // High frequency MIFARE commands\r | |
9 | //-----------------------------------------------------------------------------\r | |
10 | \r | |
11 | #include "cmdhfmf.h"\r | |
12 | \r | |
13 | static int CmdHelp(const char *Cmd);\r | |
14 | \r | |
15 | int CmdHF14AMifare(const char *Cmd)\r | |
16 | {\r | |
17 | uint32_t uid = 0;\r | |
18 | uint32_t nt = 0, nr = 0;\r | |
19 | uint64_t par_list = 0, ks_list = 0, r_key = 0;\r | |
20 | uint8_t isOK = 0;\r | |
21 | uint8_t keyBlock[8] = {0};\r | |
22 | \r | |
23 | UsbCommand c = {CMD_READER_MIFARE, {true, 0, 0}};\r | |
24 | \r | |
25 | // message\r | |
26 | printf("-------------------------------------------------------------------------\n");\r | |
27 | printf("Executing command. Expected execution time: 25sec on average :-)\n");\r | |
28 | printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r | |
29 | printf("-------------------------------------------------------------------------\n");\r | |
30 | \r | |
31 | \r | |
32 | start:\r | |
33 | clearCommandBuffer();\r | |
34 | SendCommand(&c);\r | |
35 | \r | |
36 | //flush queue\r | |
37 | while (ukbhit()) getchar();\r | |
38 | \r | |
39 | \r | |
40 | // wait cycle\r | |
41 | while (true) {\r | |
42 | printf(".");\r | |
43 | fflush(stdout);\r | |
44 | if (ukbhit()) {\r | |
45 | getchar();\r | |
46 | printf("\naborted via keyboard!\n");\r | |
47 | break;\r | |
48 | }\r | |
49 | \r | |
50 | UsbCommand resp;\r | |
51 | if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {\r | |
52 | isOK = resp.arg[0] & 0xff;\r | |
53 | uid = (uint32_t)bytes_to_num(resp.d.asBytes + 0, 4);\r | |
54 | nt = (uint32_t)bytes_to_num(resp.d.asBytes + 4, 4);\r | |
55 | par_list = bytes_to_num(resp.d.asBytes + 8, 8);\r | |
56 | ks_list = bytes_to_num(resp.d.asBytes + 16, 8);\r | |
57 | nr = bytes_to_num(resp.d.asBytes + 24, 4);\r | |
58 | printf("\n\n");\r | |
59 | if (!isOK) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n");\r | |
60 | break;\r | |
61 | }\r | |
62 | } \r | |
63 | \r | |
64 | printf("\n");\r | |
65 | \r | |
66 | // error\r | |
67 | if (isOK != 1) return 1;\r | |
68 | \r | |
69 | // execute original function from util nonce2key\r | |
70 | if (nonce2key(uid, nt, nr, par_list, ks_list, &r_key))\r | |
71 | {\r | |
72 | isOK = 2;\r | |
73 | PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt); \r | |
74 | } else {\r | |
75 | printf("------------------------------------------------------------------\n");\r | |
76 | PrintAndLog("Key found:%012"llx" \n", r_key);\r | |
77 | \r | |
78 | num_to_bytes(r_key, 6, keyBlock);\r | |
79 | isOK = mfCheckKeys(0, 0, 1, keyBlock, &r_key);\r | |
80 | }\r | |
81 | if (!isOK) \r | |
82 | PrintAndLog("Found valid key:%012"llx, r_key);\r | |
83 | else\r | |
84 | {\r | |
85 | if (isOK != 2) PrintAndLog("Found invalid key. "); \r | |
86 | PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");\r | |
87 | c.arg[0] = false;\r | |
88 | goto start;\r | |
89 | }\r | |
90 | \r | |
91 | return 0;\r | |
92 | }\r | |
93 | \r | |
94 | int CmdHF14AMfWrBl(const char *Cmd)\r | |
95 | {\r | |
96 | uint8_t blockNo = 0;\r | |
97 | uint8_t keyType = 0;\r | |
98 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
99 | uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r | |
100 | \r | |
101 | char cmdp = 0x00;\r | |
102 | \r | |
103 | if (strlen(Cmd)<3) {\r | |
104 | PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");\r | |
105 | PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");\r | |
106 | return 0;\r | |
107 | } \r | |
108 | \r | |
109 | blockNo = param_get8(Cmd, 0);\r | |
110 | cmdp = param_getchar(Cmd, 1);\r | |
111 | if (cmdp == 0x00) {\r | |
112 | PrintAndLog("Key type must be A or B");\r | |
113 | return 1;\r | |
114 | }\r | |
115 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
116 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
117 | PrintAndLog("Key must include 12 HEX symbols");\r | |
118 | return 1;\r | |
119 | }\r | |
120 | if (param_gethex(Cmd, 3, bldata, 32)) {\r | |
121 | PrintAndLog("Block data must include 32 HEX symbols");\r | |
122 | return 1;\r | |
123 | }\r | |
124 | PrintAndLog("--block no:%02x key type:%02x key:%s", blockNo, keyType, sprint_hex(key, 6));\r | |
125 | PrintAndLog("--data: %s", sprint_hex(bldata, 16));\r | |
126 | \r | |
127 | UsbCommand c = {CMD_MIFARE_WRITEBL, {blockNo, keyType, 0}};\r | |
128 | memcpy(c.d.asBytes, key, 6);\r | |
129 | memcpy(c.d.asBytes + 10, bldata, 16);\r | |
130 | SendCommand(&c);\r | |
131 | \r | |
132 | UsbCommand resp;\r | |
133 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
134 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
135 | PrintAndLog("isOk:%02x", isOK);\r | |
136 | } else {\r | |
137 | PrintAndLog("Command execute timeout");\r | |
138 | }\r | |
139 | \r | |
140 | return 0;\r | |
141 | }\r | |
142 | \r | |
143 | int CmdHF14AMfUWrBl(const char *Cmd)\r | |
144 | {\r | |
145 | uint8_t blockNo = 0;\r | |
146 | uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r | |
147 | UsbCommand resp;\r | |
148 | \r | |
149 | if (strlen(Cmd)<3) {\r | |
150 | PrintAndLog("Usage: hf mf uwrbl <block number> <block data (8 hex symbols)>");\r | |
151 | PrintAndLog(" sample: hf mf uwrbl 0 01020304");\r | |
152 | return 0;\r | |
153 | } \r | |
154 | \r | |
155 | blockNo = param_get8(Cmd, 0);\r | |
156 | if (param_gethex(Cmd, 1, bldata, 8)) {\r | |
157 | PrintAndLog("Block data must include 8 HEX symbols");\r | |
158 | return 1;\r | |
159 | }\r | |
160 | \r | |
161 | switch(blockNo)\r | |
162 | {\r | |
163 | case 0:\r | |
164 | PrintAndLog("Access Denied");\r | |
165 | break;\r | |
166 | case 1:\r | |
167 | PrintAndLog("Access Denied");\r | |
168 | break;\r | |
169 | case 2:\r | |
170 | PrintAndLog("--specialblock no:%02x", blockNo);\r | |
171 | PrintAndLog("--data: %s", sprint_hex(bldata, 4));\r | |
172 | UsbCommand c = {CMD_MIFAREU_WRITEBL, {blockNo}};\r | |
173 | memcpy(c.d.asBytes, bldata, 4);\r | |
174 | SendCommand(&c);\r | |
175 | \r | |
176 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
177 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
178 | PrintAndLog("isOk:%02x", isOK);\r | |
179 | } else {\r | |
180 | PrintAndLog("Command execute timeout");\r | |
181 | }\r | |
182 | break;\r | |
183 | case 3:\r | |
184 | PrintAndLog("--specialblock no:%02x", blockNo);\r | |
185 | PrintAndLog("--data: %s", sprint_hex(bldata, 4));\r | |
186 | UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};\r | |
187 | memcpy(d.d.asBytes,bldata, 4);\r | |
188 | SendCommand(&d);\r | |
189 | \r | |
190 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
191 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
192 | PrintAndLog("isOk:%02x", isOK);\r | |
193 | } else {\r | |
194 | PrintAndLog("Command execute timeout");\r | |
195 | }\r | |
196 | break;\r | |
197 | default: \r | |
198 | PrintAndLog("--block no:%02x", blockNo);\r | |
199 | PrintAndLog("--data: %s", sprint_hex(bldata, 4)); \r | |
200 | //UsbCommand e = {CMD_MIFAREU_WRITEBL_COMPAT, {blockNo}};\r | |
201 | //memcpy(e.d.asBytes,bldata, 16);\r | |
202 | UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}};\r | |
203 | memcpy(e.d.asBytes,bldata, 4);\r | |
204 | SendCommand(&e);\r | |
205 | \r | |
206 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
207 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
208 | PrintAndLog("isOk:%02x", isOK);\r | |
209 | } else {\r | |
210 | PrintAndLog("Command execute timeout");\r | |
211 | }\r | |
212 | break;\r | |
213 | }\r | |
214 | return 0;\r | |
215 | }\r | |
216 | \r | |
217 | int CmdHF14AMfRdBl(const char *Cmd)\r | |
218 | {\r | |
219 | uint8_t blockNo = 0;\r | |
220 | uint8_t keyType = 0;\r | |
221 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
222 | \r | |
223 | char cmdp = 0x00;\r | |
224 | \r | |
225 | \r | |
226 | if (strlen(Cmd)<3) {\r | |
227 | PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");\r | |
228 | PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");\r | |
229 | return 0;\r | |
230 | } \r | |
231 | \r | |
232 | blockNo = param_get8(Cmd, 0);\r | |
233 | cmdp = param_getchar(Cmd, 1);\r | |
234 | if (cmdp == 0x00) {\r | |
235 | PrintAndLog("Key type must be A or B");\r | |
236 | return 1;\r | |
237 | }\r | |
238 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
239 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
240 | PrintAndLog("Key must include 12 HEX symbols");\r | |
241 | return 1;\r | |
242 | }\r | |
243 | PrintAndLog("--block no:%02x key type:%02x key:%s ", blockNo, keyType, sprint_hex(key, 6));\r | |
244 | \r | |
245 | UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};\r | |
246 | memcpy(c.d.asBytes, key, 6);\r | |
247 | SendCommand(&c);\r | |
248 | \r | |
249 | UsbCommand resp;\r | |
250 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
251 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
252 | uint8_t * data = resp.d.asBytes;\r | |
253 | \r | |
254 | if (isOK)\r | |
255 | PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 16));\r | |
256 | else\r | |
257 | PrintAndLog("isOk:%02x", isOK);\r | |
258 | } else {\r | |
259 | PrintAndLog("Command execute timeout");\r | |
260 | }\r | |
261 | \r | |
262 | return 0;\r | |
263 | }\r | |
264 | \r | |
265 | int CmdHF14AMfURdBl(const char *Cmd)\r | |
266 | {\r | |
267 | uint8_t blockNo = 0;\r | |
268 | \r | |
269 | if (strlen(Cmd)<1) {\r | |
270 | PrintAndLog("Usage: hf mf urdbl <block number>");\r | |
271 | PrintAndLog(" sample: hf mf urdbl 0");\r | |
272 | return 0;\r | |
273 | } \r | |
274 | \r | |
275 | blockNo = param_get8(Cmd, 0);\r | |
276 | PrintAndLog("--block no:%02x", blockNo);\r | |
277 | \r | |
278 | UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}};\r | |
279 | SendCommand(&c);\r | |
280 | \r | |
281 | UsbCommand resp;\r | |
282 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
283 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
284 | uint8_t * data = resp.d.asBytes;\r | |
285 | \r | |
286 | if (isOK)\r | |
287 | PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 4));\r | |
288 | else\r | |
289 | PrintAndLog("isOk:%02x", isOK);\r | |
290 | } else {\r | |
291 | PrintAndLog("Command execute timeout");\r | |
292 | }\r | |
293 | \r | |
294 | return 0;\r | |
295 | }\r | |
296 | \r | |
297 | int CmdHF14AMfURdCard(const char *Cmd)\r | |
298 | {\r | |
299 | int i;\r | |
300 | uint8_t sectorNo = 0;\r | |
301 | uint8_t *lockbytes_t=NULL;\r | |
302 | uint8_t lockbytes[2]={0,0};\r | |
303 | bool bit[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};\r | |
304 | \r | |
305 | uint8_t isOK = 0;\r | |
306 | uint8_t * data = NULL;\r | |
307 | \r | |
308 | if (sectorNo > 15) {\r | |
309 | PrintAndLog("Sector number must be less than 16");\r | |
310 | return 1;\r | |
311 | }\r | |
312 | PrintAndLog("Attempting to Read Ultralight... ");\r | |
313 | \r | |
314 | UsbCommand c = {CMD_MIFAREU_READCARD, {sectorNo}};\r | |
315 | SendCommand(&c);\r | |
316 | \r | |
317 | UsbCommand resp;\r | |
318 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
319 | isOK = resp.arg[0] & 0xff;\r | |
320 | data = resp.d.asBytes;\r | |
321 | \r | |
322 | PrintAndLog("isOk:%02x", isOK);\r | |
323 | if (isOK) \r | |
324 | for (i = 0; i < 16; i++) {\r | |
325 | switch(i){\r | |
326 | case 2:\r | |
327 | //process lock bytes\r | |
328 | lockbytes_t=data+(i*4);\r | |
329 | lockbytes[0]=lockbytes_t[2];\r | |
330 | lockbytes[1]=lockbytes_t[3];\r | |
331 | for(int j=0; j<16; j++){\r | |
332 | bit[j]=lockbytes[j/8] & ( 1 <<(7-j%8));\r | |
333 | }\r | |
334 | //PrintAndLog("LB %02x %02x", lockbytes[0],lockbytes[1]);\r | |
335 | //PrintAndLog("LB2b %02x %02x %02x %02x %02x %02x %02x %02x",bit[8],bit[9],bit[10],bit[11],bit[12],bit[13],bit[14],bit[15]); \r | |
336 | PrintAndLog("Block %02x:%s ", i,sprint_hex(data + i * 4, 4));\r | |
337 | break;\r | |
338 | case 3: \r | |
339 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[4]);\r | |
340 | break;\r | |
341 | case 4:\r | |
342 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[3]);\r | |
343 | break;\r | |
344 | case 5:\r | |
345 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[2]);\r | |
346 | break;\r | |
347 | case 6:\r | |
348 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[1]);\r | |
349 | break;\r | |
350 | case 7:\r | |
351 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[0]);\r | |
352 | break;\r | |
353 | case 8:\r | |
354 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[15]);\r | |
355 | break;\r | |
356 | case 9:\r | |
357 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[14]);\r | |
358 | break;\r | |
359 | case 10:\r | |
360 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[13]);\r | |
361 | break;\r | |
362 | case 11:\r | |
363 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[12]);\r | |
364 | break;\r | |
365 | case 12:\r | |
366 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[11]);\r | |
367 | break;\r | |
368 | case 13:\r | |
369 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[10]);\r | |
370 | break;\r | |
371 | case 14:\r | |
372 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[9]);\r | |
373 | break;\r | |
374 | case 15:\r | |
375 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[8]);\r | |
376 | break;\r | |
377 | default:\r | |
378 | PrintAndLog("Block %02x:%s ", i,sprint_hex(data + i * 4, 4));\r | |
379 | break;\r | |
380 | }\r | |
381 | }\r | |
382 | } else {\r | |
383 | PrintAndLog("Command1 execute timeout");\r | |
384 | }\r | |
385 | return 0;\r | |
386 | }\r | |
387 | \r | |
388 | int CmdHF14AMfRdSc(const char *Cmd)\r | |
389 | {\r | |
390 | int i;\r | |
391 | uint8_t sectorNo = 0;\r | |
392 | uint8_t keyType = 0;\r | |
393 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
394 | \r | |
395 | uint8_t isOK = 0;\r | |
396 | uint8_t * data = NULL;\r | |
397 | \r | |
398 | char cmdp = 0x00;\r | |
399 | \r | |
400 | if (strlen(Cmd)<3) {\r | |
401 | PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");\r | |
402 | PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");\r | |
403 | return 0;\r | |
404 | } \r | |
405 | \r | |
406 | sectorNo = param_get8(Cmd, 0);\r | |
407 | if (sectorNo > 63) {\r | |
408 | PrintAndLog("Sector number must be less than 64");\r | |
409 | return 1;\r | |
410 | }\r | |
411 | cmdp = param_getchar(Cmd, 1);\r | |
412 | if (cmdp == 0x00) {\r | |
413 | PrintAndLog("Key type must be A or B");\r | |
414 | return 1;\r | |
415 | }\r | |
416 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
417 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
418 | PrintAndLog("Key must include 12 HEX symbols");\r | |
419 | return 1;\r | |
420 | }\r | |
421 | PrintAndLog("--sector no:%02x key type:%02x key:%s ", sectorNo, keyType, sprint_hex(key, 6));\r | |
422 | \r | |
423 | UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};\r | |
424 | memcpy(c.d.asBytes, key, 6);\r | |
425 | SendCommand(&c);\r | |
426 | PrintAndLog(" ");\r | |
427 | \r | |
428 | UsbCommand resp;\r | |
429 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
430 | isOK = resp.arg[0] & 0xff;\r | |
431 | data = resp.d.asBytes;\r | |
432 | \r | |
433 | PrintAndLog("isOk:%02x", isOK);\r | |
434 | if (isOK) \r | |
435 | for (i = 0; i < 2; i++) {\r | |
436 | PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));\r | |
437 | }\r | |
438 | } else {\r | |
439 | PrintAndLog("Command1 execute timeout");\r | |
440 | }\r | |
441 | \r | |
442 | // response2\r | |
443 | PrintAndLog(" ");\r | |
444 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
445 | isOK = resp.arg[0] & 0xff;\r | |
446 | data = resp.d.asBytes;\r | |
447 | \r | |
448 | if (isOK) \r | |
449 | for (i = 0; i < 2; i++) {\r | |
450 | PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));\r | |
451 | }\r | |
452 | } else {\r | |
453 | PrintAndLog("Command2 execute timeout");\r | |
454 | }\r | |
455 | \r | |
456 | return 0;\r | |
457 | }\r | |
458 | \r | |
459 | int CmdHF14AMfDump(const char *Cmd)\r | |
460 | {\r | |
461 | int i, j;\r | |
462 | \r | |
463 | uint8_t keyA[40][6];\r | |
464 | uint8_t keyB[40][6];\r | |
465 | uint8_t rights[40][4];\r | |
466 | \r | |
467 | FILE *fin;\r | |
468 | FILE *fout;\r | |
469 | \r | |
470 | UsbCommand resp;\r | |
471 | \r | |
472 | if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {\r | |
473 | PrintAndLog("Could not find file dumpkeys.bin");\r | |
474 | return 1;\r | |
475 | }\r | |
476 | \r | |
477 | if ((fout = fopen("dumpdata.bin","wb")) == NULL) { \r | |
478 | PrintAndLog("Could not create file name dumpdata.bin");\r | |
479 | return 1;\r | |
480 | }\r | |
481 | \r | |
482 | // Read key file\r | |
483 | \r | |
484 | for (i=0 ; i<16 ; i++) {\r | |
485 | if (fread( keyA[i], 1, 6, fin ) == 0) {\r | |
486 | PrintAndLog("File reading error.");\r | |
487 | return 2;\r | |
488 | }\r | |
489 | }\r | |
490 | for (i=0 ; i<16 ; i++) {\r | |
491 | if (fread( keyB[i], 1, 6, fin ) == 0) {\r | |
492 | PrintAndLog("File reading error.");\r | |
493 | return 2;\r | |
494 | }\r | |
495 | }\r | |
496 | \r | |
497 | // Read access rights to sectors\r | |
498 | \r | |
499 | PrintAndLog("|-----------------------------------------|");\r | |
500 | PrintAndLog("|------ Reading sector access bits...-----|");\r | |
501 | PrintAndLog("|-----------------------------------------|");\r | |
502 | \r | |
503 | for (i = 0 ; i < 16 ; i++) {\r | |
504 | UsbCommand c = {CMD_MIFARE_READBL, {4*i + 3, 0, 0}};\r | |
505 | memcpy(c.d.asBytes, keyA[i], 6);\r | |
506 | SendCommand(&c);\r | |
507 | \r | |
508 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
509 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
510 | uint8_t *data = resp.d.asBytes;\r | |
511 | if (isOK){\r | |
512 | rights[i][0] = ((data[7] & 0x10)>>4) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>2);\r | |
513 | rights[i][1] = ((data[7] & 0x20)>>5) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>3);\r | |
514 | rights[i][2] = ((data[7] & 0x40)>>6) | ((data[8] & 0x4)>>1) | ((data[8] & 0x40)>>4);\r | |
515 | rights[i][3] = ((data[7] & 0x80)>>7) | ((data[8] & 0x8)>>2) | ((data[8] & 0x80)>>5);\r | |
516 | }\r | |
517 | else{\r | |
518 | PrintAndLog("Could not get access rights for block %d", i);\r | |
519 | }\r | |
520 | }\r | |
521 | else {\r | |
522 | PrintAndLog("Command execute timeout");\r | |
523 | }\r | |
524 | }\r | |
525 | \r | |
526 | // Read blocks and print to file\r | |
527 | \r | |
528 | PrintAndLog("|-----------------------------------------|");\r | |
529 | PrintAndLog("|----- Dumping all blocks to file... -----|");\r | |
530 | PrintAndLog("|-----------------------------------------|");\r | |
531 | \r | |
532 | \r | |
533 | for (i=0 ; i<16 ; i++) {\r | |
534 | for (j=0 ; j<4 ; j++) {\r | |
535 | bool received = false;\r | |
536 | \r | |
537 | if (j == 3){\r | |
538 | UsbCommand c = {CMD_MIFARE_READBL, {i*4 + j, 0, 0}};\r | |
539 | memcpy(c.d.asBytes, keyA[i], 6);\r | |
540 | SendCommand(&c);\r | |
541 | received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r | |
542 | }\r | |
543 | else{\r | |
544 | if ((rights[i][j] == 6) | (rights[i][j] == 5)) {\r | |
545 | UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 1, 0}};\r | |
546 | memcpy(c.d.asBytes, keyB[i], 6);\r | |
547 | SendCommand(&c);\r | |
548 | received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r | |
549 | }\r | |
550 | else if (rights[i][j] == 7) {\r | |
551 | PrintAndLog("Access rights do not allow reading of sector %d block %d",i,j);\r | |
552 | }\r | |
553 | else {\r | |
554 | UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 0, 0}};\r | |
555 | memcpy(c.d.asBytes, keyA[i], 6);\r | |
556 | SendCommand(&c);\r | |
557 | received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r | |
558 | }\r | |
559 | }\r | |
560 | \r | |
561 | if (received) {\r | |
562 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
563 | uint8_t *data = resp.d.asBytes;\r | |
564 | if (j == 3) {\r | |
565 | data[0] = (keyA[i][0]);\r | |
566 | data[1] = (keyA[i][1]);\r | |
567 | data[2] = (keyA[i][2]);\r | |
568 | data[3] = (keyA[i][3]);\r | |
569 | data[4] = (keyA[i][4]);\r | |
570 | data[5] = (keyA[i][5]);\r | |
571 | data[10] = (keyB[i][0]);\r | |
572 | data[11] = (keyB[i][1]);\r | |
573 | data[12] = (keyB[i][2]);\r | |
574 | data[13] = (keyB[i][3]);\r | |
575 | data[14] = (keyB[i][4]);\r | |
576 | data[15] = (keyB[i][5]);\r | |
577 | }\r | |
578 | if (isOK) {\r | |
579 | fwrite ( data, 1, 16, fout );\r | |
580 | PrintAndLog("Dumped card data into 'dumpdata.bin'");\r | |
581 | \r | |
582 | }\r | |
583 | else {\r | |
584 | PrintAndLog("Could not get access rights for block %d", i);\r | |
585 | }\r | |
586 | }\r | |
587 | else {\r | |
588 | PrintAndLog("Command execute timeout");\r | |
589 | }\r | |
590 | }\r | |
591 | }\r | |
592 | \r | |
593 | fclose(fin);\r | |
594 | fclose(fout);\r | |
595 | return 0;\r | |
596 | }\r | |
597 | \r | |
598 | int CmdHF14AMfRestore(const char *Cmd)\r | |
599 | {\r | |
600 | \r | |
601 | int i,j;\r | |
602 | uint8_t keyType = 0;\r | |
603 | uint8_t key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r | |
604 | uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r | |
605 | uint8_t keyA[16][6];\r | |
606 | uint8_t keyB[16][6];\r | |
607 | \r | |
608 | FILE *fdump;\r | |
609 | FILE *fkeys;\r | |
610 | \r | |
611 | if ((fdump = fopen("dumpdata.bin","rb")) == NULL) {\r | |
612 | PrintAndLog("Could not find file dumpdata.bin");\r | |
613 | return 1;\r | |
614 | }\r | |
615 | if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) {\r | |
616 | PrintAndLog("Could not find file dumpkeys.bin");\r | |
617 | return 1;\r | |
618 | }\r | |
619 | \r | |
620 | for (i=0 ; i<16 ; i++) {\r | |
621 | if (fread(keyA[i], 1, 6, fkeys) == 0) {\r | |
622 | PrintAndLog("File reading error.");\r | |
623 | return 2;\r | |
624 | }\r | |
625 | }\r | |
626 | for (i=0 ; i<16 ; i++) {\r | |
627 | if (fread(keyB[i], 1, 6, fkeys) == 0) {\r | |
628 | PrintAndLog("File reading error.");\r | |
629 | return 2;\r | |
630 | }\r | |
631 | }\r | |
632 | \r | |
633 | PrintAndLog("Restoring dumpdata.bin to card");\r | |
634 | \r | |
635 | for (i=0 ; i<16 ; i++) {\r | |
636 | for( j=0 ; j<4 ; j++) {\r | |
637 | UsbCommand c = {CMD_MIFARE_WRITEBL, {i*4 + j, keyType, 0}};\r | |
638 | memcpy(c.d.asBytes, key, 6);\r | |
639 | \r | |
640 | if (fread(bldata, 1, 16, fdump) == 0) {\r | |
641 | PrintAndLog("File reading error.");\r | |
642 | return 2;\r | |
643 | }\r | |
644 | \r | |
645 | if (j == 3) {\r | |
646 | bldata[0] = (keyA[i][0]);\r | |
647 | bldata[1] = (keyA[i][1]);\r | |
648 | bldata[2] = (keyA[i][2]);\r | |
649 | bldata[3] = (keyA[i][3]);\r | |
650 | bldata[4] = (keyA[i][4]);\r | |
651 | bldata[5] = (keyA[i][5]);\r | |
652 | bldata[10] = (keyB[i][0]);\r | |
653 | bldata[11] = (keyB[i][1]);\r | |
654 | bldata[12] = (keyB[i][2]);\r | |
655 | bldata[13] = (keyB[i][3]);\r | |
656 | bldata[14] = (keyB[i][4]);\r | |
657 | bldata[15] = (keyB[i][5]);\r | |
658 | } \r | |
659 | \r | |
660 | PrintAndLog("Writing to block %2d: %s", i*4+j, sprint_hex(bldata, 16));\r | |
661 | \r | |
662 | /*\r | |
663 | PrintAndLog("Writing to block %2d: %s Confirm? [Y,N]", i*4+j, sprint_hex(bldata, 16));\r | |
664 | \r | |
665 | scanf("%c",&ch);\r | |
666 | if ((ch != 'y') && (ch != 'Y')){\r | |
667 | PrintAndLog("Aborting !");\r | |
668 | return 1;\r | |
669 | }\r | |
670 | */\r | |
671 | \r | |
672 | memcpy(c.d.asBytes + 10, bldata, 16);\r | |
673 | SendCommand(&c);\r | |
674 | \r | |
675 | UsbCommand resp;\r | |
676 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
677 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
678 | PrintAndLog("isOk:%02x", isOK);\r | |
679 | } else {\r | |
680 | PrintAndLog("Command execute timeout");\r | |
681 | }\r | |
682 | }\r | |
683 | }\r | |
684 | \r | |
685 | fclose(fdump);\r | |
686 | fclose(fkeys);\r | |
687 | return 0;\r | |
688 | }\r | |
689 | \r | |
690 | int CmdHF14AMfNested(const char *Cmd)\r | |
691 | {\r | |
692 | int i, j, res, iterations;\r | |
693 | sector * e_sector = NULL;\r | |
694 | uint8_t blockNo = 0;\r | |
695 | uint8_t keyType = 0;\r | |
696 | uint8_t trgBlockNo = 0;\r | |
697 | uint8_t trgKeyType = 0;\r | |
698 | uint8_t blDiff = 0;\r | |
699 | int SectorsCnt = 0;\r | |
700 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
701 | uint8_t keyBlock[6*6];\r | |
702 | uint64_t key64 = 0;\r | |
703 | int transferToEml = 0;\r | |
704 | \r | |
705 | int createDumpFile = 0;\r | |
706 | FILE *fkeys;\r | |
707 | uint8_t standart[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r | |
708 | uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r | |
709 | \r | |
710 | char cmdp, ctmp;\r | |
711 | \r | |
712 | if (strlen(Cmd)<3) {\r | |
713 | PrintAndLog("Usage:");\r | |
714 | PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");\r | |
715 | PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");\r | |
716 | PrintAndLog(" <target block number> <target key A/B> [t]");\r | |
717 | PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r | |
718 | PrintAndLog("t - transfer keys into emulator memory");\r | |
719 | PrintAndLog("d - write keys to binary file");\r | |
720 | PrintAndLog(" ");\r | |
721 | PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");\r | |
722 | PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");\r | |
723 | PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");\r | |
724 | PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");\r | |
725 | return 0;\r | |
726 | } \r | |
727 | \r | |
728 | cmdp = param_getchar(Cmd, 0);\r | |
729 | blockNo = param_get8(Cmd, 1);\r | |
730 | ctmp = param_getchar(Cmd, 2);\r | |
731 | if (ctmp == 0x00) {\r | |
732 | PrintAndLog("Key type must be A or B");\r | |
733 | return 1;\r | |
734 | }\r | |
735 | if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r | |
736 | if (param_gethex(Cmd, 3, key, 12)) {\r | |
737 | PrintAndLog("Key must include 12 HEX symbols");\r | |
738 | return 1;\r | |
739 | }\r | |
740 | \r | |
741 | if (cmdp == 'o' || cmdp == 'O') {\r | |
742 | cmdp = 'o';\r | |
743 | trgBlockNo = param_get8(Cmd, 4);\r | |
744 | ctmp = param_getchar(Cmd, 5);\r | |
745 | if (ctmp == 0x00) {\r | |
746 | PrintAndLog("Target key type must be A or B");\r | |
747 | return 1;\r | |
748 | }\r | |
749 | if (ctmp != 'A' && ctmp != 'a') trgKeyType = 1;\r | |
750 | } else {\r | |
751 | switch (cmdp) {\r | |
752 | case '0': SectorsCnt = 05; break;\r | |
753 | case '1': SectorsCnt = 16; break;\r | |
754 | case '2': SectorsCnt = 32; break;\r | |
755 | case '4': SectorsCnt = 64; break;\r | |
756 | default: SectorsCnt = 16;\r | |
757 | }\r | |
758 | }\r | |
759 | \r | |
760 | ctmp = param_getchar(Cmd, 4);\r | |
761 | if (ctmp == 't' || ctmp == 'T') transferToEml = 1;\r | |
762 | else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;\r | |
763 | \r | |
764 | ctmp = param_getchar(Cmd, 6);\r | |
765 | transferToEml |= (ctmp == 't' || ctmp == 'T');\r | |
766 | transferToEml |= (ctmp == 'd' || ctmp == 'D');\r | |
767 | \r | |
768 | PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo, keyType, sprint_hex(key, 6), transferToEml);\r | |
769 | if (cmdp == 'o')\r | |
770 | PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo, trgKeyType);\r | |
771 | \r | |
772 | if (cmdp == 'o') {\r | |
773 | if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, true)) {\r | |
774 | PrintAndLog("Nested error.");\r | |
775 | return 2;\r | |
776 | }\r | |
777 | key64 = bytes_to_num(keyBlock, 6);\r | |
778 | if (key64) {\r | |
779 | PrintAndLog("Found valid key:%012"llx, key64);\r | |
780 | \r | |
781 | // transfer key to the emulator\r | |
782 | if (transferToEml) {\r | |
783 | mfEmlGetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1);\r | |
784 | \r | |
785 | if (!trgKeyType)\r | |
786 | num_to_bytes(key64, 6, keyBlock);\r | |
787 | else\r | |
788 | num_to_bytes(key64, 6, &keyBlock[10]);\r | |
789 | mfEmlSetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1); \r | |
790 | }\r | |
791 | } else {\r | |
792 | PrintAndLog("No valid key found");\r | |
793 | }\r | |
794 | }\r | |
795 | else { // ------------------------------------ multiple sectors working\r | |
796 | clock_t time1;\r | |
797 | time1 = clock();\r | |
798 | \r | |
799 | blDiff = blockNo % 4;\r | |
800 | PrintAndLog("Block shift=%d", blDiff);\r | |
801 | e_sector = calloc(SectorsCnt, sizeof(sector));\r | |
802 | if (e_sector == NULL) return 1;\r | |
803 | \r | |
804 | //test current key 4 sectors\r | |
805 | memcpy(keyBlock, key, 6);\r | |
806 | num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 1 * 6));\r | |
807 | num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 2 * 6));\r | |
808 | num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 3 * 6));\r | |
809 | num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 4 * 6));\r | |
810 | num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));\r | |
811 | \r | |
812 | PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);\r | |
813 | for (i = 0; i < SectorsCnt; i++) {\r | |
814 | for (j = 0; j < 2; j++) {\r | |
815 | if (e_sector[i].foundKey[j]) continue;\r | |
816 | \r | |
817 | res = mfCheckKeys(i * 4 + blDiff, j, 6, keyBlock, &key64);\r | |
818 | \r | |
819 | if (!res) {\r | |
820 | e_sector[i].Key[j] = key64;\r | |
821 | e_sector[i].foundKey[j] = 1;\r | |
822 | }\r | |
823 | }\r | |
824 | }\r | |
825 | \r | |
826 | \r | |
827 | // nested sectors\r | |
828 | iterations = 0;\r | |
829 | PrintAndLog("nested...");\r | |
830 | bool calibrate = true;\r | |
831 | for (i = 0; i < NESTED_SECTOR_RETRY; i++) {\r | |
832 | for (trgBlockNo = blDiff; trgBlockNo < SectorsCnt * 4; trgBlockNo = trgBlockNo + 4) {\r | |
833 | for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) { \r | |
834 | if (e_sector[trgBlockNo / 4].foundKey[trgKeyType]) continue;\r | |
835 | PrintAndLog("-----------------------------------------------");\r | |
836 | if(mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, calibrate)) {\r | |
837 | PrintAndLog("Nested error.\n");\r | |
838 | return 2;\r | |
839 | }\r | |
840 | else {\r | |
841 | calibrate = false;\r | |
842 | }\r | |
843 | \r | |
844 | iterations++;\r | |
845 | \r | |
846 | key64 = bytes_to_num(keyBlock, 6);\r | |
847 | if (key64) {\r | |
848 | PrintAndLog("Found valid key:%012"llx, key64);\r | |
849 | e_sector[trgBlockNo / 4].foundKey[trgKeyType] = 1;\r | |
850 | e_sector[trgBlockNo / 4].Key[trgKeyType] = key64;\r | |
851 | }\r | |
852 | }\r | |
853 | }\r | |
854 | }\r | |
855 | \r | |
856 | printf("Time in nested: %1.3f (%1.3f sec per key)\n\n", ((float)clock() - time1)/CLOCKS_PER_SEC, ((float)clock() - time1)/iterations/CLOCKS_PER_SEC);\r | |
857 | \r | |
858 | PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations);\r | |
859 | //print them\r | |
860 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
861 | PrintAndLog("|sec|key A |res|key B |res|");\r | |
862 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
863 | for (i = 0; i < SectorsCnt; i++) {\r | |
864 | PrintAndLog("|%03d| %012"llx" | %d | %012"llx" | %d |", i,\r | |
865 | e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);\r | |
866 | }\r | |
867 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
868 | \r | |
869 | // transfer them to the emulator\r | |
870 | if (transferToEml) {\r | |
871 | for (i = 0; i < SectorsCnt; i++) {\r | |
872 | mfEmlGetMem(keyBlock, i * 4 + 3, 1);\r | |
873 | if (e_sector[i].foundKey[0])\r | |
874 | num_to_bytes(e_sector[i].Key[0], 6, keyBlock);\r | |
875 | if (e_sector[i].foundKey[1])\r | |
876 | num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);\r | |
877 | mfEmlSetMem(keyBlock, i * 4 + 3, 1);\r | |
878 | } \r | |
879 | }\r | |
880 | \r | |
881 | // Create dump file\r | |
882 | if (createDumpFile) {\r | |
883 | if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) { \r | |
884 | PrintAndLog("Could not create file dumpkeys.bin");\r | |
885 | free(e_sector);\r | |
886 | return 1;\r | |
887 | }\r | |
888 | PrintAndLog("Printing keys to bynary file dumpkeys.bin...");\r | |
889 | for(i=0; i<16; i++) {\r | |
890 | if (e_sector[i].foundKey[0]){\r | |
891 | num_to_bytes(e_sector[i].Key[0], 6, tempkey);\r | |
892 | fwrite ( tempkey, 1, 6, fkeys );\r | |
893 | }\r | |
894 | else{\r | |
895 | fwrite ( &standart, 1, 6, fkeys );\r | |
896 | }\r | |
897 | }\r | |
898 | for(i=0; i<16; i++) {\r | |
899 | if (e_sector[i].foundKey[1]){\r | |
900 | num_to_bytes(e_sector[i].Key[1], 6, tempkey);\r | |
901 | fwrite ( tempkey, 1, 6, fkeys );\r | |
902 | }\r | |
903 | else{\r | |
904 | fwrite ( &standart, 1, 6, fkeys );\r | |
905 | }\r | |
906 | }\r | |
907 | fclose(fkeys);\r | |
908 | }\r | |
909 | \r | |
910 | free(e_sector);\r | |
911 | }\r | |
912 | \r | |
913 | return 0;\r | |
914 | }\r | |
915 | \r | |
916 | static uint32_t\r | |
917 | get_trailer_block (uint32_t uiBlock)\r | |
918 | {\r | |
919 | // Test if we are in the small or big sectors\r | |
920 | uint32_t trailer_block = 0;\r | |
921 | if (uiBlock < 128) {\r | |
922 | trailer_block = uiBlock + (3 - (uiBlock % 4));\r | |
923 | } else {\r | |
924 | trailer_block = uiBlock + (15 - (uiBlock % 16));\r | |
925 | }\r | |
926 | return trailer_block;\r | |
927 | }\r | |
928 | int CmdHF14AMfChk(const char *Cmd)\r | |
929 | {\r | |
930 | FILE * f;\r | |
931 | char filename[256]={0};\r | |
932 | char buf[13];\r | |
933 | uint8_t *keyBlock = NULL, *p;\r | |
934 | uint8_t stKeyBlock = 20;\r | |
935 | \r | |
936 | int i, res;\r | |
937 | int keycnt = 0;\r | |
938 | char ctmp = 0x00;\r | |
939 | uint8_t blockNo = 0;\r | |
940 | uint8_t SectorsCnt = 1;\r | |
941 | uint8_t keyType = 0;\r | |
942 | uint64_t key64 = 0;\r | |
943 | \r | |
944 | int transferToEml = 0;\r | |
945 | int createDumpFile = 0;\r | |
946 | \r | |
947 | keyBlock = calloc(stKeyBlock, 6);\r | |
948 | if (keyBlock == NULL) return 1;\r | |
949 | \r | |
950 | num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 0 * 6)); // Default key (first key used by program if no user defined key)\r | |
951 | num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 1 * 6)); // Blank key\r | |
952 | num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 2 * 6)); // NFCForum MAD key\r | |
953 | num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 3 * 6));\r | |
954 | num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 4 * 6));\r | |
955 | num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock + 5 * 6));\r | |
956 | num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock + 6 * 6));\r | |
957 | num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock + 7 * 6));\r | |
958 | num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock + 8 * 6));\r | |
959 | num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock + 9 * 6));\r | |
960 | num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock + 10 * 6));\r | |
961 | num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock + 11 * 6));\r | |
962 | num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock + 12 * 6));\r | |
963 | \r | |
964 | if (strlen(Cmd)<3) {\r | |
965 | PrintAndLog("Usage: hf mf chk <block number>/<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");\r | |
966 | PrintAndLog(" * - all sectors");\r | |
967 | PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r | |
968 | // PrintAndLog("d - write keys to binary file\n");\r | |
969 | \r | |
970 | PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");\r | |
971 | PrintAndLog(" hf mf chk *1 ? t");\r | |
972 | return 0;\r | |
973 | } \r | |
974 | \r | |
975 | if (param_getchar(Cmd, 0)=='*') {\r | |
976 | blockNo = 3;\r | |
977 | switch(param_getchar(Cmd+1, 0)) {\r | |
978 | case '0': SectorsCnt = 5; break;\r | |
979 | case '1': SectorsCnt = 16; break;\r | |
980 | case '2': SectorsCnt = 32; break;\r | |
981 | case '4': SectorsCnt = 40; break;\r | |
982 | default: SectorsCnt = 16;\r | |
983 | }\r | |
984 | }\r | |
985 | else\r | |
986 | blockNo = param_get8(Cmd, 0);\r | |
987 | \r | |
988 | ctmp = param_getchar(Cmd, 1);\r | |
989 | switch (ctmp) { \r | |
990 | case 'a': case 'A':\r | |
991 | keyType = !0;\r | |
992 | break;\r | |
993 | case 'b': case 'B':\r | |
994 | keyType = !1;\r | |
995 | break;\r | |
996 | case '?':\r | |
997 | keyType = 2;\r | |
998 | break;\r | |
999 | default:\r | |
1000 | PrintAndLog("Key type must be A , B or ?");\r | |
1001 | return 1;\r | |
1002 | };\r | |
1003 | \r | |
1004 | ctmp = param_getchar(Cmd, 2);\r | |
1005 | if (ctmp == 't' || ctmp == 'T') transferToEml = 1;\r | |
1006 | else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;\r | |
1007 | \r | |
1008 | for (i = transferToEml || createDumpFile; param_getchar(Cmd, 2 + i); i++) {\r | |
1009 | if (!param_gethex(Cmd, 2 + i, keyBlock + 6 * keycnt, 12)) {\r | |
1010 | if ( stKeyBlock - keycnt < 2) {\r | |
1011 | p = realloc(keyBlock, 6*(stKeyBlock+=10));\r | |
1012 | if (!p) {\r | |
1013 | PrintAndLog("Cannot allocate memory for Keys");\r | |
1014 | free(keyBlock);\r | |
1015 | return 2;\r | |
1016 | }\r | |
1017 | keyBlock = p;\r | |
1018 | }\r | |
1019 | PrintAndLog("chk key[%d] %02x%02x%02x%02x%02x%02x", keycnt,\r | |
1020 | (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r | |
1021 | (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r | |
1022 | keycnt++;\r | |
1023 | } else {\r | |
1024 | // May be a dic file\r | |
1025 | if ( param_getstr(Cmd, 2 + i,filename) > 255 ) {\r | |
1026 | PrintAndLog("File name too long");\r | |
1027 | free(keyBlock);\r | |
1028 | return 2;\r | |
1029 | }\r | |
1030 | \r | |
1031 | if ( (f = fopen( filename , "r")) ) {\r | |
1032 | while( !feof(f) ){\r | |
1033 | memset(buf, 0, sizeof(buf));\r | |
1034 | if (fgets(buf, sizeof(buf), f) == NULL) {\r | |
1035 | PrintAndLog("File reading error.");\r | |
1036 | return 2;\r | |
1037 | }\r | |
1038 | \r | |
1039 | if (strlen(buf) < 12 || buf[11] == '\n')\r | |
1040 | continue;\r | |
1041 | \r | |
1042 | while (fgetc(f) != '\n' && !feof(f)) ; //goto next line\r | |
1043 | \r | |
1044 | if( buf[0]=='#' ) continue; //The line start with # is comment, skip\r | |
1045 | \r | |
1046 | if (!isxdigit(buf[0])){\r | |
1047 | PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);\r | |
1048 | continue;\r | |
1049 | }\r | |
1050 | \r | |
1051 | buf[12] = 0;\r | |
1052 | \r | |
1053 | if ( stKeyBlock - keycnt < 2) {\r | |
1054 | p = realloc(keyBlock, 6*(stKeyBlock+=10));\r | |
1055 | if (!p) {\r | |
1056 | PrintAndLog("Cannot allocate memory for defKeys");\r | |
1057 | free(keyBlock);\r | |
1058 | return 2;\r | |
1059 | }\r | |
1060 | keyBlock = p;\r | |
1061 | }\r | |
1062 | memset(keyBlock + 6 * keycnt, 0, 6);\r | |
1063 | num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);\r | |
1064 | PrintAndLog("chk custom key[%d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r | |
1065 | keycnt++;\r | |
1066 | }\r | |
1067 | } else {\r | |
1068 | PrintAndLog("File: %s: not found or locked.", filename);\r | |
1069 | free(keyBlock);\r | |
1070 | return 1;\r | |
1071 | fclose(f);\r | |
1072 | }\r | |
1073 | }\r | |
1074 | }\r | |
1075 | \r | |
1076 | if (keycnt == 0) {\r | |
1077 | PrintAndLog("No key specified,try default keys");\r | |
1078 | for (;keycnt <=12; keycnt++)\r | |
1079 | PrintAndLog("chk default key[%d] %02x%02x%02x%02x%02x%02x", keycnt,\r | |
1080 | (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r | |
1081 | (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r | |
1082 | }\r | |
1083 | \r | |
1084 | for ( int t = !keyType ; t < 2 ; keyType==2?(t++):(t=2) ) {\r | |
1085 | int b=blockNo;\r | |
1086 | for (int i=0; i<SectorsCnt; ++i) {\r | |
1087 | PrintAndLog("--SectorsCnt:%d block no:0x%02x key type:%C key count:%d ", i, b, t?'B':'A', keycnt);\r | |
1088 | uint32_t max_keys = keycnt>USB_CMD_DATA_SIZE/6?USB_CMD_DATA_SIZE/6:keycnt;\r | |
1089 | for (uint32_t c = 0; c < keycnt; c+=max_keys) {\r | |
1090 | uint32_t size = keycnt-c>max_keys?max_keys:keycnt-c;\r | |
1091 | res = mfCheckKeys(b, t, size, &keyBlock[6*c], &key64);\r | |
1092 | if (res !=1) {\r | |
1093 | if (!res) {\r | |
1094 | PrintAndLog("Found valid key:[%012"llx"]",key64);\r | |
1095 | if (transferToEml) {\r | |
1096 | uint8_t block[16];\r | |
1097 | mfEmlGetMem(block, get_trailer_block(b), 1);\r | |
1098 | num_to_bytes(key64, 6, block + t*10);\r | |
1099 | mfEmlSetMem(block, get_trailer_block(b), 1);\r | |
1100 | }\r | |
1101 | }\r | |
1102 | } else {\r | |
1103 | PrintAndLog("Command execute timeout");\r | |
1104 | }\r | |
1105 | }\r | |
1106 | b<127?(b+=4):(b+=16); \r | |
1107 | }\r | |
1108 | }\r | |
1109 | \r | |
1110 | free(keyBlock);\r | |
1111 | \r | |
1112 | /*\r | |
1113 | // Create dump file\r | |
1114 | if (createDumpFile) {\r | |
1115 | if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) { \r | |
1116 | PrintAndLog("Could not create file dumpkeys.bin");\r | |
1117 | free(e_sector);\r | |
1118 | return 1;\r | |
1119 | }\r | |
1120 | PrintAndLog("Printing keys to bynary file dumpkeys.bin...");\r | |
1121 | for(i=0; i<16; i++) {\r | |
1122 | if (e_sector[i].foundKey[0]){\r | |
1123 | num_to_bytes(e_sector[i].Key[0], 6, tempkey);\r | |
1124 | fwrite ( tempkey, 1, 6, fkeys );\r | |
1125 | }\r | |
1126 | else{\r | |
1127 | fwrite ( &standart, 1, 6, fkeys );\r | |
1128 | }\r | |
1129 | }\r | |
1130 | for(i=0; i<16; i++) {\r | |
1131 | if (e_sector[i].foundKey[1]){\r | |
1132 | num_to_bytes(e_sector[i].Key[1], 6, tempkey);\r | |
1133 | fwrite ( tempkey, 1, 6, fkeys );\r | |
1134 | }\r | |
1135 | else{\r | |
1136 | fwrite ( &standart, 1, 6, fkeys );\r | |
1137 | }\r | |
1138 | }\r | |
1139 | fclose(fkeys);\r | |
1140 | }\r | |
1141 | */\r | |
1142 | return 0;\r | |
1143 | }\r | |
1144 | \r | |
1145 | int CmdHF14AMf1kSim(const char *Cmd)\r | |
1146 | {\r | |
1147 | uint8_t uid[7] = {0, 0, 0, 0, 0, 0, 0};\r | |
1148 | uint8_t exitAfterNReads = 0;\r | |
1149 | uint8_t flags = 0;\r | |
1150 | \r | |
1151 | if (param_getchar(Cmd, 0) == 'h') {\r | |
1152 | PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");\r | |
1153 | PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");\r | |
1154 | PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");\r | |
1155 | PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");\r | |
1156 | PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");\r | |
1157 | PrintAndLog(" sample: hf mf sim 0a0a0a0a ");\r | |
1158 | return 0;\r | |
1159 | }\r | |
1160 | uint8_t pnr = 0;\r | |
1161 | if (param_getchar(Cmd, pnr) == 'u') {\r | |
1162 | if(param_gethex(Cmd, pnr+1, uid, 8) == 0)\r | |
1163 | {\r | |
1164 | flags |=FLAG_4B_UID_IN_DATA; // UID from packet\r | |
1165 | }else if(param_gethex(Cmd,pnr+1,uid,14) == 0)\r | |
1166 | {\r | |
1167 | flags |= FLAG_7B_UID_IN_DATA;// UID from packet\r | |
1168 | }else\r | |
1169 | {\r | |
1170 | PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");\r | |
1171 | return 1;\r | |
1172 | }\r | |
1173 | pnr +=2;\r | |
1174 | }\r | |
1175 | if (param_getchar(Cmd, pnr) == 'n') {\r | |
1176 | exitAfterNReads = param_get8(Cmd,pnr+1);\r | |
1177 | pnr += 2;\r | |
1178 | }\r | |
1179 | if (param_getchar(Cmd, pnr) == 'i' ) {\r | |
1180 | //Using a flag to signal interactiveness, least significant bit\r | |
1181 | flags |= FLAG_INTERACTIVE;\r | |
1182 | pnr++;\r | |
1183 | }\r | |
1184 | \r | |
1185 | if (param_getchar(Cmd, pnr) == 'x' ) {\r | |
1186 | //Using a flag to signal interactiveness, least significant bit\r | |
1187 | flags |= FLAG_NR_AR_ATTACK;\r | |
1188 | }\r | |
1189 | PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",\r | |
1190 | flags & FLAG_4B_UID_IN_DATA ? sprint_hex(uid,4):\r | |
1191 | flags & FLAG_7B_UID_IN_DATA ? sprint_hex(uid,7): "N/A"\r | |
1192 | , exitAfterNReads, flags,flags);\r | |
1193 | \r | |
1194 | \r | |
1195 | UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {flags, exitAfterNReads,0}};\r | |
1196 | memcpy(c.d.asBytes, uid, sizeof(uid));\r | |
1197 | SendCommand(&c);\r | |
1198 | \r | |
1199 | if(flags & FLAG_INTERACTIVE)\r | |
1200 | {\r | |
1201 | UsbCommand resp;\r | |
1202 | PrintAndLog("Press pm3-button to abort simulation");\r | |
1203 | while(! WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
1204 | //We're waiting only 1.5 s at a time, otherwise we get the\r | |
1205 | // annoying message about "Waiting for a response... "\r | |
1206 | }\r | |
1207 | }\r | |
1208 | \r | |
1209 | return 0;\r | |
1210 | }\r | |
1211 | \r | |
1212 | int CmdHF14AMfDbg(const char *Cmd)\r | |
1213 | {\r | |
1214 | int dbgMode = param_get32ex(Cmd, 0, 0, 10);\r | |
1215 | if (dbgMode > 4) {\r | |
1216 | PrintAndLog("Max debud mode parameter is 4 \n");\r | |
1217 | }\r | |
1218 | \r | |
1219 | if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {\r | |
1220 | PrintAndLog("Usage: hf mf dbg <debug level>");\r | |
1221 | PrintAndLog(" 0 - no debug messages");\r | |
1222 | PrintAndLog(" 1 - error messages");\r | |
1223 | PrintAndLog(" 2 - all messages");\r | |
1224 | PrintAndLog(" 4 - extended debug mode");\r | |
1225 | return 0;\r | |
1226 | } \r | |
1227 | \r | |
1228 | UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};\r | |
1229 | SendCommand(&c);\r | |
1230 | \r | |
1231 | return 0;\r | |
1232 | }\r | |
1233 | \r | |
1234 | int CmdHF14AMfEGet(const char *Cmd)\r | |
1235 | {\r | |
1236 | uint8_t blockNo = 0;\r | |
1237 | uint8_t data[3 * 16];\r | |
1238 | int i;\r | |
1239 | \r | |
1240 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1241 | PrintAndLog("Usage: hf mf eget <block number>");\r | |
1242 | PrintAndLog(" sample: hf mf eget 0 ");\r | |
1243 | return 0;\r | |
1244 | } \r | |
1245 | \r | |
1246 | blockNo = param_get8(Cmd, 0);\r | |
1247 | \r | |
1248 | PrintAndLog(" ");\r | |
1249 | if (!mfEmlGetMem(data, blockNo, 3)) {\r | |
1250 | for (i = 0; i < 3; i++) {\r | |
1251 | PrintAndLog("data[%d]:%s", blockNo + i, sprint_hex(data + i * 16, 16));\r | |
1252 | }\r | |
1253 | } else {\r | |
1254 | PrintAndLog("Command execute timeout");\r | |
1255 | }\r | |
1256 | \r | |
1257 | return 0;\r | |
1258 | }\r | |
1259 | \r | |
1260 | int CmdHF14AMfEClear(const char *Cmd)\r | |
1261 | {\r | |
1262 | if (param_getchar(Cmd, 0) == 'h') {\r | |
1263 | PrintAndLog("Usage: hf mf eclr");\r | |
1264 | PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");\r | |
1265 | return 0;\r | |
1266 | } \r | |
1267 | \r | |
1268 | UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};\r | |
1269 | SendCommand(&c);\r | |
1270 | return 0;\r | |
1271 | }\r | |
1272 | \r | |
1273 | int CmdHF14AMfESet(const char *Cmd)\r | |
1274 | {\r | |
1275 | uint8_t memBlock[16];\r | |
1276 | uint8_t blockNo = 0;\r | |
1277 | \r | |
1278 | memset(memBlock, 0x00, sizeof(memBlock));\r | |
1279 | \r | |
1280 | if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {\r | |
1281 | PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");\r | |
1282 | PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");\r | |
1283 | return 0;\r | |
1284 | } \r | |
1285 | \r | |
1286 | blockNo = param_get8(Cmd, 0);\r | |
1287 | \r | |
1288 | if (param_gethex(Cmd, 1, memBlock, 32)) {\r | |
1289 | PrintAndLog("block data must include 32 HEX symbols");\r | |
1290 | return 1;\r | |
1291 | }\r | |
1292 | \r | |
1293 | // 1 - blocks count\r | |
1294 | UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};\r | |
1295 | memcpy(c.d.asBytes, memBlock, 16);\r | |
1296 | SendCommand(&c);\r | |
1297 | return 0;\r | |
1298 | }\r | |
1299 | \r | |
1300 | int CmdHF14AMfELoad(const char *Cmd)\r | |
1301 | {\r | |
1302 | FILE * f;\r | |
1303 | char filename[20];\r | |
1304 | char * fnameptr = filename;\r | |
1305 | char buf[64];\r | |
1306 | uint8_t buf8[64];\r | |
1307 | int i, len, blockNum;\r | |
1308 | \r | |
1309 | memset(filename, 0, sizeof(filename));\r | |
1310 | memset(buf, 0, sizeof(buf));\r | |
1311 | \r | |
1312 | if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {\r | |
1313 | PrintAndLog("It loads emul dump from the file `filename.eml`");\r | |
1314 | PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");\r | |
1315 | PrintAndLog(" sample: hf mf eload filename");\r | |
1316 | return 0;\r | |
1317 | } \r | |
1318 | \r | |
1319 | len = strlen(Cmd);\r | |
1320 | if (len > 14) len = 14;\r | |
1321 | \r | |
1322 | memcpy(filename, Cmd, len);\r | |
1323 | fnameptr += len;\r | |
1324 | \r | |
1325 | sprintf(fnameptr, ".eml"); \r | |
1326 | \r | |
1327 | // open file\r | |
1328 | f = fopen(filename, "r");\r | |
1329 | if (f == NULL) {\r | |
1330 | PrintAndLog("File not found or locked.");\r | |
1331 | return 1;\r | |
1332 | }\r | |
1333 | \r | |
1334 | blockNum = 0;\r | |
1335 | while(!feof(f)){\r | |
1336 | memset(buf, 0, sizeof(buf));\r | |
1337 | if (fgets(buf, sizeof(buf), f) == NULL) {\r | |
1338 | if(blockNum == 16 * 4)\r | |
1339 | {\r | |
1340 | break;\r | |
1341 | }\r | |
1342 | PrintAndLog("File reading error.");\r | |
1343 | return 2;\r | |
1344 | }\r | |
1345 | \r | |
1346 | if (strlen(buf) < 32){\r | |
1347 | if(strlen(buf) && feof(f))\r | |
1348 | break;\r | |
1349 | PrintAndLog("File content error. Block data must include 32 HEX symbols");\r | |
1350 | return 2;\r | |
1351 | }\r | |
1352 | for (i = 0; i < 32; i += 2)\r | |
1353 | sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r | |
1354 | // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));\r | |
1355 | \r | |
1356 | if (mfEmlSetMem(buf8, blockNum, 1)) {\r | |
1357 | PrintAndLog("Cant set emul block: %d", blockNum);\r | |
1358 | return 3;\r | |
1359 | }\r | |
1360 | blockNum++;\r | |
1361 | \r | |
1362 | if (blockNum >= 32 * 4 + 8 * 16) break;\r | |
1363 | }\r | |
1364 | fclose(f);\r | |
1365 | \r | |
1366 | if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){\r | |
1367 | PrintAndLog("File content error. There must be 64 blocks");\r | |
1368 | return 4;\r | |
1369 | }\r | |
1370 | PrintAndLog("Loaded %d blocks from file: %s", blockNum, filename);\r | |
1371 | return 0;\r | |
1372 | }\r | |
1373 | \r | |
1374 | int CmdHF14AMfESave(const char *Cmd)\r | |
1375 | {\r | |
1376 | FILE * f;\r | |
1377 | char filename[20];\r | |
1378 | char * fnameptr = filename;\r | |
1379 | uint8_t buf[64];\r | |
1380 | int i, j, len;\r | |
1381 | \r | |
1382 | memset(filename, 0, sizeof(filename));\r | |
1383 | memset(buf, 0, sizeof(buf));\r | |
1384 | \r | |
1385 | if (param_getchar(Cmd, 0) == 'h') {\r | |
1386 | PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");\r | |
1387 | PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");\r | |
1388 | PrintAndLog(" sample: hf mf esave ");\r | |
1389 | PrintAndLog(" hf mf esave filename");\r | |
1390 | return 0;\r | |
1391 | } \r | |
1392 | \r | |
1393 | len = strlen(Cmd);\r | |
1394 | if (len > 14) len = 14;\r | |
1395 | \r | |
1396 | if (len < 1) {\r | |
1397 | // get filename\r | |
1398 | if (mfEmlGetMem(buf, 0, 1)) {\r | |
1399 | PrintAndLog("Cant get block: %d", 0);\r | |
1400 | return 1;\r | |
1401 | }\r | |
1402 | for (j = 0; j < 7; j++, fnameptr += 2)\r | |
1403 | sprintf(fnameptr, "%02x", buf[j]); \r | |
1404 | } else {\r | |
1405 | memcpy(filename, Cmd, len);\r | |
1406 | fnameptr += len;\r | |
1407 | }\r | |
1408 | \r | |
1409 | sprintf(fnameptr, ".eml"); \r | |
1410 | \r | |
1411 | // open file\r | |
1412 | f = fopen(filename, "w+");\r | |
1413 | \r | |
1414 | // put hex\r | |
1415 | for (i = 0; i < 32 * 4 + 8 * 16; i++) {\r | |
1416 | if (mfEmlGetMem(buf, i, 1)) {\r | |
1417 | PrintAndLog("Cant get block: %d", i);\r | |
1418 | break;\r | |
1419 | }\r | |
1420 | for (j = 0; j < 16; j++)\r | |
1421 | fprintf(f, "%02x", buf[j]); \r | |
1422 | fprintf(f,"\n");\r | |
1423 | }\r | |
1424 | fclose(f);\r | |
1425 | \r | |
1426 | PrintAndLog("Saved to file: %s", filename);\r | |
1427 | \r | |
1428 | return 0;\r | |
1429 | }\r | |
1430 | \r | |
1431 | int CmdHF14AMfECFill(const char *Cmd)\r | |
1432 | {\r | |
1433 | uint8_t keyType = 0;\r | |
1434 | \r | |
1435 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1436 | PrintAndLog("Usage: hf mf efill <key A/B>");\r | |
1437 | PrintAndLog("sample: hf mf efill A");\r | |
1438 | PrintAndLog("Card data blocks transfers to card emulator memory.");\r | |
1439 | PrintAndLog("Keys must be laid in the simulator memory. \n");\r | |
1440 | return 0;\r | |
1441 | } \r | |
1442 | \r | |
1443 | char ctmp = param_getchar(Cmd, 0);\r | |
1444 | if (ctmp == 0x00) {\r | |
1445 | PrintAndLog("Key type must be A or B");\r | |
1446 | return 1;\r | |
1447 | }\r | |
1448 | if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r | |
1449 | \r | |
1450 | UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {0, keyType, 0}};\r | |
1451 | SendCommand(&c);\r | |
1452 | return 0;\r | |
1453 | }\r | |
1454 | \r | |
1455 | int CmdHF14AMfEKeyPrn(const char *Cmd)\r | |
1456 | {\r | |
1457 | int i,b=-1;\r | |
1458 | uint8_t data[16];\r | |
1459 | uint64_t keyA, keyB;\r | |
1460 | \r | |
1461 | PrintAndLog("|---|----------------|----------------|");\r | |
1462 | PrintAndLog("|sec|key A |key B |");\r | |
1463 | PrintAndLog("|---|----------------|----------------|");\r | |
1464 | for (i = 0; i < 40; i++) {\r | |
1465 | b<127?(b+=4):(b+=16);\r | |
1466 | if (mfEmlGetMem(data, b, 1)) {\r | |
1467 | PrintAndLog("error get block %d", b);\r | |
1468 | break;\r | |
1469 | }\r | |
1470 | keyA = bytes_to_num(data, 6);\r | |
1471 | keyB = bytes_to_num(data + 10, 6);\r | |
1472 | PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB);\r | |
1473 | }\r | |
1474 | PrintAndLog("|---|----------------|----------------|");\r | |
1475 | \r | |
1476 | return 0;\r | |
1477 | }\r | |
1478 | \r | |
1479 | int CmdHF14AMfCSetUID(const char *Cmd)\r | |
1480 | {\r | |
1481 | uint8_t wipeCard = 0;\r | |
1482 | uint8_t uid[8];\r | |
1483 | uint8_t oldUid[8];\r | |
1484 | int res;\r | |
1485 | \r | |
1486 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1487 | PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");\r | |
1488 | PrintAndLog("sample: hf mf csetuid 01020304 w");\r | |
1489 | PrintAndLog("Set UID for magic Chinese card (only works with!!!)");\r | |
1490 | PrintAndLog("If you want wipe card then add 'w' into command line. \n");\r | |
1491 | return 0;\r | |
1492 | } \r | |
1493 | \r | |
1494 | if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {\r | |
1495 | PrintAndLog("UID must include 8 HEX symbols");\r | |
1496 | return 1;\r | |
1497 | }\r | |
1498 | \r | |
1499 | char ctmp = param_getchar(Cmd, 1);\r | |
1500 | if (ctmp == 'w' || ctmp == 'W') wipeCard = 1;\r | |
1501 | \r | |
1502 | PrintAndLog("--wipe card:%02x uid:%s", wipeCard, sprint_hex(uid, 4));\r | |
1503 | \r | |
1504 | res = mfCSetUID(uid, oldUid, wipeCard);\r | |
1505 | if (res) {\r | |
1506 | PrintAndLog("Can't set UID. error=%d", res);\r | |
1507 | return 1;\r | |
1508 | }\r | |
1509 | \r | |
1510 | PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));\r | |
1511 | return 0;\r | |
1512 | }\r | |
1513 | \r | |
1514 | int CmdHF14AMfCSetBlk(const char *Cmd)\r | |
1515 | {\r | |
1516 | uint8_t uid[8];\r | |
1517 | uint8_t memBlock[16];\r | |
1518 | uint8_t blockNo = 0;\r | |
1519 | int res;\r | |
1520 | memset(memBlock, 0x00, sizeof(memBlock));\r | |
1521 | \r | |
1522 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1523 | PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");\r | |
1524 | PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");\r | |
1525 | PrintAndLog("Set block data for magic Chinese card (only works with!!!)");\r | |
1526 | PrintAndLog("If you want wipe card then add 'w' into command line. \n");\r | |
1527 | return 0;\r | |
1528 | } \r | |
1529 | \r | |
1530 | blockNo = param_get8(Cmd, 0);\r | |
1531 | \r | |
1532 | if (param_gethex(Cmd, 1, memBlock, 32)) {\r | |
1533 | PrintAndLog("block data must include 32 HEX symbols");\r | |
1534 | return 1;\r | |
1535 | }\r | |
1536 | \r | |
1537 | PrintAndLog("--block number:%02x data:%s", blockNo, sprint_hex(memBlock, 16));\r | |
1538 | \r | |
1539 | res = mfCSetBlock(blockNo, memBlock, uid, 0, CSETBLOCK_SINGLE_OPER);\r | |
1540 | if (res) {\r | |
1541 | PrintAndLog("Can't write block. error=%d", res);\r | |
1542 | return 1;\r | |
1543 | }\r | |
1544 | \r | |
1545 | PrintAndLog("UID:%s", sprint_hex(uid, 4));\r | |
1546 | return 0;\r | |
1547 | }\r | |
1548 | \r | |
1549 | int CmdHF14AMfCLoad(const char *Cmd)\r | |
1550 | {\r | |
1551 | FILE * f;\r | |
1552 | char filename[20];\r | |
1553 | char * fnameptr = filename;\r | |
1554 | char buf[64];\r | |
1555 | uint8_t buf8[64];\r | |
1556 | uint8_t fillFromEmulator = 0;\r | |
1557 | int i, len, blockNum, flags;\r | |
1558 | \r | |
1559 | memset(filename, 0, sizeof(filename));\r | |
1560 | memset(buf, 0, sizeof(buf));\r | |
1561 | \r | |
1562 | if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {\r | |
1563 | PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");\r | |
1564 | PrintAndLog("or from emulator memory (option `e`)");\r | |
1565 | PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");\r | |
1566 | PrintAndLog(" or: hf mf cload e ");\r | |
1567 | PrintAndLog(" sample: hf mf cload filename");\r | |
1568 | return 0;\r | |
1569 | } \r | |
1570 | \r | |
1571 | char ctmp = param_getchar(Cmd, 0);\r | |
1572 | if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r | |
1573 | \r | |
1574 | if (fillFromEmulator) {\r | |
1575 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
1576 | for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {\r | |
1577 | if (mfEmlGetMem(buf8, blockNum, 1)) {\r | |
1578 | PrintAndLog("Cant get block: %d", blockNum);\r | |
1579 | return 2;\r | |
1580 | }\r | |
1581 | \r | |
1582 | if (blockNum == 2) flags = 0;\r | |
1583 | if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r | |
1584 | \r | |
1585 | if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r | |
1586 | PrintAndLog("Cant set magic card block: %d", blockNum);\r | |
1587 | return 3;\r | |
1588 | }\r | |
1589 | }\r | |
1590 | return 0;\r | |
1591 | } else {\r | |
1592 | len = strlen(Cmd);\r | |
1593 | if (len > 14) len = 14;\r | |
1594 | \r | |
1595 | memcpy(filename, Cmd, len);\r | |
1596 | fnameptr += len;\r | |
1597 | \r | |
1598 | sprintf(fnameptr, ".eml"); \r | |
1599 | \r | |
1600 | // open file\r | |
1601 | f = fopen(filename, "r");\r | |
1602 | if (f == NULL) {\r | |
1603 | PrintAndLog("File not found or locked.");\r | |
1604 | return 1;\r | |
1605 | }\r | |
1606 | \r | |
1607 | blockNum = 0;\r | |
1608 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
1609 | while(!feof(f)){\r | |
1610 | memset(buf, 0, sizeof(buf));\r | |
1611 | if (fgets(buf, sizeof(buf), f) == NULL) {\r | |
1612 | PrintAndLog("File reading error.");\r | |
1613 | return 2;\r | |
1614 | }\r | |
1615 | \r | |
1616 | if (strlen(buf) < 32){\r | |
1617 | if(strlen(buf) && feof(f))\r | |
1618 | break;\r | |
1619 | PrintAndLog("File content error. Block data must include 32 HEX symbols");\r | |
1620 | return 2;\r | |
1621 | }\r | |
1622 | for (i = 0; i < 32; i += 2)\r | |
1623 | sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r | |
1624 | \r | |
1625 | if (blockNum == 2) flags = 0;\r | |
1626 | if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r | |
1627 | \r | |
1628 | if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r | |
1629 | PrintAndLog("Cant set magic card block: %d", blockNum);\r | |
1630 | return 3;\r | |
1631 | }\r | |
1632 | blockNum++;\r | |
1633 | \r | |
1634 | if (blockNum >= 16 * 4) break; // magic card type - mifare 1K\r | |
1635 | }\r | |
1636 | fclose(f);\r | |
1637 | \r | |
1638 | if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){\r | |
1639 | PrintAndLog("File content error. There must be 64 blocks");\r | |
1640 | return 4;\r | |
1641 | }\r | |
1642 | PrintAndLog("Loaded from file: %s", filename);\r | |
1643 | return 0;\r | |
1644 | }\r | |
1645 | }\r | |
1646 | \r | |
1647 | int CmdHF14AMfCGetBlk(const char *Cmd) {\r | |
1648 | uint8_t memBlock[16];\r | |
1649 | uint8_t blockNo = 0;\r | |
1650 | int res;\r | |
1651 | memset(memBlock, 0x00, sizeof(memBlock));\r | |
1652 | \r | |
1653 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1654 | PrintAndLog("Usage: hf mf cgetblk <block number>");\r | |
1655 | PrintAndLog("sample: hf mf cgetblk 1");\r | |
1656 | PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");\r | |
1657 | return 0;\r | |
1658 | } \r | |
1659 | \r | |
1660 | blockNo = param_get8(Cmd, 0);\r | |
1661 | \r | |
1662 | PrintAndLog("--block number:%02x ", blockNo);\r | |
1663 | \r | |
1664 | res = mfCGetBlock(blockNo, memBlock, CSETBLOCK_SINGLE_OPER);\r | |
1665 | if (res) {\r | |
1666 | PrintAndLog("Can't read block. error=%d", res);\r | |
1667 | return 1;\r | |
1668 | }\r | |
1669 | \r | |
1670 | PrintAndLog("block data:%s", sprint_hex(memBlock, 16));\r | |
1671 | return 0;\r | |
1672 | }\r | |
1673 | \r | |
1674 | int CmdHF14AMfCGetSc(const char *Cmd) {\r | |
1675 | uint8_t memBlock[16];\r | |
1676 | uint8_t sectorNo = 0;\r | |
1677 | int i, res, flags;\r | |
1678 | memset(memBlock, 0x00, sizeof(memBlock));\r | |
1679 | \r | |
1680 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1681 | PrintAndLog("Usage: hf mf cgetsc <sector number>");\r | |
1682 | PrintAndLog("sample: hf mf cgetsc 0");\r | |
1683 | PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");\r | |
1684 | return 0;\r | |
1685 | } \r | |
1686 | \r | |
1687 | sectorNo = param_get8(Cmd, 0);\r | |
1688 | if (sectorNo > 15) {\r | |
1689 | PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");\r | |
1690 | return 1;\r | |
1691 | }\r | |
1692 | \r | |
1693 | PrintAndLog("--sector number:%02x ", sectorNo);\r | |
1694 | \r | |
1695 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
1696 | for (i = 0; i < 4; i++) {\r | |
1697 | if (i == 1) flags = 0;\r | |
1698 | if (i == 3) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r | |
1699 | \r | |
1700 | res = mfCGetBlock(sectorNo * 4 + i, memBlock, flags);\r | |
1701 | if (res) {\r | |
1702 | PrintAndLog("Can't read block. %02x error=%d", sectorNo * 4 + i, res);\r | |
1703 | return 1;\r | |
1704 | }\r | |
1705 | \r | |
1706 | PrintAndLog("block %02x data:%s", sectorNo * 4 + i, sprint_hex(memBlock, 16));\r | |
1707 | }\r | |
1708 | return 0;\r | |
1709 | }\r | |
1710 | \r | |
1711 | int CmdHF14AMfCSave(const char *Cmd) {\r | |
1712 | \r | |
1713 | FILE * f;\r | |
1714 | char filename[20];\r | |
1715 | char * fnameptr = filename;\r | |
1716 | uint8_t fillFromEmulator = 0;\r | |
1717 | uint8_t buf[64];\r | |
1718 | int i, j, len, flags;\r | |
1719 | \r | |
1720 | memset(filename, 0, sizeof(filename));\r | |
1721 | memset(buf, 0, sizeof(buf));\r | |
1722 | \r | |
1723 | if (param_getchar(Cmd, 0) == 'h') {\r | |
1724 | PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");\r | |
1725 | PrintAndLog("or into emulator memory (option `e`)");\r | |
1726 | PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");\r | |
1727 | PrintAndLog(" sample: hf mf esave ");\r | |
1728 | PrintAndLog(" hf mf esave filename");\r | |
1729 | PrintAndLog(" hf mf esave e \n");\r | |
1730 | return 0;\r | |
1731 | } \r | |
1732 | \r | |
1733 | char ctmp = param_getchar(Cmd, 0);\r | |
1734 | if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r | |
1735 | \r | |
1736 | if (fillFromEmulator) {\r | |
1737 | // put into emulator\r | |
1738 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
1739 | for (i = 0; i < 16 * 4; i++) {\r | |
1740 | if (i == 1) flags = 0;\r | |
1741 | if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r | |
1742 | \r | |
1743 | if (mfCGetBlock(i, buf, flags)) {\r | |
1744 | PrintAndLog("Cant get block: %d", i);\r | |
1745 | break;\r | |
1746 | }\r | |
1747 | \r | |
1748 | if (mfEmlSetMem(buf, i, 1)) {\r | |
1749 | PrintAndLog("Cant set emul block: %d", i);\r | |
1750 | return 3;\r | |
1751 | }\r | |
1752 | }\r | |
1753 | return 0;\r | |
1754 | } else {\r | |
1755 | len = strlen(Cmd);\r | |
1756 | if (len > 14) len = 14;\r | |
1757 | \r | |
1758 | if (len < 1) {\r | |
1759 | // get filename\r | |
1760 | if (mfCGetBlock(0, buf, CSETBLOCK_SINGLE_OPER)) {\r | |
1761 | PrintAndLog("Cant get block: %d", 0);\r | |
1762 | return 1;\r | |
1763 | }\r | |
1764 | for (j = 0; j < 7; j++, fnameptr += 2)\r | |
1765 | sprintf(fnameptr, "%02x", buf[j]); \r | |
1766 | } else {\r | |
1767 | memcpy(filename, Cmd, len);\r | |
1768 | fnameptr += len;\r | |
1769 | }\r | |
1770 | \r | |
1771 | sprintf(fnameptr, ".eml"); \r | |
1772 | \r | |
1773 | // open file\r | |
1774 | f = fopen(filename, "w+");\r | |
1775 | \r | |
1776 | // put hex\r | |
1777 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
1778 | for (i = 0; i < 16 * 4; i++) {\r | |
1779 | if (i == 1) flags = 0;\r | |
1780 | if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r | |
1781 | \r | |
1782 | if (mfCGetBlock(i, buf, flags)) {\r | |
1783 | PrintAndLog("Cant get block: %d", i);\r | |
1784 | break;\r | |
1785 | }\r | |
1786 | for (j = 0; j < 16; j++)\r | |
1787 | fprintf(f, "%02x", buf[j]); \r | |
1788 | fprintf(f,"\n");\r | |
1789 | }\r | |
1790 | fclose(f);\r | |
1791 | \r | |
1792 | PrintAndLog("Saved to file: %s", filename);\r | |
1793 | \r | |
1794 | return 0;\r | |
1795 | }\r | |
1796 | }\r | |
1797 | \r | |
1798 | int CmdHF14AMfSniff(const char *Cmd){\r | |
1799 | // params\r | |
1800 | bool wantLogToFile = 0;\r | |
1801 | bool wantDecrypt = 0;\r | |
1802 | //bool wantSaveToEml = 0; TODO\r | |
1803 | bool wantSaveToEmlFile = 0;\r | |
1804 | \r | |
1805 | //var \r | |
1806 | int res = 0;\r | |
1807 | int len = 0;\r | |
1808 | int blockLen = 0;\r | |
1809 | int num = 0;\r | |
1810 | int pckNum = 0;\r | |
1811 | uint8_t uid[8];\r | |
1812 | uint8_t atqa[2];\r | |
1813 | uint8_t sak;\r | |
1814 | bool isTag;\r | |
1815 | uint32_t parity;\r | |
1816 | uint8_t buf[3000];\r | |
1817 | uint8_t * bufPtr = buf;\r | |
1818 | memset(buf, 0x00, 3000);\r | |
1819 | \r | |
1820 | if (param_getchar(Cmd, 0) == 'h') {\r | |
1821 | PrintAndLog("It continuously get data from the field and saves it to: log, emulator, emulator file.");\r | |
1822 | PrintAndLog("You can specify:");\r | |
1823 | PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");\r | |
1824 | PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");\r | |
1825 | PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");\r | |
1826 | PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");\r | |
1827 | PrintAndLog("Usage: hf mf sniff [l][d][e][r]");\r | |
1828 | PrintAndLog(" sample: hf mf sniff l d e");\r | |
1829 | return 0;\r | |
1830 | } \r | |
1831 | \r | |
1832 | for (int i = 0; i < 4; i++) {\r | |
1833 | char ctmp = param_getchar(Cmd, i);\r | |
1834 | if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;\r | |
1835 | if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;\r | |
1836 | //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO\r | |
1837 | if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;\r | |
1838 | }\r | |
1839 | \r | |
1840 | printf("-------------------------------------------------------------------------\n");\r | |
1841 | printf("Executing command. \n");\r | |
1842 | printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r | |
1843 | printf("Press the key on pc keyboard to abort the client.\n");\r | |
1844 | printf("-------------------------------------------------------------------------\n");\r | |
1845 | \r | |
1846 | UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};\r | |
1847 | SendCommand(&c);\r | |
1848 | \r | |
1849 | // wait cycle\r | |
1850 | while (true) {\r | |
1851 | printf(".");\r | |
1852 | fflush(stdout);\r | |
1853 | if (ukbhit()) {\r | |
1854 | getchar();\r | |
1855 | printf("\naborted via keyboard!\n");\r | |
1856 | break;\r | |
1857 | }\r | |
1858 | \r | |
1859 | UsbCommand resp;\r | |
1860 | if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {\r | |
1861 | res = resp.arg[0] & 0xff;\r | |
1862 | len = resp.arg[1];\r | |
1863 | num = resp.arg[2];\r | |
1864 | \r | |
1865 | if (res == 0) return 0;\r | |
1866 | if (res == 1) {\r | |
1867 | if (num ==0) {\r | |
1868 | bufPtr = buf;\r | |
1869 | memset(buf, 0x00, 3000);\r | |
1870 | }\r | |
1871 | memcpy(bufPtr, resp.d.asBytes, len);\r | |
1872 | bufPtr += len;\r | |
1873 | pckNum++;\r | |
1874 | }\r | |
1875 | if (res == 2) {\r | |
1876 | blockLen = bufPtr - buf;\r | |
1877 | bufPtr = buf;\r | |
1878 | printf(">\n");\r | |
1879 | PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);\r | |
1880 | num = 0;\r | |
1881 | while (bufPtr - buf + 9 < blockLen) {\r | |
1882 | isTag = bufPtr[3] & 0x80 ? true:false;\r | |
1883 | bufPtr += 4;\r | |
1884 | parity = *((uint32_t *)(bufPtr));\r | |
1885 | bufPtr += 4;\r | |
1886 | len = bufPtr[0];\r | |
1887 | bufPtr++;\r | |
1888 | if ((len == 14) && (bufPtr[0] = 0xff) && (bufPtr[1] = 0xff)) {\r | |
1889 | memcpy(uid, bufPtr + 2, 7);\r | |
1890 | memcpy(atqa, bufPtr + 2 + 7, 2);\r | |
1891 | sak = bufPtr[11];\r | |
1892 | \r | |
1893 | PrintAndLog("tag select uid:%s atqa:%02x %02x sak:0x%02x", sprint_hex(uid, 7), atqa[0], atqa[1], sak);\r | |
1894 | if (wantLogToFile) {\r | |
1895 | FillFileNameByUID(logHexFileName, uid, ".log", 7);\r | |
1896 | AddLogCurrentDT(logHexFileName);\r | |
1897 | } \r | |
1898 | if (wantDecrypt) mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);\r | |
1899 | } else {\r | |
1900 | PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));\r | |
1901 | if (wantLogToFile) AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);\r | |
1902 | if (wantDecrypt) mfTraceDecode(bufPtr, len, parity, wantSaveToEmlFile);\r | |
1903 | }\r | |
1904 | bufPtr += len;\r | |
1905 | num++;\r | |
1906 | }\r | |
1907 | }\r | |
1908 | } // resp not NILL\r | |
1909 | } // while (true)\r | |
1910 | return 0;\r | |
1911 | }\r | |
1912 | \r | |
1913 | static command_t CommandTable[] =\r | |
1914 | {\r | |
1915 | {"help", CmdHelp, 1, "This help"},\r | |
1916 | {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},\r | |
1917 | {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},\r | |
1918 | {"urdbl", CmdHF14AMfURdBl, 0, "Read MIFARE Ultralight block"},\r | |
1919 | {"urdcard", CmdHF14AMfURdCard, 0,"Read MIFARE Ultralight Card"},\r | |
1920 | {"uwrbl", CmdHF14AMfUWrBl, 0,"Write MIFARE Ultralight block"},\r | |
1921 | {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},\r | |
1922 | {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},\r | |
1923 | {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},\r | |
1924 | {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},\r | |
1925 | {"chk", CmdHF14AMfChk, 0, "Test block keys"},\r | |
1926 | {"mifare", CmdHF14AMifare, 0, "Read parity error messages."},\r | |
1927 | {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},\r | |
1928 | {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},\r | |
1929 | {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},\r | |
1930 | {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},\r | |
1931 | {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},\r | |
1932 | {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},\r | |
1933 | {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},\r | |
1934 | {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},\r | |
1935 | {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},\r | |
1936 | {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},\r | |
1937 | {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},\r | |
1938 | {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block into magic Chinese card"},\r | |
1939 | {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block from magic Chinese card"},\r | |
1940 | {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector from magic Chinese card"},\r | |
1941 | {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},\r | |
1942 | {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},\r | |
1943 | {NULL, NULL, 0, NULL}\r | |
1944 | };\r | |
1945 | \r | |
1946 | int CmdHFMF(const char *Cmd)\r | |
1947 | {\r | |
1948 | // flush\r | |
1949 | WaitForResponseTimeout(CMD_ACK,NULL,100);\r | |
1950 | \r | |
1951 | CmdsParse(CommandTable, Cmd);\r | |
1952 | return 0;\r | |
1953 | }\r | |
1954 | \r | |
1955 | int CmdHelp(const char *Cmd)\r | |
1956 | {\r | |
1957 | CmdsHelp(CommandTable);\r | |
1958 | return 0;\r | |
1959 | }\r |