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