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