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