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