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