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