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