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