]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
e41afb6ad2544cfcdc7a56fc4fdfbcc37f0cbbf7
[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 #include "nonce2key/nonce2key.h"
13
14 static int CmdHelp(const char *Cmd);
15
16 int CmdHF14AMifare(const char *Cmd)
17 {
18 uint32_t uid = 0;
19 uint32_t nt = 0, nr = 0;
20 uint64_t par_list = 0, ks_list = 0, r_key = 0;
21 int16_t isOK = 0;
22
23 UsbCommand c = {CMD_READER_MIFARE, {true, 0, 0}};
24
25 // message
26 printf("-------------------------------------------------------------------------\n");
27 printf("Executing command. Expected execution time: 25sec on average :-)\n");
28 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
29 printf("-------------------------------------------------------------------------\n");
30
31
32 start:
33 clearCommandBuffer();
34 SendCommand(&c);
35
36 //flush queue
37 while (ukbhit()) getchar();
38
39 // wait cycle
40 while (true) {
41 printf(".");
42 fflush(stdout);
43 if (ukbhit()) {
44 getchar();
45 printf("\naborted via keyboard!\n");
46 break;
47 }
48
49 UsbCommand resp;
50 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
51 isOK = resp.arg[0];
52 uid = (uint32_t)bytes_to_num(resp.d.asBytes + 0, 4);
53 nt = (uint32_t)bytes_to_num(resp.d.asBytes + 4, 4);
54 par_list = bytes_to_num(resp.d.asBytes + 8, 8);
55 ks_list = bytes_to_num(resp.d.asBytes + 16, 8);
56 nr = bytes_to_num(resp.d.asBytes + 24, 4);
57 printf("\n\n");
58 switch (isOK) {
59 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
60 case -2 : PrintAndLog("Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
61 case -3 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
62 case -4 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");
63 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;
64 default: ;
65 }
66 break;
67 }
68 }
69
70 printf("\n");
71
72 // error
73 if (isOK != 1) return 1;
74
75 // execute original function from util nonce2key
76 if (nonce2key(uid, nt, nr, par_list, ks_list, &r_key)) {
77 isOK = 2;
78 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt);
79 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
80 c.arg[0] = false;
81 goto start;
82 } else {
83 isOK = 0;
84 printf("------------------------------------------------------------------\n");
85 PrintAndLog("Found valid key: %012"llx" \n", r_key);
86 }
87
88 PrintAndLog("");
89 return 0;
90 }
91
92 int CmdHF14AMfWrBl(const char *Cmd)
93 {
94 uint8_t blockNo = 0;
95 uint8_t keyType = 0;
96 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
97 uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
98
99 char cmdp = 0x00;
100
101 if (strlen(Cmd)<3) {
102 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
103 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
104 return 0;
105 }
106
107 blockNo = param_get8(Cmd, 0);
108 cmdp = param_getchar(Cmd, 1);
109 if (cmdp == 0x00) {
110 PrintAndLog("Key type must be A or B");
111 return 1;
112 }
113 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
114 if (param_gethex(Cmd, 2, key, 12)) {
115 PrintAndLog("Key must include 12 HEX symbols");
116 return 1;
117 }
118 if (param_gethex(Cmd, 3, bldata, 32)) {
119 PrintAndLog("Block data must include 32 HEX symbols");
120 return 1;
121 }
122 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo, keyType?'B':'A', sprint_hex(key, 6));
123 PrintAndLog("--data: %s", sprint_hex(bldata, 16));
124
125 UsbCommand c = {CMD_MIFARE_WRITEBL, {blockNo, keyType, 0}};
126 memcpy(c.d.asBytes, key, 6);
127 memcpy(c.d.asBytes + 10, bldata, 16);
128 clearCommandBuffer();
129 SendCommand(&c);
130
131 UsbCommand resp;
132 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
133 uint8_t isOK = resp.arg[0] & 0xff;
134 PrintAndLog("isOk:%02x", isOK);
135 } else {
136 PrintAndLog("Command execute timeout");
137 }
138
139 return 0;
140 }
141
142 int CmdHF14AMfRdBl(const char *Cmd)
143 {
144 uint8_t blockNo = 0;
145 uint8_t keyType = 0;
146 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
147
148 char cmdp = 0x00;
149
150
151 if (strlen(Cmd)<3) {
152 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
153 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
154 return 0;
155 }
156
157 blockNo = param_get8(Cmd, 0);
158 cmdp = param_getchar(Cmd, 1);
159 if (cmdp == 0x00) {
160 PrintAndLog("Key type must be A or B");
161 return 1;
162 }
163 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
164 if (param_gethex(Cmd, 2, key, 12)) {
165 PrintAndLog("Key must include 12 HEX symbols");
166 return 1;
167 }
168 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo, keyType?'B':'A', sprint_hex(key, 6));
169
170 UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};
171 memcpy(c.d.asBytes, key, 6);
172 clearCommandBuffer();
173 SendCommand(&c);
174
175 UsbCommand resp;
176 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
177 uint8_t isOK = resp.arg[0] & 0xff;
178 uint8_t *data = resp.d.asBytes;
179
180 if (isOK)
181 PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 16));
182 else
183 PrintAndLog("isOk:%02x", isOK);
184 } else {
185 PrintAndLog("Command execute timeout");
186 }
187
188 return 0;
189 }
190
191 int CmdHF14AMfRdSc(const char *Cmd)
192 {
193 int i;
194 uint8_t sectorNo = 0;
195 uint8_t keyType = 0;
196 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
197 uint8_t isOK = 0;
198 uint8_t *data = NULL;
199 char cmdp = 0x00;
200
201 if (strlen(Cmd)<3) {
202 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
203 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
204 return 0;
205 }
206
207 sectorNo = param_get8(Cmd, 0);
208 if (sectorNo > 39) {
209 PrintAndLog("Sector number must be less than 40");
210 return 1;
211 }
212 cmdp = param_getchar(Cmd, 1);
213 if (cmdp != 'a' && cmdp != 'A' && cmdp != 'b' && cmdp != 'B') {
214 PrintAndLog("Key type must be A or B");
215 return 1;
216 }
217 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
218 if (param_gethex(Cmd, 2, key, 12)) {
219 PrintAndLog("Key must include 12 HEX symbols");
220 return 1;
221 }
222 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo, keyType?'B':'A', sprint_hex(key, 6));
223
224 UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};
225 memcpy(c.d.asBytes, key, 6);
226 clearCommandBuffer();
227 SendCommand(&c);
228 PrintAndLog(" ");
229
230 UsbCommand resp;
231 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
232 isOK = resp.arg[0] & 0xff;
233 data = resp.d.asBytes;
234
235 PrintAndLog("isOk:%02x", isOK);
236 if (isOK) {
237 for (i = 0; i < (sectorNo<32?3:15); i++) {
238 PrintAndLog("data : %s", sprint_hex(data + i * 16, 16));
239 }
240 PrintAndLog("trailer: %s", sprint_hex(data + (sectorNo<32?3:15) * 16, 16));
241 }
242 } else {
243 PrintAndLog("Command execute timeout");
244 }
245
246 return 0;
247 }
248
249 uint8_t FirstBlockOfSector(uint8_t sectorNo)
250 {
251 if (sectorNo < 32) {
252 return sectorNo * 4;
253 } else {
254 return 32 * 4 + (sectorNo - 32) * 16;
255 }
256 }
257
258 uint8_t NumBlocksPerSector(uint8_t sectorNo)
259 {
260 if (sectorNo < 32) {
261 return 4;
262 } else {
263 return 16;
264 }
265 }
266
267 int CmdHF14AMfDump(const char *Cmd)
268 {
269 uint8_t sectorNo, blockNo;
270
271 uint8_t keyA[40][6];
272 uint8_t keyB[40][6];
273 uint8_t rights[40][4];
274 uint8_t carddata[256][16];
275 uint8_t numSectors = 16;
276
277 FILE *fin;
278 FILE *fout;
279
280 UsbCommand resp;
281
282 char cmdp = param_getchar(Cmd, 0);
283 switch (cmdp) {
284 case '0' : numSectors = 5; break;
285 case '1' :
286 case '\0': numSectors = 16; break;
287 case '2' : numSectors = 32; break;
288 case '4' : numSectors = 40; break;
289 default: numSectors = 16;
290 }
291
292 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {
293 PrintAndLog("Usage: hf mf dump [card memory]");
294 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
295 PrintAndLog("");
296 PrintAndLog("Samples: hf mf dump");
297 PrintAndLog(" hf mf dump 4");
298 return 0;
299 }
300
301 if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {
302 PrintAndLog("Could not find file dumpkeys.bin");
303 return 1;
304 }
305
306 // Read keys A from file
307 for (sectorNo=0; sectorNo<numSectors; sectorNo++) {
308 if (fread( keyA[sectorNo], 1, 6, fin ) == 0) {
309 PrintAndLog("File reading error.");
310 fclose(fin);
311 return 2;
312 }
313 }
314
315 // Read keys B from file
316 for (sectorNo=0; sectorNo<numSectors; sectorNo++) {
317 if (fread( keyB[sectorNo], 1, 6, fin ) == 0) {
318 PrintAndLog("File reading error.");
319 fclose(fin);
320 return 2;
321 }
322 }
323
324 fclose(fin);
325
326 PrintAndLog("|-----------------------------------------|");
327 PrintAndLog("|------ Reading sector access bits...-----|");
328 PrintAndLog("|-----------------------------------------|");
329
330 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
331 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 0, 0}};
332 memcpy(c.d.asBytes, keyA[sectorNo], 6);
333 clearCommandBuffer();
334 SendCommand(&c);
335
336 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
337 uint8_t isOK = resp.arg[0] & 0xff;
338 uint8_t *data = resp.d.asBytes;
339 if (isOK){
340 rights[sectorNo][0] = ((data[7] & 0x10)>>2) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>4); // C1C2C3 for data area 0
341 rights[sectorNo][1] = ((data[7] & 0x20)>>3) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>5); // C1C2C3 for data area 1
342 rights[sectorNo][2] = ((data[7] & 0x40)>>4) | ((data[8] & 0x4)>>1) | ((data[8] & 0x40)>>6); // C1C2C3 for data area 2
343 rights[sectorNo][3] = ((data[7] & 0x80)>>5) | ((data[8] & 0x8)>>2) | ((data[8] & 0x80)>>7); // C1C2C3 for sector trailer
344 } else {
345 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo);
346 rights[sectorNo][0] = rights[sectorNo][1] = rights[sectorNo][2] = 0x00;
347 rights[sectorNo][3] = 0x01;
348 }
349 } else {
350 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo);
351 rights[sectorNo][0] = rights[sectorNo][1] = rights[sectorNo][2] = 0x00;
352 rights[sectorNo][3] = 0x01;
353 }
354 }
355
356 PrintAndLog("|-----------------------------------------|");
357 PrintAndLog("|----- Dumping all blocks to file... -----|");
358 PrintAndLog("|-----------------------------------------|");
359
360 bool isOK = true;
361 for (sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {
362 for (blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
363 bool received = false;
364
365 if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
366 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 0, 0}};
367 memcpy(c.d.asBytes, keyA[sectorNo], 6);
368 clearCommandBuffer();
369 SendCommand(&c);
370 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);
371 } else { // data block. Check if it can be read with key A or key B
372 uint8_t data_area = sectorNo<32?blockNo:blockNo/5;
373 if ((rights[sectorNo][data_area] == 0x03) || (rights[sectorNo][data_area] == 0x05)) { // only key B would work
374 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 1, 0}};
375 memcpy(c.d.asBytes, keyB[sectorNo], 6);
376 SendCommand(&c);
377 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);
378 } else if (rights[sectorNo][data_area] == 0x07) { // no key would work
379 isOK = false;
380 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo, blockNo);
381 } else { // key A would work
382 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 0, 0}};
383 memcpy(c.d.asBytes, keyA[sectorNo], 6);
384 clearCommandBuffer();
385 SendCommand(&c);
386 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);
387 }
388 }
389
390 if (received) {
391 isOK = resp.arg[0] & 0xff;
392 uint8_t *data = resp.d.asBytes;
393 if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer. Fill in the keys.
394 data[0] = (keyA[sectorNo][0]);
395 data[1] = (keyA[sectorNo][1]);
396 data[2] = (keyA[sectorNo][2]);
397 data[3] = (keyA[sectorNo][3]);
398 data[4] = (keyA[sectorNo][4]);
399 data[5] = (keyA[sectorNo][5]);
400 data[10] = (keyB[sectorNo][0]);
401 data[11] = (keyB[sectorNo][1]);
402 data[12] = (keyB[sectorNo][2]);
403 data[13] = (keyB[sectorNo][3]);
404 data[14] = (keyB[sectorNo][4]);
405 data[15] = (keyB[sectorNo][5]);
406 }
407 if (isOK) {
408 memcpy(carddata[FirstBlockOfSector(sectorNo) + blockNo], data, 16);
409 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo, sectorNo);
410 } else {
411 PrintAndLog("Could not read block %2d of sector %2d", blockNo, sectorNo);
412 break;
413 }
414 }
415 else {
416 isOK = false;
417 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo, sectorNo);
418 break;
419 }
420 }
421 }
422
423 if (isOK) {
424 if ((fout = fopen("dumpdata.bin","wb")) == NULL) {
425 PrintAndLog("Could not create file name dumpdata.bin");
426 return 1;
427 }
428 uint16_t numblocks = FirstBlockOfSector(numSectors - 1) + NumBlocksPerSector(numSectors - 1);
429 fwrite(carddata, 1, 16*numblocks, fout);
430 fclose(fout);
431 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks, 16*numblocks);
432 }
433
434 return 0;
435 }
436
437 int CmdHF14AMfRestore(const char *Cmd)
438 {
439 uint8_t sectorNo,blockNo;
440 uint8_t keyType = 0;
441 uint8_t key[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
442 uint8_t bldata[16] = {0x00};
443 uint8_t keyA[40][6];
444 uint8_t keyB[40][6];
445 uint8_t numSectors;
446
447 FILE *fdump;
448 FILE *fkeys;
449
450 char cmdp = param_getchar(Cmd, 0);
451 switch (cmdp) {
452 case '0' : numSectors = 5; break;
453 case '1' :
454 case '\0': numSectors = 16; break;
455 case '2' : numSectors = 32; break;
456 case '4' : numSectors = 40; break;
457 default: numSectors = 16;
458 }
459
460 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {
461 PrintAndLog("Usage: hf mf restore [card memory]");
462 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
463 PrintAndLog("");
464 PrintAndLog("Samples: hf mf restore");
465 PrintAndLog(" hf mf restore 4");
466 return 0;
467 }
468
469 if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) {
470 PrintAndLog("Could not find file dumpkeys.bin");
471 return 1;
472 }
473
474 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
475 if (fread(keyA[sectorNo], 1, 6, fkeys) == 0) {
476 PrintAndLog("File reading error (dumpkeys.bin).");
477 fclose(fkeys);
478 return 2;
479 }
480 }
481
482 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
483 if (fread(keyB[sectorNo], 1, 6, fkeys) == 0) {
484 PrintAndLog("File reading error (dumpkeys.bin).");
485 fclose(fkeys);
486 return 2;
487 }
488 }
489
490 fclose(fkeys);
491
492 if ((fdump = fopen("dumpdata.bin","rb")) == NULL) {
493 PrintAndLog("Could not find file dumpdata.bin");
494 return 1;
495 }
496 PrintAndLog("Restoring dumpdata.bin to card");
497
498 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
499 for(blockNo = 0; blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
500 UsbCommand c = {CMD_MIFARE_WRITEBL, {FirstBlockOfSector(sectorNo) + blockNo, keyType, 0}};
501 memcpy(c.d.asBytes, key, 6);
502
503 if (fread(bldata, 1, 16, fdump) == 0) {
504 PrintAndLog("File reading error (dumpdata.bin).");
505 fclose(fdump);
506 return 2;
507 }
508
509 if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer
510 bldata[0] = (keyA[sectorNo][0]);
511 bldata[1] = (keyA[sectorNo][1]);
512 bldata[2] = (keyA[sectorNo][2]);
513 bldata[3] = (keyA[sectorNo][3]);
514 bldata[4] = (keyA[sectorNo][4]);
515 bldata[5] = (keyA[sectorNo][5]);
516 bldata[10] = (keyB[sectorNo][0]);
517 bldata[11] = (keyB[sectorNo][1]);
518 bldata[12] = (keyB[sectorNo][2]);
519 bldata[13] = (keyB[sectorNo][3]);
520 bldata[14] = (keyB[sectorNo][4]);
521 bldata[15] = (keyB[sectorNo][5]);
522 }
523
524 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo) + blockNo, sprint_hex(bldata, 16));
525
526 memcpy(c.d.asBytes + 10, bldata, 16);
527 clearCommandBuffer();
528 SendCommand(&c);
529
530 UsbCommand resp;
531 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
532 uint8_t isOK = resp.arg[0] & 0xff;
533 PrintAndLog("isOk:%02x", isOK);
534 } else {
535 PrintAndLog("Command execute timeout");
536 }
537 }
538 }
539
540 fclose(fdump);
541 return 0;
542 }
543
544 int CmdHF14AMfNested(const char *Cmd)
545 {
546 int i, j, res, iterations;
547 sector *e_sector = NULL;
548 uint8_t blockNo = 0;
549 uint8_t keyType = 0;
550 uint8_t trgBlockNo = 0;
551 uint8_t trgKeyType = 0;
552 uint8_t SectorsCnt = 0;
553 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
554 uint8_t keyBlock[14*6];
555 uint64_t key64 = 0;
556 bool transferToEml = false;
557
558 bool createDumpFile = false;
559 FILE *fkeys;
560 uint8_t standart[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
561 uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
562
563 char cmdp, ctmp;
564
565 if (strlen(Cmd)<3) {
566 PrintAndLog("Usage:");
567 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
568 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
569 PrintAndLog(" <target block number> <target key A/B> [t]");
570 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
571 PrintAndLog("t - transfer keys into emulator memory");
572 PrintAndLog("d - write keys to binary file");
573 PrintAndLog(" ");
574 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
575 PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");
576 PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");
577 PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
578 return 0;
579 }
580
581 cmdp = param_getchar(Cmd, 0);
582 blockNo = param_get8(Cmd, 1);
583 ctmp = param_getchar(Cmd, 2);
584
585 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
586 PrintAndLog("Key type must be A or B");
587 return 1;
588 }
589
590 if (ctmp != 'A' && ctmp != 'a')
591 keyType = 1;
592
593 if (param_gethex(Cmd, 3, key, 12)) {
594 PrintAndLog("Key must include 12 HEX symbols");
595 return 1;
596 }
597
598 if (cmdp == 'o' || cmdp == 'O') {
599 cmdp = 'o';
600 trgBlockNo = param_get8(Cmd, 4);
601 ctmp = param_getchar(Cmd, 5);
602 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
603 PrintAndLog("Target key type must be A or B");
604 return 1;
605 }
606 if (ctmp != 'A' && ctmp != 'a')
607 trgKeyType = 1;
608 } else {
609
610 switch (cmdp) {
611 case '0': SectorsCnt = 05; break;
612 case '1': SectorsCnt = 16; break;
613 case '2': SectorsCnt = 32; break;
614 case '4': SectorsCnt = 40; break;
615 default: SectorsCnt = 16;
616 }
617 }
618
619 ctmp = param_getchar(Cmd, 4);
620 if (ctmp == 't' || ctmp == 'T') transferToEml = true;
621 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = true;
622
623 ctmp = param_getchar(Cmd, 6);
624 transferToEml |= (ctmp == 't' || ctmp == 'T');
625 transferToEml |= (ctmp == 'd' || ctmp == 'D');
626
627 if (cmdp == 'o') {
628 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo, trgKeyType?'B':'A');
629 int16_t isOK = mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, true);
630 if (isOK) {
631 switch (isOK) {
632 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
633 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
634 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
635 default : PrintAndLog("Unknown Error.\n");
636 }
637 return 2;
638 }
639 key64 = bytes_to_num(keyBlock, 6);
640 if (key64) {
641 PrintAndLog("Found valid key:%012"llx, key64);
642
643 // transfer key to the emulator
644 if (transferToEml) {
645 uint8_t sectortrailer;
646 if (trgBlockNo < 32*4) { // 4 block sector
647 sectortrailer = (trgBlockNo & 0x03) + 3;
648 } else { // 16 block sector
649 sectortrailer = (trgBlockNo & 0x0f) + 15;
650 }
651 mfEmlGetMem(keyBlock, sectortrailer, 1);
652
653 if (!trgKeyType)
654 num_to_bytes(key64, 6, keyBlock);
655 else
656 num_to_bytes(key64, 6, &keyBlock[10]);
657 mfEmlSetMem(keyBlock, sectortrailer, 1);
658 }
659 } else {
660 PrintAndLog("No valid key found");
661 }
662 }
663 else { // ------------------------------------ multiple sectors working
664 clock_t time1;
665 time1 = clock();
666
667 e_sector = calloc(SectorsCnt, sizeof(sector));
668 if (e_sector == NULL) return 1;
669
670 //test current key and additional standard keys first
671 memcpy(keyBlock, key, 6);
672 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 1 * 6));
673 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 2 * 6));
674 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 3 * 6));
675 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 4 * 6));
676 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));
677 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock + 6 * 6));
678 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock + 7 * 6));
679 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock + 8 * 6));
680 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock + 9 * 6));
681 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock + 10 * 6));
682 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock + 11 * 6));
683 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock + 12 * 6));
684 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock + 13 * 6));
685
686 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);
687 for (i = 0; i < SectorsCnt; i++) {
688 for (j = 0; j < 2; j++) {
689 if (e_sector[i].foundKey[j]) continue;
690
691 res = mfCheckKeys(FirstBlockOfSector(i), j, true, 6, keyBlock, &key64);
692
693 if (!res) {
694 e_sector[i].Key[j] = key64;
695 e_sector[i].foundKey[j] = 1;
696 }
697 }
698 }
699
700 // nested sectors
701 iterations = 0;
702 PrintAndLog("nested...");
703 bool calibrate = true;
704 for (i = 0; i < NESTED_SECTOR_RETRY; i++) {
705 for (uint8_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {
706 for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) {
707 if (e_sector[sectorNo].foundKey[trgKeyType]) continue;
708 PrintAndLog("-----------------------------------------------");
709 int16_t isOK = mfnested(blockNo, keyType, key, FirstBlockOfSector(sectorNo), trgKeyType, keyBlock, calibrate);
710 if(isOK) {
711 switch (isOK) {
712 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
713 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
714 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;
715 default : PrintAndLog("Unknown Error.\n");
716 }
717 free(e_sector);
718 return 2;
719 } else {
720 calibrate = false;
721 }
722
723 iterations++;
724
725 key64 = bytes_to_num(keyBlock, 6);
726 if (key64) {
727 PrintAndLog("Found valid key:%012"llx, key64);
728 e_sector[sectorNo].foundKey[trgKeyType] = 1;
729 e_sector[sectorNo].Key[trgKeyType] = key64;
730 }
731 }
732 }
733 }
734
735 printf("Time in nested: %1.3f (%1.3f sec per key)\n\n", ((float)clock() - time1)/CLOCKS_PER_SEC, ((float)clock() - time1)/iterations/CLOCKS_PER_SEC);
736
737 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations);
738 //print them
739 PrintAndLog("|---|----------------|---|----------------|---|");
740 PrintAndLog("|sec|key A |res|key B |res|");
741 PrintAndLog("|---|----------------|---|----------------|---|");
742 for (i = 0; i < SectorsCnt; i++) {
743 PrintAndLog("|%03d| %012"llx" | %d | %012"llx" | %d |", i,
744 e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);
745 }
746 PrintAndLog("|---|----------------|---|----------------|---|");
747
748 // transfer them to the emulator
749 if (transferToEml) {
750 for (i = 0; i < SectorsCnt; i++) {
751 mfEmlGetMem(keyBlock, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);
752 if (e_sector[i].foundKey[0])
753 num_to_bytes(e_sector[i].Key[0], 6, keyBlock);
754 if (e_sector[i].foundKey[1])
755 num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);
756 mfEmlSetMem(keyBlock, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);
757 }
758 }
759
760 // Create dump file
761 if (createDumpFile) {
762 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
763 PrintAndLog("Could not create file dumpkeys.bin");
764 free(e_sector);
765 return 1;
766 }
767 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
768 for(i=0; i<SectorsCnt; i++) {
769 if (e_sector[i].foundKey[0]){
770 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
771 fwrite ( tempkey, 1, 6, fkeys );
772 }
773 else{
774 fwrite ( &standart, 1, 6, fkeys );
775 }
776 }
777 for(i=0; i<SectorsCnt; i++) {
778 if (e_sector[i].foundKey[1]){
779 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
780 fwrite ( tempkey, 1, 6, fkeys );
781 }
782 else{
783 fwrite ( &standart, 1, 6, fkeys );
784 }
785 }
786 fclose(fkeys);
787 }
788
789 free(e_sector);
790 }
791 return 0;
792 }
793
794 int CmdHF14AMfChk(const char *Cmd)
795 {
796 if (strlen(Cmd)<3) {
797 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
798 PrintAndLog(" * - all sectors");
799 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
800 PrintAndLog("d - write keys to binary file");
801 PrintAndLog("t - write keys to emulator memory\n");
802 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
803 PrintAndLog(" hf mf chk *1 ? t");
804 PrintAndLog(" hf mf chk *1 ? d");
805 return 0;
806 }
807
808 FILE * f;
809 char filename[FILE_PATH_SIZE]={0};
810 char buf[13];
811 uint8_t *keyBlock = NULL, *p;
812 uint8_t stKeyBlock = 20;
813
814 int i, res;
815 int keycnt = 0;
816 char ctmp = 0x00;
817 uint8_t blockNo = 0;
818 uint8_t SectorsCnt = 1;
819 uint8_t keyType = 0;
820 uint64_t key64 = 0;
821
822 int transferToEml = 0;
823 int createDumpFile = 0;
824
825 keyBlock = calloc(stKeyBlock, 6);
826 if (keyBlock == NULL) return 1;
827
828 uint64_t defaultKeys[] =
829 {
830 0xffffffffffff, // Default key (first key used by program if no user defined key)
831 0x000000000000, // Blank key
832 0xa0a1a2a3a4a5, // NFCForum MAD key
833 0xb0b1b2b3b4b5,
834 0xaabbccddeeff,
835 0x4d3a99c351dd,
836 0x1a982c7e459a,
837 0xd3f7d3f7d3f7,
838 0x714c5c886e97,
839 0x587ee5f9350f,
840 0xa0478cc39091,
841 0x533cb6c723f6,
842 0x8fd0a4f256e9
843 };
844 int defaultKeysSize = sizeof(defaultKeys) / sizeof(uint64_t);
845
846 for (int defaultKeyCounter = 0; defaultKeyCounter < defaultKeysSize; defaultKeyCounter++)
847 {
848 num_to_bytes(defaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));
849 }
850
851 if (param_getchar(Cmd, 0)=='*') {
852 blockNo = 3;
853 switch(param_getchar(Cmd+1, 0)) {
854 case '0': SectorsCnt = 5; break;
855 case '1': SectorsCnt = 16; break;
856 case '2': SectorsCnt = 32; break;
857 case '4': SectorsCnt = 40; break;
858 default: SectorsCnt = 16;
859 }
860 }
861 else
862 blockNo = param_get8(Cmd, 0);
863
864 ctmp = param_getchar(Cmd, 1);
865 switch (ctmp) {
866 case 'a': case 'A':
867 keyType = !0;
868 break;
869 case 'b': case 'B':
870 keyType = !1;
871 break;
872 case '?':
873 keyType = 2;
874 break;
875 default:
876 PrintAndLog("Key type must be A , B or ?");
877 return 1;
878 };
879
880 ctmp = param_getchar(Cmd, 2);
881 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;
882 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;
883
884 for (i = transferToEml || createDumpFile; param_getchar(Cmd, 2 + i); i++) {
885 if (!param_gethex(Cmd, 2 + i, keyBlock + 6 * keycnt, 12)) {
886 if ( stKeyBlock - keycnt < 2) {
887 p = realloc(keyBlock, 6*(stKeyBlock+=10));
888 if (!p) {
889 PrintAndLog("Cannot allocate memory for Keys");
890 free(keyBlock);
891 return 2;
892 }
893 keyBlock = p;
894 }
895 PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,
896 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],
897 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);
898 keycnt++;
899 } else {
900 // May be a dic file
901 if ( param_getstr(Cmd, 2 + i,filename) >= FILE_PATH_SIZE ) {
902 PrintAndLog("File name too long");
903 free(keyBlock);
904 return 2;
905 }
906
907 if ( (f = fopen( filename , "r")) ) {
908 while( fgets(buf, sizeof(buf), f) ){
909 if (strlen(buf) < 12 || buf[11] == '\n')
910 continue;
911
912 while (fgetc(f) != '\n' && !feof(f)) ; //goto next line
913
914 if( buf[0]=='#' ) continue; //The line start with # is comment, skip
915
916 if (!isxdigit(buf[0])){
917 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);
918 continue;
919 }
920
921 buf[12] = 0;
922
923 if ( stKeyBlock - keycnt < 2) {
924 p = realloc(keyBlock, 6*(stKeyBlock+=10));
925 if (!p) {
926 PrintAndLog("Cannot allocate memory for defKeys");
927 free(keyBlock);
928 return 2;
929 }
930 keyBlock = p;
931 }
932 memset(keyBlock + 6 * keycnt, 0, 6);
933 num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);
934 PrintAndLog("chk custom key[%2d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));
935 keycnt++;
936 memset(buf, 0, sizeof(buf));
937 }
938 fclose(f);
939 } else {
940 PrintAndLog("File: %s: not found or locked.", filename);
941 free(keyBlock);
942 return 1;
943
944 }
945 }
946 }
947
948 if (keycnt == 0) {
949 PrintAndLog("No key specified, trying default keys");
950 for (;keycnt < defaultKeysSize; keycnt++)
951 PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,
952 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],
953 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);
954 }
955
956 // initialize storage for found keys
957 bool validKey[2][40];
958 uint8_t foundKey[2][40][6];
959 for (uint16_t t = 0; t < 2; t++) {
960 for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {
961 validKey[t][sectorNo] = false;
962 for (uint16_t i = 0; i < 6; i++) {
963 foundKey[t][sectorNo][i] = 0xff;
964 }
965 }
966 }
967
968 for ( int t = !keyType; t < 2; keyType==2?(t++):(t=2) ) {
969 int b=blockNo;
970 for (int i = 0; i < SectorsCnt; ++i) {
971 PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i, b, t?'B':'A', keycnt);
972 uint32_t max_keys = keycnt>USB_CMD_DATA_SIZE/6?USB_CMD_DATA_SIZE/6:keycnt;
973 for (uint32_t c = 0; c < keycnt; c+=max_keys) {
974 uint32_t size = keycnt-c>max_keys?max_keys:keycnt-c;
975 res = mfCheckKeys(b, t, true, size, &keyBlock[6*c], &key64);
976 if (res != 1) {
977 if (!res) {
978 PrintAndLog("Found valid key:[%012"llx"]",key64);
979 num_to_bytes(key64, 6, foundKey[t][i]);
980 validKey[t][i] = true;
981 }
982 } else {
983 PrintAndLog("Command execute timeout");
984 }
985 }
986 b<127?(b+=4):(b+=16);
987 }
988 }
989
990 if (transferToEml) {
991 uint8_t block[16];
992 for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {
993 if (validKey[0][sectorNo] || validKey[1][sectorNo]) {
994 mfEmlGetMem(block, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1);
995 for (uint16_t t = 0; t < 2; t++) {
996 if (validKey[t][sectorNo]) {
997 memcpy(block + t*10, foundKey[t][sectorNo], 6);
998 }
999 }
1000 mfEmlSetMem(block, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1);
1001 }
1002 }
1003 PrintAndLog("Found keys have been transferred to the emulator memory");
1004 }
1005
1006 if (createDumpFile) {
1007 FILE *fkeys = fopen("dumpkeys.bin","wb");
1008 if (fkeys == NULL) {
1009 PrintAndLog("Could not create file dumpkeys.bin");
1010 free(keyBlock);
1011 return 1;
1012 }
1013 for (uint16_t t = 0; t < 2; t++) {
1014 fwrite(foundKey[t], 1, 6*SectorsCnt, fkeys);
1015 }
1016 fclose(fkeys);
1017 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1018 }
1019
1020 free(keyBlock);
1021 PrintAndLog("");
1022 return 0;
1023 }
1024
1025 int CmdHF14AMf1kSim(const char *Cmd)
1026 {
1027 uint8_t uid[7] = {0, 0, 0, 0, 0, 0, 0};
1028 uint8_t exitAfterNReads = 0;
1029 uint8_t flags = 0;
1030
1031 uint8_t cmdp = param_getchar(Cmd, 0);
1032
1033 if (cmdp == 'h' || cmdp == 'H') {
1034 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1035 PrintAndLog(" h this help");
1036 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1037 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1038 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1039 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1040 PrintAndLog("");
1041 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");
1042 return 0;
1043 }
1044 uint8_t pnr = 0;
1045 if (param_getchar(Cmd, pnr) == 'u') {
1046 if(param_gethex(Cmd, pnr+1, uid, 8) == 0)
1047 {
1048 flags |= FLAG_4B_UID_IN_DATA; // UID from packet
1049 } else if(param_gethex(Cmd,pnr+1,uid,14) == 0) {
1050 flags |= FLAG_7B_UID_IN_DATA;// UID from packet
1051 } else {
1052 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1053 return 1;
1054 }
1055 pnr +=2;
1056 }
1057 if (param_getchar(Cmd, pnr) == 'n') {
1058 exitAfterNReads = param_get8(Cmd,pnr+1);
1059 pnr += 2;
1060 }
1061 if (param_getchar(Cmd, pnr) == 'i' ) {
1062 //Using a flag to signal interactiveness, least significant bit
1063 flags |= FLAG_INTERACTIVE;
1064 pnr++;
1065 }
1066
1067 if (param_getchar(Cmd, pnr) == 'x' ) {
1068 //Using a flag to signal interactiveness, least significant bit
1069 flags |= FLAG_NR_AR_ATTACK;
1070 }
1071 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1072 flags & FLAG_4B_UID_IN_DATA ? sprint_hex(uid,4):
1073 flags & FLAG_7B_UID_IN_DATA ? sprint_hex(uid,7): "N/A"
1074 , exitAfterNReads, flags,flags);
1075
1076
1077 UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {flags, exitAfterNReads,0}};
1078 memcpy(c.d.asBytes, uid, sizeof(uid));
1079 clearCommandBuffer();
1080 SendCommand(&c);
1081
1082 if(flags & FLAG_INTERACTIVE)
1083 {
1084 PrintAndLog("Press pm3-button to abort simulation");
1085
1086 uint8_t data[40];
1087 uint8_t key[6];
1088
1089 UsbCommand resp;
1090 while(!ukbhit() ){
1091 if ( WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {
1092 if ( (resp.arg[0] & 0xffff) == CMD_SIMULATE_MIFARE_CARD ){
1093 memset(data, 0x00, sizeof(data));
1094 memset(key, 0x00, sizeof(key));
1095 int len = (resp.arg[1] > sizeof(data)) ? sizeof(data) : resp.arg[1];
1096
1097 memcpy(data, resp.d.asBytes, len);
1098
1099 uint64_t corr_uid = 0;
1100 if ( memcmp(data, "\x00\x00\x00\x00", 4) == 0 ) {
1101 corr_uid = (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
1102 }
1103 else {
1104 corr_uid |= (uint64_t)data[2] << 48;
1105 corr_uid |= (uint64_t)data[1] << 40;
1106 corr_uid |= (uint64_t)data[0] << 32;
1107 corr_uid |= data[7] << 24;
1108 corr_uid |= data[6] << 16;
1109 corr_uid |= data[5] << 8;
1110 corr_uid |= data[4];
1111 }
1112 tryMfk32(corr_uid, data, key);
1113 //tryMfk64(corr_uid, data, key);
1114 PrintAndLog("--");
1115 }
1116 }
1117 }
1118 }
1119 return 0;
1120 }
1121
1122 int CmdHF14AMfDbg(const char *Cmd)
1123 {
1124 int dbgMode = param_get32ex(Cmd, 0, 0, 10);
1125 if (dbgMode > 4) {
1126 PrintAndLog("Max debug mode parameter is 4 \n");
1127 }
1128
1129 if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {
1130 PrintAndLog("Usage: hf mf dbg <debug level>");
1131 PrintAndLog(" 0 - no debug messages");
1132 PrintAndLog(" 1 - error messages");
1133 PrintAndLog(" 2 - plus information messages");
1134 PrintAndLog(" 3 - plus debug messages");
1135 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1136 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1137 return 0;
1138 }
1139
1140 UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};
1141 SendCommand(&c);
1142
1143 return 0;
1144 }
1145
1146 int CmdHF14AMfEGet(const char *Cmd)
1147 {
1148 uint8_t blockNo = 0;
1149 uint8_t data[16] = {0x00};
1150
1151 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1152 PrintAndLog("Usage: hf mf eget <block number>");
1153 PrintAndLog(" sample: hf mf eget 0 ");
1154 return 0;
1155 }
1156
1157 blockNo = param_get8(Cmd, 0);
1158
1159 PrintAndLog(" ");
1160 if (!mfEmlGetMem(data, blockNo, 1)) {
1161 PrintAndLog("data[%3d]:%s", blockNo, sprint_hex(data, 16));
1162 } else {
1163 PrintAndLog("Command execute timeout");
1164 }
1165
1166 return 0;
1167 }
1168
1169 int CmdHF14AMfEClear(const char *Cmd)
1170 {
1171 if (param_getchar(Cmd, 0) == 'h') {
1172 PrintAndLog("Usage: hf mf eclr");
1173 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1174 return 0;
1175 }
1176
1177 UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};
1178 SendCommand(&c);
1179 return 0;
1180 }
1181
1182 int CmdHF14AMfESet(const char *Cmd)
1183 {
1184 uint8_t memBlock[16];
1185 uint8_t blockNo = 0;
1186
1187 memset(memBlock, 0x00, sizeof(memBlock));
1188
1189 if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {
1190 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1191 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1192 return 0;
1193 }
1194
1195 blockNo = param_get8(Cmd, 0);
1196
1197 if (param_gethex(Cmd, 1, memBlock, 32)) {
1198 PrintAndLog("block data must include 32 HEX symbols");
1199 return 1;
1200 }
1201
1202 // 1 - blocks count
1203 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};
1204 memcpy(c.d.asBytes, memBlock, 16);
1205 SendCommand(&c);
1206 return 0;
1207 }
1208
1209 int CmdHF14AMfELoad(const char *Cmd)
1210 {
1211 FILE * f;
1212 char filename[FILE_PATH_SIZE];
1213 char *fnameptr = filename;
1214 char buf[64] = {0x00};
1215 uint8_t buf8[64] = {0x00};
1216 int i, len, blockNum, numBlocks;
1217 int nameParamNo = 1;
1218 uint8_t blockWidth = 32;
1219 char ctmp = param_getchar(Cmd, 0);
1220
1221 if ( ctmp == 'h' || ctmp == 'H' || ctmp == 0x00) {
1222 PrintAndLog("It loads emul dump from the file `filename.eml`");
1223 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`>");
1224 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1225 PrintAndLog("");
1226 PrintAndLog(" sample: hf mf eload filename");
1227 PrintAndLog(" hf mf eload 4 filename");
1228 return 0;
1229 }
1230
1231 switch (ctmp) {
1232 case '0' : numBlocks = 5*4; break;
1233 case '1' :
1234 case '\0': numBlocks = 16*4; break;
1235 case '2' : numBlocks = 32*4; break;
1236 case '4' : numBlocks = 256; break;
1237 case 'U' : // fall through , NTAG 215 has 135blocks a 540 bytes.
1238 case 'u' : numBlocks = 135; blockWidth = 8; break;
1239 default: {
1240 numBlocks = 16*4;
1241 nameParamNo = 0;
1242 }
1243 }
1244
1245 len = param_getstr(Cmd,nameParamNo,filename);
1246
1247 if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;
1248
1249 fnameptr += len;
1250
1251 sprintf(fnameptr, ".eml");
1252
1253 // open file
1254 f = fopen(filename, "r");
1255 if (f == NULL) {
1256 PrintAndLog("File %s not found or locked", filename);
1257 return 1;
1258 }
1259
1260 blockNum = 0;
1261 while(!feof(f)){
1262 memset(buf, 0, sizeof(buf));
1263
1264 if (fgets(buf, sizeof(buf), f) == NULL) {
1265
1266 if (blockNum >= numBlocks) break;
1267
1268 PrintAndLog("File reading error.");
1269 fclose(f);
1270 return 2;
1271 }
1272
1273 if (strlen(buf) < blockWidth){
1274 if(strlen(buf) && feof(f))
1275 break;
1276 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth);
1277 fclose(f);
1278 return 2;
1279 }
1280
1281 for (i = 0; i < blockWidth; i += 2) {
1282 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
1283 }
1284 if (mfEmlSetMem_xt(buf8, blockNum, 1, blockWidth/2)) {
1285 PrintAndLog("Cant set emul block: %3d", blockNum);
1286 fclose(f);
1287 return 3;
1288 }
1289 printf(".");
1290 blockNum++;
1291
1292 if (blockNum >= numBlocks) break;
1293 }
1294 fclose(f);
1295 printf("\n");
1296
1297 if ((blockNum != numBlocks)) {
1298 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum, numBlocks);
1299 return 4;
1300 }
1301 PrintAndLog("Loaded %d blocks from file: %s", blockNum, filename);
1302 return 0;
1303 }
1304
1305 int CmdHF14AMfESave(const char *Cmd)
1306 {
1307 FILE * f;
1308 char filename[FILE_PATH_SIZE];
1309 char * fnameptr = filename;
1310 uint8_t buf[64];
1311 int i, j, len, numBlocks;
1312 int nameParamNo = 1;
1313
1314 memset(filename, 0, sizeof(filename));
1315 memset(buf, 0, sizeof(buf));
1316
1317 char ctmp = param_getchar(Cmd, 0);
1318
1319 if ( ctmp == 'h' || ctmp == 'H') {
1320 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1321 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1322 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1323 PrintAndLog("");
1324 PrintAndLog(" sample: hf mf esave ");
1325 PrintAndLog(" hf mf esave 4");
1326 PrintAndLog(" hf mf esave 4 filename");
1327 return 0;
1328 }
1329
1330 switch (ctmp) {
1331 case '0' : numBlocks = 5*4; break;
1332 case '1' :
1333 case '\0': numBlocks = 16*4; break;
1334 case '2' : numBlocks = 32*4; break;
1335 case '4' : numBlocks = 256; break;
1336 default: {
1337 numBlocks = 16*4;
1338 nameParamNo = 0;
1339 }
1340 }
1341
1342 len = param_getstr(Cmd,nameParamNo,filename);
1343
1344 if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;
1345
1346 // user supplied filename?
1347 if (len < 1) {
1348 // get filename (UID from memory)
1349 if (mfEmlGetMem(buf, 0, 1)) {
1350 PrintAndLog("Can\'t get UID from block: %d", 0);
1351 len = sprintf(fnameptr, "dump");
1352 fnameptr += len;
1353 }
1354 else {
1355 for (j = 0; j < 7; j++, fnameptr += 2)
1356 sprintf(fnameptr, "%02X", buf[j]);
1357 }
1358 } else {
1359 fnameptr += len;
1360 }
1361
1362 // add file extension
1363 sprintf(fnameptr, ".eml");
1364
1365 // open file
1366 f = fopen(filename, "w+");
1367
1368 if ( !f ) {
1369 PrintAndLog("Can't open file %s ", filename);
1370 return 1;
1371 }
1372
1373 // put hex
1374 for (i = 0; i < numBlocks; i++) {
1375 if (mfEmlGetMem(buf, i, 1)) {
1376 PrintAndLog("Cant get block: %d", i);
1377 break;
1378 }
1379 for (j = 0; j < 16; j++)
1380 fprintf(f, "%02X", buf[j]);
1381 fprintf(f,"\n");
1382 }
1383 fclose(f);
1384
1385 PrintAndLog("Saved %d blocks to file: %s", numBlocks, filename);
1386
1387 return 0;
1388 }
1389
1390 int CmdHF14AMfECFill(const char *Cmd)
1391 {
1392 uint8_t keyType = 0;
1393 uint8_t numSectors = 16;
1394
1395 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1396 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1397 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1398 PrintAndLog("");
1399 PrintAndLog("samples: hf mf ecfill A");
1400 PrintAndLog(" hf mf ecfill A 4");
1401 PrintAndLog("Read card and transfer its data to emulator memory.");
1402 PrintAndLog("Keys must be laid in the emulator memory. \n");
1403 return 0;
1404 }
1405
1406 char ctmp = param_getchar(Cmd, 0);
1407 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
1408 PrintAndLog("Key type must be A or B");
1409 return 1;
1410 }
1411 if (ctmp != 'A' && ctmp != 'a') keyType = 1;
1412
1413 ctmp = param_getchar(Cmd, 1);
1414 switch (ctmp) {
1415 case '0' : numSectors = 5; break;
1416 case '1' :
1417 case '\0': numSectors = 16; break;
1418 case '2' : numSectors = 32; break;
1419 case '4' : numSectors = 40; break;
1420 default: numSectors = 16;
1421 }
1422
1423 printf("--params: numSectors: %d, keyType:%d", numSectors, keyType);
1424 UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {numSectors, keyType, 0}};
1425 SendCommand(&c);
1426 return 0;
1427 }
1428
1429 int CmdHF14AMfEKeyPrn(const char *Cmd)
1430 {
1431 int i;
1432 uint8_t numSectors;
1433 uint8_t data[16];
1434 uint64_t keyA, keyB;
1435
1436 char cmdp = param_getchar(Cmd, 0);
1437
1438 if ( cmdp == 'h' || cmdp == 'H' ) {
1439 PrintAndLog("It prints the keys loaded in the emulator memory");
1440 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1441 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1442 PrintAndLog("");
1443 PrintAndLog(" sample: hf mf ekeyprn 1");
1444 return 0;
1445 }
1446
1447 switch (cmdp) {
1448 case '0' : numSectors = 5; break;
1449 case '1' :
1450 case '\0': numSectors = 16; break;
1451 case '2' : numSectors = 32; break;
1452 case '4' : numSectors = 40; break;
1453 default: numSectors = 16;
1454 }
1455
1456 PrintAndLog("|---|----------------|----------------|");
1457 PrintAndLog("|sec|key A |key B |");
1458 PrintAndLog("|---|----------------|----------------|");
1459 for (i = 0; i < numSectors; i++) {
1460 if (mfEmlGetMem(data, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1)) {
1461 PrintAndLog("error get block %d", FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);
1462 break;
1463 }
1464 keyA = bytes_to_num(data, 6);
1465 keyB = bytes_to_num(data + 10, 6);
1466 PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB);
1467 }
1468 PrintAndLog("|---|----------------|----------------|");
1469
1470 return 0;
1471 }
1472
1473 int CmdHF14AMfCSetUID(const char *Cmd)
1474 {
1475 uint8_t wipeCard = 0;
1476 uint8_t uid[8] = {0x00};
1477 uint8_t oldUid[8] = {0x00};
1478 uint8_t atqa[2] = {0x00};
1479 uint8_t sak[1] = {0x00};
1480 uint8_t atqaPresent = 1;
1481 int res;
1482 char ctmp;
1483 int argi=0;
1484
1485 if (strlen(Cmd) < 1 || param_getchar(Cmd, argi) == 'h') {
1486 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
1487 PrintAndLog("sample: hf mf csetuid 01020304");
1488 PrintAndLog("sample: hf mf csetuid 01020304 0004 08 w");
1489 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
1490 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
1491 return 0;
1492 }
1493
1494 if (param_getchar(Cmd, argi) && param_gethex(Cmd, argi, uid, 8)) {
1495 PrintAndLog("UID must include 8 HEX symbols");
1496 return 1;
1497 }
1498 argi++;
1499
1500 ctmp = param_getchar(Cmd, argi);
1501 if (ctmp == 'w' || ctmp == 'W') {
1502 wipeCard = 1;
1503 atqaPresent = 0;
1504 }
1505
1506 if (atqaPresent) {
1507 if (param_getchar(Cmd, argi)) {
1508 if (param_gethex(Cmd, argi, atqa, 4)) {
1509 PrintAndLog("ATQA must include 4 HEX symbols");
1510 return 1;
1511 }
1512 argi++;
1513 if (!param_getchar(Cmd, argi) || param_gethex(Cmd, argi, sak, 2)) {
1514 PrintAndLog("SAK must include 2 HEX symbols");
1515 return 1;
1516 }
1517 argi++;
1518 } else
1519 atqaPresent = 0;
1520 }
1521
1522 if(!wipeCard) {
1523 ctmp = param_getchar(Cmd, argi);
1524 if (ctmp == 'w' || ctmp == 'W') {
1525 wipeCard = 1;
1526 }
1527 }
1528
1529 PrintAndLog("--wipe card:%s uid:%s", (wipeCard)?"YES":"NO", sprint_hex(uid, 4));
1530
1531 res = mfCSetUID(uid, (atqaPresent)?atqa:NULL, (atqaPresent)?sak:NULL, oldUid, wipeCard);
1532 if (res) {
1533 PrintAndLog("Can't set UID. error=%d", res);
1534 return 1;
1535 }
1536
1537 PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));
1538 PrintAndLog("new UID:%s", sprint_hex(uid, 4));
1539 return 0;
1540 }
1541
1542 int CmdHF14AMfCSetBlk(const char *Cmd)
1543 {
1544 uint8_t memBlock[16] = {0x00};
1545 uint8_t blockNo = 0;
1546 bool wipeCard = FALSE;
1547 int res;
1548
1549 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1550 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
1551 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1552 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
1553 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
1554 return 0;
1555 }
1556
1557 blockNo = param_get8(Cmd, 0);
1558
1559 if (param_gethex(Cmd, 1, memBlock, 32)) {
1560 PrintAndLog("block data must include 32 HEX symbols");
1561 return 1;
1562 }
1563
1564 char ctmp = param_getchar(Cmd, 2);
1565 wipeCard = (ctmp == 'w' || ctmp == 'W');
1566 PrintAndLog("--block number:%2d data:%s", blockNo, sprint_hex(memBlock, 16));
1567
1568 res = mfCSetBlock(blockNo, memBlock, NULL, wipeCard, CSETBLOCK_SINGLE_OPER);
1569 if (res) {
1570 PrintAndLog("Can't write block. error=%d", res);
1571 return 1;
1572 }
1573 return 0;
1574 }
1575
1576 int CmdHF14AMfCLoad(const char *Cmd)
1577 {
1578 FILE * f;
1579 char filename[FILE_PATH_SIZE] = {0x00};
1580 char * fnameptr = filename;
1581 char buf[64] = {0x00};
1582 uint8_t buf8[64] = {0x00};
1583 uint8_t fillFromEmulator = 0;
1584 int i, len, blockNum, flags=0;
1585
1586 char ctmp = param_getchar(Cmd, 0);
1587
1588 if (ctmp == 'h' || ctmp == 'H' || ctmp == 0x00) {
1589 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
1590 PrintAndLog("or from emulator memory (option `e`)");
1591 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1592 PrintAndLog(" or: hf mf cload e ");
1593 PrintAndLog(" sample: hf mf cload filename");
1594 return 0;
1595 }
1596
1597 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
1598
1599 if (fillFromEmulator) {
1600 for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {
1601 if (mfEmlGetMem(buf8, blockNum, 1)) {
1602 PrintAndLog("Cant get block: %d", blockNum);
1603 return 2;
1604 }
1605 if (blockNum == 0) flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC; // switch on field and send magic sequence
1606 if (blockNum == 1) flags = 0; // just write
1607 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD; // Done. Magic Halt and switch off field.
1608
1609 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {
1610 PrintAndLog("Cant set magic card block: %d", blockNum);
1611 return 3;
1612 }
1613 }
1614 return 0;
1615 } else {
1616 len = strlen(Cmd);
1617 if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;
1618
1619 memcpy(filename, Cmd, len);
1620 fnameptr += len;
1621
1622 sprintf(fnameptr, ".eml");
1623
1624 // open file
1625 f = fopen(filename, "r");
1626 if (f == NULL) {
1627 PrintAndLog("File not found or locked.");
1628 return 1;
1629 }
1630
1631 blockNum = 0;
1632 while(!feof(f)){
1633
1634 memset(buf, 0, sizeof(buf));
1635
1636 if (fgets(buf, sizeof(buf), f) == NULL) {
1637 fclose(f);
1638 PrintAndLog("File reading error.");
1639 return 2;
1640 }
1641
1642 if (strlen(buf) < 32) {
1643 if(strlen(buf) && feof(f))
1644 break;
1645 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1646 fclose(f);
1647 return 2;
1648 }
1649 for (i = 0; i < 32; i += 2)
1650 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
1651
1652 if (blockNum == 0) flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC; // switch on field and send magic sequence
1653 if (blockNum == 1) flags = 0; // just write
1654 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD; // Done. Switch off field.
1655
1656 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {
1657 PrintAndLog("Can't set magic card block: %d", blockNum);
1658 return 3;
1659 }
1660 blockNum++;
1661
1662 if (blockNum >= 16 * 4) break; // magic card type - mifare 1K
1663 }
1664 fclose(f);
1665
1666 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
1667 PrintAndLog("File content error. There must be 64 blocks");
1668 return 4;
1669 }
1670 PrintAndLog("Loaded from file: %s", filename);
1671 return 0;
1672 }
1673 return 0;
1674 }
1675
1676 int CmdHF14AMfCGetBlk(const char *Cmd) {
1677 uint8_t memBlock[16];
1678 uint8_t blockNo = 0;
1679 int res;
1680 memset(memBlock, 0x00, sizeof(memBlock));
1681
1682 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1683 PrintAndLog("Usage: hf mf cgetblk <block number>");
1684 PrintAndLog("sample: hf mf cgetblk 1");
1685 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
1686 return 0;
1687 }
1688
1689 blockNo = param_get8(Cmd, 0);
1690
1691 PrintAndLog("--block number:%2d ", blockNo);
1692
1693 res = mfCGetBlock(blockNo, memBlock, CSETBLOCK_SINGLE_OPER);
1694 if (res) {
1695 PrintAndLog("Can't read block. error=%d", res);
1696 return 1;
1697 }
1698
1699 PrintAndLog("block data:%s", sprint_hex(memBlock, 16));
1700 return 0;
1701 }
1702
1703 int CmdHF14AMfCGetSc(const char *Cmd) {
1704 uint8_t memBlock[16] = {0x00};
1705 uint8_t sectorNo = 0;
1706 int i, res, flags;
1707
1708 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1709 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1710 PrintAndLog("sample: hf mf cgetsc 0");
1711 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
1712 return 0;
1713 }
1714
1715 sectorNo = param_get8(Cmd, 0);
1716 if (sectorNo > 15) {
1717 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1718 return 1;
1719 }
1720
1721 PrintAndLog("--sector number:%d ", sectorNo);
1722
1723 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1724 for (i = 0; i < 4; i++) {
1725 if (i == 1) flags = 0;
1726 if (i == 3) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1727
1728 res = mfCGetBlock(sectorNo * 4 + i, memBlock, flags);
1729 if (res) {
1730 PrintAndLog("Can't read block. %d error=%d", sectorNo * 4 + i, res);
1731 return 1;
1732 }
1733
1734 PrintAndLog("block %3d data:%s", sectorNo * 4 + i, sprint_hex(memBlock, 16));
1735 }
1736 return 0;
1737 }
1738
1739 int CmdHF14AMfCSave(const char *Cmd) {
1740
1741 FILE * f;
1742 char filename[FILE_PATH_SIZE] = {0x00};
1743 char * fnameptr = filename;
1744 uint8_t fillFromEmulator = 0;
1745 uint8_t buf[64] = {0x00};
1746 int i, j, len, flags;
1747
1748 // memset(filename, 0, sizeof(filename));
1749 // memset(buf, 0, sizeof(buf));
1750 char ctmp = param_getchar(Cmd, 0);
1751
1752 if ( ctmp == 'h' || ctmp == 'H' ) {
1753 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1754 PrintAndLog("or into emulator memory (option `e`)");
1755 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1756 PrintAndLog(" sample: hf mf esave ");
1757 PrintAndLog(" hf mf esave filename");
1758 PrintAndLog(" hf mf esave e \n");
1759 return 0;
1760 }
1761 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
1762
1763 if (fillFromEmulator) {
1764 // put into emulator
1765 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1766 for (i = 0; i < 16 * 4; i++) {
1767 if (i == 1) flags = 0;
1768 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1769
1770 if (mfCGetBlock(i, buf, flags)) {
1771 PrintAndLog("Cant get block: %d", i);
1772 break;
1773 }
1774
1775 if (mfEmlSetMem(buf, i, 1)) {
1776 PrintAndLog("Cant set emul block: %d", i);
1777 return 3;
1778 }
1779 }
1780 return 0;
1781 } else {
1782 len = strlen(Cmd);
1783 if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;
1784
1785 if (len < 1) {
1786 // get filename
1787 if (mfCGetBlock(0, buf, CSETBLOCK_SINGLE_OPER)) {
1788 PrintAndLog("Cant get block: %d", 0);
1789 len = sprintf(fnameptr, "dump");
1790 fnameptr += len;
1791 } else {
1792 for (j = 0; j < 7; j++, fnameptr += 2)
1793 sprintf(fnameptr, "%02x", buf[j]);
1794 }
1795 } else {
1796 memcpy(filename, Cmd, len);
1797 fnameptr += len;
1798 }
1799
1800 sprintf(fnameptr, ".eml");
1801
1802 // open file
1803 f = fopen(filename, "w+");
1804
1805 if (f == NULL) {
1806 PrintAndLog("File not found or locked.");
1807 return 1;
1808 }
1809
1810 // put hex
1811 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1812 for (i = 0; i < 16 * 4; i++) {
1813 if (i == 1) flags = 0;
1814 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1815
1816 if (mfCGetBlock(i, buf, flags)) {
1817 PrintAndLog("Cant get block: %d", i);
1818 break;
1819 }
1820 for (j = 0; j < 16; j++)
1821 fprintf(f, "%02x", buf[j]);
1822 fprintf(f,"\n");
1823 }
1824 fclose(f);
1825
1826 PrintAndLog("Saved to file: %s", filename);
1827
1828 return 0;
1829 }
1830 }
1831
1832 int CmdHF14AMfSniff(const char *Cmd){
1833
1834 bool wantLogToFile = 0;
1835 bool wantDecrypt = 0;
1836 //bool wantSaveToEml = 0; TODO
1837 bool wantSaveToEmlFile = 0;
1838
1839 //var
1840 int res = 0;
1841 int len = 0;
1842 int blockLen = 0;
1843 int pckNum = 0;
1844 int num = 0;
1845 uint8_t uid[7];
1846 uint8_t uid_len;
1847 uint8_t atqa[2] = {0x00};
1848 uint8_t sak;
1849 bool isTag;
1850 uint8_t *buf = NULL;
1851 uint16_t bufsize = 0;
1852 uint8_t *bufPtr = NULL;
1853
1854 char ctmp = param_getchar(Cmd, 0);
1855 if ( ctmp == 'h' || ctmp == 'H' ) {
1856 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
1857 PrintAndLog("You can specify:");
1858 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1859 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1860 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1861 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1862 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");
1863 PrintAndLog(" sample: hf mf sniff l d e");
1864 return 0;
1865 }
1866
1867 for (int i = 0; i < 4; i++) {
1868 ctmp = param_getchar(Cmd, i);
1869 if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;
1870 if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;
1871 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1872 if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;
1873 }
1874
1875 printf("-------------------------------------------------------------------------\n");
1876 printf("Executing command. \n");
1877 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1878 printf("Press the key on pc keyboard to abort the client.\n");
1879 printf("-------------------------------------------------------------------------\n");
1880
1881 UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};
1882 clearCommandBuffer();
1883 SendCommand(&c);
1884
1885 // wait cycle
1886 while (true) {
1887 printf(".");
1888 fflush(stdout);
1889 if (ukbhit()) {
1890 getchar();
1891 printf("\naborted via keyboard!\n");
1892 break;
1893 }
1894
1895 UsbCommand resp;
1896 if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {
1897 res = resp.arg[0] & 0xff;
1898 uint16_t traceLen = resp.arg[1];
1899 len = resp.arg[2];
1900
1901 if (res == 0) return 0; // we are done
1902
1903 if (res == 1) { // there is (more) data to be transferred
1904 if (pckNum == 0) { // first packet, (re)allocate necessary buffer
1905 if (traceLen > bufsize) {
1906 uint8_t *p;
1907 if (buf == NULL) { // not yet allocated
1908 p = malloc(traceLen);
1909 } else { // need more memory
1910 p = realloc(buf, traceLen);
1911 }
1912 if (p == NULL) {
1913 PrintAndLog("Cannot allocate memory for trace");
1914 free(buf);
1915 return 2;
1916 }
1917 buf = p;
1918 }
1919 bufPtr = buf;
1920 bufsize = traceLen;
1921 memset(buf, 0x00, traceLen);
1922 }
1923 memcpy(bufPtr, resp.d.asBytes, len);
1924 bufPtr += len;
1925 pckNum++;
1926 }
1927
1928 if (res == 2) { // received all data, start displaying
1929 blockLen = bufPtr - buf;
1930 bufPtr = buf;
1931 printf(">\n");
1932 PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);
1933 while (bufPtr - buf < blockLen) {
1934 bufPtr += 6; // skip (void) timing information
1935 len = *((uint16_t *)bufPtr);
1936 if(len & 0x8000) {
1937 isTag = true;
1938 len &= 0x7fff;
1939 } else {
1940 isTag = false;
1941 }
1942 bufPtr += 2;
1943 if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff) && (bufPtr[12] == 0xff) && (bufPtr[13] == 0xff)) {
1944 memcpy(uid, bufPtr + 2, 7);
1945 memcpy(atqa, bufPtr + 2 + 7, 2);
1946 uid_len = (atqa[0] & 0xC0) == 0x40 ? 7 : 4;
1947 sak = bufPtr[11];
1948 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
1949 sprint_hex(uid + (7 - uid_len), uid_len),
1950 atqa[1],
1951 atqa[0],
1952 sak);
1953 if (wantLogToFile || wantDecrypt) {
1954 FillFileNameByUID(logHexFileName, uid + (7 - uid_len), ".log", uid_len);
1955 AddLogCurrentDT(logHexFileName);
1956 }
1957 if (wantDecrypt)
1958 mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);
1959 } else {
1960 PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));
1961 if (wantLogToFile)
1962 AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);
1963 if (wantDecrypt)
1964 mfTraceDecode(bufPtr, len, wantSaveToEmlFile);
1965 num++;
1966 }
1967 bufPtr += len;
1968 bufPtr += ((len-1)/8+1); // ignore parity
1969 }
1970 pckNum = 0;
1971 }
1972 } // resp not NULL
1973 } // while (true)
1974
1975 free(buf);
1976 return 0;
1977 }
1978
1979 //needs nt, ar, at, Data to decrypt
1980 int CmdHf14MfDecryptBytes(const char *Cmd){
1981 uint8_t data[50];
1982
1983 uint32_t nt = param_get32ex(Cmd,0,0,16);
1984 uint32_t ar_enc = param_get32ex(Cmd,1,0,16);
1985 uint32_t at_enc = param_get32ex(Cmd,2,0,16);
1986
1987 int len = 0;
1988 param_gethex_ex(Cmd, 3, data, &len);
1989
1990 len /= 2;
1991 int limit = sizeof(data) / 2;
1992
1993 if ( len >= limit )
1994 len = limit;
1995
1996 return tryDecryptWord( nt, ar_enc, at_enc, data, len);
1997 }
1998
1999 static command_t CommandTable[] =
2000 {
2001 {"help", CmdHelp, 1, "This help"},
2002 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},
2003 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},
2004 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},
2005 {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},
2006 {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},
2007 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},
2008 {"chk", CmdHF14AMfChk, 0, "Test block keys"},
2009 {"mifare", CmdHF14AMifare, 0, "Read parity error messages."},
2010 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},
2011 {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},
2012 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},
2013 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},
2014 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},
2015 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},
2016 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},
2017 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},
2018 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},
2019 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},
2020 {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},
2021 {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block - Magic Chinese card"},
2022 {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block - Magic Chinese card"},
2023 {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector - Magic Chinese card"},
2024 {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},
2025 {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},
2026 {"decrypt", CmdHf14MfDecryptBytes, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2027 {NULL, NULL, 0, NULL}
2028 };
2029
2030 int CmdHFMF(const char *Cmd)
2031 {
2032 // flush
2033 WaitForResponseTimeout(CMD_ACK,NULL,100);
2034
2035 CmdsParse(CommandTable, Cmd);
2036 return 0;
2037 }
2038
2039 int CmdHelp(const char *Cmd)
2040 {
2041 CmdsHelp(CommandTable);
2042 return 0;
2043 }
Impressum, Datenschutz