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