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