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