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