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