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