]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
CHG: syntax suger
[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 - %4.2f sec\n", (float)t1, ((float)t1)/CLOCKS_PER_SEC);
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(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
597 PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");
598 PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");
599 PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
600 return 0;
601 }
602
603 cmdp = param_getchar(Cmd, 0);
604 blockNo = param_get8(Cmd, 1);
605 ctmp = param_getchar(Cmd, 2);
606
607 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
608 PrintAndLog("Key type must be A or B");
609 return 1;
610 }
611
612 if (ctmp != 'A' && ctmp != 'a')
613 keyType = 1;
614
615 if (param_gethex(Cmd, 3, key, 12)) {
616 PrintAndLog("Key must include 12 HEX symbols");
617 return 1;
618 }
619
620 if (cmdp == 'o' || cmdp == 'O') {
621 cmdp = 'o';
622 trgBlockNo = param_get8(Cmd, 4);
623 ctmp = param_getchar(Cmd, 5);
624 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
625 PrintAndLog("Target key type must be A or B");
626 return 1;
627 }
628 if (ctmp != 'A' && ctmp != 'a')
629 trgKeyType = 1;
630 } else {
631
632 switch (cmdp) {
633 case '0': SectorsCnt = 05; break;
634 case '1': SectorsCnt = 16; break;
635 case '2': SectorsCnt = 32; break;
636 case '4': SectorsCnt = 40; break;
637 default: SectorsCnt = 16;
638 }
639 }
640
641 ctmp = param_getchar(Cmd, 4);
642 if (ctmp == 't' || ctmp == 'T') transferToEml = true;
643 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = true;
644
645 ctmp = param_getchar(Cmd, 6);
646 transferToEml |= (ctmp == 't' || ctmp == 'T');
647 transferToEml |= (ctmp == 'd' || ctmp == 'D');
648
649 if (cmdp == 'o') {
650 int16_t isOK = mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, true);
651 switch (isOK) {
652 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
653 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
654 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
655 case -4 : PrintAndLog("No valid key found"); break;
656 case -5 :
657 key64 = bytes_to_num(keyBlock, 6);
658
659 // transfer key to the emulator
660 if (transferToEml) {
661 uint8_t sectortrailer;
662 if (trgBlockNo < 32*4) { // 4 block sector
663 sectortrailer = (trgBlockNo & 0x03) + 3;
664 } else { // 16 block sector
665 sectortrailer = (trgBlockNo & 0x0f) + 15;
666 }
667 mfEmlGetMem(keyBlock, sectortrailer, 1);
668
669 if (!trgKeyType)
670 num_to_bytes(key64, 6, keyBlock);
671 else
672 num_to_bytes(key64, 6, &keyBlock[10]);
673 mfEmlSetMem(keyBlock, sectortrailer, 1);
674 }
675 return 0;
676 default : PrintAndLog("Unknown Error.\n");
677 }
678 return 2;
679 }
680 else { // ------------------------------------ multiple sectors working
681 clock_t t1 = clock();
682
683 e_sector = calloc(SectorsCnt, sizeof(sector));
684 if (e_sector == NULL) return 1;
685
686 //test current key and additional standard keys first
687 memcpy(keyBlock, key, 6);
688 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 1 * 6));
689 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 2 * 6));
690 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 3 * 6));
691 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 4 * 6));
692 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));
693
694 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);
695 for (i = 0; i < SectorsCnt; i++) {
696 for (j = 0; j < 2; j++) {
697 if (e_sector[i].foundKey[j]) continue;
698
699 res = mfCheckKeys(FirstBlockOfSector(i), j, true, 6, keyBlock, &key64);
700
701 if (!res) {
702 e_sector[i].Key[j] = key64;
703 e_sector[i].foundKey[j] = TRUE;
704 }
705 }
706 }
707 clock_t t2 = clock() - t1;
708 if ( t2 > 0 )
709 PrintAndLog("Time to check 6 known keys: %.0f ticks %4.2f sec", (float)t2, ((float)t2)/CLOCKS_PER_SEC);
710
711
712 // nested sectors
713 iterations = 0;
714 PrintAndLog("enter nested...");
715 bool calibrate = true;
716 for (i = 0; i < NESTED_SECTOR_RETRY; i++) {
717 for (uint8_t sectorNo = 0; sectorNo < SectorsCnt; ++sectorNo) {
718 for (trgKeyType = 0; trgKeyType < 2; ++trgKeyType) {
719
720 if (e_sector[sectorNo].foundKey[trgKeyType]) continue;
721
722 int16_t isOK = mfnested(blockNo, keyType, key, FirstBlockOfSector(sectorNo), trgKeyType, keyBlock, calibrate);
723 switch (isOK) {
724 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
725 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
726 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
727 case -4 : //key not found
728 calibrate = false;
729 iterations++;
730 continue;
731 case -5 :
732 calibrate = false;
733 iterations++;
734 e_sector[sectorNo].foundKey[trgKeyType] = 1;
735 e_sector[sectorNo].Key[trgKeyType] = bytes_to_num(keyBlock, 6);
736 continue;
737
738 default : PrintAndLog("Unknown Error.\n");
739 }
740 free(e_sector);
741 return 2;
742 }
743 }
744 }
745
746 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
747 PrintAndLog("trying to read key B...");
748 for (i = 0; i < SectorsCnt; i++) {
749 // KEY A but not KEY B
750 if ( e_sector[i].foundKey[0] && !e_sector[i].foundKey[1] ) {
751
752 uint8_t sectrail = (FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);
753
754 PrintAndLog("Reading block %d", sectrail);
755
756 UsbCommand c = {CMD_MIFARE_READBL, {sectrail, 0, 0}};
757 num_to_bytes(e_sector[i].Key[0], 6, c.d.asBytes); // KEY A
758 clearCommandBuffer();
759 SendCommand(&c);
760
761 UsbCommand resp;
762 if ( !WaitForResponseTimeout(CMD_ACK,&resp,1500)) continue;
763
764 uint8_t isOK = resp.arg[0] & 0xff;
765 if (!isOK) continue;
766
767 uint8_t *data = resp.d.asBytes;
768 key64 = bytes_to_num(data+10, 6);
769 if (key64) {
770 PrintAndLog("Data:%s", sprint_hex(data+10, 6));
771 e_sector[i].foundKey[1] = 1;
772 e_sector[i].Key[1] = key64;
773 }
774 }
775 }
776
777 t1 = clock() - t1;
778 if ( t1 > 0 )
779 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);
780
781 //print them
782 printKeyTable( SectorsCnt, e_sector );
783
784 // transfer them to the emulator
785 if (transferToEml) {
786 for (i = 0; i < SectorsCnt; i++) {
787 mfEmlGetMem(keyBlock, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);
788 if (e_sector[i].foundKey[0])
789 num_to_bytes(e_sector[i].Key[0], 6, keyBlock);
790 if (e_sector[i].foundKey[1])
791 num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);
792 mfEmlSetMem(keyBlock, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);
793 }
794 }
795
796 // Create dump file
797 if (createDumpFile) {
798 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
799 PrintAndLog("Could not create file dumpkeys.bin");
800 free(e_sector);
801 return 1;
802 }
803 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
804 for(i=0; i<SectorsCnt; i++) {
805 if (e_sector[i].foundKey[0]){
806 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
807 fwrite ( tempkey, 1, 6, fkeys );
808 }
809 else{
810 fwrite ( &standart, 1, 6, fkeys );
811 }
812 }
813 for(i=0; i<SectorsCnt; i++) {
814 if (e_sector[i].foundKey[1]){
815 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
816 fwrite ( tempkey, 1, 6, fkeys );
817 }
818 else{
819 fwrite ( &standart, 1, 6, fkeys );
820 }
821 }
822 fclose(fkeys);
823 }
824
825 free(e_sector);
826 }
827 return 0;
828 }
829
830 int CmdHF14AMfNestedHard(const char *Cmd)
831 {
832 uint8_t blockNo = 0;
833 uint8_t keyType = 0;
834 uint8_t trgBlockNo = 0;
835 uint8_t trgKeyType = 0;
836 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
837 uint8_t trgkey[6] = {0, 0, 0, 0, 0, 0};
838
839 char ctmp;
840 ctmp = param_getchar(Cmd, 0);
841
842 if (ctmp != 'R' && ctmp != 'r' && ctmp != 'T' && ctmp != 't' && strlen(Cmd) < 20) {
843 PrintAndLog("Usage:");
844 PrintAndLog(" hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>");
845 PrintAndLog(" <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]");
846 PrintAndLog(" or hf mf hardnested r [known target key]");
847 PrintAndLog(" ");
848 PrintAndLog("Options: ");
849 PrintAndLog(" w: Acquire nonces and write them to binary file nonces.bin");
850 PrintAndLog(" s: Slower acquisition (required by some non standard cards)");
851 PrintAndLog(" r: Read nonces.bin and start attack");
852 PrintAndLog(" ");
853 PrintAndLog(" sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A");
854 PrintAndLog(" sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");
855 PrintAndLog(" sample3: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");
856 PrintAndLog(" sample4: hf mf hardnested r");
857 PrintAndLog(" ");
858 PrintAndLog("Add the known target key to check if it is present in the remaining key space:");
859 PrintAndLog(" sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF");
860 return 0;
861 }
862
863 bool know_target_key = false;
864 bool nonce_file_read = false;
865 bool nonce_file_write = false;
866 bool slow = false;
867 int tests = 0;
868
869
870 if (ctmp == 'R' || ctmp == 'r') {
871 nonce_file_read = true;
872 if (!param_gethex(Cmd, 1, trgkey, 12)) {
873 know_target_key = true;
874 }
875 } else if (ctmp == 'T' || ctmp == 't') {
876 tests = param_get32ex(Cmd, 1, 100, 10);
877 } else {
878 blockNo = param_get8(Cmd, 0);
879 ctmp = param_getchar(Cmd, 1);
880 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
881 PrintAndLog("Key type must be A or B");
882 return 1;
883 }
884 if (ctmp != 'A' && ctmp != 'a') {
885 keyType = 1;
886 }
887
888 if (param_gethex(Cmd, 2, key, 12)) {
889 PrintAndLog("Key must include 12 HEX symbols");
890 return 1;
891 }
892
893 trgBlockNo = param_get8(Cmd, 3);
894 ctmp = param_getchar(Cmd, 4);
895 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
896 PrintAndLog("Target key type must be A or B");
897 return 1;
898 }
899 if (ctmp != 'A' && ctmp != 'a') {
900 trgKeyType = 1;
901 }
902
903 uint16_t i = 5;
904
905 if (!param_gethex(Cmd, 5, trgkey, 12)) {
906 know_target_key = true;
907 i++;
908 }
909
910 while ((ctmp = param_getchar(Cmd, i))) {
911 if (ctmp == 's' || ctmp == 'S') {
912 slow = true;
913 } else if (ctmp == 'w' || ctmp == 'W') {
914 nonce_file_write = true;
915 } else {
916 PrintAndLog("Possible options are w and/or s");
917 return 1;
918 }
919 i++;
920 }
921 }
922
923 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 ",
924 trgBlockNo,
925 trgKeyType?'B':'A',
926 trgkey[0], trgkey[1], trgkey[2], trgkey[3], trgkey[4], trgkey[5],
927 know_target_key?"":" (not set)",
928 nonce_file_write?"write":nonce_file_read?"read":"none",
929 slow?"Yes":"No",
930 tests);
931
932 int16_t isOK = mfnestedhard(blockNo, keyType, key, trgBlockNo, trgKeyType, know_target_key?trgkey:NULL, nonce_file_read, nonce_file_write, slow, tests);
933
934 if (isOK) {
935 switch (isOK) {
936 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
937 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
938 default : break;
939 }
940 return 2;
941 }
942
943 return 0;
944 }
945
946 int CmdHF14AMfChk(const char *Cmd)
947 {
948 if (strlen(Cmd)<3) {
949 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
950 PrintAndLog(" * - all sectors");
951 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
952 PrintAndLog("d - write keys to binary file");
953 PrintAndLog("t - write keys to emulator memory\n");
954 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
955 PrintAndLog(" hf mf chk *1 ? t");
956 PrintAndLog(" hf mf chk *1 ? d");
957 return 0;
958 }
959
960 FILE * f;
961 char filename[FILE_PATH_SIZE]={0};
962 char buf[13];
963 uint8_t *keyBlock = NULL, *p;
964 uint8_t stKeyBlock = 20;
965
966 sector *e_sector = NULL;
967
968 int i, res;
969 int keycnt = 0;
970 char ctmp = 0x00;
971 uint8_t blockNo = 0;
972 uint8_t SectorsCnt = 1;
973 uint8_t keyType = 0;
974 uint64_t key64 = 0;
975
976 uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
977
978 int transferToEml = 0;
979 int createDumpFile = 0;
980
981 keyBlock = calloc(stKeyBlock, 6);
982 if (keyBlock == NULL) return 1;
983
984 uint64_t defaultKeys[] = {
985 0xffffffffffff, // Default key (first key used by program if no user defined key)
986 0x000000000000, // Blank key
987 0xa0a1a2a3a4a5, // NFCForum MAD key
988 0xb0b1b2b3b4b5,
989 0xaabbccddeeff,
990 0x4d3a99c351dd,
991 0x1a982c7e459a,
992 0xd3f7d3f7d3f7,
993 0x714c5c886e97,
994 0x587ee5f9350f,
995 0xa0478cc39091,
996 0x533cb6c723f6,
997 0x8fd0a4f256e9
998 };
999 int defaultKeysSize = sizeof(defaultKeys) / sizeof(uint64_t);
1000
1001 for (int defaultKeyCounter = 0; defaultKeyCounter < defaultKeysSize; defaultKeyCounter++)
1002 num_to_bytes(defaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));
1003
1004
1005 if (param_getchar(Cmd, 0)=='*') {
1006 blockNo = 3;
1007 switch(param_getchar(Cmd+1, 0)) {
1008 case '0': SectorsCnt = 5; break;
1009 case '1': SectorsCnt = 16; break;
1010 case '2': SectorsCnt = 32; break;
1011 case '4': SectorsCnt = 40; break;
1012 default: SectorsCnt = 16;
1013 }
1014 } else {
1015 blockNo = param_get8(Cmd, 0);
1016 }
1017
1018 ctmp = param_getchar(Cmd, 1);
1019 switch (ctmp) {
1020 case 'a': case 'A':
1021 keyType = !0;
1022 break;
1023 case 'b': case 'B':
1024 keyType = !1;
1025 break;
1026 case '?':
1027 keyType = 2;
1028 break;
1029 default:
1030 PrintAndLog("Key type must be A , B or ?");
1031 free(keyBlock);
1032 return 1;
1033 };
1034
1035 ctmp = param_getchar(Cmd, 2);
1036 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;
1037 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;
1038
1039 for (i = transferToEml || createDumpFile; param_getchar(Cmd, 2 + i); i++) {
1040 if (!param_gethex(Cmd, 2 + i, keyBlock + 6 * keycnt, 12)) {
1041 if ( stKeyBlock - keycnt < 2) {
1042 p = realloc(keyBlock, 6*(stKeyBlock+=10));
1043 if (!p) {
1044 PrintAndLog("Cannot allocate memory for Keys");
1045 free(keyBlock);
1046 return 2;
1047 }
1048 keyBlock = p;
1049 }
1050 PrintAndLog("check key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,
1051 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],
1052 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);
1053 keycnt++;
1054 } else {
1055 // May be a dic file
1056 if ( param_getstr(Cmd, 2 + i,filename) >= FILE_PATH_SIZE ) {
1057 PrintAndLog("File name too long");
1058 free(keyBlock);
1059 return 2;
1060 }
1061
1062 if ( (f = fopen( filename , "r")) ) {
1063 while( fgets(buf, sizeof(buf), f) ){
1064 if (strlen(buf) < 12 || buf[11] == '\n')
1065 continue;
1066
1067 while (fgetc(f) != '\n' && !feof(f)) ; //goto next line
1068
1069 if( buf[0]=='#' ) continue; //The line start with # is comment, skip
1070
1071 if (!isxdigit(buf[0])){
1072 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);
1073 continue;
1074 }
1075
1076 buf[12] = 0;
1077
1078 if ( stKeyBlock - keycnt < 2) {
1079 p = realloc(keyBlock, 6*(stKeyBlock+=10));
1080 if (!p) {
1081 PrintAndLog("Cannot allocate memory for defKeys");
1082 free(keyBlock);
1083 fclose(f);
1084 return 2;
1085 }
1086 keyBlock = p;
1087 }
1088 memset(keyBlock + 6 * keycnt, 0, 6);
1089 num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);
1090 PrintAndLog("check custom key[%2d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));
1091 keycnt++;
1092 memset(buf, 0, sizeof(buf));
1093 }
1094 fclose(f);
1095 } else {
1096 PrintAndLog("File: %s: not found or locked.", filename);
1097 free(keyBlock);
1098 return 1;
1099
1100 }
1101 }
1102 }
1103
1104 if (keycnt == 0) {
1105 PrintAndLog("No key specified, trying default keys");
1106 for (;keycnt < defaultKeysSize; keycnt++)
1107 PrintAndLog("check default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,
1108 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],
1109 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);
1110 }
1111
1112 // initialize storage for found keys
1113 e_sector = calloc(SectorsCnt, sizeof(sector));
1114 if (e_sector == NULL) {
1115 free(keyBlock);
1116 return 1;
1117 }
1118
1119 uint8_t trgKeyType = 0;
1120
1121 // time
1122 clock_t t1 = clock();
1123
1124 // check keys.
1125 for (trgKeyType = 0; trgKeyType < 2; ++trgKeyType) {
1126 int b = blockNo;
1127 for (int i = 0; i < SectorsCnt; ++i) {
1128
1129 // skip already found keys.
1130 if (e_sector[i].foundKey[trgKeyType]) continue;
1131
1132 uint32_t max_keys = keycnt > (USB_CMD_DATA_SIZE/6) ? (USB_CMD_DATA_SIZE/6) : keycnt;
1133
1134 for (uint32_t c = 0; c < keycnt; c += max_keys) {
1135
1136 uint32_t size = keycnt-c > max_keys ? max_keys : keycnt-c;
1137
1138 res = mfCheckKeys(b, trgKeyType, true, size, &keyBlock[6*c], &key64);
1139 if (!res) {
1140 PrintAndLog("Sector:%3d Block:%3d, key type: %C -- Found key [%012"llx"]", i, b, trgKeyType ? 'B':'A', key64);
1141
1142 e_sector[i].Key[trgKeyType] = key64;
1143 e_sector[i].foundKey[trgKeyType] = TRUE;
1144 break;
1145 } else {
1146 e_sector[i].Key[trgKeyType] = 0xffffffffffff;
1147 e_sector[i].foundKey[trgKeyType] = FALSE;
1148 }
1149 }
1150 b < 127 ? ( b +=4 ) : ( b += 16 );
1151 }
1152 }
1153 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
1154 PrintAndLog("testing to read B...");
1155 for (i = 0; i < SectorsCnt; i++) {
1156 // KEY A but not KEY B
1157 if ( e_sector[i].foundKey[0] && !e_sector[i].foundKey[1] ) {
1158
1159 uint8_t sectrail = (FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);
1160
1161 PrintAndLog("Reading block %d", sectrail);
1162
1163 UsbCommand c = {CMD_MIFARE_READBL, {sectrail, 0, 0}};
1164 num_to_bytes(e_sector[i].Key[0], 6, c.d.asBytes); // KEY A
1165 clearCommandBuffer();
1166 SendCommand(&c);
1167
1168 UsbCommand resp;
1169 if ( !WaitForResponseTimeout(CMD_ACK,&resp,1500)) continue;
1170
1171 uint8_t isOK = resp.arg[0] & 0xff;
1172 if (!isOK) continue;
1173
1174 uint8_t *data = resp.d.asBytes;
1175 key64 = bytes_to_num(data+10, 6);
1176 if (key64) {
1177 PrintAndLog("Data:%s", sprint_hex(data+10, 6));
1178 e_sector[i].foundKey[1] = 1;
1179 e_sector[i].Key[1] = key64;
1180 }
1181 }
1182 }
1183
1184 t1 = clock() - t1;
1185 if ( t1 > 0 )
1186 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);
1187
1188 //print them
1189 printKeyTable( SectorsCnt, e_sector );
1190
1191 if (transferToEml) {
1192 uint8_t block[16] = {0x00};
1193 for (uint8_t i = 0; i < SectorsCnt; ++i ) {
1194 mfEmlGetMem(block, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);
1195 if (e_sector[i].foundKey[0])
1196 num_to_bytes(e_sector[i].Key[0], 6, block);
1197 if (e_sector[i].foundKey[1])
1198 num_to_bytes(e_sector[i].Key[1], 6, block+10);
1199 mfEmlSetMem(block, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);
1200 }
1201 PrintAndLog("Found keys have been transferred to the emulator memory");
1202 }
1203
1204 if (createDumpFile) {
1205 FILE *fkeys = fopen("dumpkeys.bin","wb");
1206 if (fkeys == NULL) {
1207 PrintAndLog("Could not create file dumpkeys.bin");
1208 free(keyBlock);
1209 free(e_sector);
1210 return 1;
1211 }
1212 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
1213
1214 for( i=0; i<SectorsCnt; i++) {
1215 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
1216 fwrite ( tempkey, 1, 6, fkeys );
1217 }
1218 for(i=0; i<SectorsCnt; i++) {
1219 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
1220 fwrite ( tempkey, 1, 6, fkeys );
1221 }
1222 fclose(fkeys);
1223 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1224 }
1225
1226 free(keyBlock);
1227 free(e_sector);
1228 PrintAndLog("");
1229 return 0;
1230 }
1231
1232 void printKeyTable( uint8_t sectorscnt, sector *e_sector ){
1233 PrintAndLog("|---|----------------|---|----------------|---|");
1234 PrintAndLog("|sec|key A |res|key B |res|");
1235 PrintAndLog("|---|----------------|---|----------------|---|");
1236 for (uint8_t i = 0; i < sectorscnt; ++i) {
1237 PrintAndLog("|%03d| %012"llx" | %d | %012"llx" | %d |", i,
1238 e_sector[i].Key[0], e_sector[i].foundKey[0],
1239 e_sector[i].Key[1], e_sector[i].foundKey[1]
1240 );
1241 }
1242 PrintAndLog("|---|----------------|---|----------------|---|");
1243 }
1244
1245 int CmdHF14AMf1kSim(const char *Cmd)
1246 {
1247 uint8_t uid[7] = {0, 0, 0, 0, 0, 0, 0};
1248 uint8_t exitAfterNReads = 0;
1249 uint8_t flags = 0;
1250
1251 uint8_t cmdp = param_getchar(Cmd, 0);
1252
1253 if (cmdp == 'h' || cmdp == 'H') {
1254 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1255 PrintAndLog(" h this help");
1256 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1257 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1258 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1259 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1260 PrintAndLog("");
1261 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");
1262 return 0;
1263 }
1264 uint8_t pnr = 0;
1265 if (param_getchar(Cmd, pnr) == 'u') {
1266 if(param_gethex(Cmd, pnr+1, uid, 8) == 0)
1267 {
1268 flags |= FLAG_4B_UID_IN_DATA; // UID from packet
1269 } else if(param_gethex(Cmd,pnr+1,uid,14) == 0) {
1270 flags |= FLAG_7B_UID_IN_DATA;// UID from packet
1271 } else {
1272 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1273 return 1;
1274 }
1275 pnr +=2;
1276 }
1277
1278 if (param_getchar(Cmd, pnr) == 'n') {
1279 exitAfterNReads = param_get8(Cmd,pnr+1);
1280 pnr += 2;
1281 }
1282
1283 if (param_getchar(Cmd, pnr) == 'i' ) {
1284 //Using a flag to signal interactiveness, least significant bit
1285 flags |= FLAG_INTERACTIVE;
1286 pnr++;
1287 }
1288
1289 if (param_getchar(Cmd, pnr) == 'x' ) {
1290 //Using a flag to signal interactiveness, least significant bit
1291 flags |= FLAG_NR_AR_ATTACK;
1292 }
1293
1294 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1295 flags & FLAG_4B_UID_IN_DATA ? sprint_hex(uid,4):
1296 flags & FLAG_7B_UID_IN_DATA ? sprint_hex(uid,7): "N/A"
1297 , exitAfterNReads
1298 , flags
1299 , flags);
1300
1301
1302 UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {flags, exitAfterNReads,0}};
1303 memcpy(c.d.asBytes, uid, sizeof(uid));
1304 clearCommandBuffer();
1305 SendCommand(&c);
1306
1307 if(flags & FLAG_INTERACTIVE)
1308 {
1309 uint8_t data[40];
1310 uint8_t key[6];
1311
1312 UsbCommand resp;
1313 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1314
1315 while( !ukbhit() ){
1316 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) continue;
1317
1318 if ( !(flags & FLAG_NR_AR_ATTACK) ) break;
1319
1320 if ( (resp.arg[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD ) break;
1321
1322 memset(data, 0x00, sizeof(data));
1323 memset(key, 0x00, sizeof(key));
1324 int len = (resp.arg[1] > sizeof(data)) ? sizeof(data) : resp.arg[1];
1325
1326 memcpy(data, resp.d.asBytes, len);
1327
1328 uint64_t corr_uid = 0;
1329
1330 // this IF? what was I thinking of?
1331 if ( memcmp(data, "\x00\x00\x00\x00", 4) == 0 ) {
1332 corr_uid = ((uint64_t)(data[3] << 24)) | (data[2] << 16) | (data[1] << 8) | data[0];
1333 tryMfk32(corr_uid, data, key);
1334 } else {
1335 corr_uid |= (uint64_t)data[2] << 48;
1336 corr_uid |= (uint64_t)data[1] << 40;
1337 corr_uid |= (uint64_t)data[0] << 32;
1338 corr_uid |= (uint64_t)data[7] << 24;
1339 corr_uid |= (uint64_t)data[6] << 16;
1340 corr_uid |= (uint64_t)data[5] << 8;
1341 corr_uid |= (uint64_t)data[4];
1342 tryMfk64(corr_uid, data, key);
1343 }
1344 PrintAndLog("--");
1345 }
1346 }
1347 return 0;
1348 }
1349
1350 int CmdHF14AMfDbg(const char *Cmd)
1351 {
1352 int dbgMode = param_get32ex(Cmd, 0, 0, 10);
1353 if (dbgMode > 4) {
1354 PrintAndLog("Max debug mode parameter is 4 \n");
1355 }
1356
1357 if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {
1358 PrintAndLog("Usage: hf mf dbg <debug level>");
1359 PrintAndLog(" 0 - no debug messages");
1360 PrintAndLog(" 1 - error messages");
1361 PrintAndLog(" 2 - plus information messages");
1362 PrintAndLog(" 3 - plus debug messages");
1363 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1364 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1365 return 0;
1366 }
1367
1368 UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};
1369 SendCommand(&c);
1370
1371 return 0;
1372 }
1373
1374 int CmdHF14AMfEGet(const char *Cmd)
1375 {
1376 uint8_t blockNo = 0;
1377 uint8_t data[16] = {0x00};
1378
1379 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1380 PrintAndLog("Usage: hf mf eget <block number>");
1381 PrintAndLog(" sample: hf mf eget 0 ");
1382 return 0;
1383 }
1384
1385 blockNo = param_get8(Cmd, 0);
1386
1387 PrintAndLog(" ");
1388 if (!mfEmlGetMem(data, blockNo, 1)) {
1389 PrintAndLog("data[%3d]:%s", blockNo, sprint_hex(data, 16));
1390 } else {
1391 PrintAndLog("Command execute timeout");
1392 }
1393
1394 return 0;
1395 }
1396
1397 int CmdHF14AMfEClear(const char *Cmd)
1398 {
1399 if (param_getchar(Cmd, 0) == 'h') {
1400 PrintAndLog("Usage: hf mf eclr");
1401 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1402 return 0;
1403 }
1404
1405 UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};
1406 SendCommand(&c);
1407 return 0;
1408 }
1409
1410 int CmdHF14AMfESet(const char *Cmd)
1411 {
1412 uint8_t memBlock[16];
1413 uint8_t blockNo = 0;
1414
1415 memset(memBlock, 0x00, sizeof(memBlock));
1416
1417 if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {
1418 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1419 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1420 return 0;
1421 }
1422
1423 blockNo = param_get8(Cmd, 0);
1424
1425 if (param_gethex(Cmd, 1, memBlock, 32)) {
1426 PrintAndLog("block data must include 32 HEX symbols");
1427 return 1;
1428 }
1429
1430 // 1 - blocks count
1431 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};
1432 memcpy(c.d.asBytes, memBlock, 16);
1433 SendCommand(&c);
1434 return 0;
1435 }
1436
1437 int CmdHF14AMfELoad(const char *Cmd)
1438 {
1439 FILE * f;
1440 char filename[FILE_PATH_SIZE];
1441 char *fnameptr = filename;
1442 char buf[64] = {0x00};
1443 uint8_t buf8[64] = {0x00};
1444 int i, len, blockNum, numBlocks;
1445 int nameParamNo = 1;
1446 uint8_t blockWidth = 32;
1447 char ctmp = param_getchar(Cmd, 0);
1448
1449 if ( ctmp == 'h' || ctmp == 'H' || ctmp == 0x00) {
1450 PrintAndLog("It loads emul dump from the file `filename.eml`");
1451 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1452 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1453 PrintAndLog("");
1454 PrintAndLog(" sample: hf mf eload filename");
1455 PrintAndLog(" hf mf eload 4 filename");
1456 return 0;
1457 }
1458
1459 switch (ctmp) {
1460 case '0' : numBlocks = 5*4; break;
1461 case '1' :
1462 case '\0': numBlocks = 16*4; break;
1463 case '2' : numBlocks = 32*4; break;
1464 case '4' : numBlocks = 256; break;
1465 case 'U' : // fall through
1466 case 'u' : numBlocks = 255; blockWidth = 8; break;
1467 default: {
1468 numBlocks = 16*4;
1469 nameParamNo = 0;
1470 }
1471 }
1472 uint32_t numblk2 = param_get32ex(Cmd,2,0,10);
1473 if (numblk2 > 0) numBlocks = numblk2;
1474
1475 len = param_getstr(Cmd,nameParamNo,filename);
1476
1477 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;
1478
1479 fnameptr += len;
1480
1481 sprintf(fnameptr, ".eml");
1482
1483 // open file
1484 f = fopen(filename, "r");
1485 if (f == NULL) {
1486 PrintAndLog("File %s not found or locked", filename);
1487 return 1;
1488 }
1489
1490 blockNum = 0;
1491 while(!feof(f)){
1492 memset(buf, 0, sizeof(buf));
1493
1494 if (fgets(buf, sizeof(buf), f) == NULL) {
1495
1496 if (blockNum >= numBlocks) break;
1497
1498 PrintAndLog("File reading error.");
1499 fclose(f);
1500 return 2;
1501 }
1502
1503 if (strlen(buf) < blockWidth){
1504 if(strlen(buf) && feof(f))
1505 break;
1506 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth);
1507 fclose(f);
1508 return 2;
1509 }
1510
1511 for (i = 0; i < blockWidth; i += 2) {
1512 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
1513 }
1514 if (mfEmlSetMem_xt(buf8, blockNum, 1, blockWidth/2)) {
1515 PrintAndLog("Cant set emul block: %3d", blockNum);
1516 fclose(f);
1517 return 3;
1518 }
1519 printf(".");
1520 blockNum++;
1521
1522 if (blockNum >= numBlocks) break;
1523 }
1524 fclose(f);
1525 printf("\n");
1526
1527 if ((blockNum != numBlocks)) {
1528 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum, numBlocks);
1529 return 4;
1530 }
1531 PrintAndLog("Loaded %d blocks from file: %s", blockNum, filename);
1532 return 0;
1533 }
1534
1535 int CmdHF14AMfESave(const char *Cmd)
1536 {
1537 FILE * f;
1538 char filename[FILE_PATH_SIZE];
1539 char * fnameptr = filename;
1540 uint8_t buf[64];
1541 int i, j, len, numBlocks;
1542 int nameParamNo = 1;
1543
1544 memset(filename, 0, sizeof(filename));
1545 memset(buf, 0, sizeof(buf));
1546
1547 char ctmp = param_getchar(Cmd, 0);
1548
1549 if ( ctmp == 'h' || ctmp == 'H') {
1550 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1551 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1552 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1553 PrintAndLog("");
1554 PrintAndLog(" sample: hf mf esave ");
1555 PrintAndLog(" hf mf esave 4");
1556 PrintAndLog(" hf mf esave 4 filename");
1557 return 0;
1558 }
1559
1560 switch (ctmp) {
1561 case '0' : numBlocks = 5*4; break;
1562 case '1' :
1563 case '\0': numBlocks = 16*4; break;
1564 case '2' : numBlocks = 32*4; break;
1565 case '4' : numBlocks = 256; break;
1566 default: {
1567 numBlocks = 16*4;
1568 nameParamNo = 0;
1569 }
1570 }
1571
1572 len = param_getstr(Cmd,nameParamNo,filename);
1573
1574 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;
1575
1576 // user supplied filename?
1577 if (len < 1) {
1578 // get filename (UID from memory)
1579 if (mfEmlGetMem(buf, 0, 1)) {
1580 PrintAndLog("Can\'t get UID from block: %d", 0);
1581 len = sprintf(fnameptr, "dump");
1582 fnameptr += len;
1583 }
1584 else {
1585 for (j = 0; j < 7; j++, fnameptr += 2)
1586 sprintf(fnameptr, "%02X", buf[j]);
1587 }
1588 } else {
1589 fnameptr += len;
1590 }
1591
1592 // add file extension
1593 sprintf(fnameptr, ".eml");
1594
1595 // open file
1596 f = fopen(filename, "w+");
1597
1598 if ( !f ) {
1599 PrintAndLog("Can't open file %s ", filename);
1600 return 1;
1601 }
1602
1603 // put hex
1604 for (i = 0; i < numBlocks; i++) {
1605 if (mfEmlGetMem(buf, i, 1)) {
1606 PrintAndLog("Cant get block: %d", i);
1607 break;
1608 }
1609 for (j = 0; j < 16; j++)
1610 fprintf(f, "%02X", buf[j]);
1611 fprintf(f,"\n");
1612 }
1613 fclose(f);
1614
1615 PrintAndLog("Saved %d blocks to file: %s", numBlocks, filename);
1616
1617 return 0;
1618 }
1619
1620 int CmdHF14AMfECFill(const char *Cmd)
1621 {
1622 uint8_t keyType = 0;
1623 uint8_t numSectors = 16;
1624
1625 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1626 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1627 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1628 PrintAndLog("");
1629 PrintAndLog("samples: hf mf ecfill A");
1630 PrintAndLog(" hf mf ecfill A 4");
1631 PrintAndLog("Read card and transfer its data to emulator memory.");
1632 PrintAndLog("Keys must be laid in the emulator memory. \n");
1633 return 0;
1634 }
1635
1636 char ctmp = param_getchar(Cmd, 0);
1637 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
1638 PrintAndLog("Key type must be A or B");
1639 return 1;
1640 }
1641 if (ctmp != 'A' && ctmp != 'a') keyType = 1;
1642
1643 ctmp = param_getchar(Cmd, 1);
1644 switch (ctmp) {
1645 case '0' : numSectors = 5; break;
1646 case '1' :
1647 case '\0': numSectors = 16; break;
1648 case '2' : numSectors = 32; break;
1649 case '4' : numSectors = 40; break;
1650 default: numSectors = 16;
1651 }
1652
1653 printf("--params: numSectors: %d, keyType:%d", numSectors, keyType);
1654 UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {numSectors, keyType, 0}};
1655 SendCommand(&c);
1656 return 0;
1657 }
1658
1659 int CmdHF14AMfEKeyPrn(const char *Cmd)
1660 {
1661 int i;
1662 uint8_t numSectors;
1663 uint8_t data[16];
1664 uint64_t keyA, keyB;
1665
1666 char cmdp = param_getchar(Cmd, 0);
1667
1668 if ( cmdp == 'h' || cmdp == 'H' ) {
1669 PrintAndLog("It prints the keys loaded in the emulator memory");
1670 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1671 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1672 PrintAndLog("");
1673 PrintAndLog(" sample: hf mf ekeyprn 1");
1674 return 0;
1675 }
1676
1677 switch (cmdp) {
1678 case '0' : numSectors = 5; break;
1679 case '1' :
1680 case '\0': numSectors = 16; break;
1681 case '2' : numSectors = 32; break;
1682 case '4' : numSectors = 40; break;
1683 default: numSectors = 16;
1684 }
1685
1686 PrintAndLog("|---|----------------|----------------|");
1687 PrintAndLog("|sec|key A |key B |");
1688 PrintAndLog("|---|----------------|----------------|");
1689 for (i = 0; i < numSectors; i++) {
1690 if (mfEmlGetMem(data, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1)) {
1691 PrintAndLog("error get block %d", FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);
1692 break;
1693 }
1694 keyA = bytes_to_num(data, 6);
1695 keyB = bytes_to_num(data + 10, 6);
1696 PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB);
1697 }
1698 PrintAndLog("|---|----------------|----------------|");
1699
1700 return 0;
1701 }
1702
1703 int CmdHF14AMfCSetUID(const char *Cmd)
1704 {
1705 uint8_t wipeCard = 0;
1706 uint8_t uid[8] = {0x00};
1707 uint8_t oldUid[8] = {0x00};
1708 uint8_t atqa[2] = {0x00};
1709 uint8_t sak[1] = {0x00};
1710 uint8_t atqaPresent = 1;
1711 int res;
1712 char ctmp;
1713 int argi=0;
1714
1715 if (strlen(Cmd) < 1 || param_getchar(Cmd, argi) == 'h') {
1716 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
1717 PrintAndLog("sample: hf mf csetuid 01020304");
1718 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");
1719 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
1720 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
1721 return 0;
1722 }
1723
1724 if (param_getchar(Cmd, argi) && param_gethex(Cmd, argi, uid, 8)) {
1725 PrintAndLog("UID must include 8 HEX symbols");
1726 return 1;
1727 }
1728 argi++;
1729
1730 ctmp = param_getchar(Cmd, argi);
1731 if (ctmp == 'w' || ctmp == 'W') {
1732 wipeCard = 1;
1733 atqaPresent = 0;
1734 }
1735
1736 if (atqaPresent) {
1737 if (param_getchar(Cmd, argi)) {
1738 if (param_gethex(Cmd, argi, atqa, 4)) {
1739 PrintAndLog("ATQA must include 4 HEX symbols");
1740 return 1;
1741 }
1742 argi++;
1743 if (!param_getchar(Cmd, argi) || param_gethex(Cmd, argi, sak, 2)) {
1744 PrintAndLog("SAK must include 2 HEX symbols");
1745 return 1;
1746 }
1747 argi++;
1748 } else
1749 atqaPresent = 0;
1750 }
1751
1752 if(!wipeCard) {
1753 ctmp = param_getchar(Cmd, argi);
1754 if (ctmp == 'w' || ctmp == 'W') {
1755 wipeCard = 1;
1756 }
1757 }
1758
1759 PrintAndLog("--wipe card:%s uid:%s", (wipeCard)?"YES":"NO", sprint_hex(uid, 4));
1760
1761 res = mfCSetUID(uid, (atqaPresent)?atqa:NULL, (atqaPresent)?sak:NULL, oldUid, wipeCard);
1762 if (res) {
1763 PrintAndLog("Can't set UID. error=%d", res);
1764 return 1;
1765 }
1766
1767 PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));
1768 PrintAndLog("new UID:%s", sprint_hex(uid, 4));
1769 return 0;
1770 }
1771
1772 int CmdHF14AMfCSetBlk(const char *Cmd)
1773 {
1774 uint8_t block[16] = {0x00};
1775 uint8_t blockNo = 0;
1776 uint8_t params = MAGIC_SINGLE;
1777 int res;
1778
1779 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1780 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
1781 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1782 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
1783 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
1784 return 0;
1785 }
1786
1787 blockNo = param_get8(Cmd, 0);
1788
1789 if (param_gethex(Cmd, 1, block, 32)) {
1790 PrintAndLog("block data must include 32 HEX symbols");
1791 return 1;
1792 }
1793
1794 char ctmp = param_getchar(Cmd, 2);
1795 if (ctmp == 'w' || ctmp == 'W')
1796 params |= MAGIC_WIPE;
1797
1798 PrintAndLog("--block number:%2d data:%s", blockNo, sprint_hex(block, 16));
1799
1800 res = mfCSetBlock(blockNo, block, NULL, params);
1801 if (res) {
1802 PrintAndLog("Can't write block. error=%d", res);
1803 return 1;
1804 }
1805 return 0;
1806 }
1807
1808 int CmdHF14AMfCLoad(const char *Cmd)
1809 {
1810 FILE * f;
1811 char filename[FILE_PATH_SIZE];
1812 char * fnameptr = filename;
1813 char buf[64] = {0x00};
1814 uint8_t buf8[64] = {0x00};
1815 uint8_t fillFromEmulator = 0;
1816 int i, len, blockNum, flags=0;
1817
1818 memset(filename, 0, sizeof(filename));
1819
1820 char ctmp = param_getchar(Cmd, 0);
1821
1822 if (ctmp == 'h' || ctmp == 'H' || ctmp == 0x00) {
1823 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
1824 PrintAndLog("or from emulator memory (option `e`)");
1825 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1826 PrintAndLog(" or: hf mf cload e ");
1827 PrintAndLog(" sample: hf mf cload filename");
1828 return 0;
1829 }
1830
1831 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
1832
1833 if (fillFromEmulator) {
1834 for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {
1835 if (mfEmlGetMem(buf8, blockNum, 1)) {
1836 PrintAndLog("Cant get block: %d", blockNum);
1837 return 2;
1838 }
1839 if (blockNum == 0) flags = MAGIC_INIT + MAGIC_WUPC; // switch on field and send magic sequence
1840 if (blockNum == 1) flags = 0; // just write
1841 if (blockNum == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF; // Done. Magic Halt and switch off field.
1842
1843 if (mfCSetBlock(blockNum, buf8, NULL, flags)) {
1844 PrintAndLog("Cant set magic card block: %d", blockNum);
1845 return 3;
1846 }
1847 }
1848 return 0;
1849 } else {
1850 len = strlen(Cmd);
1851 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;
1852
1853 memcpy(filename, Cmd, len);
1854 fnameptr += len;
1855
1856 sprintf(fnameptr, ".eml");
1857
1858 // open file
1859 f = fopen(filename, "r");
1860 if (f == NULL) {
1861 PrintAndLog("File not found or locked.");
1862 return 1;
1863 }
1864
1865 blockNum = 0;
1866 while(!feof(f)){
1867
1868 memset(buf, 0, sizeof(buf));
1869
1870 if (fgets(buf, sizeof(buf), f) == NULL) {
1871 fclose(f);
1872 PrintAndLog("File reading error.");
1873 return 2;
1874 }
1875
1876 if (strlen(buf) < 32) {
1877 if(strlen(buf) && feof(f))
1878 break;
1879 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1880 fclose(f);
1881 return 2;
1882 }
1883 for (i = 0; i < 32; i += 2)
1884 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
1885
1886 if (blockNum == 0) flags = MAGIC_INIT + MAGIC_WUPC; // switch on field and send magic sequence
1887 if (blockNum == 1) flags = 0; // just write
1888 if (blockNum == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF; // Done. Switch off field.
1889
1890 if (mfCSetBlock(blockNum, buf8, NULL, flags)) {
1891 PrintAndLog("Can't set magic card block: %d", blockNum);
1892 fclose(f);
1893 return 3;
1894 }
1895 blockNum++;
1896
1897 if (blockNum >= 16 * 4) break; // magic card type - mifare 1K
1898 }
1899 fclose(f);
1900
1901 // 64 or 256blocks.
1902 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
1903 PrintAndLog("File content error. There must be 64 blocks");
1904 return 4;
1905 }
1906 PrintAndLog("Loaded from file: %s", filename);
1907 return 0;
1908 }
1909 return 0;
1910 }
1911
1912 int CmdHF14AMfCGetBlk(const char *Cmd) {
1913 uint8_t data[16];
1914 uint8_t blockNo = 0;
1915 int res;
1916 memset(data, 0x00, sizeof(data));
1917 char ctmp = param_getchar(Cmd, 0);
1918
1919 if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') {
1920 PrintAndLog("Usage: hf mf cgetblk <block number>");
1921 PrintAndLog("sample: hf mf cgetblk 1");
1922 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
1923 return 0;
1924 }
1925
1926 blockNo = param_get8(Cmd, 0);
1927
1928 PrintAndLog("--block number:%2d ", blockNo);
1929
1930 res = mfCGetBlock(blockNo, data, MAGIC_SINGLE);
1931 if (res) {
1932 PrintAndLog("Can't read block. error=%d", res);
1933 return 1;
1934 }
1935
1936 PrintAndLog("data: %s", sprint_hex(data, sizeof(data)));
1937 return 0;
1938 }
1939
1940 int CmdHF14AMfCGetSc(const char *Cmd) {
1941 uint8_t data[16];
1942 uint8_t sectorNo = 0;
1943 int i, res, flags;
1944 memset(data, 0x00, sizeof(data));
1945 char ctmp = param_getchar(Cmd, 0);
1946
1947 if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') {
1948 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1949 PrintAndLog("sample: hf mf cgetsc 0");
1950 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
1951 return 0;
1952 }
1953
1954 sectorNo = param_get8(Cmd, 0);
1955 if (sectorNo > 15) {
1956 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1957 return 1;
1958 }
1959
1960 PrintAndLog("--sector number:%d ", sectorNo);
1961 PrintAndLog("block | data");
1962
1963 flags = MAGIC_INIT + MAGIC_WUPC;
1964 for (i = 0; i < 4; i++) {
1965 if (i == 1) flags = 0;
1966 if (i == 3) flags = MAGIC_HALT + MAGIC_OFF;
1967
1968 res = mfCGetBlock(sectorNo * 4 + i, data, flags);
1969 if (res) {
1970 PrintAndLog("Can't read block. %d error=%d", sectorNo * 4 + i, res);
1971 return 1;
1972 }
1973 PrintAndLog(" %3d | %s", sectorNo * 4 + i, sprint_hex(data, sizeof(data)));
1974 }
1975 return 0;
1976 }
1977
1978 int CmdHF14AMfCSave(const char *Cmd) {
1979
1980 FILE * f;
1981 char filename[FILE_PATH_SIZE];
1982 char * fnameptr = filename;
1983 uint8_t fillFromEmulator = 0;
1984 uint8_t buf[64];
1985 int i, j, len, flags;
1986
1987 memset(filename, 0, sizeof(filename));
1988 memset(buf, 0, sizeof(buf));
1989 char ctmp = param_getchar(Cmd, 0);
1990
1991 if ( ctmp == 'h' || ctmp == 'H' ) {
1992 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1993 PrintAndLog("or into emulator memory (option `e`)");
1994 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1995 PrintAndLog(" sample: hf mf esave ");
1996 PrintAndLog(" hf mf esave filename");
1997 PrintAndLog(" hf mf esave e \n");
1998 return 0;
1999 }
2000 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
2001
2002 if (fillFromEmulator) {
2003 // put into emulator
2004 flags = MAGIC_INIT + MAGIC_WUPC;
2005 for (i = 0; i < 16 * 4; i++) {
2006 if (i == 1) flags = 0;
2007 if (i == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF;
2008
2009 if (mfCGetBlock(i, buf, flags)) {
2010 PrintAndLog("Cant get block: %d", i);
2011 break;
2012 }
2013
2014 if (mfEmlSetMem(buf, i, 1)) {
2015 PrintAndLog("Cant set emul block: %d", i);
2016 return 3;
2017 }
2018 }
2019 return 0;
2020 } else {
2021 len = strlen(Cmd);
2022 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;
2023
2024 // get filename based on UID
2025 if (len < 1) {
2026
2027 if (mfCGetBlock(0, buf, MAGIC_SINGLE)) {
2028 PrintAndLog("Cant get block: %d", 0);
2029 len = sprintf(fnameptr, "dump");
2030 fnameptr += len;
2031 } else {
2032 for (j = 0; j < 7; j++, fnameptr += 2)
2033 sprintf(fnameptr, "%02x", buf[j]);
2034 }
2035 } else {
2036 memcpy(filename, Cmd, len);
2037 fnameptr += len;
2038 }
2039
2040 // add .eml extension
2041 sprintf(fnameptr, ".eml");
2042
2043 // open file
2044 f = fopen(filename, "w+");
2045
2046 if (f == NULL) {
2047 PrintAndLog("File not found or locked.");
2048 return 1;
2049 }
2050
2051 // put hex
2052 flags = MAGIC_INIT + MAGIC_WUPC;
2053 for (i = 0; i < 16 * 4; i++) {
2054 if (i == 1) flags = 0;
2055 if (i == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF;
2056
2057 if (mfCGetBlock(i, buf, flags)) {
2058 PrintAndLog("Cant get block: %d", i);
2059 break;
2060 }
2061 for (j = 0; j < 16; j++)
2062 fprintf(f, "%02x", buf[j]);
2063 fprintf(f,"\n");
2064 }
2065 fflush(f);
2066 fclose(f);
2067 PrintAndLog("Saved to file: %s", filename);
2068 return 0;
2069 }
2070 }
2071
2072 int CmdHF14AMfSniff(const char *Cmd){
2073
2074 bool wantLogToFile = 0;
2075 bool wantDecrypt = 0;
2076 //bool wantSaveToEml = 0; TODO
2077 bool wantSaveToEmlFile = 0;
2078
2079 //var
2080 int tmpchar;
2081 int res = 0;
2082 int len = 0;
2083 int blockLen = 0;
2084 int pckNum = 0;
2085 int num = 0;
2086 uint8_t uid[7];
2087 uint8_t uid_len;
2088 uint8_t atqa[2] = {0x00};
2089 uint8_t sak;
2090 bool isTag;
2091 uint8_t *buf = NULL;
2092 uint16_t bufsize = 0;
2093 uint8_t *bufPtr = NULL;
2094
2095 char ctmp = param_getchar(Cmd, 0);
2096 if ( ctmp == 'h' || ctmp == 'H' ) {
2097 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
2098 PrintAndLog("You can specify:");
2099 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
2100 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
2101 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
2102 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
2103 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");
2104 PrintAndLog(" sample: hf mf sniff l d e");
2105 return 0;
2106 }
2107
2108 for (int i = 0; i < 4; i++) {
2109 ctmp = param_getchar(Cmd, i);
2110 if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;
2111 if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;
2112 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
2113 if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;
2114 }
2115
2116 printf("-------------------------------------------------------------------------\n");
2117 printf("Executing command. \n");
2118 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
2119 printf("Press the key on pc keyboard to abort the client.\n");
2120 printf("-------------------------------------------------------------------------\n");
2121
2122 UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};
2123 clearCommandBuffer();
2124 SendCommand(&c);
2125
2126 // wait cycle
2127 while (true) {
2128 printf(".");
2129 fflush(stdout);
2130 if (ukbhit()) {
2131 tmpchar = getchar();
2132 (void)tmpchar;
2133 printf("\naborted via keyboard!\n");
2134 break;
2135 }
2136
2137 UsbCommand resp;
2138 if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {
2139 res = resp.arg[0] & 0xff;
2140 uint16_t traceLen = resp.arg[1];
2141 len = resp.arg[2];
2142
2143 if (res == 0) {
2144 free(buf);
2145 return 0; // we are done
2146 }
2147
2148 if (res == 1) { // there is (more) data to be transferred
2149 if (pckNum == 0) { // first packet, (re)allocate necessary buffer
2150 if (traceLen > bufsize) {
2151 uint8_t *p;
2152 if (buf == NULL) { // not yet allocated
2153 p = malloc(traceLen);
2154 } else { // need more memory
2155 p = realloc(buf, traceLen);
2156 }
2157 if (p == NULL) {
2158 PrintAndLog("Cannot allocate memory for trace");
2159 free(buf);
2160 return 2;
2161 }
2162 buf = p;
2163 }
2164 bufPtr = buf;
2165 bufsize = traceLen;
2166 memset(buf, 0x00, traceLen);
2167 }
2168 if (bufPtr == NULL) {
2169 PrintAndLog("Cannot allocate memory for trace");
2170 free(buf);
2171 return 2;
2172 }
2173 memcpy(bufPtr, resp.d.asBytes, len);
2174 bufPtr += len;
2175 pckNum++;
2176 }
2177
2178 if (res == 2) { // received all data, start displaying
2179 blockLen = bufPtr - buf;
2180 bufPtr = buf;
2181 printf(">\n");
2182 PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);
2183 while (bufPtr - buf < blockLen) {
2184 bufPtr += 6; // skip (void) timing information
2185 len = *((uint16_t *)bufPtr);
2186 if(len & 0x8000) {
2187 isTag = true;
2188 len &= 0x7fff;
2189 } else {
2190 isTag = false;
2191 }
2192 bufPtr += 2;
2193 if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff) && (bufPtr[12] == 0xff) && (bufPtr[13] == 0xff)) {
2194 memcpy(uid, bufPtr + 2, 7);
2195 memcpy(atqa, bufPtr + 2 + 7, 2);
2196 uid_len = (atqa[0] & 0xC0) == 0x40 ? 7 : 4;
2197 sak = bufPtr[11];
2198 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
2199 sprint_hex(uid + (7 - uid_len), uid_len),
2200 atqa[1],
2201 atqa[0],
2202 sak);
2203 if (wantLogToFile || wantDecrypt) {
2204 FillFileNameByUID(logHexFileName, uid + (7 - uid_len), ".log", uid_len);
2205 AddLogCurrentDT(logHexFileName);
2206 }
2207 if (wantDecrypt)
2208 mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);
2209 } else {
2210 PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));
2211 if (wantLogToFile)
2212 AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);
2213 if (wantDecrypt)
2214 mfTraceDecode(bufPtr, len, wantSaveToEmlFile);
2215 num++;
2216 }
2217 bufPtr += len;
2218 bufPtr += ((len-1)/8+1); // ignore parity
2219 }
2220 pckNum = 0;
2221 }
2222 } // resp not NULL
2223 } // while (true)
2224
2225 free(buf);
2226 return 0;
2227 }
2228
2229 //needs nt, ar, at, Data to decrypt
2230 int CmdHf14MfDecryptBytes(const char *Cmd){
2231 uint8_t data[50];
2232
2233 uint32_t nt = param_get32ex(Cmd,0,0,16);
2234 uint32_t ar_enc = param_get32ex(Cmd,1,0,16);
2235 uint32_t at_enc = param_get32ex(Cmd,2,0,16);
2236
2237 int len = 0;
2238 param_gethex_ex(Cmd, 3, data, &len);
2239
2240 len /= 2;
2241 int limit = sizeof(data) / 2;
2242
2243 if ( len >= limit )
2244 len = limit;
2245
2246 return tryDecryptWord( nt, ar_enc, at_enc, data, len);
2247 }
2248
2249 static command_t CommandTable[] = {
2250 {"help", CmdHelp, 1, "This help"},
2251 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},
2252 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},
2253 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},
2254 {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},
2255 {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},
2256 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},
2257 {"chk", CmdHF14AMfChk, 0, "Test block keys"},
2258 {"mifare", CmdHF14AMifare, 0, "Read parity error messages."},
2259 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},
2260 {"hardnested", CmdHF14AMfNestedHard, 0, "Nested attack for hardened Mifare cards"},
2261 {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},
2262 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},
2263 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},
2264 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},
2265 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},
2266 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},
2267 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},
2268 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},
2269 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},
2270 {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},
2271 {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block - Magic Chinese card"},
2272 {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block - Magic Chinese card"},
2273 {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector - Magic Chinese card"},
2274 {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},
2275 {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},
2276 {"decrypt", CmdHf14MfDecryptBytes, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2277 {NULL, NULL, 0, NULL}
2278 };
2279
2280 int CmdHFMF(const char *Cmd) {
2281 // flush
2282 clearCommandBuffer();
2283 //WaitForResponseTimeout(CMD_ACK,NULL,100);
2284 CmdsParse(CommandTable, Cmd);
2285 return 0;
2286 }
2287
2288 int CmdHelp(const char *Cmd) {
2289 CmdsHelp(CommandTable);
2290 return 0;
2291 }
Impressum, Datenschutz