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