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