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