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