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