]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
ADD: lf indalademod output, The binary string is now printed with linebreaks every...
[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 block[16] = {0x00};
1545 uint8_t blockNo = 0;
1546 uint8_t params = MAGIC_SINGLE;
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, block, 32)) {
1560 PrintAndLog("block data must include 32 HEX symbols");
1561 return 1;
1562 }
1563
1564 char ctmp = param_getchar(Cmd, 2);
1565 if (ctmp == 'w' || ctmp == 'W')
1566 params |= MAGIC_WIPE;
1567
1568 PrintAndLog("--block number:%2d data:%s", blockNo, sprint_hex(block, 16));
1569
1570 res = mfCSetBlock(blockNo, block, NULL, params);
1571 if (res) {
1572 PrintAndLog("Can't write block. error=%d", res);
1573 return 1;
1574 }
1575 return 0;
1576 }
1577
1578 int CmdHF14AMfCLoad(const char *Cmd)
1579 {
1580 FILE * f;
1581 char filename[FILE_PATH_SIZE];
1582 char * fnameptr = filename;
1583 char buf[64] = {0x00};
1584 uint8_t buf8[64] = {0x00};
1585 uint8_t fillFromEmulator = 0;
1586 int i, len, blockNum, flags=0;
1587
1588 memset(filename, 0, sizeof(filename));
1589
1590 char ctmp = param_getchar(Cmd, 0);
1591
1592 if (ctmp == 'h' || ctmp == 'H' || ctmp == 0x00) {
1593 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
1594 PrintAndLog("or from emulator memory (option `e`)");
1595 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1596 PrintAndLog(" or: hf mf cload e ");
1597 PrintAndLog(" sample: hf mf cload filename");
1598 return 0;
1599 }
1600
1601 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
1602
1603 if (fillFromEmulator) {
1604 for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {
1605 if (mfEmlGetMem(buf8, blockNum, 1)) {
1606 PrintAndLog("Cant get block: %d", blockNum);
1607 return 2;
1608 }
1609 if (blockNum == 0) flags = MAGIC_INIT + MAGIC_WUPC; // switch on field and send magic sequence
1610 if (blockNum == 1) flags = 0; // just write
1611 if (blockNum == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF; // Done. Magic Halt and switch off field.
1612
1613 if (mfCSetBlock(blockNum, buf8, NULL, flags)) {
1614 PrintAndLog("Cant set magic card block: %d", blockNum);
1615 return 3;
1616 }
1617 }
1618 return 0;
1619 } else {
1620 len = strlen(Cmd);
1621 if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;
1622
1623 memcpy(filename, Cmd, len);
1624 fnameptr += len;
1625
1626 sprintf(fnameptr, ".eml");
1627
1628 // open file
1629 f = fopen(filename, "r");
1630 if (f == NULL) {
1631 PrintAndLog("File not found or locked.");
1632 return 1;
1633 }
1634
1635 blockNum = 0;
1636 while(!feof(f)){
1637
1638 memset(buf, 0, sizeof(buf));
1639
1640 if (fgets(buf, sizeof(buf), f) == NULL) {
1641 fclose(f);
1642 PrintAndLog("File reading error.");
1643 return 2;
1644 }
1645
1646 if (strlen(buf) < 32) {
1647 if(strlen(buf) && feof(f))
1648 break;
1649 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1650 fclose(f);
1651 return 2;
1652 }
1653 for (i = 0; i < 32; i += 2)
1654 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
1655
1656 if (blockNum == 0) flags = MAGIC_INIT + MAGIC_WUPC; // switch on field and send magic sequence
1657 if (blockNum == 1) flags = 0; // just write
1658 if (blockNum == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF; // Done. Switch off field.
1659
1660 if (mfCSetBlock(blockNum, buf8, NULL, flags)) {
1661 PrintAndLog("Can't set magic card block: %d", blockNum);
1662 return 3;
1663 }
1664 blockNum++;
1665
1666 if (blockNum >= 16 * 4) break; // magic card type - mifare 1K
1667 }
1668 fclose(f);
1669
1670 // 64 or 256blocks.
1671 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
1672 PrintAndLog("File content error. There must be 64 blocks");
1673 return 4;
1674 }
1675 PrintAndLog("Loaded from file: %s", filename);
1676 return 0;
1677 }
1678 return 0;
1679 }
1680
1681 int CmdHF14AMfCGetBlk(const char *Cmd) {
1682 uint8_t data[16];
1683 uint8_t blockNo = 0;
1684 int res;
1685 memset(data, 0x00, sizeof(data));
1686 char ctmp = param_getchar(Cmd, 0);
1687
1688 if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') {
1689 PrintAndLog("Usage: hf mf cgetblk <block number>");
1690 PrintAndLog("sample: hf mf cgetblk 1");
1691 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
1692 return 0;
1693 }
1694
1695 blockNo = param_get8(Cmd, 0);
1696
1697 PrintAndLog("--block number:%2d ", blockNo);
1698
1699 res = mfCGetBlock(blockNo, data, MAGIC_SINGLE);
1700 if (res) {
1701 PrintAndLog("Can't read block. error=%d", res);
1702 return 1;
1703 }
1704
1705 PrintAndLog("data:%s", sprint_hex(data, sizeof(data)));
1706 return 0;
1707 }
1708
1709 int CmdHF14AMfCGetSc(const char *Cmd) {
1710 uint8_t data[16];
1711 uint8_t sectorNo = 0;
1712 int i, res, flags;
1713 memset(data, 0x00, sizeof(data));
1714 char ctmp = param_getchar(Cmd, 0);
1715
1716 if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') {
1717 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1718 PrintAndLog("sample: hf mf cgetsc 0");
1719 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
1720 return 0;
1721 }
1722
1723 sectorNo = param_get8(Cmd, 0);
1724 if (sectorNo > 15) {
1725 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1726 return 1;
1727 }
1728
1729 PrintAndLog("--sector number:%d ", sectorNo);
1730 PrintAndLog("block | data");
1731
1732 flags = MAGIC_INIT + MAGIC_WUPC;
1733 for (i = 0; i < 4; i++) {
1734 if (i == 1) flags = 0;
1735 if (i == 3) flags = MAGIC_HALT + MAGIC_OFF;
1736
1737 res = mfCGetBlock(sectorNo * 4 + i, data, flags);
1738 if (res) {
1739 PrintAndLog("Can't read block. %d error=%d", sectorNo * 4 + i, res);
1740 return 1;
1741 }
1742 PrintAndLog(" %3d | %s", sectorNo * 4 + i, sprint_hex(data, sizeof(data)));
1743 }
1744 return 0;
1745 }
1746
1747 int CmdHF14AMfCSave(const char *Cmd) {
1748
1749 FILE * f;
1750 char filename[FILE_PATH_SIZE];
1751 char * fnameptr = filename;
1752 uint8_t fillFromEmulator = 0;
1753 uint8_t buf[64];
1754 int i, j, len, flags;
1755
1756 memset(filename, 0, sizeof(filename));
1757 memset(buf, 0, sizeof(buf));
1758 char ctmp = param_getchar(Cmd, 0);
1759
1760 if ( ctmp == 'h' || ctmp == 'H' ) {
1761 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1762 PrintAndLog("or into emulator memory (option `e`)");
1763 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1764 PrintAndLog(" sample: hf mf esave ");
1765 PrintAndLog(" hf mf esave filename");
1766 PrintAndLog(" hf mf esave e \n");
1767 return 0;
1768 }
1769 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
1770
1771 if (fillFromEmulator) {
1772 // put into emulator
1773 flags = MAGIC_INIT + MAGIC_WUPC;
1774 for (i = 0; i < 16 * 4; i++) {
1775 if (i == 1) flags = 0;
1776 if (i == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF;
1777
1778 if (mfCGetBlock(i, buf, flags)) {
1779 PrintAndLog("Cant get block: %d", i);
1780 break;
1781 }
1782
1783 if (mfEmlSetMem(buf, i, 1)) {
1784 PrintAndLog("Cant set emul block: %d", i);
1785 return 3;
1786 }
1787 }
1788 return 0;
1789 } else {
1790 len = strlen(Cmd);
1791 if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;
1792
1793 // get filename based on UID
1794 if (len < 1) {
1795
1796 if (mfCGetBlock(0, buf, MAGIC_SINGLE)) {
1797 PrintAndLog("Cant get block: %d", 0);
1798 len = sprintf(fnameptr, "dump");
1799 fnameptr += len;
1800 } else {
1801 for (j = 0; j < 7; j++, fnameptr += 2)
1802 sprintf(fnameptr, "%02x", buf[j]);
1803 }
1804 } else {
1805 memcpy(filename, Cmd, len);
1806 fnameptr += len;
1807 }
1808
1809 // add .eml extension
1810 sprintf(fnameptr, ".eml");
1811
1812 // open file
1813 f = fopen(filename, "w+");
1814
1815 if (f == NULL) {
1816 PrintAndLog("File not found or locked.");
1817 return 1;
1818 }
1819
1820 // put hex
1821 flags = MAGIC_INIT + MAGIC_WUPC;
1822 for (i = 0; i < 16 * 4; i++) {
1823 if (i == 1) flags = 0;
1824 if (i == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF;
1825
1826 if (mfCGetBlock(i, buf, flags)) {
1827 PrintAndLog("Cant get block: %d", i);
1828 break;
1829 }
1830 for (j = 0; j < 16; j++)
1831 fprintf(f, "%02x", buf[j]);
1832 fprintf(f,"\n");
1833 }
1834 fflush(f);
1835 fclose(f);
1836 PrintAndLog("Saved to file: %s", filename);
1837 return 0;
1838 }
1839 }
1840
1841 int CmdHF14AMfSniff(const char *Cmd){
1842
1843 bool wantLogToFile = 0;
1844 bool wantDecrypt = 0;
1845 //bool wantSaveToEml = 0; TODO
1846 bool wantSaveToEmlFile = 0;
1847
1848 //var
1849 int res = 0;
1850 int len = 0;
1851 int blockLen = 0;
1852 int pckNum = 0;
1853 int num = 0;
1854 uint8_t uid[7];
1855 uint8_t uid_len;
1856 uint8_t atqa[2] = {0x00};
1857 uint8_t sak;
1858 bool isTag;
1859 uint8_t *buf = NULL;
1860 uint16_t bufsize = 0;
1861 uint8_t *bufPtr = NULL;
1862
1863 char ctmp = param_getchar(Cmd, 0);
1864 if ( ctmp == 'h' || ctmp == 'H' ) {
1865 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
1866 PrintAndLog("You can specify:");
1867 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1868 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1869 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1870 PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1871 PrintAndLog("Usage: hf mf sniff [l][d][e][f]");
1872 PrintAndLog(" sample: hf mf sniff l d e");
1873 return 0;
1874 }
1875
1876 for (int i = 0; i < 4; i++) {
1877 ctmp = param_getchar(Cmd, i);
1878 if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;
1879 if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;
1880 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1881 if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;
1882 }
1883
1884 printf("-------------------------------------------------------------------------\n");
1885 printf("Executing command. \n");
1886 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1887 printf("Press the key on pc keyboard to abort the client.\n");
1888 printf("-------------------------------------------------------------------------\n");
1889
1890 UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};
1891 clearCommandBuffer();
1892 SendCommand(&c);
1893
1894 // wait cycle
1895 while (true) {
1896 printf(".");
1897 fflush(stdout);
1898 if (ukbhit()) {
1899 getchar();
1900 printf("\naborted via keyboard!\n");
1901 break;
1902 }
1903
1904 UsbCommand resp;
1905 if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {
1906 res = resp.arg[0] & 0xff;
1907 uint16_t traceLen = resp.arg[1];
1908 len = resp.arg[2];
1909
1910 if (res == 0) return 0; // we are done
1911
1912 if (res == 1) { // there is (more) data to be transferred
1913 if (pckNum == 0) { // first packet, (re)allocate necessary buffer
1914 if (traceLen > bufsize) {
1915 uint8_t *p;
1916 if (buf == NULL) { // not yet allocated
1917 p = malloc(traceLen);
1918 } else { // need more memory
1919 p = realloc(buf, traceLen);
1920 }
1921 if (p == NULL) {
1922 PrintAndLog("Cannot allocate memory for trace");
1923 free(buf);
1924 return 2;
1925 }
1926 buf = p;
1927 }
1928 bufPtr = buf;
1929 bufsize = traceLen;
1930 memset(buf, 0x00, traceLen);
1931 }
1932 memcpy(bufPtr, resp.d.asBytes, len);
1933 bufPtr += len;
1934 pckNum++;
1935 }
1936
1937 if (res == 2) { // received all data, start displaying
1938 blockLen = bufPtr - buf;
1939 bufPtr = buf;
1940 printf(">\n");
1941 PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);
1942 while (bufPtr - buf < blockLen) {
1943 bufPtr += 6; // skip (void) timing information
1944 len = *((uint16_t *)bufPtr);
1945 if(len & 0x8000) {
1946 isTag = true;
1947 len &= 0x7fff;
1948 } else {
1949 isTag = false;
1950 }
1951 bufPtr += 2;
1952 if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff) && (bufPtr[12] == 0xff) && (bufPtr[13] == 0xff)) {
1953 memcpy(uid, bufPtr + 2, 7);
1954 memcpy(atqa, bufPtr + 2 + 7, 2);
1955 uid_len = (atqa[0] & 0xC0) == 0x40 ? 7 : 4;
1956 sak = bufPtr[11];
1957 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
1958 sprint_hex(uid + (7 - uid_len), uid_len),
1959 atqa[1],
1960 atqa[0],
1961 sak);
1962 if (wantLogToFile || wantDecrypt) {
1963 FillFileNameByUID(logHexFileName, uid + (7 - uid_len), ".log", uid_len);
1964 AddLogCurrentDT(logHexFileName);
1965 }
1966 if (wantDecrypt)
1967 mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);
1968 } else {
1969 PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));
1970 if (wantLogToFile)
1971 AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);
1972 if (wantDecrypt)
1973 mfTraceDecode(bufPtr, len, wantSaveToEmlFile);
1974 num++;
1975 }
1976 bufPtr += len;
1977 bufPtr += ((len-1)/8+1); // ignore parity
1978 }
1979 pckNum = 0;
1980 }
1981 } // resp not NULL
1982 } // while (true)
1983
1984 free(buf);
1985 return 0;
1986 }
1987
1988 //needs nt, ar, at, Data to decrypt
1989 int CmdHf14MfDecryptBytes(const char *Cmd){
1990 uint8_t data[50];
1991
1992 uint32_t nt = param_get32ex(Cmd,0,0,16);
1993 uint32_t ar_enc = param_get32ex(Cmd,1,0,16);
1994 uint32_t at_enc = param_get32ex(Cmd,2,0,16);
1995
1996 int len = 0;
1997 param_gethex_ex(Cmd, 3, data, &len);
1998
1999 len /= 2;
2000 int limit = sizeof(data) / 2;
2001
2002 if ( len >= limit )
2003 len = limit;
2004
2005 return tryDecryptWord( nt, ar_enc, at_enc, data, len);
2006 }
2007
2008 static command_t CommandTable[] =
2009 {
2010 {"help", CmdHelp, 1, "This help"},
2011 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},
2012 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},
2013 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},
2014 {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},
2015 {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},
2016 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},
2017 {"chk", CmdHF14AMfChk, 0, "Test block keys"},
2018 {"mifare", CmdHF14AMifare, 0, "Read parity error messages."},
2019 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},
2020 {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},
2021 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},
2022 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},
2023 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},
2024 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},
2025 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},
2026 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},
2027 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},
2028 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},
2029 {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},
2030 {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block - Magic Chinese card"},
2031 {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block - Magic Chinese card"},
2032 {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector - Magic Chinese card"},
2033 {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},
2034 {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},
2035 {"decrypt", CmdHf14MfDecryptBytes, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2036 {NULL, NULL, 0, NULL}
2037 };
2038
2039 int CmdHFMF(const char *Cmd)
2040 {
2041 // flush
2042 WaitForResponseTimeout(CMD_ACK,NULL,100);
2043 CmdsParse(CommandTable, Cmd);
2044 return 0;
2045 }
2046
2047 int CmdHelp(const char *Cmd)
2048 {
2049 CmdsHelp(CommandTable);
2050 return 0;
2051 }
Impressum, Datenschutz