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