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