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