]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
c98b25dac543e3ee2ffdb93fe12c8371b4ab1591
[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
13 static int CmdHelp(const char *Cmd);
14
15 int CmdHF14AMifare(const char *Cmd)
16 {
17 uint32_t uid = 0;
18 uint32_t nt = 0;
19 uint64_t par_list = 0, ks_list = 0, r_key = 0;
20 uint8_t isOK = 0;
21 uint8_t keyBlock[8] = {0};
22
23 if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, keyBlock, 8)) {
24 PrintAndLog("Nt must include 8 HEX symbols");
25 return 1;
26 }
27
28
29 UsbCommand c = {CMD_READER_MIFARE, {(uint32_t)bytes_to_num(keyBlock, 4), 0, 0}};
30 start:
31 clearCommandBuffer();
32 SendCommand(&c);
33
34 //flush queue
35 while (ukbhit()) getchar();
36
37 // message
38 printf("-------------------------------------------------------------------------\n");
39 printf("Executing command. It may take up to 30 min.\n");
40 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
41 printf("-------------------------------------------------------------------------\n");
42
43 // wait cycle
44 while (true) {
45 //printf(".");
46 fflush(stdout);
47 if (ukbhit()) {
48 getchar();
49 printf("\naborted via keyboard!\n");
50 break;
51 }
52
53 UsbCommand resp;
54 if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {
55 isOK = resp.arg[0] & 0xff;
56
57 uid = (uint32_t)bytes_to_num(resp.d.asBytes + 0, 4);
58 nt = (uint32_t)bytes_to_num(resp.d.asBytes + 4, 4);
59 par_list = bytes_to_num(resp.d.asBytes + 8, 8);
60 ks_list = bytes_to_num(resp.d.asBytes + 16, 8);
61
62 printf("\n\n");
63 PrintAndLog("isOk:%02x", isOK);
64 if (!isOK) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n");
65 break;
66 }
67 }
68 printf("\n");
69
70 // error
71 if (isOK != 1) return 1;
72
73 // execute original function from util nonce2key
74 if (nonce2key(uid, nt, par_list, ks_list, &r_key))
75 {
76 isOK = 2;
77 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt);
78 } else {
79 printf("------------------------------------------------------------------\n");
80 PrintAndLog("Key found:%012"llx" \n", r_key);
81
82 num_to_bytes(r_key, 6, keyBlock);
83 isOK = mfCheckKeys(0, 0, 1, keyBlock, &r_key);
84 }
85 if (!isOK)
86 PrintAndLog("Found valid key:%012"llx, r_key);
87 else
88 {
89 if (isOK != 2) PrintAndLog("Found invalid key. ( Nt=%08x ,Trying use it to run again...", nt);
90 c.arg[0] = nt;
91 goto start;
92 }
93
94 return 0;
95 }
96
97 int CmdHF14AMfWrBl(const char *Cmd)
98 {
99 uint8_t blockNo = 0;
100 uint8_t keyType = 0;
101 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
102 uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
103
104 char cmdp = 0x00;
105
106 if (strlen(Cmd)<3) {
107 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
108 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
109 return 0;
110 }
111
112 blockNo = param_get8(Cmd, 0);
113 cmdp = param_getchar(Cmd, 1);
114 if (cmdp == 0x00) {
115 PrintAndLog("Key type must be A or B");
116 return 1;
117 }
118 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
119 if (param_gethex(Cmd, 2, key, 12)) {
120 PrintAndLog("Key must include 12 HEX symbols");
121 return 1;
122 }
123 if (param_gethex(Cmd, 3, bldata, 32)) {
124 PrintAndLog("Block data must include 32 HEX symbols");
125 return 1;
126 }
127 PrintAndLog("--block no:%02x key type:%02x key:%s", blockNo, keyType, sprint_hex(key, 6));
128 PrintAndLog("--data: %s", sprint_hex(bldata, 16));
129
130 UsbCommand c = {CMD_MIFARE_WRITEBL, {blockNo, keyType, 0}};
131 memcpy(c.d.asBytes, key, 6);
132 memcpy(c.d.asBytes + 10, bldata, 16);
133 SendCommand(&c);
134
135 UsbCommand resp;
136 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
137 uint8_t isOK = resp.arg[0] & 0xff;
138 PrintAndLog("isOk:%02x", isOK);
139 } else {
140 PrintAndLog("Command execute timeout");
141 }
142
143 return 0;
144 }
145
146 int CmdHF14AMfRdBl(const char *Cmd)
147 {
148 uint8_t blockNo = 0;
149 uint8_t keyType = 0;
150 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
151
152 char cmdp = 0x00;
153
154
155 if (strlen(Cmd)<3) {
156 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
157 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
158 return 0;
159 }
160
161 blockNo = param_get8(Cmd, 0);
162 cmdp = param_getchar(Cmd, 1);
163 if (cmdp == 0x00) {
164 PrintAndLog("Key type must be A or B");
165 return 1;
166 }
167 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
168 if (param_gethex(Cmd, 2, key, 12)) {
169 PrintAndLog("Key must include 12 HEX symbols");
170 return 1;
171 }
172 PrintAndLog("--block no:%02x key type:%02x key:%s ", blockNo, keyType, sprint_hex(key, 6));
173
174 UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};
175 memcpy(c.d.asBytes, key, 6);
176 SendCommand(&c);
177
178 UsbCommand resp;
179 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
180 uint8_t isOK = resp.arg[0] & 0xff;
181 uint8_t * data = resp.d.asBytes;
182
183 if (isOK)
184 PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 16));
185 else
186 PrintAndLog("isOk:%02x", isOK);
187 } else {
188 PrintAndLog("Command execute timeout");
189 }
190
191 return 0;
192 }
193
194 int CmdHF14AMfRdSc(const char *Cmd)
195 {
196 int i;
197 uint8_t sectorNo = 0;
198 uint8_t keyType = 0;
199 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
200
201 uint8_t isOK = 0;
202 uint8_t * data = NULL;
203
204 char cmdp = 0x00;
205
206 if (strlen(Cmd)<3) {
207 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
208 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
209 return 0;
210 }
211
212 sectorNo = param_get8(Cmd, 0);
213 if (sectorNo > 63) {
214 PrintAndLog("Sector number must be less than 64");
215 return 1;
216 }
217 cmdp = param_getchar(Cmd, 1);
218 if (cmdp == 0x00) {
219 PrintAndLog("Key type must be A or B");
220 return 1;
221 }
222 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
223 if (param_gethex(Cmd, 2, key, 12)) {
224 PrintAndLog("Key must include 12 HEX symbols");
225 return 1;
226 }
227 PrintAndLog("--sector no:%02x key type:%02x key:%s ", sectorNo, keyType, sprint_hex(key, 6));
228
229 UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};
230 memcpy(c.d.asBytes, key, 6);
231 SendCommand(&c);
232 PrintAndLog(" ");
233
234 UsbCommand resp;
235 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
236 isOK = resp.arg[0] & 0xff;
237 data = resp.d.asBytes;
238
239 PrintAndLog("isOk:%02x", isOK);
240 if (isOK)
241 for (i = 0; i < 2; i++) {
242 PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));
243 }
244 } else {
245 PrintAndLog("Command1 execute timeout");
246 }
247
248 // response2
249 PrintAndLog(" ");
250 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
251 isOK = resp.arg[0] & 0xff;
252 data = resp.d.asBytes;
253
254 if (isOK)
255 for (i = 0; i < 2; i++) {
256 PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));
257 }
258 } else {
259 PrintAndLog("Command2 execute timeout");
260 }
261
262 return 0;
263 }
264
265 int CmdHF14AMfDump(const char *Cmd)
266 {
267 int i, j;
268
269 uint8_t keyA[40][6];
270 uint8_t keyB[40][6];
271 uint8_t rights[40][4];
272
273 FILE *fin;
274 FILE *fout;
275
276 UsbCommand resp;
277
278 if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {
279 PrintAndLog("Could not find file dumpkeys.bin");
280 return 1;
281 }
282
283 if ((fout = fopen("dumpdata.bin","wb")) == NULL) {
284 PrintAndLog("Could not create file name dumpdata.bin");
285 return 1;
286 }
287
288 // Read key file
289
290 for (i=0 ; i<16 ; i++) {
291 if (fread( keyA[i], 1, 6, fin ) == 0) {
292 PrintAndLog("File reading error.");
293 return 2;
294 }
295 }
296 for (i=0 ; i<16 ; i++) {
297 if (fread( keyB[i], 1, 6, fin ) == 0) {
298 PrintAndLog("File reading error.");
299 return 2;
300 }
301 }
302
303 // Read access rights to sectors
304
305 PrintAndLog("|-----------------------------------------|");
306 PrintAndLog("|------ Reading sector access bits...-----|");
307 PrintAndLog("|-----------------------------------------|");
308
309 for (i = 0 ; i < 16 ; i++) {
310 UsbCommand c = {CMD_MIFARE_READBL, {4*i + 3, 0, 0}};
311 memcpy(c.d.asBytes, keyA[i], 6);
312 SendCommand(&c);
313
314 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
315 uint8_t isOK = resp.arg[0] & 0xff;
316 uint8_t *data = resp.d.asBytes;
317 if (isOK){
318 rights[i][0] = ((data[7] & 0x10)>>4) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>2);
319 rights[i][1] = ((data[7] & 0x20)>>5) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>3);
320 rights[i][2] = ((data[7] & 0x40)>>6) | ((data[8] & 0x4)>>1) | ((data[8] & 0x40)>>4);
321 rights[i][3] = ((data[7] & 0x80)>>7) | ((data[8] & 0x8)>>2) | ((data[8] & 0x80)>>5);
322 }
323 else{
324 PrintAndLog("Could not get access rights for block %d", i);
325 }
326 }
327 else {
328 PrintAndLog("Command execute timeout");
329 }
330 }
331
332 // Read blocks and print to file
333
334 PrintAndLog("|-----------------------------------------|");
335 PrintAndLog("|----- Dumping all blocks to file... -----|");
336 PrintAndLog("|-----------------------------------------|");
337
338
339 for (i=0 ; i<16 ; i++) {
340 for (j=0 ; j<4 ; j++) {
341 bool received = false;
342
343 if (j == 3){
344 UsbCommand c = {CMD_MIFARE_READBL, {i*4 + j, 0, 0}};
345 memcpy(c.d.asBytes, keyA[i], 6);
346 SendCommand(&c);
347 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);
348 }
349 else{
350 if ((rights[i][j] == 6) | (rights[i][j] == 5)) {
351 UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 1, 0}};
352 memcpy(c.d.asBytes, keyB[i], 6);
353 SendCommand(&c);
354 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);
355 }
356 else if (rights[i][j] == 7) {
357 PrintAndLog("Access rights do not allow reading of sector %d block %d",i,j);
358 }
359 else {
360 UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 0, 0}};
361 memcpy(c.d.asBytes, keyA[i], 6);
362 SendCommand(&c);
363 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);
364 }
365 }
366
367 if (received) {
368 uint8_t isOK = resp.arg[0] & 0xff;
369 uint8_t *data = resp.d.asBytes;
370 if (j == 3) {
371 data[0] = (keyA[i][0]);
372 data[1] = (keyA[i][1]);
373 data[2] = (keyA[i][2]);
374 data[3] = (keyA[i][3]);
375 data[4] = (keyA[i][4]);
376 data[5] = (keyA[i][5]);
377 data[10] = (keyB[i][0]);
378 data[11] = (keyB[i][1]);
379 data[12] = (keyB[i][2]);
380 data[13] = (keyB[i][3]);
381 data[14] = (keyB[i][4]);
382 data[15] = (keyB[i][5]);
383 }
384 if (isOK) {
385 fwrite ( data, 1, 16, fout );
386 }
387 else {
388 PrintAndLog("Could not get access rights for block %d", i);
389 }
390 }
391 else {
392 PrintAndLog("Command execute timeout");
393 }
394 }
395 }
396
397 fclose(fin);
398 fclose(fout);
399
400 return 0;
401 }
402
403 int CmdHF14AMfRestore(const char *Cmd)
404 {
405
406 int i,j;
407 uint8_t keyType = 0;
408 uint8_t key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
409 uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
410 uint8_t keyA[16][6];
411 uint8_t keyB[16][6];
412
413 FILE *fdump;
414 FILE *fkeys;
415
416 if ((fdump = fopen("dumpdata.bin","rb")) == NULL) {
417 PrintAndLog("Could not find file dumpdata.bin");
418 return 1;
419 }
420 if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) {
421 PrintAndLog("Could not find file dumpkeys.bin");
422 return 1;
423 }
424
425 for (i=0 ; i<16 ; i++) {
426 if (fread(keyA[i], 1, 6, fkeys) == 0) {
427 PrintAndLog("File reading error.");
428 return 2;
429 }
430 }
431 for (i=0 ; i<16 ; i++) {
432 if (fread(keyB[i], 1, 6, fkeys) == 0) {
433 PrintAndLog("File reading error.");
434 return 2;
435 }
436 }
437
438 PrintAndLog("Restoring dumpdata.bin to card");
439
440 for (i=0 ; i<16 ; i++) {
441 for( j=0 ; j<4 ; j++) {
442 UsbCommand c = {CMD_MIFARE_WRITEBL, {i*4 + j, keyType, 0}};
443 memcpy(c.d.asBytes, key, 6);
444
445 if (fread(bldata, 1, 16, fdump) == 0) {
446 PrintAndLog("File reading error.");
447 return 2;
448 }
449
450 if (j == 3) {
451 bldata[0] = (keyA[i][0]);
452 bldata[1] = (keyA[i][1]);
453 bldata[2] = (keyA[i][2]);
454 bldata[3] = (keyA[i][3]);
455 bldata[4] = (keyA[i][4]);
456 bldata[5] = (keyA[i][5]);
457 bldata[10] = (keyB[i][0]);
458 bldata[11] = (keyB[i][1]);
459 bldata[12] = (keyB[i][2]);
460 bldata[13] = (keyB[i][3]);
461 bldata[14] = (keyB[i][4]);
462 bldata[15] = (keyB[i][5]);
463 }
464
465 PrintAndLog("Writing to block %2d: %s", i*4+j, sprint_hex(bldata, 16));
466
467 /*
468 PrintAndLog("Writing to block %2d: %s Confirm? [Y,N]", i*4+j, sprint_hex(bldata, 16));
469
470 scanf("%c",&ch);
471 if ((ch != 'y') && (ch != 'Y')){
472 PrintAndLog("Aborting !");
473 return 1;
474 }
475 */
476
477 memcpy(c.d.asBytes + 10, bldata, 16);
478 SendCommand(&c);
479
480 UsbCommand resp;
481 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
482 uint8_t isOK = resp.arg[0] & 0xff;
483 PrintAndLog("isOk:%02x", isOK);
484 } else {
485 PrintAndLog("Command execute timeout");
486 }
487 }
488 }
489
490 fclose(fdump);
491 fclose(fkeys);
492 return 0;
493 }
494
495 int CmdHF14AMfNested(const char *Cmd)
496 {
497 int i, j, res, iterations;
498 sector * e_sector = NULL;
499 uint8_t blockNo = 0;
500 uint8_t keyType = 0;
501 uint8_t trgBlockNo = 0;
502 uint8_t trgKeyType = 0;
503 uint8_t blDiff = 0;
504 int SectorsCnt = 0;
505 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
506 uint8_t keyBlock[16 * 6];
507 uint64_t key64 = 0;
508 int transferToEml = 0;
509
510 int createDumpFile = 0;
511 FILE *fkeys;
512 uint8_t standart[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
513 uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
514
515 char cmdp, ctmp;
516
517 if (strlen(Cmd)<3) {
518 PrintAndLog("Usage:");
519 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
520 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
521 PrintAndLog(" <target block number> <target key A/B> [t]");
522 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
523 PrintAndLog("t - transfer keys into emulator memory");
524 PrintAndLog("d - write keys to binary file");
525 PrintAndLog(" ");
526 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
527 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");
528 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");
529 PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
530 return 0;
531 }
532
533 cmdp = param_getchar(Cmd, 0);
534 blockNo = param_get8(Cmd, 1);
535 ctmp = param_getchar(Cmd, 2);
536 if (ctmp == 0x00) {
537 PrintAndLog("Key type must be A or B");
538 return 1;
539 }
540 if (ctmp != 'A' && ctmp != 'a') keyType = 1;
541 if (param_gethex(Cmd, 3, key, 12)) {
542 PrintAndLog("Key must include 12 HEX symbols");
543 return 1;
544 }
545
546 if (cmdp == 'o' || cmdp == 'O') {
547 cmdp = 'o';
548 trgBlockNo = param_get8(Cmd, 4);
549 ctmp = param_getchar(Cmd, 5);
550 if (ctmp == 0x00) {
551 PrintAndLog("Target key type must be A or B");
552 return 1;
553 }
554 if (ctmp != 'A' && ctmp != 'a') trgKeyType = 1;
555 } else {
556 switch (cmdp) {
557 case '0': SectorsCnt = 05; break;
558 case '1': SectorsCnt = 16; break;
559 case '2': SectorsCnt = 32; break;
560 case '4': SectorsCnt = 64; break;
561 default: SectorsCnt = 16;
562 }
563 }
564
565 ctmp = param_getchar(Cmd, 4);
566 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;
567 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;
568
569 ctmp = param_getchar(Cmd, 6);
570 transferToEml |= (ctmp == 't' || ctmp == 'T');
571 transferToEml |= (ctmp == 'd' || ctmp == 'D');
572
573 PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo, keyType, sprint_hex(key, 6), transferToEml);
574 if (cmdp == 'o')
575 PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo, trgKeyType);
576
577 if (cmdp == 'o') {
578 if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock)) {
579 PrintAndLog("Nested error.");
580 return 2;
581 }
582
583 for (i = 0; i < 16; i++) {
584 PrintAndLog("count=%d key= %s", i, sprint_hex(keyBlock + i * 6, 6));
585 }
586
587 // test keys
588 res = mfCheckKeys(trgBlockNo, trgKeyType, 8, keyBlock, &key64);
589 if (res)
590 res = mfCheckKeys(trgBlockNo, trgKeyType, 8, &keyBlock[6 * 8], &key64);
591 if (!res) {
592 PrintAndLog("Found valid key:%012"llx, key64);
593
594 // transfer key to the emulator
595 if (transferToEml) {
596 mfEmlGetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1);
597
598 if (!trgKeyType)
599 num_to_bytes(key64, 6, keyBlock);
600 else
601 num_to_bytes(key64, 6, &keyBlock[10]);
602 mfEmlSetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1);
603 }
604 } else {
605 PrintAndLog("No valid key found");
606 }
607 }
608 else { // ------------------------------------ multiple sectors working
609 blDiff = blockNo % 4;
610 PrintAndLog("Block shift=%d", blDiff);
611 e_sector = calloc(SectorsCnt, sizeof(sector));
612 if (e_sector == NULL) return 1;
613
614 //test current key 4 sectors
615 memcpy(keyBlock, key, 6);
616 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 1 * 6));
617 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 2 * 6));
618 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 3 * 6));
619 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 4 * 6));
620 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));
621
622 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);
623 for (i = 0; i < SectorsCnt; i++) {
624 for (j = 0; j < 2; j++) {
625 if (e_sector[i].foundKey[j]) continue;
626
627 res = mfCheckKeys(i * 4 + blDiff, j, 6, keyBlock, &key64);
628
629 if (!res) {
630 e_sector[i].Key[j] = key64;
631 e_sector[i].foundKey[j] = 1;
632 }
633 }
634 }
635
636 // nested sectors
637 iterations = 0;
638 PrintAndLog("nested...");
639 for (i = 0; i < NESTED_SECTOR_RETRY; i++) {
640 for (trgBlockNo = blDiff; trgBlockNo < SectorsCnt * 4; trgBlockNo = trgBlockNo + 4)
641 for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) {
642 if (e_sector[trgBlockNo / 4].foundKey[trgKeyType]) continue;
643 if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock)) continue;
644
645 iterations++;
646
647 //try keys from nested
648 res = mfCheckKeys(trgBlockNo, trgKeyType, 8, keyBlock, &key64);
649 if (res)
650 res = mfCheckKeys(trgBlockNo, trgKeyType, 8, &keyBlock[6 * 8], &key64);
651 if (!res) {
652 PrintAndLog("Found valid key:%012"llx, key64);
653 e_sector[trgBlockNo / 4].foundKey[trgKeyType] = 1;
654 e_sector[trgBlockNo / 4].Key[trgKeyType] = key64;
655 }
656 }
657 }
658
659 PrintAndLog("Iterations count: %d", iterations);
660 //print them
661 PrintAndLog("|---|----------------|---|----------------|---|");
662 PrintAndLog("|sec|key A |res|key B |res|");
663 PrintAndLog("|---|----------------|---|----------------|---|");
664 for (i = 0; i < SectorsCnt; i++) {
665 PrintAndLog("|%03d| %012"llx" | %d | %012"llx" | %d |", i,
666 e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);
667 }
668 PrintAndLog("|---|----------------|---|----------------|---|");
669
670 // transfer them to the emulator
671 if (transferToEml) {
672 for (i = 0; i < SectorsCnt; i++) {
673 mfEmlGetMem(keyBlock, i * 4 + 3, 1);
674 if (e_sector[i].foundKey[0])
675 num_to_bytes(e_sector[i].Key[0], 6, keyBlock);
676 if (e_sector[i].foundKey[1])
677 num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);
678 mfEmlSetMem(keyBlock, i * 4 + 3, 1);
679 }
680 }
681
682 // Create dump file
683 if (createDumpFile) {
684 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
685 PrintAndLog("Could not create file dumpkeys.bin");
686 free(e_sector);
687 return 1;
688 }
689 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
690 for(i=0; i<16; i++) {
691 if (e_sector[i].foundKey[0]){
692 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
693 fwrite ( tempkey, 1, 6, fkeys );
694 }
695 else{
696 fwrite ( &standart, 1, 6, fkeys );
697 }
698 }
699 for(i=0; i<16; i++) {
700 if (e_sector[i].foundKey[1]){
701 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
702 fwrite ( tempkey, 1, 6, fkeys );
703 }
704 else{
705 fwrite ( &standart, 1, 6, fkeys );
706 }
707 }
708 fclose(fkeys);
709 }
710
711 free(e_sector);
712 }
713
714 return 0;
715 }
716
717 static uint32_t
718 get_trailer_block (uint32_t uiBlock)
719 {
720 // Test if we are in the small or big sectors
721 uint32_t trailer_block = 0;
722 if (uiBlock < 128) {
723 trailer_block = uiBlock + (3 - (uiBlock % 4));
724 } else {
725 trailer_block = uiBlock + (15 - (uiBlock % 16));
726 }
727 return trailer_block;
728 }
729 int CmdHF14AMfChk(const char *Cmd)
730 {
731 FILE * f;
732 char filename[256]={0};
733 char buf[13];
734 uint8_t *keyBlock = NULL, *p;
735 uint8_t stKeyBlock = 20;
736
737 int i, res;
738 int keycnt = 0;
739 char ctmp = 0x00;
740 uint8_t blockNo = 0;
741 uint8_t SectorsCnt = 1;
742 uint8_t keyType = 0;
743 uint64_t key64 = 0;
744
745 int transferToEml = 0;
746 int createDumpFile = 0;
747
748 keyBlock = calloc(stKeyBlock, 6);
749 if (keyBlock == NULL) return 1;
750
751 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 0 * 6)); // Default key (first key used by program if no user defined key)
752 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 1 * 6)); // Blank key
753 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 2 * 6)); // NFCForum MAD key
754 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 3 * 6));
755 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 4 * 6));
756 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock + 5 * 6));
757 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock + 6 * 6));
758 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock + 7 * 6));
759 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock + 8 * 6));
760 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock + 9 * 6));
761 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock + 10 * 6));
762 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock + 11 * 6));
763 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock + 12 * 6));
764
765 if (strlen(Cmd)<3) {
766 PrintAndLog("Usage: hf mf chk <block number>/<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");
767 PrintAndLog(" * - all sectors");
768 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
769 // PrintAndLog("d - write keys to binary file\n");
770
771 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
772 PrintAndLog(" hf mf chk *1 ? t");
773 return 0;
774 }
775
776 if (param_getchar(Cmd, 0)=='*') {
777 blockNo = 3;
778 switch(param_getchar(Cmd+1, 0)) {
779 case '0': SectorsCnt = 5; break;
780 case '1': SectorsCnt = 16; break;
781 case '2': SectorsCnt = 32; break;
782 case '4': SectorsCnt = 40; break;
783 default: SectorsCnt = 16;
784 }
785 }
786 else
787 blockNo = param_get8(Cmd, 0);
788
789 ctmp = param_getchar(Cmd, 1);
790 switch (ctmp) {
791 case 'a': case 'A':
792 keyType = !0;
793 break;
794 case 'b': case 'B':
795 keyType = !1;
796 break;
797 case '?':
798 keyType = 2;
799 break;
800 default:
801 PrintAndLog("Key type must be A , B or ?");
802 return 1;
803 };
804
805 ctmp = param_getchar(Cmd, 2);
806 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;
807 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;
808
809 for (i = transferToEml || createDumpFile; param_getchar(Cmd, 2 + i); i++) {
810 if (!param_gethex(Cmd, 2 + i, keyBlock + 6 * keycnt, 12)) {
811 if ( stKeyBlock - keycnt < 2) {
812 p = realloc(keyBlock, 6*(stKeyBlock+=10));
813 if (!p) {
814 PrintAndLog("Cannot allocate memory for Keys");
815 free(keyBlock);
816 return 2;
817 }
818 keyBlock = p;
819 }
820 PrintAndLog("chk key[%d] %02x%02x%02x%02x%02x%02x", keycnt,
821 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],
822 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);
823 keycnt++;
824 } else {
825 // May be a dic file
826 if ( param_getstr(Cmd, 2 + i,filename) > 255 ) {
827 PrintAndLog("File name too long");
828 free(keyBlock);
829 return 2;
830 }
831
832 if ( (f = fopen( filename , "r")) ) {
833 while( !feof(f) ){
834 memset(buf, 0, sizeof(buf));
835 if (fgets(buf, sizeof(buf), f) == NULL) {
836 PrintAndLog("File reading error.");
837 return 2;
838 }
839
840 if (strlen(buf) < 12 || buf[11] == '\n')
841 continue;
842
843 while (fgetc(f) != '\n' && !feof(f)) ; //goto next line
844
845 if( buf[0]=='#' ) continue; //The line start with # is remcommnet,skip
846
847 if (!isxdigit(buf[0])){
848 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);
849 continue;
850 }
851
852 buf[12] = 0;
853
854 if ( stKeyBlock - keycnt < 2) {
855 p = realloc(keyBlock, 6*(stKeyBlock+=10));
856 if (!p) {
857 PrintAndLog("Cannot allocate memory for defKeys");
858 free(keyBlock);
859 return 2;
860 }
861 keyBlock = p;
862 }
863 memset(keyBlock + 6 * keycnt, 0, 6);
864 num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);
865 PrintAndLog("chk custom key[%d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));
866 keycnt++;
867 }
868 } else {
869 PrintAndLog("File: %s: not found or locked.", filename);
870 free(keyBlock);
871 return 1;
872 fclose(f);
873 }
874 }
875 }
876
877 if (keycnt == 0) {
878 PrintAndLog("No key specified,try default keys");
879 for (;keycnt <=12; keycnt++)
880 PrintAndLog("chk default key[%d] %02x%02x%02x%02x%02x%02x", keycnt,
881 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],
882 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);
883 }
884
885 for ( int t = !keyType ; t < 2 ; keyType==2?(t++):(t=2) ) {
886 int b=blockNo;
887 for (int i=0; i<SectorsCnt; ++i) {
888 PrintAndLog("--SectorsCnt:%d block no:0x%02x key type:%C key count:%d ", i, b, t?'B':'A', keycnt);
889 int size = keycnt>8?8:keycnt;
890 for (int c = 0; c < keycnt; c+=size) {
891 size=keycnt-c>8?8:keycnt-c;
892 res = mfCheckKeys(b, t, size, keyBlock +6*c, &key64);
893 if (res !=1) {
894 if (!res) {
895 PrintAndLog("Found valid key:[%012"llx"]",key64);
896 if (transferToEml) {
897 uint8_t block[16];
898 mfEmlGetMem(block, get_trailer_block(b), 1);
899 num_to_bytes(key64, 6, block + t*10);
900 mfEmlSetMem(block, get_trailer_block(b), 1);
901 }
902 break;
903 }
904 else {
905 printf("Not found yet, keycnt:%d\r", c+size);
906 fflush(stdout);
907 }
908 } else {
909 PrintAndLog("Command execute timeout");
910 }
911 }
912 b<127?(b+=4):(b+=16);
913 }
914 }
915
916 free(keyBlock);
917
918 /*
919 // Create dump file
920 if (createDumpFile) {
921 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
922 PrintAndLog("Could not create file dumpkeys.bin");
923 free(e_sector);
924 return 1;
925 }
926 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
927 for(i=0; i<16; i++) {
928 if (e_sector[i].foundKey[0]){
929 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
930 fwrite ( tempkey, 1, 6, fkeys );
931 }
932 else{
933 fwrite ( &standart, 1, 6, fkeys );
934 }
935 }
936 for(i=0; i<16; i++) {
937 if (e_sector[i].foundKey[1]){
938 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
939 fwrite ( tempkey, 1, 6, fkeys );
940 }
941 else{
942 fwrite ( &standart, 1, 6, fkeys );
943 }
944 }
945 fclose(fkeys);
946 }
947 */
948 return 0;
949 }
950
951 int CmdHF14AMf1kSim(const char *Cmd)
952 {
953 uint8_t uid[4] = {0, 0, 0, 0};
954
955 if (param_getchar(Cmd, 0) == 'h') {
956 PrintAndLog("Usage: hf mf sim <uid (8 hex symbols)>");
957 PrintAndLog(" sample: hf mf sim 0a0a0a0a ");
958 return 0;
959 }
960
961 if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {
962 PrintAndLog("UID must include 8 HEX symbols");
963 return 1;
964 }
965 PrintAndLog(" uid:%s ", sprint_hex(uid, 4));
966
967 UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {0, 0, 0}};
968 memcpy(c.d.asBytes, uid, 4);
969 SendCommand(&c);
970
971 return 0;
972 }
973
974 int CmdHF14AMfDbg(const char *Cmd)
975 {
976 int dbgMode = param_get32ex(Cmd, 0, 0, 10);
977 if (dbgMode > 4) {
978 PrintAndLog("Max debud mode parameter is 4 \n");
979 }
980
981 if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {
982 PrintAndLog("Usage: hf mf dbg <debug level>");
983 PrintAndLog(" 0 - no debug messages");
984 PrintAndLog(" 1 - error messages");
985 PrintAndLog(" 2 - all messages");
986 PrintAndLog(" 4 - extended debug mode");
987 return 0;
988 }
989
990 UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};
991 SendCommand(&c);
992
993 return 0;
994 }
995
996 int CmdHF14AMfEGet(const char *Cmd)
997 {
998 uint8_t blockNo = 0;
999 uint8_t data[3 * 16];
1000 int i;
1001
1002 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1003 PrintAndLog("Usage: hf mf eget <block number>");
1004 PrintAndLog(" sample: hf mf eget 0 ");
1005 return 0;
1006 }
1007
1008 blockNo = param_get8(Cmd, 0);
1009
1010 PrintAndLog(" ");
1011 if (!mfEmlGetMem(data, blockNo, 3)) {
1012 for (i = 0; i < 3; i++) {
1013 PrintAndLog("data[%d]:%s", blockNo + i, sprint_hex(data + i * 16, 16));
1014 }
1015 } else {
1016 PrintAndLog("Command execute timeout");
1017 }
1018
1019 return 0;
1020 }
1021
1022 int CmdHF14AMfEClear(const char *Cmd)
1023 {
1024 if (param_getchar(Cmd, 0) == 'h') {
1025 PrintAndLog("Usage: hf mf eclr");
1026 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1027 return 0;
1028 }
1029
1030 UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};
1031 SendCommand(&c);
1032 return 0;
1033 }
1034
1035 int CmdHF14AMfESet(const char *Cmd)
1036 {
1037 uint8_t memBlock[16];
1038 uint8_t blockNo = 0;
1039
1040 memset(memBlock, 0x00, sizeof(memBlock));
1041
1042 if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {
1043 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1044 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1045 return 0;
1046 }
1047
1048 blockNo = param_get8(Cmd, 0);
1049
1050 if (param_gethex(Cmd, 1, memBlock, 32)) {
1051 PrintAndLog("block data must include 32 HEX symbols");
1052 return 1;
1053 }
1054
1055 // 1 - blocks count
1056 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};
1057 memcpy(c.d.asBytes, memBlock, 16);
1058 SendCommand(&c);
1059 return 0;
1060 }
1061
1062 int CmdHF14AMfELoad(const char *Cmd)
1063 {
1064 FILE * f;
1065 char filename[20];
1066 char * fnameptr = filename;
1067 char buf[64];
1068 uint8_t buf8[64];
1069 int i, len, blockNum;
1070
1071 memset(filename, 0, sizeof(filename));
1072 memset(buf, 0, sizeof(buf));
1073
1074 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {
1075 PrintAndLog("It loads emul dump from the file `filename.eml`");
1076 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");
1077 PrintAndLog(" sample: hf mf eload filename");
1078 return 0;
1079 }
1080
1081 len = strlen(Cmd);
1082 if (len > 14) len = 14;
1083
1084 memcpy(filename, Cmd, len);
1085 fnameptr += len;
1086
1087 sprintf(fnameptr, ".eml");
1088
1089 // open file
1090 f = fopen(filename, "r");
1091 if (f == NULL) {
1092 PrintAndLog("File not found or locked.");
1093 return 1;
1094 }
1095
1096 blockNum = 0;
1097 while(!feof(f)){
1098 memset(buf, 0, sizeof(buf));
1099 if (fgets(buf, sizeof(buf), f) == NULL) {
1100 PrintAndLog("File reading error.");
1101 return 2;
1102 }
1103
1104 if (strlen(buf) < 32){
1105 if(strlen(buf) && feof(f))
1106 break;
1107 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1108 return 2;
1109 }
1110 for (i = 0; i < 32; i += 2)
1111 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
1112 // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));
1113
1114 if (mfEmlSetMem(buf8, blockNum, 1)) {
1115 PrintAndLog("Cant set emul block: %d", blockNum);
1116 return 3;
1117 }
1118 blockNum++;
1119
1120 if (blockNum >= 32 * 4 + 8 * 16) break;
1121 }
1122 fclose(f);
1123
1124 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
1125 PrintAndLog("File content error. There must be 64 blocks");
1126 return 4;
1127 }
1128 PrintAndLog("Loaded from file: %s", filename);
1129 return 0;
1130 }
1131
1132 int CmdHF14AMfESave(const char *Cmd)
1133 {
1134 FILE * f;
1135 char filename[20];
1136 char * fnameptr = filename;
1137 uint8_t buf[64];
1138 int i, j, len;
1139
1140 memset(filename, 0, sizeof(filename));
1141 memset(buf, 0, sizeof(buf));
1142
1143 if (param_getchar(Cmd, 0) == 'h') {
1144 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1145 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");
1146 PrintAndLog(" sample: hf mf esave ");
1147 PrintAndLog(" hf mf esave filename");
1148 return 0;
1149 }
1150
1151 len = strlen(Cmd);
1152 if (len > 14) len = 14;
1153
1154 if (len < 1) {
1155 // get filename
1156 if (mfEmlGetMem(buf, 0, 1)) {
1157 PrintAndLog("Cant get block: %d", 0);
1158 return 1;
1159 }
1160 for (j = 0; j < 7; j++, fnameptr += 2)
1161 sprintf(fnameptr, "%02x", buf[j]);
1162 } else {
1163 memcpy(filename, Cmd, len);
1164 fnameptr += len;
1165 }
1166
1167 sprintf(fnameptr, ".eml");
1168
1169 // open file
1170 f = fopen(filename, "w+");
1171
1172 // put hex
1173 for (i = 0; i < 32 * 4 + 8 * 16; i++) {
1174 if (mfEmlGetMem(buf, i, 1)) {
1175 PrintAndLog("Cant get block: %d", i);
1176 break;
1177 }
1178 for (j = 0; j < 16; j++)
1179 fprintf(f, "%02x", buf[j]);
1180 fprintf(f,"\n");
1181 }
1182 fclose(f);
1183
1184 PrintAndLog("Saved to file: %s", filename);
1185
1186 return 0;
1187 }
1188
1189 int CmdHF14AMfECFill(const char *Cmd)
1190 {
1191 uint8_t keyType = 0;
1192
1193 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1194 PrintAndLog("Usage: hf mf efill <key A/B>");
1195 PrintAndLog("sample: hf mf efill A");
1196 PrintAndLog("Card data blocks transfers to card emulator memory.");
1197 PrintAndLog("Keys must be laid in the simulator memory. \n");
1198 return 0;
1199 }
1200
1201 char ctmp = param_getchar(Cmd, 0);
1202 if (ctmp == 0x00) {
1203 PrintAndLog("Key type must be A or B");
1204 return 1;
1205 }
1206 if (ctmp != 'A' && ctmp != 'a') keyType = 1;
1207
1208 UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {0, keyType, 0}};
1209 SendCommand(&c);
1210 return 0;
1211 }
1212
1213 int CmdHF14AMfEKeyPrn(const char *Cmd)
1214 {
1215 int i,b=-1;
1216 uint8_t data[16];
1217 uint64_t keyA, keyB;
1218
1219 PrintAndLog("|---|----------------|----------------|");
1220 PrintAndLog("|sec|key A |key B |");
1221 PrintAndLog("|---|----------------|----------------|");
1222 for (i = 0; i < 40; i++) {
1223 b<127?(b+=4):(b+=16);
1224 if (mfEmlGetMem(data, b, 1)) {
1225 PrintAndLog("error get block %d", b);
1226 break;
1227 }
1228 keyA = bytes_to_num(data, 6);
1229 keyB = bytes_to_num(data + 10, 6);
1230 PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB);
1231 }
1232 PrintAndLog("|---|----------------|----------------|");
1233
1234 return 0;
1235 }
1236
1237 int CmdHF14AMfCSetUID(const char *Cmd)
1238 {
1239 uint8_t wipeCard = 0;
1240 uint8_t uid[8];
1241 uint8_t oldUid[8];
1242 int res;
1243
1244 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1245 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");
1246 PrintAndLog("sample: hf mf csetuid 01020304 w");
1247 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");
1248 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1249 return 0;
1250 }
1251
1252 if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {
1253 PrintAndLog("UID must include 8 HEX symbols");
1254 return 1;
1255 }
1256
1257 char ctmp = param_getchar(Cmd, 1);
1258 if (ctmp == 'w' || ctmp == 'W') wipeCard = 1;
1259
1260 PrintAndLog("--wipe card:%02x uid:%s", wipeCard, sprint_hex(uid, 4));
1261
1262 res = mfCSetUID(uid, oldUid, wipeCard);
1263 if (res) {
1264 PrintAndLog("Can't set UID. error=%d", res);
1265 return 1;
1266 }
1267
1268 PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));
1269 return 0;
1270 }
1271
1272 int CmdHF14AMfCSetBlk(const char *Cmd)
1273 {
1274 uint8_t uid[8];
1275 uint8_t memBlock[16];
1276 uint8_t blockNo = 0;
1277 int res;
1278 memset(memBlock, 0x00, sizeof(memBlock));
1279
1280 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1281 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");
1282 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1283 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");
1284 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1285 return 0;
1286 }
1287
1288 blockNo = param_get8(Cmd, 0);
1289
1290 if (param_gethex(Cmd, 1, memBlock, 32)) {
1291 PrintAndLog("block data must include 32 HEX symbols");
1292 return 1;
1293 }
1294
1295 PrintAndLog("--block number:%02x data:%s", blockNo, sprint_hex(memBlock, 16));
1296
1297 res = mfCSetBlock(blockNo, memBlock, uid, 0, CSETBLOCK_SINGLE_OPER);
1298 if (res) {
1299 PrintAndLog("Can't write block. error=%d", res);
1300 return 1;
1301 }
1302
1303 PrintAndLog("UID:%s", sprint_hex(uid, 4));
1304 return 0;
1305 }
1306
1307 int CmdHF14AMfCLoad(const char *Cmd)
1308 {
1309 FILE * f;
1310 char filename[20];
1311 char * fnameptr = filename;
1312 char buf[64];
1313 uint8_t buf8[64];
1314 uint8_t fillFromEmulator = 0;
1315 int i, len, blockNum, flags;
1316
1317 memset(filename, 0, sizeof(filename));
1318 memset(buf, 0, sizeof(buf));
1319
1320 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {
1321 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");
1322 PrintAndLog("or from emulator memory (option `e`)");
1323 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1324 PrintAndLog(" or: hf mf cload e ");
1325 PrintAndLog(" sample: hf mf cload filename");
1326 return 0;
1327 }
1328
1329 char ctmp = param_getchar(Cmd, 0);
1330 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
1331
1332 if (fillFromEmulator) {
1333 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1334 for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {
1335 if (mfEmlGetMem(buf8, blockNum, 1)) {
1336 PrintAndLog("Cant get block: %d", blockNum);
1337 return 2;
1338 }
1339
1340 if (blockNum == 2) flags = 0;
1341 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1342
1343 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {
1344 PrintAndLog("Cant set magic card block: %d", blockNum);
1345 return 3;
1346 }
1347 }
1348 return 0;
1349 } else {
1350 len = strlen(Cmd);
1351 if (len > 14) len = 14;
1352
1353 memcpy(filename, Cmd, len);
1354 fnameptr += len;
1355
1356 sprintf(fnameptr, ".eml");
1357
1358 // open file
1359 f = fopen(filename, "r");
1360 if (f == NULL) {
1361 PrintAndLog("File not found or locked.");
1362 return 1;
1363 }
1364
1365 blockNum = 0;
1366 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1367 while(!feof(f)){
1368 memset(buf, 0, sizeof(buf));
1369 if (fgets(buf, sizeof(buf), f) == NULL) {
1370 PrintAndLog("File reading error.");
1371 return 2;
1372 }
1373
1374 if (strlen(buf) < 32){
1375 if(strlen(buf) && feof(f))
1376 break;
1377 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1378 return 2;
1379 }
1380 for (i = 0; i < 32; i += 2)
1381 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
1382
1383 if (blockNum == 2) flags = 0;
1384 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1385
1386 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {
1387 PrintAndLog("Cant set magic card block: %d", blockNum);
1388 return 3;
1389 }
1390 blockNum++;
1391
1392 if (blockNum >= 16 * 4) break; // magic card type - mifare 1K
1393 }
1394 fclose(f);
1395
1396 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
1397 PrintAndLog("File content error. There must be 64 blocks");
1398 return 4;
1399 }
1400 PrintAndLog("Loaded from file: %s", filename);
1401 return 0;
1402 }
1403 }
1404
1405 int CmdHF14AMfCGetBlk(const char *Cmd) {
1406 uint8_t memBlock[16];
1407 uint8_t blockNo = 0;
1408 int res;
1409 memset(memBlock, 0x00, sizeof(memBlock));
1410
1411 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1412 PrintAndLog("Usage: hf mf cgetblk <block number>");
1413 PrintAndLog("sample: hf mf cgetblk 1");
1414 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");
1415 return 0;
1416 }
1417
1418 blockNo = param_get8(Cmd, 0);
1419
1420 PrintAndLog("--block number:%02x ", blockNo);
1421
1422 res = mfCGetBlock(blockNo, memBlock, CSETBLOCK_SINGLE_OPER);
1423 if (res) {
1424 PrintAndLog("Can't read block. error=%d", res);
1425 return 1;
1426 }
1427
1428 PrintAndLog("block data:%s", sprint_hex(memBlock, 16));
1429 return 0;
1430 }
1431
1432 int CmdHF14AMfCGetSc(const char *Cmd) {
1433 uint8_t memBlock[16];
1434 uint8_t sectorNo = 0;
1435 int i, res, flags;
1436 memset(memBlock, 0x00, sizeof(memBlock));
1437
1438 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1439 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1440 PrintAndLog("sample: hf mf cgetsc 0");
1441 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");
1442 return 0;
1443 }
1444
1445 sectorNo = param_get8(Cmd, 0);
1446 if (sectorNo > 15) {
1447 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1448 return 1;
1449 }
1450
1451 PrintAndLog("--sector number:%02x ", sectorNo);
1452
1453 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1454 for (i = 0; i < 4; i++) {
1455 if (i == 1) flags = 0;
1456 if (i == 3) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1457
1458 res = mfCGetBlock(sectorNo * 4 + i, memBlock, flags);
1459 if (res) {
1460 PrintAndLog("Can't read block. %02x error=%d", sectorNo * 4 + i, res);
1461 return 1;
1462 }
1463
1464 PrintAndLog("block %02x data:%s", sectorNo * 4 + i, sprint_hex(memBlock, 16));
1465 }
1466 return 0;
1467 }
1468
1469 int CmdHF14AMfCSave(const char *Cmd) {
1470
1471 FILE * f;
1472 char filename[20];
1473 char * fnameptr = filename;
1474 uint8_t fillFromEmulator = 0;
1475 uint8_t buf[64];
1476 int i, j, len, flags;
1477
1478 memset(filename, 0, sizeof(filename));
1479 memset(buf, 0, sizeof(buf));
1480
1481 if (param_getchar(Cmd, 0) == 'h') {
1482 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1483 PrintAndLog("or into emulator memory (option `e`)");
1484 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1485 PrintAndLog(" sample: hf mf esave ");
1486 PrintAndLog(" hf mf esave filename");
1487 PrintAndLog(" hf mf esave e \n");
1488 return 0;
1489 }
1490
1491 char ctmp = param_getchar(Cmd, 0);
1492 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
1493
1494 if (fillFromEmulator) {
1495 // put into emulator
1496 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1497 for (i = 0; i < 16 * 4; i++) {
1498 if (i == 1) flags = 0;
1499 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1500
1501 if (mfCGetBlock(i, buf, flags)) {
1502 PrintAndLog("Cant get block: %d", i);
1503 break;
1504 }
1505
1506 if (mfEmlSetMem(buf, i, 1)) {
1507 PrintAndLog("Cant set emul block: %d", i);
1508 return 3;
1509 }
1510 }
1511 return 0;
1512 } else {
1513 len = strlen(Cmd);
1514 if (len > 14) len = 14;
1515
1516 if (len < 1) {
1517 // get filename
1518 if (mfCGetBlock(0, buf, CSETBLOCK_SINGLE_OPER)) {
1519 PrintAndLog("Cant get block: %d", 0);
1520 return 1;
1521 }
1522 for (j = 0; j < 7; j++, fnameptr += 2)
1523 sprintf(fnameptr, "%02x", buf[j]);
1524 } else {
1525 memcpy(filename, Cmd, len);
1526 fnameptr += len;
1527 }
1528
1529 sprintf(fnameptr, ".eml");
1530
1531 // open file
1532 f = fopen(filename, "w+");
1533
1534 // put hex
1535 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1536 for (i = 0; i < 16 * 4; i++) {
1537 if (i == 1) flags = 0;
1538 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1539
1540 if (mfCGetBlock(i, buf, flags)) {
1541 PrintAndLog("Cant get block: %d", i);
1542 break;
1543 }
1544 for (j = 0; j < 16; j++)
1545 fprintf(f, "%02x", buf[j]);
1546 fprintf(f,"\n");
1547 }
1548 fclose(f);
1549
1550 PrintAndLog("Saved to file: %s", filename);
1551
1552 return 0;
1553 }
1554 }
1555
1556 int CmdHF14AMfSniff(const char *Cmd){
1557 // params
1558 bool wantLogToFile = 0;
1559 bool wantDecrypt = 0;
1560 //bool wantSaveToEml = 0; TODO
1561 bool wantSaveToEmlFile = 0;
1562
1563 //var
1564 int res = 0;
1565 int len = 0;
1566 int blockLen = 0;
1567 int num = 0;
1568 int pckNum = 0;
1569 uint8_t uid[8];
1570 uint8_t atqa[2];
1571 uint8_t sak;
1572 bool isTag;
1573 uint32_t parity;
1574 uint8_t buf[3000];
1575 uint8_t * bufPtr = buf;
1576 memset(buf, 0x00, 3000);
1577
1578 if (param_getchar(Cmd, 0) == 'h') {
1579 PrintAndLog("It continuously get data from the field and saves it to: log, emulator, emulator file.");
1580 PrintAndLog("You can specify:");
1581 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1582 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1583 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1584 PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1585 PrintAndLog("Usage: hf mf sniff [l][d][e][r]");
1586 PrintAndLog(" sample: hf mf sniff l d e");
1587 return 0;
1588 }
1589
1590 for (int i = 0; i < 4; i++) {
1591 char ctmp = param_getchar(Cmd, i);
1592 if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;
1593 if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;
1594 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1595 if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;
1596 }
1597
1598 printf("-------------------------------------------------------------------------\n");
1599 printf("Executing command. \n");
1600 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1601 printf("Press the key on pc keyboard to abort the client.\n");
1602 printf("-------------------------------------------------------------------------\n");
1603
1604 UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};
1605 SendCommand(&c);
1606
1607 // wait cycle
1608 while (true) {
1609 printf(".");
1610 fflush(stdout);
1611 if (ukbhit()) {
1612 getchar();
1613 printf("\naborted via keyboard!\n");
1614 break;
1615 }
1616
1617 UsbCommand resp;
1618 if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {
1619 res = resp.arg[0] & 0xff;
1620 len = resp.arg[1];
1621 num = resp.arg[2];
1622
1623 if (res == 0) return 0;
1624 if (res == 1) {
1625 if (num ==0) {
1626 bufPtr = buf;
1627 memset(buf, 0x00, 3000);
1628 }
1629 memcpy(bufPtr, resp.d.asBytes, len);
1630 bufPtr += len;
1631 pckNum++;
1632 }
1633 if (res == 2) {
1634 blockLen = bufPtr - buf;
1635 bufPtr = buf;
1636 printf(">\n");
1637 PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);
1638 num = 0;
1639 while (bufPtr - buf + 9 < blockLen) {
1640 isTag = bufPtr[3] & 0x80 ? true:false;
1641 bufPtr += 4;
1642 parity = *((uint32_t *)(bufPtr));
1643 bufPtr += 4;
1644 len = bufPtr[0];
1645 bufPtr++;
1646 if ((len == 14) && (bufPtr[0] = 0xff) && (bufPtr[1] = 0xff)) {
1647 memcpy(uid, bufPtr + 2, 7);
1648 memcpy(atqa, bufPtr + 2 + 7, 2);
1649 sak = bufPtr[11];
1650
1651 PrintAndLog("tag select uid:%s atqa:%02x %02x sak:0x%02x", sprint_hex(uid, 7), atqa[0], atqa[1], sak);
1652 if (wantLogToFile) {
1653 FillFileNameByUID(logHexFileName, uid, ".log", 7);
1654 AddLogCurrentDT(logHexFileName);
1655 }
1656 if (wantDecrypt) mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);
1657 } else {
1658 PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));
1659 if (wantLogToFile) AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);
1660 if (wantDecrypt) mfTraceDecode(bufPtr, len, parity, wantSaveToEmlFile);
1661 }
1662 bufPtr += len;
1663 num++;
1664 }
1665 }
1666 } // resp not NILL
1667 } // while (true)
1668 return 0;
1669 }
1670
1671 static command_t CommandTable[] =
1672 {
1673 {"help", CmdHelp, 1, "This help"},
1674 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},
1675 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},
1676 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},
1677 {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},
1678 {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},
1679 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},
1680 {"chk", CmdHF14AMfChk, 0, "Test block keys"},
1681 {"mifare", CmdHF14AMifare, 0, "Read parity error messages. param - <used card nonce>"},
1682 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},
1683 {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},
1684 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},
1685 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},
1686 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},
1687 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},
1688 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},
1689 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},
1690 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},
1691 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},
1692 {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},
1693 {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block into magic Chinese card"},
1694 {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block from magic Chinese card"},
1695 {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector from magic Chinese card"},
1696 {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},
1697 {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},
1698 {NULL, NULL, 0, NULL}
1699 };
1700
1701 int CmdHFMF(const char *Cmd)
1702 {
1703 // flush
1704 WaitForResponseTimeout(CMD_ACK,NULL,100);
1705
1706 CmdsParse(CommandTable, Cmd);
1707 return 0;
1708 }
1709
1710 int CmdHelp(const char *Cmd)
1711 {
1712 CmdsHelp(CommandTable);
1713 return 0;
1714 }
Impressum, Datenschutz