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