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