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