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