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