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