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