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