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