]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
removed redundant Dbprintfs from SnoopIso1443a (issue 25)
[proxmark3-svn] / client / cmdhfmf.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2011 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 "proxmark3.h"
13
14 static int CmdHelp(const char *Cmd);
15
16 int CmdHF14AMifare(const char *Cmd)
17 {
18 uint32_t uid = 0;
19 uint32_t nt = 0;
20 uint64_t par_list = 0, ks_list = 0, r_key = 0;
21 uint8_t isOK = 0;
22 uint8_t keyBlock[6] = {0,0,0,0,0,0};
23
24 if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, keyBlock, 8)) {
25 PrintAndLog("Nt must include 8 HEX symbols");
26 return 1;
27 }
28
29 UsbCommand c = {CMD_READER_MIFARE, {(uint32_t)bytes_to_num(keyBlock, 4), 0, 0}};
30 SendCommand(&c);
31
32 //flush queue
33 while (ukbhit()) getchar();
34
35 // message
36 printf("-------------------------------------------------------------------------\n");
37 printf("Executing command. It may take up to 30 min.\n");
38 printf("Press the key on proxmark3 device to abort proxmark3.\n");
39 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
40 printf("-------------------------------------------------------------------------\n");
41
42 // wait cycle
43 while (true) {
44 printf(".");
45 if (ukbhit()) {
46 getchar();
47 printf("\naborted via keyboard!\n");
48 break;
49 }
50
51 UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 2000);
52 if (resp != NULL) {
53 isOK = resp->arg[0] & 0xff;
54
55 uid = (uint32_t)bytes_to_num(resp->d.asBytes + 0, 4);
56 nt = (uint32_t)bytes_to_num(resp->d.asBytes + 4, 4);
57 par_list = bytes_to_num(resp->d.asBytes + 8, 8);
58 ks_list = bytes_to_num(resp->d.asBytes + 16, 8);
59
60 printf("\n\n");
61 PrintAndLog("isOk:%02x", isOK);
62 if (!isOK) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n");
63 break;
64 }
65 }
66 printf("\n");
67
68 // error
69 if (isOK != 1) return 1;
70
71 // execute original function from util nonce2key
72 if (nonce2key(uid, nt, par_list, ks_list, &r_key)) return 2;
73 printf("------------------------------------------------------------------\n");
74 PrintAndLog("Key found:%012llx \n", r_key);
75
76 num_to_bytes(r_key, 6, keyBlock);
77 isOK = mfCheckKeys(0, 0, 1, keyBlock, &r_key);
78 if (!isOK)
79 PrintAndLog("Found valid key:%012llx", r_key);
80 else
81 PrintAndLog("Found invalid key. ( Nt=%08x", nt);
82
83
84 return 0;
85 }
86
87 int CmdHF14AMfWrBl(const char *Cmd)
88 {
89 uint8_t blockNo = 0;
90 uint8_t keyType = 0;
91 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
92 uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
93
94 char cmdp = 0x00;
95
96 if (strlen(Cmd)<3) {
97 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
98 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
99 return 0;
100 }
101
102 blockNo = param_get8(Cmd, 0);
103 cmdp = param_getchar(Cmd, 1);
104 if (cmdp == 0x00) {
105 PrintAndLog("Key type must be A or B");
106 return 1;
107 }
108 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
109 if (param_gethex(Cmd, 2, key, 12)) {
110 PrintAndLog("Key must include 12 HEX symbols");
111 return 1;
112 }
113 if (param_gethex(Cmd, 3, bldata, 32)) {
114 PrintAndLog("Block data must include 32 HEX symbols");
115 return 1;
116 }
117 PrintAndLog("--block no:%02x key type:%02x key:%s", blockNo, keyType, sprint_hex(key, 6));
118 PrintAndLog("--data: %s", sprint_hex(bldata, 16));
119
120 UsbCommand c = {CMD_MIFARE_WRITEBL, {blockNo, keyType, 0}};
121 memcpy(c.d.asBytes, key, 6);
122 memcpy(c.d.asBytes + 10, bldata, 16);
123 SendCommand(&c);
124 UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);
125
126 if (resp != NULL) {
127 uint8_t isOK = resp->arg[0] & 0xff;
128
129 PrintAndLog("isOk:%02x", isOK);
130 } else {
131 PrintAndLog("Command execute timeout");
132 }
133
134 return 0;
135 }
136
137 int CmdHF14AMfRdBl(const char *Cmd)
138 {
139 uint8_t blockNo = 0;
140 uint8_t keyType = 0;
141 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
142
143 char cmdp = 0x00;
144
145
146 if (strlen(Cmd)<3) {
147 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
148 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
149 return 0;
150 }
151
152 blockNo = param_get8(Cmd, 0);
153 cmdp = param_getchar(Cmd, 1);
154 if (cmdp == 0x00) {
155 PrintAndLog("Key type must be A or B");
156 return 1;
157 }
158 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
159 if (param_gethex(Cmd, 2, key, 12)) {
160 PrintAndLog("Key must include 12 HEX symbols");
161 return 1;
162 }
163 PrintAndLog("--block no:%02x key type:%02x key:%s ", blockNo, keyType, sprint_hex(key, 6));
164
165 UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};
166 memcpy(c.d.asBytes, key, 6);
167 SendCommand(&c);
168 UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);
169
170 if (resp != NULL) {
171 uint8_t isOK = resp->arg[0] & 0xff;
172 uint8_t * data = resp->d.asBytes;
173
174 if (isOK)
175 PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 16));
176 else
177 PrintAndLog("isOk:%02x", isOK);
178 } else {
179 PrintAndLog("Command execute timeout");
180 }
181
182 return 0;
183 }
184
185 int CmdHF14AMfRdSc(const char *Cmd)
186 {
187 int i;
188 uint8_t sectorNo = 0;
189 uint8_t keyType = 0;
190 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
191
192 uint8_t isOK = 0;
193 uint8_t * data = NULL;
194
195 char cmdp = 0x00;
196
197 if (strlen(Cmd)<3) {
198 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
199 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
200 return 0;
201 }
202
203 sectorNo = param_get8(Cmd, 0);
204 if (sectorNo > 63) {
205 PrintAndLog("Sector number must be less than 64");
206 return 1;
207 }
208 cmdp = param_getchar(Cmd, 1);
209 if (cmdp == 0x00) {
210 PrintAndLog("Key type must be A or B");
211 return 1;
212 }
213 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
214 if (param_gethex(Cmd, 2, key, 12)) {
215 PrintAndLog("Key must include 12 HEX symbols");
216 return 1;
217 }
218 PrintAndLog("--sector no:%02x key type:%02x key:%s ", sectorNo, keyType, sprint_hex(key, 6));
219
220 UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};
221 memcpy(c.d.asBytes, key, 6);
222 SendCommand(&c);
223 UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);
224 PrintAndLog(" ");
225
226 if (resp != NULL) {
227 isOK = resp->arg[0] & 0xff;
228 data = resp->d.asBytes;
229
230 PrintAndLog("isOk:%02x", isOK);
231 if (isOK)
232 for (i = 0; i < 2; i++) {
233 PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));
234 }
235 } else {
236 PrintAndLog("Command1 execute timeout");
237 }
238
239 // response2
240 resp = WaitForResponseTimeout(CMD_ACK, 500);
241 PrintAndLog(" ");
242
243 if (resp != NULL) {
244 isOK = resp->arg[0] & 0xff;
245 data = resp->d.asBytes;
246
247 if (isOK)
248 for (i = 0; i < 2; i++) {
249 PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));
250 }
251 } else {
252 PrintAndLog("Command2 execute timeout");
253 }
254
255 return 0;
256 }
257
258 int CmdHF14AMfDump1k(const char *Cmd)
259 {
260 int i, j;
261
262 uint8_t keyA[16][6];
263 uint8_t keyB[16][6];
264 uint8_t rights[16][4];
265
266 FILE *fin;
267 FILE *fout;
268
269 UsbCommand *resp;
270
271 if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {
272 PrintAndLog("Could not find file keys.bin");
273 return 1;
274 }
275
276 if ((fout = fopen("dumpdata.bin","wb")) == NULL) {
277 PrintAndLog("Could not create file name dump.bin");
278 return 1;
279 }
280
281 // Read key file
282
283 for (i=0 ; i<16 ; i++) {
284 fread ( keyA[i], 1, 6, fin );
285 }
286 for (i=0 ; i<16 ; i++) {
287 fread ( keyB[i], 1, 6, fin );
288 }
289
290 // Read access rights to sectors
291
292 PrintAndLog("|-----------------------------------------|");
293 PrintAndLog("|------ Reading sector access bits...-----|");
294 PrintAndLog("|-----------------------------------------|");
295
296 for (i = 0 ; i < 16 ; i++) {
297 UsbCommand c = {CMD_MIFARE_READBL, {4*i + 3, 0, 0}};
298 memcpy(c.d.asBytes, keyA[i], 6);
299 SendCommand(&c);
300 resp = WaitForResponseTimeout(CMD_ACK, 1500);
301
302 if (resp != NULL) {
303 uint8_t isOK = resp->arg[0] & 0xff;
304 uint8_t *data = resp->d.asBytes;
305 if (isOK){
306 rights[i][0] = ((data[7] & 0x10)>>4) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>2);
307 rights[i][1] = ((data[7] & 0x20)>>5) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>3);
308 rights[i][2] = ((data[7] & 0x40)>>6) | ((data[8] & 0x4)>>1) | ((data[8] & 0x40)>>4);
309 rights[i][3] = ((data[7] & 0x80)>>7) | ((data[8] & 0x8)>>2) | ((data[8] & 0x80)>>5);
310 }
311 else{
312 PrintAndLog("Could not get access rights for block %d", i);
313 }
314 }
315 else {
316 PrintAndLog("Command execute timeout");
317 }
318 }
319
320 // Read blocks and print to file
321
322 PrintAndLog("|-----------------------------------------|");
323 PrintAndLog("|----- Dumping all blocks to file... -----|");
324 PrintAndLog("|-----------------------------------------|");
325
326 for (i=0 ; i<16 ; i++) {
327 for (j=0 ; j<4 ; j++) {
328 if (j == 3){
329 UsbCommand c = {CMD_MIFARE_READBL, {i*4 + j, 0, 0}};
330 memcpy(c.d.asBytes, keyA[i], 6);
331 SendCommand(&c);
332 resp = WaitForResponseTimeout(CMD_ACK, 1500);
333 }
334 else{
335 if ((rights[i][j] == 6) | (rights[i][j] == 5)) {
336 UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 1, 0}};
337 memcpy(c.d.asBytes, keyB[i], 6);
338 SendCommand(&c);
339 resp = WaitForResponseTimeout(CMD_ACK, 1500);
340 }
341 else if (rights[i][j] == 7) {
342 PrintAndLog("Access rights do not allow reading of sector %d block %d",i,j);
343 }
344 else {
345 UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 0, 0}};
346 memcpy(c.d.asBytes, keyA[i], 6);
347 SendCommand(&c);
348 resp = WaitForResponseTimeout(CMD_ACK, 1500);
349 }
350 }
351
352 if (resp != NULL) {
353 uint8_t isOK = resp->arg[0] & 0xff;
354 uint8_t *data = resp->d.asBytes;
355 if (j == 3) {
356 data[0] = (keyA[i][0]);
357 data[1] = (keyA[i][1]);
358 data[2] = (keyA[i][2]);
359 data[3] = (keyA[i][3]);
360 data[4] = (keyA[i][4]);
361 data[5] = (keyA[i][5]);
362 data[10] = (keyB[i][0]);
363 data[11] = (keyB[i][1]);
364 data[12] = (keyB[i][2]);
365 data[13] = (keyB[i][3]);
366 data[14] = (keyB[i][4]);
367 data[15] = (keyB[i][5]);
368 }
369 if (isOK) {
370 fwrite ( data, 1, 16, fout );
371 }
372 else {
373 PrintAndLog("Could not get access rights for block %d", i);
374 }
375 }
376 else {
377 PrintAndLog("Command execute timeout");
378 }
379 }
380 }
381
382 fclose(fin);
383 fclose(fout);
384
385 return 0;
386 }
387
388 int CmdHF14AMfRestore1k(const char *Cmd)
389 {
390
391 int i,j;
392 uint8_t keyType = 0;
393 uint8_t key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
394 uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
395 uint8_t keyA[16][6];
396 uint8_t keyB[16][6];
397
398 FILE *fdump;
399 FILE *fkeys;
400
401 if ((fdump = fopen("dumpdata.bin","rb")) == NULL) {
402 PrintAndLog("Could not find file dump.bin");
403 return 1;
404 }
405 if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) {
406 PrintAndLog("Could not find file keys.bin");
407 return 1;
408 }
409
410 for (i=0 ; i<16 ; i++) {
411 fread(keyA[i], 1, 6, fkeys);
412 }
413 for (i=0 ; i<16 ; i++) {
414 fread(keyB[i], 1, 6, fkeys);
415 }
416
417 PrintAndLog("Restoring dumpdata.bin to card");
418
419 for (i=0 ; i<16 ; i++) {
420 for( j=0 ; j<4 ; j++) {
421 UsbCommand c = {CMD_MIFARE_WRITEBL, {i*4 + j, keyType, 0}};
422 memcpy(c.d.asBytes, key, 6);
423
424 fread(bldata, 1, 16, fdump);
425
426 if (j == 3) {
427 bldata[0] = (keyA[i][0]);
428 bldata[1] = (keyA[i][1]);
429 bldata[2] = (keyA[i][2]);
430 bldata[3] = (keyA[i][3]);
431 bldata[4] = (keyA[i][4]);
432 bldata[5] = (keyA[i][5]);
433 bldata[10] = (keyB[i][0]);
434 bldata[11] = (keyB[i][1]);
435 bldata[12] = (keyB[i][2]);
436 bldata[13] = (keyB[i][3]);
437 bldata[14] = (keyB[i][4]);
438 bldata[15] = (keyB[i][5]);
439 }
440
441 PrintAndLog("Writing to block %2d: %s", i*4+j, sprint_hex(bldata, 16));
442
443 /*
444 PrintAndLog("Writing to block %2d: %s Confirm? [Y,N]", i*4+j, sprint_hex(bldata, 16));
445
446 scanf("%c",&ch);
447 if ((ch != 'y') && (ch != 'Y')){
448 PrintAndLog("Aborting !");
449 return 1;
450 }
451 */
452
453 memcpy(c.d.asBytes + 10, bldata, 16);
454 SendCommand(&c);
455 UsbCommand *resp = WaitForResponseTimeout(CMD_ACK, 1500);
456
457 if (resp != NULL) {
458 uint8_t isOK = resp->arg[0] & 0xff;
459 PrintAndLog("isOk:%02x", isOK);
460 } else {
461 PrintAndLog("Command execute timeout");
462 }
463 }
464 }
465
466 fclose(fdump);
467 fclose(fkeys);
468 return 0;
469 }
470
471 int CmdHF14AMfNested(const char *Cmd)
472 {
473 int i, j, res, iterations;
474 sector * e_sector = NULL;
475 uint8_t blockNo = 0;
476 uint8_t keyType = 0;
477 uint8_t trgBlockNo = 0;
478 uint8_t trgKeyType = 0;
479 uint8_t blDiff = 0;
480 int SectorsCnt = 0;
481 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
482 uint8_t keyBlock[16 * 6];
483 uint64_t key64 = 0;
484 int transferToEml = 0;
485
486 int createDumpFile = 0;
487 FILE *fkeys;
488 uint8_t standart[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
489 uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
490
491 char cmdp, ctmp;
492
493 if (strlen(Cmd)<3) {
494 PrintAndLog("Usage:");
495 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
496 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
497 PrintAndLog(" <target block number> <target key A/B> [t]");
498 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
499 PrintAndLog("t - transfer keys into emulator memory");
500 PrintAndLog("d - write keys to binary file");
501 PrintAndLog(" ");
502 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
503 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");
504 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");
505 PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
506 return 0;
507 }
508
509 cmdp = param_getchar(Cmd, 0);
510 blockNo = param_get8(Cmd, 1);
511 ctmp = param_getchar(Cmd, 2);
512 if (ctmp == 0x00) {
513 PrintAndLog("Key type must be A or B");
514 return 1;
515 }
516 if (ctmp != 'A' && ctmp != 'a') keyType = 1;
517 if (param_gethex(Cmd, 3, key, 12)) {
518 PrintAndLog("Key must include 12 HEX symbols");
519 return 1;
520 }
521
522 if (cmdp == 'o' || cmdp == 'O') {
523 cmdp = 'o';
524 trgBlockNo = param_get8(Cmd, 4);
525 ctmp = param_getchar(Cmd, 5);
526 if (ctmp == 0x00) {
527 PrintAndLog("Target key type must be A or B");
528 return 1;
529 }
530 if (ctmp != 'A' && ctmp != 'a') trgKeyType = 1;
531 } else {
532 switch (cmdp) {
533 case '0': SectorsCnt = 05; break;
534 case '1': SectorsCnt = 16; break;
535 case '2': SectorsCnt = 32; break;
536 case '4': SectorsCnt = 64; break;
537 default: SectorsCnt = 16;
538 }
539 }
540
541 ctmp = param_getchar(Cmd, 4);
542 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;
543 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;
544
545 ctmp = param_getchar(Cmd, 6);
546 transferToEml |= (ctmp == 't' || ctmp == 'T');
547 transferToEml |= (ctmp == 'd' || ctmp == 'D');
548
549 PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo, keyType, sprint_hex(key, 6), transferToEml);
550 if (cmdp == 'o')
551 PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo, trgKeyType);
552
553 if (cmdp == 'o') {
554 if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock)) {
555 PrintAndLog("Nested error.");
556 return 2;
557 }
558
559 for (i = 0; i < 16; i++) {
560 PrintAndLog("cnt=%d key= %s", i, sprint_hex(keyBlock + i * 6, 6));
561 }
562
563 // test keys
564 res = mfCheckKeys(trgBlockNo, trgKeyType, 8, keyBlock, &key64);
565 if (res)
566 res = mfCheckKeys(trgBlockNo, trgKeyType, 8, &keyBlock[6 * 8], &key64);
567 if (!res) {
568 PrintAndLog("Found valid key:%012llx", key64);
569
570 // transfer key to the emulator
571 if (transferToEml) {
572 mfEmlGetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1);
573
574 if (!trgKeyType)
575 num_to_bytes(key64, 6, keyBlock);
576 else
577 num_to_bytes(key64, 6, &keyBlock[10]);
578 mfEmlSetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1);
579 }
580 } else {
581 PrintAndLog("No valid key found");
582 }
583 }
584 else { // ------------------------------------ multiple sectors working
585 blDiff = blockNo % 4;
586 PrintAndLog("Block shift=%d", blDiff);
587 e_sector = calloc(SectorsCnt, sizeof(sector));
588 if (e_sector == NULL) return 1;
589
590 //test current key 4 sectors
591 memcpy(keyBlock, key, 6);
592 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 1 * 6));
593 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 2 * 6));
594 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 3 * 6));
595 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 4 * 6));
596 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));
597
598 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);
599 for (i = 0; i < SectorsCnt; i++) {
600 for (j = 0; j < 2; j++) {
601 if (e_sector[i].foundKey[j]) continue;
602
603 res = mfCheckKeys(i * 4 + blDiff, j, 6, keyBlock, &key64);
604
605 if (!res) {
606 e_sector[i].Key[j] = key64;
607 e_sector[i].foundKey[j] = 1;
608 }
609 }
610 }
611
612 // nested sectors
613 iterations = 0;
614 PrintAndLog("nested...");
615 for (i = 0; i < NESTED_SECTOR_RETRY; i++) {
616 for (trgBlockNo = blDiff; trgBlockNo < SectorsCnt * 4; trgBlockNo = trgBlockNo + 4)
617 for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) {
618 if (e_sector[trgBlockNo / 4].foundKey[trgKeyType]) continue;
619 if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock)) continue;
620
621 iterations++;
622
623 //try keys from nested
624 res = mfCheckKeys(trgBlockNo, trgKeyType, 8, keyBlock, &key64);
625 if (res)
626 res = mfCheckKeys(trgBlockNo, trgKeyType, 8, &keyBlock[6 * 8], &key64);
627 if (!res) {
628 PrintAndLog("Found valid key:%012llx", key64);
629 e_sector[trgBlockNo / 4].foundKey[trgKeyType] = 1;
630 e_sector[trgBlockNo / 4].Key[trgKeyType] = key64;
631 }
632 }
633 }
634
635 PrintAndLog("Iterations count: %d", iterations);
636 //print them
637 PrintAndLog("|---|----------------|---|----------------|---|");
638 PrintAndLog("|sec|key A |res|key B |res|");
639 PrintAndLog("|---|----------------|---|----------------|---|");
640 for (i = 0; i < SectorsCnt; i++) {
641 PrintAndLog("|%03d| %012llx | %d | %012llx | %d |", i,
642 e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);
643 }
644 PrintAndLog("|---|----------------|---|----------------|---|");
645
646 // transfer them to the emulator
647 if (transferToEml) {
648 for (i = 0; i < SectorsCnt; i++) {
649 mfEmlGetMem(keyBlock, i * 4 + 3, 1);
650 if (e_sector[i].foundKey[0])
651 num_to_bytes(e_sector[i].Key[0], 6, keyBlock);
652 if (e_sector[i].foundKey[1])
653 num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);
654 mfEmlSetMem(keyBlock, i * 4 + 3, 1);
655 }
656 }
657
658 // Create dump file
659 if (createDumpFile) {
660 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
661 PrintAndLog("Could not create file keys.bin");
662 free(e_sector);
663 return 1;
664 }
665 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
666 for(i=0; i<16; i++) {
667 if (e_sector[i].foundKey[0]){
668 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
669 fwrite ( tempkey, 1, 6, fkeys );
670 }
671 else{
672 fwrite ( &standart, 1, 6, fkeys );
673 }
674 }
675 for(i=0; i<16; i++) {
676 if (e_sector[i].foundKey[1]){
677 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
678 fwrite ( tempkey, 1, 6, fkeys );
679 }
680 else{
681 fwrite ( &standart, 1, 6, fkeys );
682 }
683 }
684 fclose(fkeys);
685 }
686
687 free(e_sector);
688 }
689
690 return 0;
691 }
692
693 int CmdHF14AMfChk(const char *Cmd)
694 {
695 int i, res;
696 int keycnt = 0;
697 char ctmp = 0x00;
698 uint8_t blockNo = 0;
699 uint8_t keyType = 0;
700 uint8_t keyBlock[8 * 6];
701 uint64_t key64 = 0;
702
703 memset(keyBlock, 0x00, sizeof(keyBlock));
704
705 if (strlen(Cmd)<3) {
706 PrintAndLog("Usage: hf mf chk <block number> <key A/B> [<key (12 hex symbols)>]");
707 PrintAndLog(" sample: hf mf chk 0 A FFFFFFFFFFFF a0a1a2a3a4a5 b0b1b2b3b4b5 ");
708 return 0;
709 }
710
711 blockNo = param_get8(Cmd, 0);
712 ctmp = param_getchar(Cmd, 1);
713 if (ctmp == 0x00) {
714 PrintAndLog("Key type must be A or B");
715 return 1;
716 }
717 if (ctmp != 'A' && ctmp != 'a') keyType = 1;
718
719 for (i = 0; i < 6; i++) {
720 if (!isxdigit(param_getchar(Cmd, 2 + i))) break;
721
722 if (param_gethex(Cmd, 2 + i, keyBlock + 6 * i, 12)) {
723 PrintAndLog("Key[%d] must include 12 HEX symbols", i);
724 return 1;
725 }
726 keycnt = i + 1;
727 }
728
729 if (keycnt == 0) {
730 PrintAndLog("There is must be at least one key");
731 return 1;
732 }
733
734 PrintAndLog("--block no:%02x key type:%02x key count:%d ", blockNo, keyType, keycnt);
735
736 res = mfCheckKeys(blockNo, keyType, keycnt, keyBlock, &key64);
737 if (res !=1) {
738 if (!res)
739 PrintAndLog("isOk:%02x valid key:%012llx", 1, key64);
740 else
741 PrintAndLog("isOk:%02x", 0);
742 } else {
743 PrintAndLog("Command execute timeout");
744 }
745
746 return 0;
747 }
748
749 int CmdHF14AMf1kSim(const char *Cmd)
750 {
751 uint8_t uid[4] = {0, 0, 0, 0};
752
753 if (param_getchar(Cmd, 0) == 'h') {
754 PrintAndLog("Usage: hf mf sim <uid (8 hex symbols)>");
755 PrintAndLog(" sample: hf mf sim 0a0a0a0a ");
756 return 0;
757 }
758
759 if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {
760 PrintAndLog("UID must include 8 HEX symbols");
761 return 1;
762 }
763 PrintAndLog(" uid:%s ", sprint_hex(uid, 4));
764
765 UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {0, 0, 0}};
766 memcpy(c.d.asBytes, uid, 4);
767 SendCommand(&c);
768
769 return 0;
770 }
771
772 int CmdHF14AMfDbg(const char *Cmd)
773 {
774 int dbgMode = param_get32ex(Cmd, 0, 0, 10);
775 if (dbgMode > 4) {
776 PrintAndLog("Max debud mode parameter is 4 \n");
777 }
778
779 if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {
780 PrintAndLog("Usage: hf mf dbg <debug level>");
781 PrintAndLog(" 0 - no debug messages");
782 PrintAndLog(" 1 - error messages");
783 PrintAndLog(" 2 - all messages");
784 PrintAndLog(" 4 - extended debug mode");
785 return 0;
786 }
787
788 UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};
789 SendCommand(&c);
790
791 return 0;
792 }
793
794 int CmdHF14AMfEGet(const char *Cmd)
795 {
796 uint8_t blockNo = 0;
797 uint8_t data[3 * 16];
798 int i;
799
800 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
801 PrintAndLog("Usage: hf mf eget <block number>");
802 PrintAndLog(" sample: hf mf eget 0 ");
803 return 0;
804 }
805
806 blockNo = param_get8(Cmd, 0);
807 if (blockNo >= 16 * 4) {
808 PrintAndLog("Block number must be in [0..63] as in MIFARE classic.");
809 return 1;
810 }
811
812 PrintAndLog(" ");
813 if (!mfEmlGetMem(data, blockNo, 3)) {
814 for (i = 0; i < 3; i++) {
815 PrintAndLog("data[%d]:%s", blockNo + i, sprint_hex(data + i * 16, 16));
816 }
817 } else {
818 PrintAndLog("Command execute timeout");
819 }
820
821 return 0;
822 }
823
824 int CmdHF14AMfEClear(const char *Cmd)
825 {
826 if (param_getchar(Cmd, 0) == 'h') {
827 PrintAndLog("Usage: hf mf eclr");
828 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
829 return 0;
830 }
831
832 UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};
833 SendCommand(&c);
834 return 0;
835 }
836
837 int CmdHF14AMfESet(const char *Cmd)
838 {
839 uint8_t memBlock[16];
840 uint8_t blockNo = 0;
841
842 memset(memBlock, 0x00, sizeof(memBlock));
843
844 if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {
845 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
846 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
847 return 0;
848 }
849
850 blockNo = param_get8(Cmd, 0);
851 if (blockNo >= 16 * 4) {
852 PrintAndLog("Block number must be in [0..63] as in MIFARE classic.");
853 return 1;
854 }
855
856 if (param_gethex(Cmd, 1, memBlock, 32)) {
857 PrintAndLog("block data must include 32 HEX symbols");
858 return 1;
859 }
860
861 // 1 - blocks count
862 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};
863 memcpy(c.d.asBytes, memBlock, 16);
864 SendCommand(&c);
865 return 0;
866 }
867
868 int CmdHF14AMfELoad(const char *Cmd)
869 {
870 FILE * f;
871 char filename[20];
872 char * fnameptr = filename;
873 char buf[64];
874 uint8_t buf8[64];
875 int i, len, blockNum;
876
877 memset(filename, 0, sizeof(filename));
878 memset(buf, 0, sizeof(buf));
879
880 if (param_getchar(Cmd, 0) == 'h') {
881 PrintAndLog("It loads emul dump from the file `filename.eml`");
882 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");
883 PrintAndLog(" sample: hf mf eload filename");
884 return 0;
885 }
886
887 len = strlen(Cmd);
888 if (len > 14) len = 14;
889
890 if (len < 1) {
891 }
892
893 memcpy(filename, Cmd, len);
894 fnameptr += len;
895
896 sprintf(fnameptr, ".eml");
897
898 // open file
899 f = fopen(filename, "r");
900 if (f == NULL) {
901 PrintAndLog("File not found or locked.");
902 return 1;
903 }
904
905 blockNum = 0;
906 while(!feof(f)){
907 memset(buf, 0, sizeof(buf));
908 fgets(buf, sizeof(buf), f);
909 if (strlen(buf) < 32){
910 PrintAndLog("File content error. Block data must include 32 HEX symbols");
911 return 2;
912 }
913 for (i = 0; i < 32; i += 2)
914 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
915 // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));
916
917 if (mfEmlSetMem(buf8, blockNum, 1)) {
918 PrintAndLog("Cant set emul block: %d", blockNum);
919 return 3;
920 }
921 blockNum++;
922
923 if (blockNum >= 16 * 4) break;
924 }
925 fclose(f);
926
927 if (blockNum != 16 * 4){
928 PrintAndLog("File content error. There must be 64 blocks");
929 return 4;
930 }
931 PrintAndLog("Loaded from file: %s", filename);
932 return 0;
933 }
934
935 int CmdHF14AMfESave(const char *Cmd)
936 {
937 FILE * f;
938 char filename[20];
939 char * fnameptr = filename;
940 uint8_t buf[64];
941 int i, j, len;
942
943 memset(filename, 0, sizeof(filename));
944 memset(buf, 0, sizeof(buf));
945
946 if (param_getchar(Cmd, 0) == 'h') {
947 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
948 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");
949 PrintAndLog(" sample: hf mf esave ");
950 PrintAndLog(" hf mf esave filename");
951 return 0;
952 }
953
954 len = strlen(Cmd);
955 if (len > 14) len = 14;
956
957 if (len < 1) {
958 // get filename
959 if (mfEmlGetMem(buf, 0, 1)) {
960 PrintAndLog("Cant get block: %d", 0);
961 return 1;
962 }
963 for (j = 0; j < 7; j++, fnameptr += 2)
964 sprintf(fnameptr, "%02x", buf[j]);
965 } else {
966 memcpy(filename, Cmd, len);
967 fnameptr += len;
968 }
969
970 sprintf(fnameptr, ".eml");
971
972 // open file
973 f = fopen(filename, "w+");
974
975 // put hex
976 for (i = 0; i < 16 * 4; i++) {
977 if (mfEmlGetMem(buf, i, 1)) {
978 PrintAndLog("Cant get block: %d", i);
979 break;
980 }
981 for (j = 0; j < 16; j++)
982 fprintf(f, "%02x", buf[j]);
983 fprintf(f,"\n");
984 }
985 fclose(f);
986
987 PrintAndLog("Saved to file: %s", filename);
988
989 return 0;
990 }
991
992 int CmdHF14AMfECFill(const char *Cmd)
993 {
994 uint8_t keyType = 0;
995
996 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
997 PrintAndLog("Usage: hf mf efill <key A/B>");
998 PrintAndLog("sample: hf mf efill A");
999 PrintAndLog("Card data blocks transfers to card emulator memory.");
1000 PrintAndLog("Keys must be laid in the simulator memory. \n");
1001 return 0;
1002 }
1003
1004 char ctmp = param_getchar(Cmd, 0);
1005 if (ctmp == 0x00) {
1006 PrintAndLog("Key type must be A or B");
1007 return 1;
1008 }
1009 if (ctmp != 'A' && ctmp != 'a') keyType = 1;
1010
1011 UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {0, keyType, 0}};
1012 SendCommand(&c);
1013 return 0;
1014 }
1015
1016 int CmdHF14AMfEKeyPrn(const char *Cmd)
1017 {
1018 int i;
1019 uint8_t data[16];
1020 uint64_t keyA, keyB;
1021
1022 PrintAndLog("|---|----------------|----------------|");
1023 PrintAndLog("|sec|key A |key B |");
1024 PrintAndLog("|---|----------------|----------------|");
1025 for (i = 0; i < 16; i++) {
1026 if (mfEmlGetMem(data, i * 4 + 3, 1)) {
1027 PrintAndLog("error get block %d", i * 4 + 3);
1028 break;
1029 }
1030 keyA = bytes_to_num(data, 6);
1031 keyB = bytes_to_num(data + 10, 6);
1032 PrintAndLog("|%03d| %012llx | %012llx |", i, keyA, keyB);
1033 }
1034 PrintAndLog("|---|----------------|----------------|");
1035
1036 return 0;
1037 }
1038
1039 static command_t CommandTable[] =
1040 {
1041 {"help", CmdHelp, 1, "This help"},
1042 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},
1043 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},
1044 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},
1045 {"dump1k", CmdHF14AMfDump1k, 0, "Dump MIFARE classic tag to binary file"},
1046 {"restore1k", CmdHF14AMfRestore1k, 0, "Restore MIFARE classic binary file to BLANK tag"},
1047 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},
1048 {"chk", CmdHF14AMfChk, 0, "Test block up to 8 keys"},
1049 {"mifare", CmdHF14AMifare, 0, "Read parity error messages. param - <used card nonce>"},
1050 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},
1051 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE 1k card"},
1052 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},
1053 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},
1054 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},
1055 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},
1056 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},
1057 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},
1058 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},
1059 {NULL, NULL, 0, NULL}
1060 };
1061
1062 int CmdHFMF(const char *Cmd)
1063 {
1064 // flush
1065 while (WaitForResponseTimeout(CMD_ACK, 500) != NULL) ;
1066
1067 CmdsParse(CommandTable, Cmd);
1068 return 0;
1069 }
1070
1071 int CmdHelp(const char *Cmd)
1072 {
1073 CmdsHelp(CommandTable);
1074 return 0;
1075 }
Impressum, Datenschutz