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