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