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