]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
(no commit message)
[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 keyType = 0;
263 uint8_t c[3][4];
264 uint8_t keyA[16][6];
265 uint8_t keyB[16][6];
266 uint8_t rights[16][4];
267
268 uint8_t isOK = 0;
269 uint8_t *data = NULL;
270
271 FILE *fin;
272 FILE *fout;
273
274 UsbCommand *resp;
275
276 if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {
277 PrintAndLog("Could not find file keys.bin");
278 return 1;
279 }
280
281 if ((fout = fopen("dumpdata.bin","wb")) == NULL) {
282 PrintAndLog("Could not create file name dump.bin");
283 return 1;
284 }
285
286 // Read key file
287
288 for (i=0 ; i<16 ; i++) {
289 fread ( keyA[i], 1, 6, fin );
290 }
291 for (i=0 ; i<16 ; i++) {
292 fread ( keyB[i], 1, 6, fin );
293 }
294
295 // Read access rights to sectors
296
297 PrintAndLog("|-----------------------------------------|");
298 PrintAndLog("|------ Reading sector access bits...-----|");
299 PrintAndLog("|-----------------------------------------|");
300
301 for (i = 0 ; i < 16 ; i++) {
302 UsbCommand c = {CMD_MIFARE_READBL, {4*i + 3, 0, 0}};
303 memcpy(c.d.asBytes, keyA[i], 6);
304 SendCommand(&c);
305 resp = WaitForResponseTimeout(CMD_ACK, 1500);
306
307 if (resp != NULL) {
308 uint8_t isOK = resp->arg[0] & 0xff;
309 uint8_t *data = resp->d.asBytes;
310 if (isOK){
311 rights[i][0] = ((data[7] & 0x10)>>4) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>2);
312 rights[i][1] = ((data[7] & 0x20)>>5) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>3);
313 rights[i][2] = ((data[7] & 0x40)>>6) | ((data[8] & 0x4)>>1) | ((data[8] & 0x40)>>4);
314 rights[i][3] = ((data[7] & 0x80)>>7) | ((data[8] & 0x8)>>2) | ((data[8] & 0x80)>>5);
315 }
316 else{
317 PrintAndLog("Could not get access rights for block %d", i);
318 }
319 }
320 else {
321 PrintAndLog("Command execute timeout");
322 }
323 }
324
325 // Read blocks and print to file
326
327 PrintAndLog("|-----------------------------------------|");
328 PrintAndLog("|----- Dumping all blocks to file... -----|");
329 PrintAndLog("|-----------------------------------------|");
330
331 for (i=0 ; i<16 ; i++) {
332 for (j=0 ; j<4 ; j++) {
333
334 if (j == 3){
335 UsbCommand c = {CMD_MIFARE_READBL, {i*4 + j, 0, 0}};
336 memcpy(c.d.asBytes, keyA[i], 6);
337 SendCommand(&c);
338 resp = WaitForResponseTimeout(CMD_ACK, 1500);
339 }
340 else{
341 if ((rights[i][j] == 6) | (rights[i][j] == 5)) {
342 UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 1, 0}};
343 memcpy(c.d.asBytes, keyB[i], 6);
344 SendCommand(&c);
345 resp = WaitForResponseTimeout(CMD_ACK, 1500);
346 }
347 else if (rights[i][j] == 7) {
348 PrintAndLog("Access rights do not allow reading of sector %d block %d",i,j);
349 }
350 else {
351 UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 0, 0}};
352 memcpy(c.d.asBytes, keyA[i], 6);
353 SendCommand(&c);
354 resp = WaitForResponseTimeout(CMD_ACK, 1500);
355 }
356 }
357
358 if (resp != NULL) {
359 uint8_t isOK = resp->arg[0] & 0xff;
360 uint8_t *data = resp->d.asBytes;
361 if (isOK) {
362 fwrite ( data, 1, 16, fout );
363 }
364 else {
365 PrintAndLog("Could not get access rights for block %d", i);
366 }
367 }
368 else {
369 PrintAndLog("Command execute timeout");
370 }
371 }
372 }
373
374 fclose(fin);
375 fclose(fout);
376
377 return 0;
378 }
379
380 int CmdHF14AMfRestore1k(const char *Cmd)
381 {
382
383 int i,j;
384 uint8_t blockNo = 0;
385 uint8_t keyType = 0;
386 uint8_t key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
387 uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
388 uint8_t keyA[16][6];
389 uint8_t keyB[16][6];
390
391 char cmdp = 0x00;
392
393 FILE *fdump;
394 FILE *fkeys;
395
396 FILE *fdebug = fopen("debug.bin","wb");
397
398 if ((fdump = fopen("dumpdata.bin","rb")) == NULL) {
399 PrintAndLog("Could not find file dump.bin");
400 return 1;
401 }
402 if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) {
403 PrintAndLog("Could not find file keys.bin");
404 return 1;
405 }
406
407 for (i=0 ; i<16 ; i++) {
408 fread(keyA[i], 1, 6, fkeys);
409 }
410 for (i=0 ; i<16 ; i++) {
411 fread(keyB[i], 1, 6, fkeys);
412 }
413
414 PrintAndLog("going...");
415
416 for (i=0 ; i<16 ; i++) {
417 for( j=0 ; j<4 ; j++) {
418 UsbCommand c = {CMD_MIFARE_WRITEBL, {i*4 + j, keyType, 0}};
419 memcpy(c.d.asBytes, key, 6);
420
421 fread(bldata, 1, 16, fdump);
422
423 if (j == 3) {
424 bldata[0] = (keyA[i][0]);
425 bldata[1] = (keyA[i][1]);
426 bldata[2] = (keyA[i][2]);
427 bldata[3] = (keyA[i][3]);
428 bldata[4] = (keyA[i][4]);
429 bldata[5] = (keyA[i][5]);
430 bldata[10] = (keyB[i][0]);
431 bldata[11] = (keyB[i][1]);
432 bldata[12] = (keyB[i][2]);
433 bldata[13] = (keyB[i][3]);
434 bldata[14] = (keyB[i][4]);
435 bldata[15] = (keyB[i][5]);
436 }
437
438 PrintAndLog("writing to block %2d: %s confirm?", i*4+j, sprint_hex(bldata, 16));
439
440 memcpy(c.d.asBytes + 10, bldata, 16);
441 SendCommand(&c);
442 UsbCommand *resp = WaitForResponseTimeout(CMD_ACK, 1500);
443
444 if (resp != NULL) {
445 uint8_t isOK = resp->arg[0] & 0xff;
446 PrintAndLog("isOk:%02x", isOK);
447 } else {
448 PrintAndLog("Command execute timeout");
449 }
450 }
451 }
452
453 fclose(fdump);
454 fclose(fkeys);
455 return 0;
456 }
457
458 int CmdHF14AMfNested(const char *Cmd)
459 {
460 int i, j, res, iterations;
461 sector * e_sector = NULL;
462 uint8_t blockNo = 0;
463 uint8_t keyType = 0;
464 uint8_t trgBlockNo = 0;
465 uint8_t trgKeyType = 0;
466 uint8_t blDiff = 0;
467 int SectorsCnt = 0;
468 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
469 uint8_t keyBlock[16 * 6];
470 uint64_t key64 = 0;
471 int transferToEml = 0;
472
473 int createDumpFile = 0;
474 FILE *fkeys;
475
476 char cmdp, ctmp;
477
478 if (strlen(Cmd)<3) {
479 PrintAndLog("Usage:");
480 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
481 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
482 PrintAndLog(" <target block number> <target key A/B> [t]");
483 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
484 PrintAndLog("t - transfer keys into emulator memory");
485 PrintAndLog("d - write keys to binary file");
486 PrintAndLog(" ");
487 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
488 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");
489 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");
490 PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
491 return 0;
492 }
493
494 cmdp = param_getchar(Cmd, 0);
495 blockNo = param_get8(Cmd, 1);
496 ctmp = param_getchar(Cmd, 2);
497 if (ctmp == 0x00) {
498 PrintAndLog("Key type must be A or B");
499 return 1;
500 }
501 if (ctmp != 'A' && ctmp != 'a') keyType = 1;
502 if (param_gethex(Cmd, 3, key, 12)) {
503 PrintAndLog("Key must include 12 HEX symbols");
504 return 1;
505 }
506
507 if (cmdp == 'o' || cmdp == 'O') {
508 cmdp = 'o';
509 trgBlockNo = param_get8(Cmd, 4);
510 ctmp = param_getchar(Cmd, 5);
511 if (ctmp == 0x00) {
512 PrintAndLog("Target key type must be A or B");
513 return 1;
514 }
515 if (ctmp != 'A' && ctmp != 'a') trgKeyType = 1;
516 } else {
517 switch (cmdp) {
518 case '0': SectorsCnt = 05; break;
519 case '1': SectorsCnt = 16; break;
520 case '2': SectorsCnt = 32; break;
521 case '4': SectorsCnt = 64; break;
522 default: SectorsCnt = 16;
523 }
524 }
525
526 ctmp = param_getchar(Cmd, 4);
527 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;
528 ctmp = param_getchar(Cmd, 6);
529 transferToEml |= (ctmp == 't' || ctmp == 'T');
530 createDumpFile |= (ctmp == 'd' || ctmp == 'D');
531
532 PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo, keyType, sprint_hex(key, 6), transferToEml);
533 if (cmdp == 'o')
534 PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo, trgKeyType);
535
536 if (cmdp == 'o') {
537 if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock)) {
538 PrintAndLog("Nested error.");
539 return 2;
540 }
541
542 for (i = 0; i < 16; i++) {
543 PrintAndLog("cnt=%d key= %s", i, sprint_hex(keyBlock + i * 6, 6));
544 }
545
546 // test keys
547 res = mfCheckKeys(trgBlockNo, trgKeyType, 8, keyBlock, &key64);
548 if (res)
549 res = mfCheckKeys(trgBlockNo, trgKeyType, 8, &keyBlock[6 * 8], &key64);
550 if (!res) {
551 PrintAndLog("Found valid key:%012llx", key64);
552
553 // transfer key to the emulator
554 if (transferToEml) {
555 mfEmlGetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1);
556
557 if (!trgKeyType)
558 num_to_bytes(key64, 6, keyBlock);
559 else
560 num_to_bytes(key64, 6, &keyBlock[10]);
561 mfEmlSetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1);
562 }
563 } else {
564 PrintAndLog("No valid key found");
565 }
566 } else // ------------------------------------ multiple sectors working
567 {
568 blDiff = blockNo % 4;
569 PrintAndLog("Block shift=%d", blDiff);
570 e_sector = calloc(SectorsCnt, sizeof(sector));
571 if (e_sector == NULL) return 1;
572
573 //test current key 4 sectors
574 memcpy(keyBlock, key, 6);
575 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 1 * 6));
576 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 2 * 6));
577 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 3 * 6));
578 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 4 * 6));
579 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));
580
581 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);
582 for (i = 0; i < SectorsCnt; i++) {
583 for (j = 0; j < 2; j++) {
584 if (e_sector[i].foundKey[j]) continue;
585
586 res = mfCheckKeys(i * 4 + blDiff, j, 6, keyBlock, &key64);
587
588 if (!res) {
589 e_sector[i].Key[j] = key64;
590 e_sector[i].foundKey[j] = 1;
591 }
592 }
593 }
594
595
596 // nested sectors
597 iterations = 0;
598 PrintAndLog("nested...");
599 for (i = 0; i < NESTED_SECTOR_RETRY; i++) {
600 for (trgBlockNo = blDiff; trgBlockNo < SectorsCnt * 4; trgBlockNo = trgBlockNo + 4)
601 for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) {
602 if (e_sector[trgBlockNo / 4].foundKey[trgKeyType]) continue;
603 if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock)) continue;
604
605 iterations++;
606
607 //try keys from nested
608 res = mfCheckKeys(trgBlockNo, trgKeyType, 8, keyBlock, &key64);
609 if (res)
610 res = mfCheckKeys(trgBlockNo, trgKeyType, 8, &keyBlock[6 * 8], &key64);
611 if (!res) {
612 PrintAndLog("Found valid key:%012llx", key64);
613 e_sector[trgBlockNo / 4].foundKey[trgKeyType] = 1;
614 e_sector[trgBlockNo / 4].Key[trgKeyType] = key64;
615 }
616 }
617 }
618
619 PrintAndLog("Iterations count: %d", iterations);
620 //print them
621 PrintAndLog("|---|----------------|---|----------------|---|");
622 PrintAndLog("|sec|key A |res|key B |res|");
623 PrintAndLog("|---|----------------|---|----------------|---|");
624 for (i = 0; i < SectorsCnt; i++) {
625 PrintAndLog("|%03d| %012llx | %d | %012llx | %d |", i,
626 e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);
627 }
628 PrintAndLog("|---|----------------|---|----------------|---|");
629
630 // transfer them to the emulator
631 if (transferToEml) {
632 for (i = 0; i < SectorsCnt; i++) {
633 mfEmlGetMem(keyBlock, i * 4 + 3, 1);
634 if (e_sector[i].foundKey[0])
635 num_to_bytes(e_sector[i].Key[0], 6, keyBlock);
636 if (e_sector[i].foundKey[1])
637 num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);
638 mfEmlSetMem(keyBlock, i * 4 + 3, 1);
639 }
640 }
641
642 if (createDumpFile) {
643 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
644 rintAndLog("Could not create file keys.bin");
645 free(e_sector);
646 return 1;
647 }
648 for(i=0; i<16; i++) {
649 fwrite ( e_sector[i].Key[0], sizeof(e_sector[i].Key[0]), 1, fkeys );
650 }
651 for(i=0; i<16; i++) {
652 fwrite ( e_sector[i].Key[1], sizeof(e_sector[i].Key[1]), 1, fkeys );
653 }
654 fclose(fkeys);
655 }
656
657 free(e_sector);
658 }
659
660 return 0;
661 }
662
663 int CmdHF14AMfChk(const char *Cmd)
664 {
665 int i, res;
666 int keycnt = 0;
667 char ctmp = 0x00;
668 uint8_t blockNo = 0;
669 uint8_t keyType = 0;
670 uint8_t keyBlock[8 * 6];
671 uint64_t key64 = 0;
672
673 memset(keyBlock, 0x00, sizeof(keyBlock));
674
675 if (strlen(Cmd)<3) {
676 PrintAndLog("Usage: hf mf chk <block number> <key A/B> [<key (12 hex symbols)>]");
677 PrintAndLog(" sample: hf mf chk 0 A FFFFFFFFFFFF a0a1a2a3a4a5 b0b1b2b3b4b5 ");
678 return 0;
679 }
680
681 blockNo = param_get8(Cmd, 0);
682 ctmp = param_getchar(Cmd, 1);
683 if (ctmp == 0x00) {
684 PrintAndLog("Key type must be A or B");
685 return 1;
686 }
687 if (ctmp != 'A' && ctmp != 'a') keyType = 1;
688
689 for (i = 0; i < 6; i++) {
690 if (!isxdigit(param_getchar(Cmd, 2 + i))) break;
691
692 if (param_gethex(Cmd, 2 + i, keyBlock + 6 * i, 12)) {
693 PrintAndLog("Key[%d] must include 12 HEX symbols", i);
694 return 1;
695 }
696 keycnt = i + 1;
697 }
698
699 if (keycnt == 0) {
700 PrintAndLog("There is must be at least one key");
701 return 1;
702 }
703
704 PrintAndLog("--block no:%02x key type:%02x key count:%d ", blockNo, keyType, keycnt);
705
706 res = mfCheckKeys(blockNo, keyType, keycnt, keyBlock, &key64);
707 if (res !=1) {
708 if (!res)
709 PrintAndLog("isOk:%02x valid key:%012llx", 1, key64);
710 else
711 PrintAndLog("isOk:%02x", 0);
712 } else {
713 PrintAndLog("Command execute timeout");
714 }
715
716 return 0;
717 }
718
719 int CmdHF14AMf1kSim(const char *Cmd)
720 {
721 uint8_t uid[4] = {0, 0, 0, 0};
722
723 if (param_getchar(Cmd, 0) == 'h') {
724 PrintAndLog("Usage: hf mf sim <uid (8 hex symbols)>");
725 PrintAndLog(" sample: hf mf sim 0a0a0a0a ");
726 return 0;
727 }
728
729 if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {
730 PrintAndLog("UID must include 8 HEX symbols");
731 return 1;
732 }
733 PrintAndLog(" uid:%s ", sprint_hex(uid, 4));
734
735 UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {0, 0, 0}};
736 memcpy(c.d.asBytes, uid, 4);
737 SendCommand(&c);
738
739 return 0;
740 }
741
742 int CmdHF14AMfDbg(const char *Cmd)
743 {
744 int dbgMode = param_get32ex(Cmd, 0, 0, 10);
745 if (dbgMode > 4) {
746 PrintAndLog("Max debud mode parameter is 4 \n");
747 }
748
749 if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {
750 PrintAndLog("Usage: hf mf dbg <debug level>");
751 PrintAndLog(" 0 - no debug messages");
752 PrintAndLog(" 1 - error messages");
753 PrintAndLog(" 2 - all messages");
754 PrintAndLog(" 4 - extended debug mode");
755 return 0;
756 }
757
758 UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};
759 SendCommand(&c);
760
761 return 0;
762 }
763
764 int CmdHF14AMfEGet(const char *Cmd)
765 {
766 uint8_t blockNo = 0;
767 uint8_t data[3 * 16];
768 int i;
769
770 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
771 PrintAndLog("Usage: hf mf eget <block number>");
772 PrintAndLog(" sample: hf mf eget 0 ");
773 return 0;
774 }
775
776 blockNo = param_get8(Cmd, 0);
777 if (blockNo >= 16 * 4) {
778 PrintAndLog("Block number must be in [0..63] as in MIFARE classic.");
779 return 1;
780 }
781
782 PrintAndLog(" ");
783 if (!mfEmlGetMem(data, blockNo, 3)) {
784 for (i = 0; i < 3; i++) {
785 PrintAndLog("data[%d]:%s", blockNo + i, sprint_hex(data + i * 16, 16));
786 }
787 } else {
788 PrintAndLog("Command execute timeout");
789 }
790
791 return 0;
792 }
793
794 int CmdHF14AMfEClear(const char *Cmd)
795 {
796 if (param_getchar(Cmd, 0) == 'h') {
797 PrintAndLog("Usage: hf mf eclr");
798 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
799 return 0;
800 }
801
802 UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};
803 SendCommand(&c);
804 return 0;
805 }
806
807 int CmdHF14AMfESet(const char *Cmd)
808 {
809 uint8_t memBlock[16];
810 uint8_t blockNo = 0;
811
812 memset(memBlock, 0x00, sizeof(memBlock));
813
814 if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {
815 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
816 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
817 return 0;
818 }
819
820 blockNo = param_get8(Cmd, 0);
821 if (blockNo >= 16 * 4) {
822 PrintAndLog("Block number must be in [0..63] as in MIFARE classic.");
823 return 1;
824 }
825
826 if (param_gethex(Cmd, 1, memBlock, 32)) {
827 PrintAndLog("block data must include 32 HEX symbols");
828 return 1;
829 }
830
831 // 1 - blocks count
832 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};
833 memcpy(c.d.asBytes, memBlock, 16);
834 SendCommand(&c);
835 return 0;
836 }
837
838 int CmdHF14AMfELoad(const char *Cmd)
839 {
840 FILE * f;
841 char filename[20];
842 char * fnameptr = filename;
843 char buf[64];
844 uint8_t buf8[64];
845 int i, len, blockNum;
846
847 memset(filename, 0, sizeof(filename));
848 memset(buf, 0, sizeof(buf));
849
850 if (param_getchar(Cmd, 0) == 'h') {
851 PrintAndLog("It loads emul dump from the file `filename.eml`");
852 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");
853 PrintAndLog(" sample: hf mf eload filename");
854 return 0;
855 }
856
857 len = strlen(Cmd);
858 if (len > 14) len = 14;
859
860 if (len < 1) {
861 }
862
863 memcpy(filename, Cmd, len);
864 fnameptr += len;
865
866 sprintf(fnameptr, ".eml");
867
868 // open file
869 f = fopen(filename, "r");
870 if (f == NULL) {
871 PrintAndLog("File not found or locked.");
872 return 1;
873 }
874
875 blockNum = 0;
876 while(!feof(f)){
877 memset(buf, 0, sizeof(buf));
878 fgets(buf, sizeof(buf), f);
879 if (strlen(buf) < 32){
880 PrintAndLog("File content error. Block data must include 32 HEX symbols");
881 return 2;
882 }
883 for (i = 0; i < 32; i += 2)
884 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
885 // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));
886
887 if (mfEmlSetMem(buf8, blockNum, 1)) {
888 PrintAndLog("Cant set emul block: %d", blockNum);
889 return 3;
890 }
891 blockNum++;
892
893 if (blockNum >= 16 * 4) break;
894 }
895 fclose(f);
896
897 if (blockNum != 16 * 4){
898 PrintAndLog("File content error. There must be 64 blocks");
899 return 4;
900 }
901 PrintAndLog("Loaded from file: %s", filename);
902 return 0;
903 }
904
905 int CmdHF14AMfESave(const char *Cmd)
906 {
907 FILE * f;
908 char filename[20];
909 char * fnameptr = filename;
910 uint8_t buf[64];
911 int i, j, len;
912
913 memset(filename, 0, sizeof(filename));
914 memset(buf, 0, sizeof(buf));
915
916 if (param_getchar(Cmd, 0) == 'h') {
917 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
918 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");
919 PrintAndLog(" sample: hf mf esave ");
920 PrintAndLog(" hf mf esave filename");
921 return 0;
922 }
923
924 len = strlen(Cmd);
925 if (len > 14) len = 14;
926
927 if (len < 1) {
928 // get filename
929 if (mfEmlGetMem(buf, 0, 1)) {
930 PrintAndLog("Cant get block: %d", 0);
931 return 1;
932 }
933 for (j = 0; j < 7; j++, fnameptr += 2)
934 sprintf(fnameptr, "%02x", buf[j]);
935 } else {
936 memcpy(filename, Cmd, len);
937 fnameptr += len;
938 }
939
940 sprintf(fnameptr, ".eml");
941
942 // open file
943 f = fopen(filename, "w+");
944
945 // put hex
946 for (i = 0; i < 16 * 4; i++) {
947 if (mfEmlGetMem(buf, i, 1)) {
948 PrintAndLog("Cant get block: %d", i);
949 break;
950 }
951 for (j = 0; j < 16; j++)
952 fprintf(f, "%02x", buf[j]);
953 fprintf(f,"\n");
954 }
955 fclose(f);
956
957 PrintAndLog("Saved to file: %s", filename);
958
959 return 0;
960 }
961
962 int CmdHF14AMfECFill(const char *Cmd)
963 {
964 uint8_t keyType = 0;
965
966 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
967 PrintAndLog("Usage: hf mf efill <key A/B>");
968 PrintAndLog("sample: hf mf efill A");
969 PrintAndLog("Card data blocks transfers to card emulator memory.");
970 PrintAndLog("Keys must be laid in the simulator memory. \n");
971 return 0;
972 }
973
974 char ctmp = param_getchar(Cmd, 0);
975 if (ctmp == 0x00) {
976 PrintAndLog("Key type must be A or B");
977 return 1;
978 }
979 if (ctmp != 'A' && ctmp != 'a') keyType = 1;
980
981 UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {0, keyType, 0}};
982 SendCommand(&c);
983 return 0;
984 }
985
986 int CmdHF14AMfEKeyPrn(const char *Cmd)
987 {
988 int i;
989 uint8_t data[16];
990 uint64_t keyA, keyB;
991
992 PrintAndLog("|---|----------------|----------------|");
993 PrintAndLog("|sec|key A |key B |");
994 PrintAndLog("|---|----------------|----------------|");
995 for (i = 0; i < 16; i++) {
996 if (mfEmlGetMem(data, i * 4 + 3, 1)) {
997 PrintAndLog("error get block %d", i * 4 + 3);
998 break;
999 }
1000 keyA = bytes_to_num(data, 6);
1001 keyB = bytes_to_num(data + 10, 6);
1002 PrintAndLog("|%03d| %012llx | %012llx |", i, keyA, keyB);
1003 }
1004 PrintAndLog("|---|----------------|----------------|");
1005
1006 return 0;
1007 }
1008
1009 static command_t CommandTable[] =
1010 {
1011 {"help", CmdHelp, 1, "This help"},
1012 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},
1013 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},
1014 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},
1015 {"dump1k", CmdHF14AMfDump1k, 0, "Dump MIFARE classic tag to binary file"},
1016 {"restore1k", CmdHF14AMfRestore1k, 0, "Restore MIFARE classic binary file to BLANK tag"},
1017 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},
1018 {"chk", CmdHF14AMfChk, 0, "Test block up to 8 keys"},
1019 {"mifare", CmdHF14AMifare, 0, "Read parity error messages. param - <used card nonce>"},
1020 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},
1021 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE 1k card"},
1022 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},
1023 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},
1024 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},
1025 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},
1026 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},
1027 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},
1028 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},
1029 {NULL, NULL, 0, NULL}
1030 };
1031
1032 int CmdHFMF(const char *Cmd)
1033 {
1034 // flush
1035 while (WaitForResponseTimeout(CMD_ACK, 500) != NULL) ;
1036
1037 CmdsParse(CommandTable, Cmd);
1038 return 0;
1039 }
1040
1041 int CmdHelp(const char *Cmd)
1042 {
1043 CmdsHelp(CommandTable);
1044 return 0;
1045 }
Impressum, Datenschutz