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