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