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