]>
Commit | Line | Data |
---|---|---|
81740aa5 | 1 | //----------------------------------------------------------------------------- |
2 | // Ultralight Code (c) 2013,2014 Midnitesnake & Andy Davies of Pentura | |
3 | // | |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
8 | // High frequency MIFARE ULTRALIGHT (C) commands | |
9 | //----------------------------------------------------------------------------- | |
7cb8516c | 10 | |
acf0582d | 11 | #include "cmdhfmfu.h" |
12 | ||
13 | #include <stdint.h> | |
14 | #include <stdio.h> | |
ac37ee81 | 15 | #include <string.h> |
ad939de5 | 16 | #include "comms.h" |
7cb8516c | 17 | #include "usb_cmd.h" |
18 | #include "cmdmain.h" | |
19 | #include "ui.h" | |
700d8687 | 20 | #include "mbedtls/des.h" |
81740aa5 | 21 | #include "cmdhfmf.h" |
4be9f36e | 22 | #include "cmdhf14a.h" // DropField() |
f168b263 | 23 | #include "mifare.h" |
f9848fd6 | 24 | #include "util.h" |
b8dd1ef6 | 25 | #include "util_posix.h" |
75377d29 | 26 | #include "protocols.h" |
1338d245 | 27 | #include "taginfo.h" |
3a5ffba7 | 28 | #include "crypto/libpcrypto.h" |
81740aa5 | 29 | |
4be9f36e | 30 | typedef enum TAGTYPE_UL { |
31 | UNKNOWN = 0x000000, | |
32 | UL = 0x000001, | |
33 | UL_C = 0x000002, | |
34 | UL_EV1_48 = 0x000004, | |
35 | UL_EV1_128 = 0x000008, | |
36 | NTAG = 0x000010, | |
37 | NTAG_203 = 0x000020, | |
38 | NTAG_210 = 0x000040, | |
39 | NTAG_212 = 0x000080, | |
40 | NTAG_213 = 0x000100, | |
41 | NTAG_215 = 0x000200, | |
42 | NTAG_216 = 0x000400, | |
43 | MY_D = 0x000800, | |
44 | MY_D_NFC = 0x001000, | |
45 | MY_D_MOVE = 0x002000, | |
b8dd1ef6 | 46 | MY_D_MOVE_LEAN= 0x004000, |
47 | NTAG_I2C_1K = 0x008000, | |
48 | NTAG_I2C_2K = 0x010000, | |
49 | FUDAN_UL = 0x020000, | |
50 | MAGIC = 0x040000, | |
4be9f36e | 51 | UL_MAGIC = UL | MAGIC, |
52 | UL_C_MAGIC = UL_C | MAGIC, | |
53 | UL_ERROR = 0xFFFFFF, | |
54 | } TagTypeUL_t; | |
55 | ||
4a74e2be | 56 | #define MAX_UL_BLOCKS 0x0f |
57 | #define MAX_ULC_BLOCKS 0x2b | |
58 | #define MAX_ULEV1a_BLOCKS 0x13 | |
59 | #define MAX_ULEV1b_BLOCKS 0x28 | |
60 | #define MAX_NTAG_203 0x29 | |
61 | #define MAX_NTAG_210 0x13 | |
62 | #define MAX_NTAG_212 0x28 | |
63 | #define MAX_NTAG_213 0x2c | |
64 | #define MAX_NTAG_215 0x86 | |
65 | #define MAX_NTAG_216 0xe6 | |
66 | #define MAX_MY_D_NFC 0xff | |
67 | #define MAX_MY_D_MOVE 0x25 | |
68 | #define MAX_MY_D_MOVE_LEAN 0x0f | |
75377d29 | 69 | |
345fb24a | 70 | #define KEYS_3DES_COUNT 7 |
4be9f36e | 71 | static uint8_t default_3des_keys[KEYS_3DES_COUNT][16] = { |
72 | { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 },// 3des std key | |
73 | { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },// all zeroes | |
74 | { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f },// 0x00-0x0F | |
75 | { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 },// NFC-key | |
76 | { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },// all ones | |
77 | { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF },// all FF | |
78 | { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF } // 11 22 33 | |
75377d29 | 79 | }; |
80 | ||
7f2114d8 | 81 | #define KEYS_PWD_COUNT 6 |
4be9f36e | 82 | static uint8_t default_pwd_pack[KEYS_PWD_COUNT][4] = { |
c585a5cf | 83 | {0xFF,0xFF,0xFF,0xFF}, // PACK 0x00,0x00 -- factory default |
84 | {0x4A,0xF8,0x4B,0x19}, // PACK 0xE5,0xBE -- italian bus (sniffed) | |
345fb24a | 85 | {0x33,0x6B,0xA1,0x19}, // PACK 0x9c,0x2d -- italian bus (sniffed) |
4be9f36e | 86 | {0xFF,0x90,0x6C,0xB2}, // PACK 0x12,0x9e -- italian bus (sniffed) |
8258f409 | 87 | {0x46,0x1c,0xA3,0x19}, // PACK 0xE9,0x5A -- italian bus (sniffed) |
88 | {0x35,0x1C,0xD0,0x19}, // PACK 0x9A,0x5a -- italian bus (sniffed) | |
c585a5cf | 89 | }; |
90 | ||
3a5ffba7 | 91 | // known public keys for the originality check (source: https://github.com/alexbatalov/node-nxp-originality-verifier) |
92 | uint8_t public_keys[2][33] = {{0x04,0x49,0x4e,0x1a,0x38,0x6d,0x3d,0x3c,0xfe,0x3d,0xc1,0x0e,0x5d,0xe6,0x8a,0x49,0x9b, // UL and NDEF | |
93 | 0x1c,0x20,0x2d,0xb5,0xb1,0x32,0x39,0x3e,0x89,0xed,0x19,0xfe,0x5b,0xe8,0xbc,0x61}, | |
94 | {0x04,0x90,0x93,0x3b,0xdc,0xd6,0xe9,0x9b,0x4e,0x25,0x5e,0x3d,0xa5,0x53,0x89,0xa8,0x27, // UL EV1 | |
95 | 0x56,0x4e,0x11,0x71,0x8e,0x01,0x72,0x92,0xfa,0xf2,0x32,0x26,0xa9,0x66,0x14,0xb8} | |
96 | }; | |
97 | ||
b8dd1ef6 | 98 | #define MAX_UL_TYPES 17 |
4be9f36e | 99 | static uint32_t UL_TYPES_ARRAY[MAX_UL_TYPES] = {UNKNOWN, UL, UL_C, UL_EV1_48, UL_EV1_128, NTAG, NTAG_203, |
b8dd1ef6 | 100 | NTAG_210, NTAG_212, NTAG_213, NTAG_215, NTAG_216, MY_D, MY_D_NFC, MY_D_MOVE, MY_D_MOVE_LEAN, FUDAN_UL}; |
5b99376a | 101 | |
4be9f36e | 102 | static uint8_t UL_MEMORY_ARRAY[MAX_UL_TYPES] = {MAX_UL_BLOCKS, MAX_UL_BLOCKS, MAX_ULC_BLOCKS, MAX_ULEV1a_BLOCKS, |
103 | MAX_ULEV1b_BLOCKS, MAX_NTAG_203, MAX_NTAG_203, MAX_NTAG_210, MAX_NTAG_212, MAX_NTAG_213, | |
b8dd1ef6 | 104 | MAX_NTAG_215, MAX_NTAG_216, MAX_UL_BLOCKS, MAX_MY_D_NFC, MAX_MY_D_MOVE, MAX_MY_D_MOVE_LEAN, MAX_UL_BLOCKS}; |
afceaf40 | 105 | |
4be9f36e | 106 | // get version nxp product type |
107 | static char *getProductTypeStr( uint8_t id){ | |
75377d29 | 108 | |
109 | static char buf[20]; | |
110 | char *retStr = buf; | |
111 | ||
112 | switch(id) { | |
1c429594 | 113 | case 3: sprintf(retStr, "%02X, Ultralight", id); break; |
4be9f36e | 114 | case 4: sprintf(retStr, "%02X, NTAG", id); break; |
1c429594 | 115 | default: sprintf(retStr, "%02X, unknown", id); break; |
75377d29 | 116 | } |
117 | return buf; | |
118 | } | |
119 | ||
120 | /* | |
4be9f36e | 121 | The 7 MSBits (=n) code the storage size itself based on 2^n, |
75377d29 | 122 | the LSBit is set to '0' if the size is exactly 2^n |
4be9f36e | 123 | and set to '1' if the storage size is between 2^n and 2^(n+1). |
75377d29 | 124 | */ |
b8dd1ef6 | 125 | static char *getUlev1CardSizeStr( uint8_t fsize ){ |
75377d29 | 126 | |
345fb24a | 127 | static char buf[40]; |
75377d29 | 128 | char *retStr = buf; |
2be768af | 129 | memset(buf, 0, sizeof(buf)); |
75377d29 | 130 | |
2be768af | 131 | uint16_t usize = 1 << ((fsize >>1) + 1); |
132 | uint16_t lsize = 1 << (fsize >>1); | |
75377d29 | 133 | |
134 | // is LSB set? | |
135 | if ( fsize & 1 ) | |
06561c34 | 136 | sprintf(retStr, "%02X, (%u <-> %u bytes)",fsize, usize, lsize); |
4be9f36e | 137 | else |
138 | sprintf(retStr, "%02X, (%u bytes)", fsize, lsize); | |
75377d29 | 139 | return buf; |
140 | } | |
81740aa5 | 141 | |
81740aa5 | 142 | |
b8dd1ef6 | 143 | static int ul_send_cmd_raw(uint8_t *cmd, uint8_t cmdlen, uint8_t *response, uint16_t responseLength) { |
75377d29 | 144 | UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT | ISO14A_APPEND_CRC, cmdlen, 0}}; |
145 | memcpy(c.d.asBytes, cmd, cmdlen); | |
22342f6d | 146 | clearCommandBuffer(); |
75377d29 | 147 | SendCommand(&c); |
148 | UsbCommand resp; | |
149 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1; | |
372a8257 | 150 | if (!resp.arg[0] && responseLength) return -1; |
75377d29 | 151 | |
152 | uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength; | |
abab60ae | 153 | memcpy(response, resp.d.asBytes, resplen); |
154 | return resplen; | |
75377d29 | 155 | } |
75377d29 | 156 | |
75377d29 | 157 | |
b8dd1ef6 | 158 | static int ul_select(iso14a_card_select_t *card, bool clear_trace) { |
75377d29 | 159 | |
b8dd1ef6 | 160 | UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT | ISO14A_NO_RATS | (clear_trace?ISO14A_CLEAR_TRACE:0), 0, 0}}; |
161 | clearCommandBuffer(); | |
162 | SendCommand(&c); | |
75377d29 | 163 | |
164 | UsbCommand resp; | |
c7442b76 | 165 | bool ans = false; |
166 | ans = WaitForResponseTimeout(CMD_ACK, &resp, 1500); | |
b8dd1ef6 | 167 | if (ans == 0 || resp.arg[0] == 0) { |
4be9f36e | 168 | PrintAndLogEx(WARNING, "iso14443a card select failed"); |
c7442b76 | 169 | return 0; |
170 | } | |
75377d29 | 171 | |
172 | memcpy(card, resp.d.asBytes, sizeof(iso14a_card_select_t)); | |
c7442b76 | 173 | return 1; |
75377d29 | 174 | } |
175 | ||
75377d29 | 176 | |
b8dd1ef6 | 177 | // This read command will return 16 bytes. |
178 | static int ul_read(uint8_t page, uint8_t *response, uint16_t responseLength) { | |
179 | uint8_t cmd[] = {MIFARE_CMD_READBLOCK, page}; | |
75377d29 | 180 | int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength); |
75377d29 | 181 | return len; |
182 | } | |
183 | ||
c585a5cf | 184 | |
b8dd1ef6 | 185 | static int ul_halt(void) { |
186 | uint8_t cmd[] = {ISO14443A_CMD_HALT, 0x00}; | |
187 | uint8_t response; | |
188 | int len = ul_send_cmd_raw(cmd, sizeof(cmd), &response, sizeof(response)); | |
189 | return len; | |
190 | } | |
191 | ||
192 | ||
193 | static int ul_comp_write_ex(uint8_t page, uint8_t *data, uint8_t datalen, bool first_part_only) { | |
194 | ||
195 | uint8_t cmd[18] = {0x00}; | |
c585a5cf | 196 | datalen = ( datalen > 16) ? 16 : datalen; |
197 | ||
b8dd1ef6 | 198 | cmd[0] = MIFARE_CMD_WRITEBLOCK; |
c585a5cf | 199 | cmd[1] = page; |
b8dd1ef6 | 200 | |
201 | uint8_t response = {0xff}; | |
202 | ul_send_cmd_raw(cmd, 2, &response, sizeof(response)); | |
203 | if (response != CARD_ACK) | |
204 | return -1; | |
205 | if (first_part_only) | |
206 | return 0; | |
207 | ||
208 | memcpy(cmd, data, datalen); | |
209 | ul_send_cmd_raw(cmd, 16, &response, sizeof(response)); | |
210 | if (response != CARD_ACK) | |
211 | return -1; | |
212 | ||
213 | return 0; | |
c585a5cf | 214 | } |
345fb24a | 215 | |
b8dd1ef6 | 216 | |
217 | // not used yet | |
218 | // static int ul_comp_write(uint8_t page, uint8_t *data, uint8_t datalen) { | |
219 | // return ul_comp_write_ex(page, data, datalen, false); | |
220 | // } | |
221 | ||
222 | ||
223 | static int ulc_requestAuthentication(uint8_t *nonce, uint16_t nonceLength) { | |
75377d29 | 224 | |
a2e2bb8a | 225 | uint8_t cmd[] = {MIFARE_ULC_AUTH_1, 0x00}; |
75377d29 | 226 | int len = ul_send_cmd_raw(cmd, sizeof(cmd), nonce, nonceLength); |
75377d29 | 227 | return len; |
228 | } | |
345fb24a | 229 | |
b8dd1ef6 | 230 | |
231 | static int ulc_authentication(uint8_t *key, bool switch_off_field) { | |
8258f409 | 232 | |
233 | UsbCommand c = {CMD_MIFAREUC_AUTH, {switch_off_field}}; | |
234 | memcpy(c.d.asBytes, key, 16); | |
22342f6d | 235 | clearCommandBuffer(); |
8258f409 | 236 | SendCommand(&c); |
237 | UsbCommand resp; | |
9d87eb66 | 238 | if ( !WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) return 0; |
239 | if ( resp.arg[0] == 1 ) return 1; | |
8258f409 | 240 | |
9d87eb66 | 241 | return 0; |
8258f409 | 242 | } |
243 | ||
b8dd1ef6 | 244 | |
245 | static int ulev1_requestAuthentication(uint8_t *pwd, uint8_t *pack, uint16_t packLength) { | |
75377d29 | 246 | |
247 | uint8_t cmd[] = {MIFARE_ULEV1_AUTH, pwd[0], pwd[1], pwd[2], pwd[3]}; | |
248 | int len = ul_send_cmd_raw(cmd, sizeof(cmd), pack, packLength); | |
75377d29 | 249 | return len; |
250 | } | |
251 | ||
b8dd1ef6 | 252 | |
253 | static int ul_auth_select(iso14a_card_select_t *card, TagTypeUL_t tagtype, bool hasAuthKey, uint8_t *authenticationkey, uint8_t *pack, uint8_t packSize) { | |
254 | ||
255 | if (hasAuthKey && (tagtype & UL_C)) { | |
1c429594 | 256 | //will select card automatically and close connection on error |
257 | if (!ulc_authentication(authenticationkey, false)) { | |
b8dd1ef6 | 258 | PrintAndLogEx(ERR, "Authentication Failed UL-C"); |
1c429594 | 259 | return 0; |
260 | } | |
261 | } else { | |
b8dd1ef6 | 262 | if (!ul_select(card, false)) return 0; |
1c429594 | 263 | |
264 | if (hasAuthKey) { | |
265 | if (ulev1_requestAuthentication(authenticationkey, pack, packSize) < 1) { | |
b8dd1ef6 | 266 | PrintAndLogEx(ERR, "Authentication Failed UL-EV1/NTAG"); |
1c429594 | 267 | return 0; |
268 | } | |
269 | } | |
270 | } | |
271 | return 1; | |
272 | } | |
273 | ||
b8dd1ef6 | 274 | static int ulev1_getVersion(uint8_t *response, uint16_t responseLength) { |
75377d29 | 275 | |
4be9f36e | 276 | uint8_t cmd[] = {MIFARE_ULEV1_VERSION}; |
75377d29 | 277 | int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength); |
75377d29 | 278 | return len; |
279 | } | |
280 | ||
281 | // static int ulev1_fastRead( uint8_t startblock, uint8_t endblock, uint8_t *response ){ | |
4be9f36e | 282 | |
75377d29 | 283 | // uint8_t cmd[] = {MIFARE_ULEV1_FASTREAD, startblock, endblock}; |
4be9f36e | 284 | |
75377d29 | 285 | // if ( !ul_send_cmd_raw(cmd, sizeof(cmd), response)){ |
75377d29 | 286 | // return -1; |
287 | // } | |
288 | // return 0; | |
289 | // } | |
290 | ||
b8dd1ef6 | 291 | |
292 | static int ulev1_readCounter(uint8_t counter, uint8_t *response, uint16_t responseLength) { | |
75377d29 | 293 | |
294 | uint8_t cmd[] = {MIFARE_ULEV1_READ_CNT, counter}; | |
295 | int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength); | |
75377d29 | 296 | return len; |
297 | } | |
298 | ||
b8dd1ef6 | 299 | |
300 | static int ulev1_readTearing(uint8_t counter, uint8_t *response, uint16_t responseLength) { | |
a2e2bb8a | 301 | |
302 | uint8_t cmd[] = {MIFARE_ULEV1_CHECKTEAR, counter}; | |
303 | int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength); | |
a2e2bb8a | 304 | return len; |
305 | } | |
306 | ||
b8dd1ef6 | 307 | |
308 | static int ulev1_readSignature(uint8_t *response, uint16_t responseLength) { | |
c585a5cf | 309 | |
310 | uint8_t cmd[] = {MIFARE_ULEV1_READSIG, 0x00}; | |
311 | int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength); | |
c585a5cf | 312 | return len; |
313 | } | |
314 | ||
7c8b5e68 | 315 | |
316 | // Fudan check checks for which error is given for a command with incorrect crc | |
317 | // NXP UL chip responds with 01, fudan 00. | |
318 | // other possible checks: | |
4be9f36e | 319 | // send a0 + crc |
7c8b5e68 | 320 | // UL responds with 00, fudan doesn't respond |
321 | // or | |
322 | // send a200 + crc | |
323 | // UL doesn't respond, fudan responds with 00 | |
324 | // or | |
325 | // send 300000 + crc (read with extra byte(s)) | |
326 | // UL responds with read of page 0, fudan doesn't respond. | |
327 | // | |
328 | // make sure field is off before calling this function | |
b8dd1ef6 | 329 | static int ul_fudan_check(void) { |
f4217d58 | 330 | iso14a_card_select_t card; |
b8dd1ef6 | 331 | if (!ul_select(&card, false)) |
f4217d58 | 332 | return UL_ERROR; |
333 | ||
334 | UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT, 4, 0}}; | |
335 | ||
336 | uint8_t cmd[4] = {0x30,0x00,0x02,0xa7}; //wrong crc on purpose should be 0xa8 | |
337 | memcpy(c.d.asBytes, cmd, 4); | |
338 | clearCommandBuffer(); | |
339 | SendCommand(&c); | |
340 | UsbCommand resp; | |
341 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return UL_ERROR; | |
342 | if (resp.arg[0] != 1) return UL_ERROR; | |
343 | ||
344 | return (!resp.d.asBytes[0]) ? FUDAN_UL : UL; //if response == 0x00 then Fudan, else Genuine NXP | |
345 | } | |
346 | ||
b8dd1ef6 | 347 | |
348 | static int ul_print_default(uint8_t *data) { | |
75377d29 | 349 | |
350 | uint8_t uid[7]; | |
b8dd1ef6 | 351 | memcpy(uid, data, 3); |
352 | memcpy(uid+3, data+4, 4); | |
353 | ||
354 | PrintAndLogEx(NORMAL," UID : %s", sprint_hex(uid, 7)); | |
4be9f36e | 355 | PrintAndLogEx(NORMAL," UID[0] : %02X, %s", uid[0], getManufacturerName(uid[0])); |
b8dd1ef6 | 356 | |
357 | if (uid[0] == 0x05 && ((uid[1] & 0xf0) >> 4) == 2 ) { // is infineon and 66RxxP | |
345fb24a | 358 | uint8_t chip = (data[8] & 0xC7); // 11000111 mask, bit 3,4,5 RFU |
359 | switch (chip){ | |
4be9f36e | 360 | case 0xc2: PrintAndLogEx(NORMAL, " IC type : SLE 66R04P 770 Bytes"); break; //77 pages |
361 | case 0xc4: PrintAndLogEx(NORMAL, " IC type : SLE 66R16P 2560 Bytes"); break; //256 pages | |
362 | case 0xc6: PrintAndLogEx(NORMAL, " IC type : SLE 66R32P 5120 Bytes"); break; //512 pages /2 sectors | |
345fb24a | 363 | } |
364 | } | |
4be9f36e | 365 | // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2 |
b8dd1ef6 | 366 | int crc0 = 0x88 ^ uid[0] ^ uid[1] ^ uid[2]; |
75377d29 | 367 | if ( data[3] == crc0 ) |
4be9f36e | 368 | PrintAndLogEx(NORMAL, " BCC0 : %02X, Ok", data[3]); |
75377d29 | 369 | else |
4be9f36e | 370 | PrintAndLogEx(NORMAL, " BCC0 : %02X, crc should be %02X", data[3], crc0); |
75377d29 | 371 | |
b8dd1ef6 | 372 | int crc1 = uid[3] ^ uid[4] ^ uid[5] ^ uid[6]; |
75377d29 | 373 | if ( data[8] == crc1 ) |
4be9f36e | 374 | PrintAndLogEx(NORMAL, " BCC1 : %02X, Ok", data[8]); |
75377d29 | 375 | else |
4be9f36e | 376 | PrintAndLogEx(NORMAL, " BCC1 : %02X, crc should be %02X", data[8], crc1 ); |
75377d29 | 377 | |
4be9f36e | 378 | PrintAndLogEx(NORMAL, " Internal : %02X, %sdefault", data[9], (data[9]==0x48)?"":"not " ); |
29250969 | 379 | |
b8dd1ef6 | 380 | PrintAndLogEx(NORMAL, " Lock : %s (binary %s %s)", |
6fdf42c6 | 381 | sprint_hex(data+10, 2), |
4be9f36e | 382 | printBits(1, data+10), |
383 | printBits(1, data+11) | |
6fdf42c6 | 384 | ); |
29250969 | 385 | |
4be9f36e | 386 | PrintAndLogEx(NORMAL, "OneTimePad : %s (binary %s %s %s %s)\n", |
b8dd1ef6 | 387 | sprint_hex(data+12, 4), |
4be9f36e | 388 | printBits(1, data+12), |
389 | printBits(1, data+13), | |
390 | printBits(1, data+14), | |
391 | printBits(1, data+15) | |
6fdf42c6 | 392 | ); |
29250969 | 393 | |
75377d29 | 394 | return 0; |
395 | } | |
396 | ||
b8dd1ef6 | 397 | |
a383f4b7 | 398 | static int ndef_print_CC(uint8_t *data) { |
1c429594 | 399 | // no NDEF message |
400 | if(data[0] != 0xe1) | |
401 | return -1; | |
75377d29 | 402 | |
4be9f36e | 403 | PrintAndLogEx(NORMAL, "--- NDEF Message"); |
404 | PrintAndLogEx(NORMAL, "Capability Container: %s", sprint_hex(data,4) ); | |
405 | PrintAndLogEx(NORMAL, " %02X : NDEF Magic Number", data[0]); | |
406 | PrintAndLogEx(NORMAL, " %02X : version %d.%d supported by tag", data[1], (data[1] & 0xF0) >> 4, data[1] & 0x0f); | |
407 | PrintAndLogEx(NORMAL, " %02X : Physical Memory Size: %d bytes", data[2], (data[2] + 1) * 8); | |
c585a5cf | 408 | if ( data[2] == 0x12 ) |
4be9f36e | 409 | PrintAndLogEx(NORMAL, " %02X : NDEF Memory Size: %d bytes", data[2], 144); |
c585a5cf | 410 | else if ( data[2] == 0x3e ) |
4be9f36e | 411 | PrintAndLogEx(NORMAL, " %02X : NDEF Memory Size: %d bytes", data[2], 496); |
c585a5cf | 412 | else if ( data[2] == 0x6d ) |
4be9f36e | 413 | PrintAndLogEx(NORMAL, " %02X : NDEF Memory Size: %d bytes", data[2], 872); |
1c429594 | 414 | |
4be9f36e | 415 | PrintAndLogEx(NORMAL, " %02X : %s / %s", data[3], |
416 | (data[3] & 0xF0) ? "(RFU)" : "Read access granted without any security", | |
75377d29 | 417 | (data[3] & 0x0F)==0 ? "Write access granted without any security" : (data[3] & 0x0F)==0x0F ? "No write access granted at all" : "(RFU)"); |
75377d29 | 418 | return 0; |
419 | } | |
420 | ||
b8dd1ef6 | 421 | |
c7442b76 | 422 | int ul_print_type(uint32_t tagtype, uint8_t spaces){ |
8ceb6b03 | 423 | char spc[11] = " "; |
424 | spc[10]=0x00; | |
425 | char *spacer = spc + (10-spaces); | |
426 | ||
b8dd1ef6 | 427 | if (tagtype & UL ) |
4be9f36e | 428 | PrintAndLogEx(NORMAL, "%sTYPE : MIFARE Ultralight (MF0ICU1) %s", spacer, (tagtype & MAGIC) ? "<magic>" : "" ); |
b8dd1ef6 | 429 | else if (tagtype & UL_C) |
4be9f36e | 430 | PrintAndLogEx(NORMAL, "%sTYPE : MIFARE Ultralight C (MF0ULC) %s", spacer, (tagtype & MAGIC) ? "<magic>" : "" ); |
b8dd1ef6 | 431 | else if (tagtype & UL_EV1_48) |
4be9f36e | 432 | PrintAndLogEx(NORMAL, "%sTYPE : MIFARE Ultralight EV1 48bytes (MF0UL1101)", spacer); |
b8dd1ef6 | 433 | else if (tagtype & UL_EV1_128) |
4be9f36e | 434 | PrintAndLogEx(NORMAL, "%sTYPE : MIFARE Ultralight EV1 128bytes (MF0UL2101)", spacer); |
b8dd1ef6 | 435 | else if (tagtype & NTAG) |
4be9f36e | 436 | PrintAndLogEx(NORMAL, "%sTYPE : NTAG UNKNOWN", spacer); |
b8dd1ef6 | 437 | else if (tagtype & NTAG_203) |
4be9f36e | 438 | PrintAndLogEx(NORMAL, "%sTYPE : NTAG 203 144bytes (NT2H0301F0DT)", spacer); |
b8dd1ef6 | 439 | else if (tagtype & NTAG_210) |
4be9f36e | 440 | PrintAndLogEx(NORMAL, "%sTYPE : NTAG 210 48bytes (NT2L1011G0DU)", spacer); |
b8dd1ef6 | 441 | else if (tagtype & NTAG_212) |
4be9f36e | 442 | PrintAndLogEx(NORMAL, "%sTYPE : NTAG 212 128bytes (NT2L1211G0DU)", spacer); |
b8dd1ef6 | 443 | else if (tagtype & NTAG_213) |
4be9f36e | 444 | PrintAndLogEx(NORMAL, "%sTYPE : NTAG 213 144bytes (NT2H1311G0DU)", spacer); |
b8dd1ef6 | 445 | else if (tagtype & NTAG_215) |
4be9f36e | 446 | PrintAndLogEx(NORMAL, "%sTYPE : NTAG 215 504bytes (NT2H1511G0DU)", spacer); |
b8dd1ef6 | 447 | else if (tagtype & NTAG_216) |
4be9f36e | 448 | PrintAndLogEx(NORMAL, "%sTYPE : NTAG 216 888bytes (NT2H1611G0DU)", spacer); |
b8dd1ef6 | 449 | else if (tagtype & NTAG_I2C_1K) |
4be9f36e | 450 | PrintAndLogEx(NORMAL, "%sTYPE : NTAG I%sC 888bytes (NT3H1101FHK)", spacer, "\xFD"); |
b8dd1ef6 | 451 | else if (tagtype & NTAG_I2C_2K) |
4be9f36e | 452 | PrintAndLogEx(NORMAL, "%sTYPE : NTAG I%sC 1904bytes (NT3H1201FHK)", spacer, "\xFD"); |
b8dd1ef6 | 453 | else if (tagtype & MY_D) |
4be9f36e | 454 | PrintAndLogEx(NORMAL, "%sTYPE : INFINEON my-d\x99 (SLE 66RxxS)", spacer); |
b8dd1ef6 | 455 | else if (tagtype & MY_D_NFC) |
4be9f36e | 456 | PrintAndLogEx(NORMAL, "%sTYPE : INFINEON my-d\x99 NFC (SLE 66RxxP)", spacer); |
b8dd1ef6 | 457 | else if (tagtype & MY_D_MOVE) |
458 | PrintAndLogEx(NORMAL, "%sTYPE : INFINEON my-d\x99 move | my-d\x99move NFC (SLE 66R01P)", spacer); | |
459 | else if (tagtype & MY_D_MOVE_LEAN) | |
4be9f36e | 460 | PrintAndLogEx(NORMAL, "%sTYPE : INFINEON my-d\x99 move lean (SLE 66R01L)", spacer); |
b8dd1ef6 | 461 | else if (tagtype & FUDAN_UL) |
4be9f36e | 462 | PrintAndLogEx(NORMAL, "%sTYPE : FUDAN Ultralight Compatible (or other compatible) %s", spacer, (tagtype & MAGIC) ? "<magic>" : "" ); |
75377d29 | 463 | else |
4be9f36e | 464 | PrintAndLogEx(NORMAL, "%sTYPE : Unknown %06x", spacer, tagtype); |
75377d29 | 465 | return 0; |
466 | } | |
467 | ||
b8dd1ef6 | 468 | |
469 | static int ulc_print_3deskey(uint8_t *data) { | |
4be9f36e | 470 | PrintAndLogEx(NORMAL, " deskey1 [44/0x2C] : %s [%.4s]", sprint_hex(data ,4),data); |
471 | PrintAndLogEx(NORMAL, " deskey1 [45/0x2D] : %s [%.4s]", sprint_hex(data+4 ,4),data+4); | |
472 | PrintAndLogEx(NORMAL, " deskey2 [46/0x2E] : %s [%.4s]", sprint_hex(data+8 ,4),data+8); | |
473 | PrintAndLogEx(NORMAL, " deskey2 [47/0x2F] : %s [%.4s]", sprint_hex(data+12,4),data+12); | |
474 | PrintAndLogEx(NORMAL, "\n 3des key : %s", sprint_hex(SwapEndian64(data, 16, 8), 16)); | |
75377d29 | 475 | return 0; |
476 | } | |
477 | ||
b8dd1ef6 | 478 | |
479 | static int ulc_print_configuration(uint8_t *data) { | |
75377d29 | 480 | |
4be9f36e | 481 | PrintAndLogEx(NORMAL, "--- UL-C Configuration"); |
b8dd1ef6 | 482 | PrintAndLogEx(NORMAL, " Higher Lockbits [40/0x28] : %s (binary %s %s)", |
4be9f36e | 483 | sprint_hex(data, 2), |
484 | printBits(1, data), | |
485 | printBits(1, data+1) | |
486 | ); | |
b8dd1ef6 | 487 | PrintAndLogEx(NORMAL, " Counter [41/0x29] : %s", sprint_hex(data+4, 2)); |
75377d29 | 488 | |
489 | bool validAuth = (data[8] >= 0x03 && data[8] <= 0x30); | |
b8dd1ef6 | 490 | if (validAuth) |
4be9f36e | 491 | PrintAndLogEx(NORMAL, " Auth0 [42/0x2A] : %s page %d/0x%02X and above need authentication", sprint_hex(data+8, 4), data[8], data[8] ); |
75377d29 | 492 | else{ |
b8dd1ef6 | 493 | if (data[8] == 0) { |
4be9f36e | 494 | PrintAndLogEx(NORMAL, " Auth0 [42/0x2A] : %s default", sprint_hex(data+8, 4) ); |
75377d29 | 495 | } else { |
4be9f36e | 496 | PrintAndLogEx(NORMAL, " Auth0 [42/0x2A] : %s auth byte is out-of-range", sprint_hex(data+8, 4) ); |
f168b263 | 497 | } |
75377d29 | 498 | } |
4be9f36e | 499 | PrintAndLogEx(NORMAL, " Auth1 [43/0x2B] : %s %s", |
75377d29 | 500 | sprint_hex(data+12, 4), |
501 | (data[12] & 1) ? "write access restricted": "read and write access restricted" | |
502 | ); | |
f168b263 | 503 | return 0; |
504 | } | |
81740aa5 | 505 | |
b8dd1ef6 | 506 | |
507 | static int ulev1_print_configuration(uint8_t *data, uint8_t startPage) { | |
f168b263 | 508 | |
4be9f36e | 509 | PrintAndLogEx(NORMAL, "\n--- Tag Configuration"); |
75377d29 | 510 | |
511 | bool strg_mod_en = (data[0] & 2); | |
512 | uint8_t authlim = (data[4] & 0x07); | |
513 | bool cfglck = (data[4] & 0x40); | |
514 | bool prot = (data[4] & 0x80); | |
515 | uint8_t vctid = data[5]; | |
516 | ||
4be9f36e | 517 | PrintAndLogEx(NORMAL, " cfg0 [%u/0x%02X] : %s", startPage, startPage, sprint_hex(data, 4)); |
b8dd1ef6 | 518 | if (data[3] < 0xff) |
519 | PrintAndLogEx(NORMAL, " - page %d and above need authentication", data[3]); | |
4be9f36e | 520 | else |
521 | PrintAndLogEx(NORMAL, " - pages don't need authentication"); | |
522 | PrintAndLogEx(NORMAL, " - strong modulation mode %s", (strg_mod_en) ? "enabled" : "disabled"); | |
523 | PrintAndLogEx(NORMAL, " cfg1 [%u/0x%02X] : %s", startPage + 1, startPage + 1, sprint_hex(data+4, 4) ); | |
b8dd1ef6 | 524 | if (authlim == 0) |
4be9f36e | 525 | PrintAndLogEx(NORMAL, " - Unlimited password attempts"); |
75377d29 | 526 | else |
4be9f36e | 527 | PrintAndLogEx(NORMAL, " - Max number of password attempts is %d", authlim); |
528 | PrintAndLogEx(NORMAL, " - user configuration %s", cfglck ? "permanently locked":"writeable"); | |
529 | PrintAndLogEx(NORMAL, " - %s access is protected with password", prot ? "read and write":"write"); | |
530 | PrintAndLogEx(NORMAL, " - %02X, Virtual Card Type Identifier is %s default", vctid, (vctid==0x05)? "":"not"); | |
b8dd1ef6 | 531 | PrintAndLogEx(NORMAL, " PWD [%u/0x%02X] : %s- (cannot be read)", startPage + 2, startPage + 2, sprint_hex(data+8, 4)); |
532 | PrintAndLogEx(NORMAL, " PACK [%u/0x%02X] : %s - (cannot be read)", startPage + 3, startPage + 3, sprint_hex(data+12, 2)); | |
533 | PrintAndLogEx(NORMAL, " RFU [%u/0x%02X] : %s- (cannot be read)", startPage + 3, startPage + 3, sprint_hex(data+14, 2)); | |
75377d29 | 534 | return 0; |
535 | } | |
536 | ||
b8dd1ef6 | 537 | |
538 | static int ulev1_print_counters(void) { | |
4be9f36e | 539 | PrintAndLogEx(NORMAL, "--- Tag Counters"); |
a2e2bb8a | 540 | uint8_t tear[1] = {0}; |
75377d29 | 541 | uint8_t counter[3] = {0,0,0}; |
c7442b76 | 542 | uint16_t len = 0; |
75377d29 | 543 | for ( uint8_t i = 0; i<3; ++i) { |
b8dd1ef6 | 544 | ulev1_readTearing(i, tear, sizeof(tear)); |
545 | len = ulev1_readCounter(i, counter, sizeof(counter) ); | |
c7442b76 | 546 | if (len == 3) { |
4be9f36e | 547 | PrintAndLogEx(NORMAL, " [%0d] : %s", i, sprint_hex(counter,3)); |
548 | PrintAndLogEx(NORMAL, " - %02X tearing %s", tear[0], ( tear[0]==0xBD)?"Ok":"failure"); | |
c7442b76 | 549 | } |
75377d29 | 550 | } |
c7442b76 | 551 | return len; |
75377d29 | 552 | } |
553 | ||
b8dd1ef6 | 554 | |
3a5ffba7 | 555 | static int ulev1_print_signature(TagTypeUL_t tagtype, uint8_t *uid, uint8_t *signature, size_t signature_len){ |
556 | uint8_t public_key = 0; | |
557 | if (tagtype == UL_EV1_48 || tagtype == UL_EV1_128) { | |
558 | public_key = 1; | |
559 | } | |
560 | int res = ecdsa_signature_r_s_verify(MBEDTLS_ECP_DP_SECP128R1, public_keys[public_key], uid, 7, signature, signature_len, false); | |
561 | bool signature_valid = (res == 0); | |
562 | ||
563 | PrintAndLogEx(NORMAL, "\n--- Tag Originality Signature"); | |
564 | //PrintAndLogEx(NORMAL, "IC signature public key name : NXP NTAG21x 2013"); // don't know if there is other NXP public keys.. :( | |
565 | PrintAndLogEx(NORMAL, " Signature public key : %s", sprint_hex(public_keys[public_key]+1, sizeof(public_keys[public_key])-1)); | |
4be9f36e | 566 | PrintAndLogEx(NORMAL, " Elliptic curve parameters : secp128r1"); |
3a5ffba7 | 567 | PrintAndLogEx(NORMAL, " Tag ECC Signature : %s", sprint_hex(signature, signature_len)); |
568 | PrintAndLogEx(NORMAL, " Originality signature check : signature is %svalid", signature_valid?"":"NOT "); | |
c585a5cf | 569 | return 0; |
570 | } | |
345fb24a | 571 | |
b8dd1ef6 | 572 | |
345fb24a | 573 | static int ulev1_print_version(uint8_t *data){ |
4be9f36e | 574 | PrintAndLogEx(NORMAL, "\n--- Tag Version"); |
b8dd1ef6 | 575 | PrintAndLogEx(NORMAL, " Raw bytes : %s", sprint_hex(data, 8) ); |
4be9f36e | 576 | PrintAndLogEx(NORMAL, " Vendor ID : %02X, %s", data[1], getManufacturerName(data[1])); |
577 | PrintAndLogEx(NORMAL, " Product type : %s", getProductTypeStr(data[2])); | |
578 | PrintAndLogEx(NORMAL, " Product subtype : %02X, %s", data[3], (data[3]==1) ?"17 pF":"50pF"); | |
579 | PrintAndLogEx(NORMAL, " Major version : %02X", data[4]); | |
580 | PrintAndLogEx(NORMAL, " Minor version : %02X", data[5]); | |
581 | PrintAndLogEx(NORMAL, " Size : %s", getUlev1CardSizeStr(data[6])); | |
582 | PrintAndLogEx(NORMAL, " Protocol type : %02X", data[7]); | |
345fb24a | 583 | return 0; |
584 | } | |
585 | ||
c585a5cf | 586 | |
b8dd1ef6 | 587 | static int ul_magic_test(void) { |
588 | // try a compatibility write to page0, and see if tag answers with ACK/NACK to the first part of the command | |
c585a5cf | 589 | iso14a_card_select_t card; |
b8dd1ef6 | 590 | if (!ul_select(&card, false)) |
c585a5cf | 591 | return UL_ERROR; |
b8dd1ef6 | 592 | int status = ul_comp_write_ex(0, NULL, 0, true); |
593 | if (status == 0) { | |
46fcd738 | 594 | return MAGIC; |
b8dd1ef6 | 595 | } |
46fcd738 | 596 | return 0; |
c585a5cf | 597 | } |
f04ef473 | 598 | |
b8dd1ef6 | 599 | |
c7442b76 | 600 | uint32_t GetHF14AMfU_Type(void){ |
81740aa5 | 601 | |
92690507 | 602 | TagTypeUL_t tagtype = UNKNOWN; |
603 | iso14a_card_select_t card; | |
75377d29 | 604 | uint8_t version[10] = {0x00}; |
75377d29 | 605 | int len; |
606 | ||
b8dd1ef6 | 607 | if (!ul_select(&card, true)) { |
872337e0 | 608 | DropField(); |
b8dd1ef6 | 609 | msleep(200); |
75377d29 | 610 | return UL_ERROR; |
611 | } | |
92690507 | 612 | |
b8dd1ef6 | 613 | // Check for Ultralight Family |
614 | if (card.uidlen != 7 || (card.sak & 0x38) != 0x00) { | |
872337e0 | 615 | DropField(); |
b8dd1ef6 | 616 | PrintAndLogEx(NORMAL, "Tag is not Ultralight | NTAG | MY-D [ATQA: %02X %02X SAK: %02X]\n", card.atqa[1], card.atqa[0], card.sak); |
617 | return UL_ERROR; | |
618 | } | |
345fb24a | 619 | |
b8dd1ef6 | 620 | if (card.uid[0] != 0x05) { |
621 | len = ulev1_getVersion(version, sizeof(version)); | |
622 | if (len == 10) { | |
623 | if (version[2] == 0x03 && version[6] == 0x0B) | |
624 | tagtype = UL_EV1_48; | |
625 | else if (version[2] == 0x03 && version[6] != 0x0B) | |
626 | tagtype = UL_EV1_128; | |
627 | else if (version[2] == 0x04 && version[3] == 0x01 && version[6] == 0x0B) | |
628 | tagtype = NTAG_210; | |
629 | else if (version[2] == 0x04 && version[3] == 0x01 && version[6] == 0x0E) | |
630 | tagtype = NTAG_212; | |
631 | else if (version[2] == 0x04 && version[3] == 0x02 && version[6] == 0x0F) | |
632 | tagtype = NTAG_213; | |
633 | else if (version[2] == 0x04 && version[3] == 0x02 && version[6] == 0x11) | |
634 | tagtype = NTAG_215; | |
635 | else if (version[2] == 0x04 && version[3] == 0x02 && version[6] == 0x13) | |
636 | tagtype = NTAG_216; | |
637 | else if (version[2] == 0x04 && version[3] == 0x05 && version[6] == 0x13) | |
638 | tagtype = NTAG_I2C_1K; | |
639 | else if (version[2] == 0x04 && version[3] == 0x05 && version[6] == 0x15) | |
640 | tagtype = NTAG_I2C_2K; | |
641 | else if (version[2] == 0x04) | |
642 | tagtype = NTAG; | |
345fb24a | 643 | } |
b8dd1ef6 | 644 | |
a383f4b7 | 645 | // UL vs UL-C vs ntag203 test |
b8dd1ef6 | 646 | if (tagtype == UNKNOWN) { |
647 | ul_halt(); | |
648 | if (!ul_select(&card, false)) { | |
649 | DropField(); | |
650 | msleep(200); | |
651 | return UL_ERROR; | |
652 | } | |
a383f4b7 | 653 | |
654 | // do UL_C check first... | |
8258f409 | 655 | uint8_t nonce[11] = {0x00}; |
b8dd1ef6 | 656 | len = ulc_requestAuthentication(nonce, sizeof(nonce)); |
657 | ul_halt(); | |
658 | if (len == 11) { | |
a383f4b7 | 659 | tagtype = UL_C; |
4be9f36e | 660 | } else { |
a383f4b7 | 661 | // need to re-select after authentication error |
b8dd1ef6 | 662 | if (!ul_select(&card, false)) { |
663 | DropField(); | |
664 | msleep(200); | |
665 | return UL_ERROR; | |
666 | } | |
c7442b76 | 667 | |
a383f4b7 | 668 | uint8_t data[16] = {0x00}; |
b8dd1ef6 | 669 | // read page 0x29 (last valid ntag203 page) |
670 | len = ul_read(0x29, data, sizeof(data)); | |
671 | if (len <= 1) { | |
a383f4b7 | 672 | tagtype = UL; |
673 | } else { | |
674 | // read page 0x30 (should error if it is a ntag203) | |
b8dd1ef6 | 675 | len = ul_read(0x30, data, sizeof(data)); |
676 | if (len <= 1) { | |
677 | ul_halt(); | |
a383f4b7 | 678 | tagtype = NTAG_203; |
a383f4b7 | 679 | } |
680 | } | |
a383f4b7 | 681 | } |
345fb24a | 682 | } |
f4217d58 | 683 | if (tagtype & UL) { |
4be9f36e | 684 | tagtype = ul_fudan_check(); |
b8dd1ef6 | 685 | ul_halt(); |
f4217d58 | 686 | } |
b8dd1ef6 | 687 | |
688 | } else { // manufacturer Infineon. Check for my-d variants | |
689 | ||
345fb24a | 690 | uint8_t nib = (card.uid[1] & 0xf0) >> 4; |
b8dd1ef6 | 691 | switch (nib) { |
692 | case 1: tagtype = MY_D; break; //or SLE 66RxxS ... up to 512 pages of 8 user bytes... | |
693 | case 2: tagtype = MY_D_NFC; break; //or SLE 66RxxP ... up to 512 pages of 8 user bytes... (or in nfc mode FF pages of 4 bytes) | |
694 | case 3: tagtype = MY_D_MOVE; break; //or SLE 66R01P // 38 pages of 4 bytes | |
695 | case 7: tagtype = MY_D_MOVE_LEAN; break; //or SLE 66R01L // 16 pages of 4 bytes | |
75377d29 | 696 | } |
f168b263 | 697 | } |
75377d29 | 698 | |
46fcd738 | 699 | tagtype |= ul_magic_test(); |
b8dd1ef6 | 700 | |
46fcd738 | 701 | if (tagtype == (UNKNOWN | MAGIC)) tagtype = (UL_MAGIC); |
b8dd1ef6 | 702 | |
703 | DropField(); | |
704 | msleep(200); | |
705 | ||
706 | printf("Tagtype: %08x\n", tagtype); | |
f168b263 | 707 | return tagtype; |
708 | } | |
709 | ||
b8dd1ef6 | 710 | |
4be9f36e | 711 | static int usage_hf_mfu_info(void) { |
712 | PrintAndLogEx(NORMAL, "It gathers information about the tag and tries to detect what kind it is."); | |
713 | PrintAndLogEx(NORMAL, "Sometimes the tags are locked down, and you may need a key to be able to read the information"); | |
714 | PrintAndLogEx(NORMAL, "The following tags can be identified:\n"); | |
715 | PrintAndLogEx(NORMAL, "Ultralight, Ultralight-C, Ultralight EV1, NTAG 203, NTAG 210,"); | |
716 | PrintAndLogEx(NORMAL, "NTAG 212, NTAG 213, NTAG 215, NTAG 216, NTAG I2C 1K & 2K"); | |
717 | PrintAndLogEx(NORMAL, "my-d, my-d NFC, my-d move, my-d move NFC\n"); | |
718 | PrintAndLogEx(NORMAL, "Usage: hf mfu info k <key> l"); | |
719 | PrintAndLogEx(NORMAL, " Options : "); | |
720 | PrintAndLogEx(NORMAL, " k <key> : (optional) key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]"); | |
721 | PrintAndLogEx(NORMAL, " l : (optional) swap entered key's endianness"); | |
722 | PrintAndLogEx(NORMAL, ""); | |
723 | PrintAndLogEx(NORMAL, " sample : hf mfu info"); | |
724 | PrintAndLogEx(NORMAL, " : hf mfu info k 00112233445566778899AABBCCDDEEFF"); | |
725 | PrintAndLogEx(NORMAL, " : hf mfu info k AABBCCDDD"); | |
726 | return 0; | |
727 | } | |
728 | ||
b8dd1ef6 | 729 | |
730 | static int CmdHF14AMfUInfo(const char *Cmd) { | |
f168b263 | 731 | |
c585a5cf | 732 | uint8_t authlim = 0xff; |
75377d29 | 733 | iso14a_card_select_t card; |
3a5ffba7 | 734 | uint8_t uid[7]; |
a2e2bb8a | 735 | bool errors = false; |
b8dd1ef6 | 736 | uint8_t keybytes[16] = {0x00}; |
737 | uint8_t *authenticationkey = keybytes; | |
738 | int keyLen = 0; | |
a2e2bb8a | 739 | bool hasAuthKey = false; |
a383f4b7 | 740 | bool locked = false; |
06561c34 | 741 | bool swapEndian = false; |
a2e2bb8a | 742 | uint8_t cmdp = 0; |
a2e2bb8a | 743 | uint8_t pack[4] = {0,0,0,0}; |
06561c34 | 744 | int len = 0; |
f168b263 | 745 | |
a2e2bb8a | 746 | while(param_getchar(Cmd, cmdp) != 0x00) |
747 | { | |
748 | switch(param_getchar(Cmd, cmdp)) | |
749 | { | |
750 | case 'h': | |
751 | case 'H': | |
752 | return usage_hf_mfu_info(); | |
753 | case 'k': | |
754 | case 'K': | |
b8dd1ef6 | 755 | keyLen = 32; |
756 | errors = param_gethex_ex(Cmd, cmdp+1, authenticationkey, &keyLen); | |
757 | if (errors || (keyLen != 32 && keyLen != 8)) { //ul-c or ev1/ntag key length | |
758 | PrintAndLogEx(ERR, "Key has incorrect length.\n"); | |
06561c34 | 759 | errors = true; |
a2e2bb8a | 760 | } |
06561c34 | 761 | cmdp += 2; |
b8dd1ef6 | 762 | keyLen /= 2; |
06561c34 | 763 | hasAuthKey = true; |
764 | break; | |
765 | case 'l': | |
766 | case 'L': | |
767 | swapEndian = true; | |
768 | cmdp++; | |
a2e2bb8a | 769 | break; |
770 | default: | |
4be9f36e | 771 | PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp)); |
a2e2bb8a | 772 | errors = true; |
773 | break; | |
774 | } | |
775 | if(errors) break; | |
776 | } | |
777 | ||
778 | //Validations | |
b8dd1ef6 | 779 | if (errors) |
780 | return usage_hf_mfu_info(); | |
efd19351 | 781 | |
75377d29 | 782 | TagTypeUL_t tagtype = GetHF14AMfU_Type(); |
b8dd1ef6 | 783 | if (tagtype == UL_ERROR) { |
784 | return -1; | |
785 | } | |
92690507 | 786 | |
4be9f36e | 787 | PrintAndLogEx(NORMAL, "\n--- Tag Information ---------"); |
788 | PrintAndLogEx(NORMAL, "-------------------------------------------------------------"); | |
8ceb6b03 | 789 | ul_print_type(tagtype, 6); |
92690507 | 790 | |
4be9f36e | 791 | // Swap endianness |
b8dd1ef6 | 792 | if (swapEndian && hasAuthKey) |
793 | authenticationkey = SwapEndian64(authenticationkey, keyLen, (keyLen == 16) ? 8 : 4 ); | |
06561c34 | 794 | |
b8dd1ef6 | 795 | if (!ul_auth_select(&card, tagtype, hasAuthKey, authenticationkey, pack, sizeof(pack))) { |
796 | DropField(); | |
797 | return -1; | |
798 | } | |
a2e2bb8a | 799 | |
a383f4b7 | 800 | // read pages 0,1,2,3 (should read 4pages) |
b8dd1ef6 | 801 | uint8_t data[16]; |
802 | len = ul_read(0, data, sizeof(data)); | |
803 | if (len == -1) { | |
872337e0 | 804 | DropField(); |
4be9f36e | 805 | PrintAndLogEx(WARNING, "Error: tag didn't answer to READ"); |
b8dd1ef6 | 806 | return -1; |
807 | } else if (len == 16) { | |
3a5ffba7 | 808 | memcpy(uid, data, 3); |
809 | memcpy(uid+3, data+4, 4); | |
a383f4b7 | 810 | ul_print_default(data); |
811 | ndef_print_CC(data+12); | |
efd19351 | 812 | } else { |
1c429594 | 813 | locked = true; |
814 | } | |
75377d29 | 815 | |
a383f4b7 | 816 | // UL_C Specific |
817 | if ((tagtype & UL_C)) { | |
75377d29 | 818 | |
819 | // read pages 0x28, 0x29, 0x2A, 0x2B | |
820 | uint8_t ulc_conf[16] = {0x00}; | |
b8dd1ef6 | 821 | len = ul_read(0x28, ulc_conf, sizeof(ulc_conf)); |
822 | if (len == -1) { | |
872337e0 | 823 | DropField(); |
b8dd1ef6 | 824 | PrintAndLogEx(WARNING, "Error: tag didn't answer to READ UL-C"); |
825 | return -1; | |
826 | } | |
827 | if (len == 16) { | |
828 | ulc_print_configuration(ulc_conf); | |
829 | } else { | |
830 | locked = true; | |
4be9f36e | 831 | } |
75377d29 | 832 | |
a383f4b7 | 833 | if ((tagtype & MAGIC)) { |
834 | //just read key | |
75377d29 | 835 | uint8_t ulc_deskey[16] = {0x00}; |
b8dd1ef6 | 836 | len = ul_read(0x2C, ulc_deskey, sizeof(ulc_deskey)); |
837 | if (len == -1) { | |
872337e0 | 838 | DropField(); |
4be9f36e | 839 | PrintAndLogEx(WARNING, "Error: tag didn't answer to READ magic"); |
b8dd1ef6 | 840 | return -1; |
75377d29 | 841 | } |
b8dd1ef6 | 842 | if (len == 16) ulc_print_3deskey(ulc_deskey); |
c585a5cf | 843 | } else { |
4be9f36e | 844 | // if we called info with key, just return |
b8dd1ef6 | 845 | if (hasAuthKey) { |
846 | DropField(); | |
847 | return 1; | |
848 | } | |
a2e2bb8a | 849 | |
06561c34 | 850 | // also try to diversify default keys.. look into CmdHF14AMfuGenDiverseKeys |
4be9f36e | 851 | PrintAndLogEx(INFO, "Trying some default 3des keys"); |
a383f4b7 | 852 | for (uint8_t i = 0; i < KEYS_3DES_COUNT; ++i ) { |
b8dd1ef6 | 853 | uint8_t *key = default_3des_keys[i]; |
a383f4b7 | 854 | if (ulc_authentication(key, true)) { |
b8dd1ef6 | 855 | DropField(); |
4be9f36e | 856 | PrintAndLogEx(SUCCESS, "Found default 3des key: "); |
2b3af97d | 857 | uint8_t keySwap[16]; |
858 | memcpy(keySwap, SwapEndian64(key,16,8), 16); | |
859 | ulc_print_3deskey(keySwap); | |
efd19351 | 860 | return 1; |
4be9f36e | 861 | } |
f168b263 | 862 | } |
b8dd1ef6 | 863 | DropField(); |
efd19351 | 864 | return 1; |
75377d29 | 865 | } |
866 | } | |
867 | ||
4be9f36e | 868 | // do counters and signature first (don't neet auth) |
a383f4b7 | 869 | |
870 | // ul counters are different than ntag counters | |
c7442b76 | 871 | if ((tagtype & (UL_EV1_48 | UL_EV1_128))) { |
872 | if (ulev1_print_counters() != 3) { | |
873 | // failed - re-select | |
b8dd1ef6 | 874 | if (!ul_auth_select( &card, tagtype, hasAuthKey, authenticationkey, pack, sizeof(pack))) { |
875 | DropField(); | |
876 | return -1; | |
877 | } | |
c7442b76 | 878 | } |
879 | } | |
345fb24a | 880 | |
4be9f36e | 881 | if ((tagtype & (UL_EV1_48 | UL_EV1_128 | NTAG_213 | NTAG_215 | NTAG_216 | NTAG_I2C_1K | NTAG_I2C_2K ))) { |
345fb24a | 882 | uint8_t ulev1_signature[32] = {0x00}; |
b8dd1ef6 | 883 | len = ulev1_readSignature(ulev1_signature, sizeof(ulev1_signature)); |
884 | if (len == -1) { | |
872337e0 | 885 | DropField(); |
b8dd1ef6 | 886 | PrintAndLogEx(WARNING, "Error: tag didn't answer to READ SIGNATURE"); |
887 | return -1; | |
75377d29 | 888 | } |
b8dd1ef6 | 889 | if (len == 32) { |
3a5ffba7 | 890 | ulev1_print_signature(tagtype, uid, ulev1_signature, sizeof(ulev1_signature)); |
b8dd1ef6 | 891 | } else { |
c7442b76 | 892 | // re-select |
b8dd1ef6 | 893 | if (!ul_auth_select( &card, tagtype, hasAuthKey, authenticationkey, pack, sizeof(pack))) { |
894 | DropField(); | |
895 | return -1; | |
896 | } | |
c7442b76 | 897 | } |
f168b263 | 898 | } |
75377d29 | 899 | |
46fcd738 | 900 | if ((tagtype & (UL_EV1_48 | UL_EV1_128 | NTAG_210 | NTAG_212 | NTAG_213 | NTAG_215 | NTAG_216 | NTAG_I2C_1K | NTAG_I2C_2K))) { |
75377d29 | 901 | uint8_t version[10] = {0x00}; |
b8dd1ef6 | 902 | len = ulev1_getVersion(version, sizeof(version)); |
903 | if (len == -1) { | |
872337e0 | 904 | DropField(); |
b8dd1ef6 | 905 | PrintAndLogEx(WARNING, "Error: tag didn't answer to GETVERSION"); |
906 | return -1; | |
907 | } else if (len == 10) { | |
1c429594 | 908 | ulev1_print_version(version); |
909 | } else { | |
910 | locked = true; | |
b8dd1ef6 | 911 | if (!ul_auth_select( &card, tagtype, hasAuthKey, authenticationkey, pack, sizeof(pack))) { |
912 | DropField(); | |
913 | return -1; | |
914 | } | |
75377d29 | 915 | } |
a383f4b7 | 916 | |
917 | uint8_t startconfigblock = 0; | |
918 | uint8_t ulev1_conf[16] = {0x00}; | |
919 | // config blocks always are last 4 pages | |
b8dd1ef6 | 920 | for (uint8_t idx = 0; idx < MAX_UL_TYPES; idx++) { |
921 | if (tagtype & UL_TYPES_ARRAY[idx]) { | |
a383f4b7 | 922 | startconfigblock = UL_MEMORY_ARRAY[idx]-3; |
b8dd1ef6 | 923 | break; |
924 | } | |
925 | } | |
c585a5cf | 926 | |
b8dd1ef6 | 927 | if (startconfigblock) { // if we know where the config block is... |
928 | len = ul_read(startconfigblock, ulev1_conf, sizeof(ulev1_conf)); | |
929 | if (len == -1) { | |
872337e0 | 930 | DropField(); |
b8dd1ef6 | 931 | PrintAndLogEx(WARNING, "Error: tag didn't answer to READ EV1"); |
932 | return -1; | |
933 | } else if (len == 16) { | |
46fcd738 | 934 | // save AUTHENTICATION LIMITS for later: |
935 | authlim = (ulev1_conf[4] & 0x07); | |
012c0761 | 936 | ulev1_print_configuration(ulev1_conf, startconfigblock); |
46fcd738 | 937 | } |
8258f409 | 938 | } |
a2e2bb8a | 939 | |
c585a5cf | 940 | // AUTHLIMIT, (number of failed authentications) |
941 | // 0 = limitless. | |
a383f4b7 | 942 | // 1-7 = limit. No automatic tries then. |
943 | // hasAuthKey, if we was called with key, skip test. | |
b8dd1ef6 | 944 | if (!authlim && !hasAuthKey) { |
4be9f36e | 945 | PrintAndLogEx(NORMAL, "\n--- Known EV1/NTAG passwords."); |
9d87eb66 | 946 | len = 0; |
a383f4b7 | 947 | for (uint8_t i = 0; i < KEYS_PWD_COUNT; ++i ) { |
b8dd1ef6 | 948 | uint8_t *key = default_pwd_pack[i]; |
9d87eb66 | 949 | len = ulev1_requestAuthentication(key, pack, sizeof(pack)); |
950 | if (len >= 1) { | |
b8dd1ef6 | 951 | PrintAndLogEx(SUCCESS, "Found a default password: %s || Pack: %02X %02X", sprint_hex(key, 4), pack[0], pack[1]); |
9d87eb66 | 952 | break; |
953 | } else { | |
b8dd1ef6 | 954 | if (!ul_auth_select( &card, tagtype, hasAuthKey, authenticationkey, pack, sizeof(pack))) { |
955 | DropField(); | |
956 | return -1; | |
957 | } | |
c585a5cf | 958 | } |
959 | } | |
4be9f36e | 960 | if (len < 1) PrintAndLogEx(WARNING, "password not known"); |
75377d29 | 961 | } |
f168b263 | 962 | } |
c585a5cf | 963 | |
872337e0 | 964 | DropField(); |
b8dd1ef6 | 965 | |
966 | if (locked) | |
967 | PrintAndLogEx(FAILED, "\nTag appears to be locked, try using the key to get more info"); | |
4be9f36e | 968 | PrintAndLogEx(NORMAL, ""); |
b8dd1ef6 | 969 | |
a2e2bb8a | 970 | return 1; |
81740aa5 | 971 | } |
972 | ||
973 | // | |
79d7bcbb | 974 | // Write Single Block |
81740aa5 | 975 | // |
4be9f36e | 976 | static int usage_hf_mfu_wrbl(void) { |
977 | PrintAndLogEx(NORMAL, "Write a block. It autodetects card type.\n"); | |
978 | PrintAndLogEx(NORMAL, "Usage: hf mfu wrbl b <block number> d <data> k <key> l\n"); | |
979 | PrintAndLogEx(NORMAL, " Options:"); | |
980 | PrintAndLogEx(NORMAL, " b <no> : block to write"); | |
981 | PrintAndLogEx(NORMAL, " d <data> : block data - (8 hex symbols)"); | |
982 | PrintAndLogEx(NORMAL, " k <key> : (optional) key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]"); | |
983 | PrintAndLogEx(NORMAL, " l : (optional) swap entered key's endianness"); | |
984 | PrintAndLogEx(NORMAL, ""); | |
985 | PrintAndLogEx(NORMAL, " sample : hf mfu wrbl b 0 d 01234567"); | |
986 | PrintAndLogEx(NORMAL, " : hf mfu wrbl b 0 d 01234567 k AABBCCDDD\n"); | |
987 | return 0; | |
988 | } | |
989 | ||
b8dd1ef6 | 990 | |
4be9f36e | 991 | static int CmdHF14AMfUWrBl(const char *Cmd){ |
81740aa5 | 992 | |
79d7bcbb | 993 | int blockNo = -1; |
994 | bool errors = false; | |
b8dd1ef6 | 995 | uint8_t keybytes[16] = {0x00}; |
996 | uint8_t *authenticationkey = keybytes; | |
997 | int keyLen = 0; | |
79d7bcbb | 998 | bool hasAuthKey = false; |
79d7bcbb | 999 | bool swapEndian = false; |
79d7bcbb | 1000 | uint8_t cmdp = 0; |
79d7bcbb | 1001 | uint8_t blockdata[20] = {0x00}; |
79d7bcbb | 1002 | |
b8dd1ef6 | 1003 | while(param_getchar(Cmd, cmdp) != 0x00) { |
1004 | switch(param_getchar(Cmd, cmdp)) { | |
79d7bcbb | 1005 | case 'h': |
1006 | case 'H': | |
1007 | return usage_hf_mfu_wrbl(); | |
1008 | case 'k': | |
1009 | case 'K': | |
b8dd1ef6 | 1010 | keyLen = 32; |
1011 | errors = param_gethex_ex(Cmd, cmdp+1, authenticationkey, &keyLen); | |
1012 | if (errors || (keyLen != 32 && keyLen != 8)) { //ul-c or ev1/ntag key length | |
1013 | PrintAndLogEx(ERR, "Key has incorrect length.\n"); | |
1014 | errors = true; | |
79d7bcbb | 1015 | } |
b8dd1ef6 | 1016 | cmdp += 2; |
1017 | keyLen /= 2; | |
1018 | hasAuthKey = true; | |
79d7bcbb | 1019 | break; |
1020 | case 'b': | |
1021 | case 'B': | |
1022 | blockNo = param_get8(Cmd, cmdp+1); | |
79d7bcbb | 1023 | if (blockNo < 0) { |
4be9f36e | 1024 | PrintAndLogEx(ERR, "Wrong block number"); |
79d7bcbb | 1025 | errors = true; |
1026 | } | |
79d7bcbb | 1027 | cmdp += 2; |
1028 | break; | |
1029 | case 'l': | |
1030 | case 'L': | |
1031 | swapEndian = true; | |
4be9f36e | 1032 | cmdp++; |
79d7bcbb | 1033 | break; |
1034 | case 'd': | |
1035 | case 'D': | |
1036 | if ( param_gethex(Cmd, cmdp+1, blockdata, 8) ) { | |
4be9f36e | 1037 | PrintAndLogEx(ERR, "Block data must include 8 HEX symbols"); |
79d7bcbb | 1038 | errors = true; |
1039 | break; | |
1040 | } | |
1041 | cmdp += 2; | |
1042 | break; | |
1043 | default: | |
4be9f36e | 1044 | PrintAndLogEx(ERR, "Unknown parameter '%c'", param_getchar(Cmd, cmdp)); |
79d7bcbb | 1045 | errors = true; |
1046 | break; | |
1047 | } | |
1048 | //Validations | |
1049 | if(errors) return usage_hf_mfu_wrbl(); | |
afceaf40 | 1050 | } |
79d7bcbb | 1051 | |
b8dd1ef6 | 1052 | if (blockNo == -1) return usage_hf_mfu_wrbl(); |
2ec87733 | 1053 | // starting with getting tagtype |
1054 | TagTypeUL_t tagtype = GetHF14AMfU_Type(); | |
b8dd1ef6 | 1055 | if (tagtype == UL_ERROR) { |
1056 | return -1; | |
1057 | } | |
2ec87733 | 1058 | |
1059 | uint8_t maxblockno = 0; | |
b8dd1ef6 | 1060 | for (uint8_t idx = 0; idx < MAX_UL_TYPES; idx++) { |
1061 | if (tagtype & UL_TYPES_ARRAY[idx]) { | |
2ec87733 | 1062 | maxblockno = UL_MEMORY_ARRAY[idx]; |
b8dd1ef6 | 1063 | break; |
1064 | } | |
2ec87733 | 1065 | } |
1066 | if (blockNo > maxblockno){ | |
b8dd1ef6 | 1067 | DropField(); |
4be9f36e | 1068 | PrintAndLogEx(WARNING, "block number too large. Max block is %u/0x%02X \n", maxblockno,maxblockno); |
2ec87733 | 1069 | return usage_hf_mfu_wrbl(); |
1070 | } | |
79d7bcbb | 1071 | |
4be9f36e | 1072 | // Swap endianness |
b8dd1ef6 | 1073 | if (swapEndian && hasAuthKey) authenticationkey = SwapEndian64(authenticationkey, keyLen, (keyLen == 16) ? 8 : 4); |
79d7bcbb | 1074 | |
1075 | if ( blockNo <= 3) | |
4be9f36e | 1076 | PrintAndLogEx(NORMAL, "Special Block: %0d (0x%02X) [ %s]", blockNo, blockNo, sprint_hex(blockdata, 4)); |
79d7bcbb | 1077 | else |
4be9f36e | 1078 | PrintAndLogEx(NORMAL, "Block: %0d (0x%02X) [ %s]", blockNo, blockNo, sprint_hex(blockdata, 4)); |
79d7bcbb | 1079 | |
1080 | //Send write Block | |
1081 | UsbCommand c = {CMD_MIFAREU_WRITEBL, {blockNo}}; | |
b8dd1ef6 | 1082 | memcpy(c.d.asBytes, blockdata, 4); |
79d7bcbb | 1083 | |
b8dd1ef6 | 1084 | if (hasAuthKey) { |
1085 | c.arg[1] = (keyLen == 16) ? 1 : 2; | |
1086 | memcpy(c.d.asBytes+4, authenticationkey, keyLen); | |
afceaf40 | 1087 | } |
79d7bcbb | 1088 | |
22342f6d | 1089 | clearCommandBuffer(); |
79d7bcbb | 1090 | SendCommand(&c); |
1091 | UsbCommand resp; | |
b8dd1ef6 | 1092 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) { |
79d7bcbb | 1093 | uint8_t isOK = resp.arg[0] & 0xff; |
4be9f36e | 1094 | PrintAndLogEx(SUCCESS, "isOk:%02x", isOK); |
81740aa5 | 1095 | } else { |
4be9f36e | 1096 | PrintAndLogEx(ERR, "Command execute timeout"); |
afceaf40 | 1097 | } |
79d7bcbb | 1098 | |
b8dd1ef6 | 1099 | DropField(); |
afceaf40 | 1100 | return 0; |
81740aa5 | 1101 | } |
4be9f36e | 1102 | |
1103 | ||
81740aa5 | 1104 | // |
79d7bcbb | 1105 | // Read Single Block |
81740aa5 | 1106 | // |
4be9f36e | 1107 | static int usage_hf_mfu_rdbl(void) { |
1108 | PrintAndLogEx(NORMAL, "Read a block and print. It autodetects card type.\n"); | |
1109 | PrintAndLogEx(NORMAL, "Usage: hf mfu rdbl b <block number> k <key> l\n"); | |
1110 | PrintAndLogEx(NORMAL, " Options:"); | |
1111 | PrintAndLogEx(NORMAL, " b <no> : block to read"); | |
1112 | PrintAndLogEx(NORMAL, " k <key> : (optional) key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]"); | |
1113 | PrintAndLogEx(NORMAL, " l : (optional) swap entered key's endianness"); | |
1114 | PrintAndLogEx(NORMAL, ""); | |
1115 | PrintAndLogEx(NORMAL, " sample : hf mfu rdbl b 0"); | |
1116 | PrintAndLogEx(NORMAL, " : hf mfu rdbl b 0 k 00112233445566778899AABBCCDDEEFF"); | |
1117 | PrintAndLogEx(NORMAL, " : hf mfu rdbl b 0 k AABBCCDDD\n"); | |
1118 | return 0; | |
1119 | } | |
81740aa5 | 1120 | |
b8dd1ef6 | 1121 | |
4be9f36e | 1122 | static int CmdHF14AMfURdBl(const char *Cmd){ |
1123 | ||
1124 | int blockNo = -1; | |
79d7bcbb | 1125 | bool errors = false; |
b8dd1ef6 | 1126 | uint8_t keybytes[16] = {0x00}; |
1127 | uint8_t *authenticationkey = keybytes; | |
1128 | int keyLen = 0; | |
79d7bcbb | 1129 | bool hasAuthKey = false; |
79d7bcbb | 1130 | bool swapEndian = false; |
1131 | uint8_t cmdp = 0; | |
f168b263 | 1132 | |
79d7bcbb | 1133 | while(param_getchar(Cmd, cmdp) != 0x00) |
1134 | { | |
1135 | switch(param_getchar(Cmd, cmdp)) | |
1136 | { | |
1137 | case 'h': | |
1138 | case 'H': | |
1139 | return usage_hf_mfu_rdbl(); | |
1140 | case 'k': | |
1141 | case 'K': | |
b8dd1ef6 | 1142 | keyLen = 32; |
1143 | errors = param_gethex_ex(Cmd, cmdp+1, authenticationkey, &keyLen); | |
1144 | if (errors || (keyLen != 32 && keyLen != 8)) { //ul-c or ev1/ntag key length | |
1145 | PrintAndLogEx(ERR, "Key has incorrect length.\n"); | |
1146 | errors = true; | |
79d7bcbb | 1147 | } |
b8dd1ef6 | 1148 | cmdp += 2; |
1149 | keyLen /= 2; | |
1150 | hasAuthKey = true; | |
79d7bcbb | 1151 | break; |
1152 | case 'b': | |
1153 | case 'B': | |
1154 | blockNo = param_get8(Cmd, cmdp+1); | |
79d7bcbb | 1155 | if (blockNo < 0) { |
4be9f36e | 1156 | PrintAndLogEx(ERR, "Wrong block number"); |
79d7bcbb | 1157 | errors = true; |
1158 | } | |
79d7bcbb | 1159 | cmdp += 2; |
1160 | break; | |
1161 | case 'l': | |
1162 | case 'L': | |
1163 | swapEndian = true; | |
1164 | cmdp++; | |
1165 | break; | |
1166 | default: | |
4be9f36e | 1167 | PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp)); |
79d7bcbb | 1168 | errors = true; |
1169 | break; | |
1170 | } | |
1171 | //Validations | |
b8dd1ef6 | 1172 | if (errors) return usage_hf_mfu_rdbl(); |
afceaf40 | 1173 | } |
f168b263 | 1174 | |
b8dd1ef6 | 1175 | if (blockNo == -1) return usage_hf_mfu_rdbl(); |
2ec87733 | 1176 | // start with getting tagtype |
1177 | TagTypeUL_t tagtype = GetHF14AMfU_Type(); | |
b8dd1ef6 | 1178 | if (tagtype == UL_ERROR) { |
1179 | return -1; | |
1180 | } | |
2ec87733 | 1181 | |
1182 | uint8_t maxblockno = 0; | |
b8dd1ef6 | 1183 | for (uint8_t idx = 0; idx < MAX_UL_TYPES; idx++) { |
1184 | if (tagtype & UL_TYPES_ARRAY[idx]) { | |
2ec87733 | 1185 | maxblockno = UL_MEMORY_ARRAY[idx]; |
b8dd1ef6 | 1186 | break; |
1187 | } | |
2ec87733 | 1188 | } |
1189 | if (blockNo > maxblockno){ | |
b8dd1ef6 | 1190 | DropField(); |
4be9f36e | 1191 | PrintAndLogEx(WARNING, "block number to large. Max block is %u/0x%02X \n", maxblockno,maxblockno); |
2ec87733 | 1192 | return usage_hf_mfu_rdbl(); |
1193 | } | |
afceaf40 | 1194 | |
4be9f36e | 1195 | // Swap endianness |
b8dd1ef6 | 1196 | if (swapEndian) authenticationkey = SwapEndian64(authenticationkey, keyLen, (keyLen == 16) ? 8 : 4); |
f168b263 | 1197 | |
79d7bcbb | 1198 | //Read Block |
1199 | UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}}; | |
b8dd1ef6 | 1200 | if (hasAuthKey) { |
1201 | c.arg[1] = (keyLen == 16) ? 1 : 2; | |
1202 | memcpy(c.d.asBytes, authenticationkey, keyLen); | |
79d7bcbb | 1203 | } |
1204 | ||
22342f6d | 1205 | clearCommandBuffer(); |
79d7bcbb | 1206 | SendCommand(&c); |
1207 | UsbCommand resp; | |
b8dd1ef6 | 1208 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) { |
f168b263 | 1209 | uint8_t isOK = resp.arg[0] & 0xff; |
1210 | if (isOK) { | |
1211 | uint8_t *data = resp.d.asBytes; | |
4be9f36e | 1212 | PrintAndLogEx(NORMAL, "\n Block# | Data | Ascii"); |
1213 | PrintAndLogEx(NORMAL, "---------+-------------+------"); | |
1214 | PrintAndLogEx(NORMAL, " %02d/0x%02X | %s| %s\n", blockNo, blockNo, sprint_hex(data, 4), sprint_ascii(data, 4)); | |
1215 | } else { | |
1216 | PrintAndLogEx(ERR, "Failed reading block: (%02x)", isOK); | |
f168b263 | 1217 | } |
81740aa5 | 1218 | } else { |
4be9f36e | 1219 | PrintAndLogEx(ERR, "Command execute time-out"); |
afceaf40 | 1220 | } |
b8dd1ef6 | 1221 | DropField(); |
afceaf40 | 1222 | return 0; |
81740aa5 | 1223 | } |
1224 | ||
79d7bcbb | 1225 | |
81740aa5 | 1226 | // |
92690507 | 1227 | // Mifare Ultralight / Ultralight-C / Ultralight-EV1 |
1228 | // Read and Dump Card Contents, using auto detection of tag size. | |
b8dd1ef6 | 1229 | |
1230 | typedef struct { | |
1231 | uint8_t version[8]; | |
1232 | uint8_t tbo[2]; | |
1233 | uint8_t tbo1[1]; | |
1234 | uint8_t pages; // max page number in dump | |
1235 | uint8_t signature[32]; | |
1236 | uint8_t counter_tearing[3][4]; // 3 bytes counter, 1 byte tearing flag | |
1237 | uint8_t data[1024]; | |
1238 | } mfu_dump_t; | |
1239 | ||
1240 | ||
1241 | static void printMFUdumpEx(mfu_dump_t *card, uint16_t pages, uint8_t startpage, TagTypeUL_t tagtype) { | |
1242 | ||
1243 | bool tmplockbit = false; | |
1244 | bool bit[16] = {false}; | |
1245 | bool bit2[16] = {false}; | |
1246 | ||
1247 | // standard lock bits | |
1248 | for(int i = 0; i < 16; i++){ | |
1249 | bit[i] = card->data[10+i/8] & (1 << (7-i%8)); | |
1250 | } | |
1251 | ||
1252 | // dynamic lock bits | |
1253 | // TODO -- FIGURE OUT LOCK BYTES FOR EV1 and/or NTAG | |
1254 | if (tagtype & UL_C) { | |
1255 | for (int i = 0; i < 16; i++) { | |
1256 | bit2[i] = card->data[40*4+i/8] & (1 << (7-i%8)); | |
1257 | } | |
1258 | } | |
1259 | ||
1260 | PrintAndLogEx(NORMAL, "\n Block# | Data |lck| Ascii"); | |
1261 | PrintAndLogEx(NORMAL, "---------+-------------+---+------"); | |
1262 | ||
1263 | for (int i = startpage; i < startpage + pages; i++) { | |
1264 | if (i < 3) { | |
1265 | PrintAndLogEx(NORMAL, "%3d/0x%02X | %s| | ", i, i, sprint_hex(card->data + i * 4, 4)); | |
1266 | continue; | |
1267 | } | |
1268 | switch(i){ | |
1269 | case 3: tmplockbit = bit[4]; break; | |
1270 | case 4: tmplockbit = bit[3]; break; | |
1271 | case 5: tmplockbit = bit[2]; break; | |
1272 | case 6: tmplockbit = bit[1]; break; | |
1273 | case 7: tmplockbit = bit[0]; break; | |
1274 | case 8: tmplockbit = bit[15]; break; | |
1275 | case 9: tmplockbit = bit[14]; break; | |
1276 | case 10: tmplockbit = bit[13]; break; | |
1277 | case 11: tmplockbit = bit[12]; break; | |
1278 | case 12: tmplockbit = bit[11]; break; | |
1279 | case 13: tmplockbit = bit[10]; break; | |
1280 | case 14: tmplockbit = bit[9]; break; | |
1281 | case 15: tmplockbit = bit[8]; break; | |
1282 | case 16: | |
1283 | case 17: | |
1284 | case 18: | |
1285 | case 19: tmplockbit = bit2[6]; break; | |
1286 | case 20: | |
1287 | case 21: | |
1288 | case 22: | |
1289 | case 23: tmplockbit = bit2[5]; break; | |
1290 | case 24: | |
1291 | case 25: | |
1292 | case 26: | |
1293 | case 27: tmplockbit = bit2[4]; break; | |
1294 | case 28: | |
1295 | case 29: | |
1296 | case 30: | |
1297 | case 31: tmplockbit = bit2[2]; break; | |
1298 | case 32: | |
1299 | case 33: | |
1300 | case 34: | |
1301 | case 35: tmplockbit = bit2[1]; break; | |
1302 | case 36: | |
1303 | case 37: | |
1304 | case 38: | |
1305 | case 39: tmplockbit = bit2[0]; break; | |
1306 | case 40: tmplockbit = bit2[12]; break; | |
1307 | case 41: tmplockbit = bit2[11]; break; | |
1308 | case 42: tmplockbit = bit2[10]; break; //auth0 | |
1309 | case 43: tmplockbit = bit2[9]; break; //auth1 | |
1310 | default: break; | |
1311 | } | |
1312 | ||
1313 | PrintAndLogEx(NORMAL, "%3d/0x%02X | %s| %d | %.4s", i, i, sprint_hex(card->data + i * 4, 4), tmplockbit, sprint_ascii(card->data + i * 4, 4)); | |
1314 | } | |
1315 | PrintAndLogEx(NORMAL, "---------------------------------"); | |
1316 | } | |
1317 | ||
1318 | ||
4be9f36e | 1319 | static int usage_hf_mfu_dump(void) { |
1320 | PrintAndLogEx(NORMAL, "Reads all pages from Ultralight, Ultralight-C, Ultralight EV1"); | |
1321 | PrintAndLogEx(NORMAL, "NTAG 203, NTAG 210, NTAG 212, NTAG 213, NTAG 215, NTAG 216"); | |
1322 | PrintAndLogEx(NORMAL, "and saves binary dump into the file `filename.bin` or `cardUID.bin`"); | |
1323 | PrintAndLogEx(NORMAL, "It autodetects card type.\n"); | |
1324 | PrintAndLogEx(NORMAL, "Usage: hf mfu dump k <key> l n <filename w/o .bin>"); | |
1325 | PrintAndLogEx(NORMAL, " Options : "); | |
1326 | PrintAndLogEx(NORMAL, " k <key> : (optional) key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]"); | |
1327 | PrintAndLogEx(NORMAL, " l : (optional) swap entered key's endianness"); | |
b8dd1ef6 | 1328 | PrintAndLogEx(NORMAL, " f <FN > : filename w/o .bin to save the dump as"); |
4be9f36e | 1329 | PrintAndLogEx(NORMAL, " p <Pg > : starting Page number to manually set a page to start the dump at"); |
1330 | PrintAndLogEx(NORMAL, " q <qty> : number of Pages to manually set how many pages to dump"); | |
1331 | ||
1332 | PrintAndLogEx(NORMAL, ""); | |
1333 | PrintAndLogEx(NORMAL, " sample : hf mfu dump"); | |
1334 | PrintAndLogEx(NORMAL, " : hf mfu dump n myfile"); | |
1335 | PrintAndLogEx(NORMAL, " : hf mfu dump k 00112233445566778899AABBCCDDEEFF"); | |
1336 | PrintAndLogEx(NORMAL, " : hf mfu dump k AABBCCDDD\n"); | |
1337 | return 0; | |
1338 | } | |
1339 | ||
b8dd1ef6 | 1340 | |
4be9f36e | 1341 | static int CmdHF14AMfUDump(const char *Cmd){ |
81740aa5 | 1342 | |
b8dd1ef6 | 1343 | char filename[FILE_PATH_SIZE] = {'\0'}; |
1344 | size_t fileNameLen = 0; | |
1345 | uint8_t keybytes[16] = {0x00}; | |
1346 | uint8_t *authenticationkey = keybytes; | |
1347 | int keyLen = 0; | |
06561c34 | 1348 | bool hasAuthKey = false; |
06561c34 | 1349 | uint8_t cmdp = 0; |
75377d29 | 1350 | bool errors = false; |
1351 | bool swapEndian = false; | |
ae8303c1 | 1352 | bool manualPages = false; |
1353 | uint8_t startPage = 0; | |
b8dd1ef6 | 1354 | int Pages = 16; |
1355 | iso14a_card_select_t card_select; | |
1356 | mfu_dump_t card; | |
cceabb79 | 1357 | |
f9848fd6 | 1358 | while(param_getchar(Cmd, cmdp) != 0x00) |
1359 | { | |
1360 | switch(param_getchar(Cmd, cmdp)) | |
1361 | { | |
1362 | case 'h': | |
1363 | case 'H': | |
1364 | return usage_hf_mfu_dump(); | |
1365 | case 'k': | |
1366 | case 'K': | |
b8dd1ef6 | 1367 | keyLen = 32; |
1368 | errors = param_gethex_ex(Cmd, cmdp+1, authenticationkey, &keyLen); | |
1369 | if (errors || (keyLen != 32 && keyLen != 8)) { //ul-c or ev1/ntag key length | |
1370 | PrintAndLogEx(ERR, "Key has incorrect length.\n"); | |
cceabb79 | 1371 | errors = true; |
9d87eb66 | 1372 | } |
f9848fd6 | 1373 | cmdp += 2; |
b8dd1ef6 | 1374 | keyLen /= 2; |
06561c34 | 1375 | hasAuthKey = true; |
f9848fd6 | 1376 | break; |
cceabb79 | 1377 | case 'l': |
1378 | case 'L': | |
1379 | swapEndian = true; | |
1380 | cmdp++; | |
1381 | break; | |
b8dd1ef6 | 1382 | case 'f': |
1383 | case 'F': | |
1384 | fileNameLen = param_getstr(Cmd, cmdp+1, filename, sizeof(filename)); | |
1385 | if (fileNameLen == 0) errors = true; | |
1386 | if (fileNameLen > FILE_PATH_SIZE-5) fileNameLen = FILE_PATH_SIZE-5; | |
f9848fd6 | 1387 | cmdp += 2; |
1388 | break; | |
ae8303c1 | 1389 | case 'p': |
1390 | case 'P': | |
1391 | startPage = param_get8(Cmd, cmdp+1); | |
1392 | manualPages = true; | |
1393 | cmdp += 2; | |
1394 | break; | |
1395 | case 'q': | |
1396 | case 'Q': | |
1397 | Pages = param_get8(Cmd, cmdp+1); | |
1398 | cmdp += 2; | |
1399 | manualPages = true; | |
1400 | break; | |
f9848fd6 | 1401 | default: |
b8dd1ef6 | 1402 | PrintAndLogEx(ERR, "Unknown parameter '%c'", param_getchar(Cmd, cmdp)); |
f9848fd6 | 1403 | errors = true; |
1404 | break; | |
1405 | } | |
b8dd1ef6 | 1406 | if (errors) break; |
f9848fd6 | 1407 | } |
1408 | ||
1409 | //Validations | |
b8dd1ef6 | 1410 | if (errors) return usage_hf_mfu_dump(); |
75377d29 | 1411 | |
4be9f36e | 1412 | if (swapEndian && hasAuthKey) |
b8dd1ef6 | 1413 | authenticationkey = SwapEndian64(authenticationkey, keyLen, (keyLen == 16) ? 8 : 4); |
81740aa5 | 1414 | |
92690507 | 1415 | TagTypeUL_t tagtype = GetHF14AMfU_Type(); |
81740aa5 | 1416 | |
b8dd1ef6 | 1417 | if (tagtype == UL_ERROR) { |
1418 | return -1; | |
1419 | } | |
1420 | ||
1421 | uint8_t maxPages = 0; | |
1422 | for (uint8_t idx = 0; idx < MAX_UL_TYPES; idx++) { | |
1423 | if (tagtype & UL_TYPES_ARRAY[idx]) { | |
1424 | maxPages = UL_MEMORY_ARRAY[idx]+1; //add one as maxblks starts at 0 | |
1425 | break; | |
1426 | } | |
1427 | } | |
1428 | ||
1429 | if (!manualPages) { | |
1430 | Pages = maxPages; | |
1431 | } else { | |
1432 | if (startPage + Pages - 1 > maxPages - 1) { | |
1433 | PrintAndLogEx(ERR, "Invalid page range. Card has only %d readable pages.", maxPages); | |
1434 | DropField(); | |
1435 | return 1; | |
1436 | } | |
1437 | } | |
ae8303c1 | 1438 | |
5b99376a | 1439 | ul_print_type(tagtype, 0); |
b8dd1ef6 | 1440 | |
4be9f36e | 1441 | PrintAndLogEx(NORMAL, "Reading tag memory..."); |
b8dd1ef6 | 1442 | memset(&card, 0x00, sizeof(card)); |
1443 | UsbCommand c = {CMD_MIFAREU_READCARD, {startPage, Pages}}; | |
1444 | if (hasAuthKey) { | |
cceabb79 | 1445 | if (tagtype & UL_C) |
1446 | c.arg[2] = 1; //UL_C auth | |
1447 | else | |
1448 | c.arg[2] = 2; //UL_EV1/NTAG auth | |
b8dd1ef6 | 1449 | memcpy(c.d.asBytes, authenticationkey, keyLen); |
f168b263 | 1450 | } |
22342f6d | 1451 | |
1452 | clearCommandBuffer(); | |
cceabb79 | 1453 | SendCommand(&c); |
1454 | UsbCommand resp; | |
b8dd1ef6 | 1455 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) { |
1456 | PrintAndLogEx(ERR, "Command execution timeout"); | |
1457 | DropField(); | |
cceabb79 | 1458 | return 1; |
92690507 | 1459 | } |
9d87eb66 | 1460 | if (resp.arg[0] != 1) { |
b8dd1ef6 | 1461 | PrintAndLogEx(ERR, "Failed reading card"); |
1462 | DropField(); | |
cceabb79 | 1463 | return 1; |
92690507 | 1464 | } |
92690507 | 1465 | |
0ce03d9a | 1466 | uint32_t startindex = resp.arg[2]; |
9d87eb66 | 1467 | uint32_t bufferSize = resp.arg[1]; |
b8dd1ef6 | 1468 | if (bufferSize > sizeof(card.data)) { |
4be9f36e | 1469 | PrintAndLogEx(FAILED, "Data exceeded Buffer size!"); |
b8dd1ef6 | 1470 | bufferSize = sizeof(card.data); |
9d87eb66 | 1471 | } |
9d87eb66 | 1472 | |
b8dd1ef6 | 1473 | if (!GetFromBigBuf(card.data + startPage*4, bufferSize, startindex, NULL, -1, false)) { |
1474 | PrintAndLogEx(ERR, "Command execution timeout"); | |
1475 | DropField(); | |
1476 | return 1; | |
92690507 | 1477 | } |
1478 | ||
b8dd1ef6 | 1479 | // not ul_c and not std ul then attempt to collect |
1480 | // VERSION, SIGNATURE, COUNTERS, TEARING, PACK | |
1481 | if (!(tagtype & UL_C || tagtype & UL)) { | |
1482 | //attempt to read pack | |
1483 | if (!ul_auth_select(&card_select, tagtype, true, authenticationkey, card.data + maxPages*4 - 4, 2)) { | |
1484 | //reset pack | |
1485 | card.data[maxPages*4 - 4] = 0; | |
1486 | card.data[maxPages*4 - 3] = 0; | |
81740aa5 | 1487 | } |
81740aa5 | 1488 | |
b8dd1ef6 | 1489 | if (hasAuthKey) { |
1490 | uint8_t dummy_pack[2]; | |
1491 | ul_auth_select(&card_select, tagtype, hasAuthKey, authenticationkey, dummy_pack, sizeof(dummy_pack)); | |
06561c34 | 1492 | } else { |
b8dd1ef6 | 1493 | ul_select(&card_select, false); |
1494 | } | |
1495 | ulev1_getVersion(card.version, sizeof(card.version)); | |
1496 | for (uint8_t n = 0; n < 3; ++n) { | |
1497 | ulev1_readTearing(n, &card.counter_tearing[n][3], 1); | |
1498 | ulev1_readCounter(n, &card.counter_tearing[n][0], 3); | |
012c0761 | 1499 | } |
1500 | ||
b8dd1ef6 | 1501 | ulev1_readSignature(card.signature, sizeof(card.signature)); |
1502 | } | |
1503 | ||
1504 | DropField(); | |
1505 | ||
1506 | // add key to dump data | |
1507 | if (hasAuthKey) { | |
1508 | authenticationkey = SwapEndian64(authenticationkey, keyLen, (keyLen == 16) ? 8 : 4); | |
1509 | if (tagtype & UL_C){ // additional 4 pages | |
1510 | memcpy(card.data + maxPages*4, authenticationkey, keyLen); | |
1511 | maxPages += 4; | |
012c0761 | 1512 | } else { // 2nd page from end |
b8dd1ef6 | 1513 | memcpy(card.data + (maxPages*4) - 8, authenticationkey, 4); |
06561c34 | 1514 | } |
9d87eb66 | 1515 | } |
cceabb79 | 1516 | |
b8dd1ef6 | 1517 | printMFUdumpEx(&card, Pages, startPage, tagtype); |
1518 | ||
1519 | if (!manualPages) { | |
1520 | // user supplied filename? | |
1521 | if (fileNameLen < 1) { | |
1522 | char *fptr = filename; | |
1523 | fptr += sprintf(fptr, "hf-mfu-"); | |
1524 | uint8_t UID[] = {card.data[0], card.data[1], card.data[2], card.data[4], card.data[5], card.data[6], card.data[7]}; | |
1525 | FillFileNameByUID(fptr, UID, "-dump.bin", 7); | |
1526 | } else { | |
1527 | sprintf(filename + fileNameLen, ".bin"); | |
81740aa5 | 1528 | } |
4bbf5ad1 | 1529 | |
b8dd1ef6 | 1530 | #define MFU_DUMP_PREFIX_LENGTH (sizeof(card) - sizeof(card.data)) |
4bbf5ad1 | 1531 | |
b8dd1ef6 | 1532 | FILE *fout; |
1533 | if ((fout = fopen(filename, "wb")) == NULL) { | |
1534 | PrintAndLogEx(ERR, "Could not create file name %s", filename); | |
1535 | return 1; | |
1536 | } | |
1537 | fwrite(&card, 1, MFU_DUMP_PREFIX_LENGTH + maxPages*4, fout); | |
1538 | fclose(fout); | |
22342f6d | 1539 | |
b8dd1ef6 | 1540 | PrintAndLogEx(SUCCESS, "Dumped %d pages, wrote %d bytes to %s", maxPages, MFU_DUMP_PREFIX_LENGTH + maxPages*4, filename); |
81740aa5 | 1541 | } |
1542 | ||
afceaf40 | 1543 | return 0; |
81740aa5 | 1544 | } |
1545 | ||
b8dd1ef6 | 1546 | |
81740aa5 | 1547 | //------------------------------------------------------------------------------- |
1548 | // Ultralight C Methods | |
1549 | //------------------------------------------------------------------------------- | |
1550 | ||
1551 | // | |
1552 | // Ultralight C Authentication Demo {currently uses hard-coded key} | |
1553 | // | |
4be9f36e | 1554 | static int CmdHF14AMfucAuth(const char *Cmd){ |
afceaf40 | 1555 | |
9d87eb66 | 1556 | uint8_t keyNo = 3; |
afceaf40 | 1557 | bool errors = false; |
f168b263 | 1558 | |
1559 | char cmdp = param_getchar(Cmd, 0); | |
1560 | ||
afceaf40 MHS |
1561 | //Change key to user defined one |
1562 | if (cmdp == 'k' || cmdp == 'K'){ | |
1563 | keyNo = param_get8(Cmd, 1); | |
4be9f36e | 1564 | if(keyNo > KEYS_3DES_COUNT-1) |
f168b263 | 1565 | errors = true; |
afceaf40 MHS |
1566 | } |
1567 | ||
f168b263 | 1568 | if (cmdp == 'h' || cmdp == 'H') |
afceaf40 | 1569 | errors = true; |
4be9f36e | 1570 | |
afceaf40 | 1571 | if (errors) { |
4be9f36e | 1572 | PrintAndLogEx(NORMAL, "Usage: hf mfu cauth k <key number>"); |
1573 | PrintAndLogEx(NORMAL, " 0 (default): 3DES standard key"); | |
1574 | PrintAndLogEx(NORMAL, " 1 : all 0x00 key"); | |
1575 | PrintAndLogEx(NORMAL, " 2 : 0x00-0x0F key"); | |
1576 | PrintAndLogEx(NORMAL, " 3 : nfc key"); | |
1577 | PrintAndLogEx(NORMAL, " 4 : all 0x01 key"); | |
1578 | PrintAndLogEx(NORMAL, " 5 : all 0xff key"); | |
1579 | PrintAndLogEx(NORMAL, " 6 : 0x00-0xFF key"); | |
1580 | PrintAndLogEx(NORMAL, "\n sample : hf mfu cauth k"); | |
1581 | PrintAndLogEx(NORMAL, " : hf mfu cauth k 3"); | |
afceaf40 | 1582 | return 0; |
4be9f36e | 1583 | } |
afceaf40 | 1584 | |
f168b263 | 1585 | uint8_t *key = default_3des_keys[keyNo]; |
b8dd1ef6 | 1586 | if (ulc_authentication(key, true)) { |
1587 | DropField(); | |
4be9f36e | 1588 | PrintAndLogEx(SUCCESS, "Authentication successful. 3des key: %s",sprint_hex(key, 16)); |
b8dd1ef6 | 1589 | } else { |
1590 | DropField(); | |
4be9f36e | 1591 | PrintAndLogEx(WARNING, "Authentication failed"); |
afceaf40 | 1592 | } |
4be9f36e | 1593 | return 0; |
afceaf40 | 1594 | } |
b8dd1ef6 | 1595 | |
f9848fd6 | 1596 | |
4be9f36e | 1597 | // |
f168b263 | 1598 | // Mifare Ultralight C - Set password |
1599 | // | |
4be9f36e | 1600 | static int CmdHF14AMfucSetPwd(const char *Cmd){ |
f168b263 | 1601 | |
1602 | uint8_t pwd[16] = {0x00}; | |
4be9f36e | 1603 | |
f168b263 | 1604 | char cmdp = param_getchar(Cmd, 0); |
4be9f36e | 1605 | |
1606 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') { | |
1607 | PrintAndLogEx(NORMAL, "Usage: hf mfu setpwd <password (32 hex symbols)>"); | |
1608 | PrintAndLogEx(NORMAL, " [password] - (32 hex symbols)"); | |
1609 | PrintAndLogEx(NORMAL, ""); | |
1610 | PrintAndLogEx(NORMAL, "sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f"); | |
1611 | PrintAndLogEx(NORMAL, ""); | |
f168b263 | 1612 | return 0; |
1613 | } | |
4be9f36e | 1614 | |
f168b263 | 1615 | if (param_gethex(Cmd, 0, pwd, 32)) { |
4be9f36e | 1616 | PrintAndLogEx(WARNING, "Password must include 32 HEX symbols"); |
f168b263 | 1617 | return 1; |
1618 | } | |
4be9f36e | 1619 | |
1620 | UsbCommand c = {CMD_MIFAREUC_SETPWD}; | |
f168b263 | 1621 | memcpy( c.d.asBytes, pwd, 16); |
22342f6d | 1622 | clearCommandBuffer(); |
f168b263 | 1623 | SendCommand(&c); |
1624 | ||
1625 | UsbCommand resp; | |
4be9f36e | 1626 | |
b8dd1ef6 | 1627 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) { |
1628 | DropField(); | |
1629 | if ((resp.arg[0] & 0xff) == 1) { | |
4be9f36e | 1630 | PrintAndLogEx(INFO, "Ultralight-C new password: %s", sprint_hex(pwd,16)); |
b8dd1ef6 | 1631 | return 0; |
1632 | } else { | |
4be9f36e | 1633 | PrintAndLogEx(ERR, "Failed writing at block %d", resp.arg[1] & 0xff); |
f168b263 | 1634 | return 1; |
1635 | } | |
b8dd1ef6 | 1636 | } else { |
1637 | DropField(); | |
1638 | PrintAndLogEx(ERR, "command execution timeout"); | |
f168b263 | 1639 | return 1; |
1640 | } | |
4be9f36e | 1641 | |
f168b263 | 1642 | return 0; |
1643 | } | |
1644 | ||
1645 | // | |
92690507 | 1646 | // Magic UL / UL-C tags - Set UID |
f168b263 | 1647 | // |
4be9f36e | 1648 | static int CmdHF14AMfucSetUid(const char *Cmd){ |
f168b263 | 1649 | |
1650 | UsbCommand c; | |
1651 | UsbCommand resp; | |
1652 | uint8_t uid[7] = {0x00}; | |
1653 | char cmdp = param_getchar(Cmd, 0); | |
4be9f36e | 1654 | |
1655 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') { | |
1656 | PrintAndLogEx(NORMAL, "Usage: hf mfu setuid <uid (14 hex symbols)>"); | |
1657 | PrintAndLogEx(NORMAL, " [uid] - (14 hex symbols)"); | |
1658 | PrintAndLogEx(NORMAL, "\nThis only works for Magic Ultralight tags."); | |
1659 | PrintAndLogEx(NORMAL, ""); | |
1660 | PrintAndLogEx(NORMAL, "sample: hf mfu setuid 11223344556677"); | |
1661 | PrintAndLogEx(NORMAL, ""); | |
f168b263 | 1662 | return 0; |
1663 | } | |
4be9f36e | 1664 | |
f168b263 | 1665 | if (param_gethex(Cmd, 0, uid, 14)) { |
4be9f36e | 1666 | PrintAndLogEx(WARNING, "UID must include 14 HEX symbols"); |
f168b263 | 1667 | return 1; |
1668 | } | |
1669 | ||
4be9f36e | 1670 | // read block2. |
f168b263 | 1671 | c.cmd = CMD_MIFAREU_READBL; |
1672 | c.arg[0] = 2; | |
22342f6d | 1673 | clearCommandBuffer(); |
f168b263 | 1674 | SendCommand(&c); |
1675 | if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
b8dd1ef6 | 1676 | DropField(); |
4be9f36e | 1677 | PrintAndLogEx(WARNING, "Command execute timeout"); |
f168b263 | 1678 | return 2; |
1679 | } | |
4be9f36e | 1680 | |
f168b263 | 1681 | // save old block2. |
1682 | uint8_t oldblock2[4] = {0x00}; | |
1683 | memcpy(resp.d.asBytes, oldblock2, 4); | |
4be9f36e | 1684 | |
f168b263 | 1685 | // block 0. |
1686 | c.cmd = CMD_MIFAREU_WRITEBL; | |
1687 | c.arg[0] = 0; | |
1688 | c.d.asBytes[0] = uid[0]; | |
1689 | c.d.asBytes[1] = uid[1]; | |
1690 | c.d.asBytes[2] = uid[2]; | |
1691 | c.d.asBytes[3] = 0x88 ^ uid[0] ^ uid[1] ^ uid[2]; | |
22342f6d | 1692 | clearCommandBuffer(); |
f168b263 | 1693 | SendCommand(&c); |
b8dd1ef6 | 1694 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) { |
1695 | DropField(); | |
4be9f36e | 1696 | PrintAndLogEx(WARNING, "Command execute timeout"); |
f168b263 | 1697 | return 3; |
1698 | } | |
4be9f36e | 1699 | |
f168b263 | 1700 | // block 1. |
1701 | c.arg[0] = 1; | |
1702 | c.d.asBytes[0] = uid[3]; | |
1703 | c.d.asBytes[1] = uid[4]; | |
1704 | c.d.asBytes[2] = uid[5]; | |
1705 | c.d.asBytes[3] = uid[6]; | |
22342f6d | 1706 | clearCommandBuffer(); |
f168b263 | 1707 | SendCommand(&c); |
b8dd1ef6 | 1708 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) { |
1709 | DropField(); | |
4be9f36e | 1710 | PrintAndLogEx(WARNING, "Command execute timeout"); |
f168b263 | 1711 | return 4; |
1712 | } | |
1713 | ||
1714 | // block 2. | |
1715 | c.arg[0] = 2; | |
1716 | c.d.asBytes[0] = uid[3] ^ uid[4] ^ uid[5] ^ uid[6]; | |
1717 | c.d.asBytes[1] = oldblock2[1]; | |
1718 | c.d.asBytes[2] = oldblock2[2]; | |
1719 | c.d.asBytes[3] = oldblock2[3]; | |
22342f6d | 1720 | clearCommandBuffer(); |
f168b263 | 1721 | SendCommand(&c); |
b8dd1ef6 | 1722 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) { |
1723 | DropField(); | |
4be9f36e | 1724 | PrintAndLogEx(WARNING, "Command execute timeout"); |
f168b263 | 1725 | return 5; |
1726 | } | |
4be9f36e | 1727 | |
b8dd1ef6 | 1728 | DropField(); |
f168b263 | 1729 | return 0; |
1730 | } | |
1731 | ||
b8dd1ef6 | 1732 | |
4be9f36e | 1733 | static int CmdHF14AMfuGenDiverseKeys(const char *Cmd){ |
79d7bcbb | 1734 | |
f168b263 | 1735 | uint8_t iv[8] = { 0x00 }; |
1736 | uint8_t block = 0x07; | |
79d7bcbb | 1737 | |
f168b263 | 1738 | // UL-EV1 |
1739 | //04 57 b6 e2 05 3f 80 UID | |
1740 | //4a f8 4b 19 PWD | |
1741 | uint8_t uid[] = { 0xF4,0xEA, 0x54, 0x8E }; | |
1742 | uint8_t mifarekeyA[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 }; | |
1743 | uint8_t mifarekeyB[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 }; | |
1744 | uint8_t dkeyA[8] = { 0x00 }; | |
1745 | uint8_t dkeyB[8] = { 0x00 }; | |
4be9f36e | 1746 | |
f168b263 | 1747 | uint8_t masterkey[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff }; |
4be9f36e | 1748 | |
f168b263 | 1749 | uint8_t mix[8] = { 0x00 }; |
1750 | uint8_t divkey[8] = { 0x00 }; | |
79d7bcbb | 1751 | |
f168b263 | 1752 | memcpy(mix, mifarekeyA, 4); |
79d7bcbb | 1753 | |
f168b263 | 1754 | mix[4] = mifarekeyA[4] ^ uid[0]; |
1755 | mix[5] = mifarekeyA[5] ^ uid[1]; | |
1756 | mix[6] = block ^ uid[2]; | |
1757 | mix[7] = uid[3]; | |
79d7bcbb | 1758 | |
aa0b1c43 | 1759 | mbedtls_des3_context ctx = { {0} }; |
700d8687 | 1760 | mbedtls_des3_set2key_enc(&ctx, masterkey); |
f168b263 | 1761 | |
700d8687 OM |
1762 | mbedtls_des3_crypt_cbc(&ctx // des3_context |
1763 | , MBEDTLS_DES_ENCRYPT // int mode | |
f168b263 | 1764 | , sizeof(mix) // length |
1765 | , iv // iv[8] | |
1766 | , mix // input | |
1767 | , divkey // output | |
1768 | ); | |
1769 | ||
4be9f36e | 1770 | PrintAndLogEx(NORMAL, "-- 3DES version"); |
1771 | PrintAndLogEx(NORMAL, "Masterkey :\t %s", sprint_hex(masterkey,sizeof(masterkey))); | |
1772 | PrintAndLogEx(NORMAL, "UID :\t %s", sprint_hex(uid, sizeof(uid))); | |
1773 | PrintAndLogEx(NORMAL, "Block :\t %0d", block); | |
1774 | PrintAndLogEx(NORMAL, "Mifare key :\t %s", sprint_hex(mifarekeyA, sizeof(mifarekeyA))); | |
1775 | PrintAndLogEx(NORMAL, "Message :\t %s", sprint_hex(mix, sizeof(mix))); | |
1776 | PrintAndLogEx(NORMAL, "Diversified key: %s", sprint_hex(divkey+1, 6)); | |
79d7bcbb | 1777 | |
f168b263 | 1778 | for (int i=0; i < sizeof(mifarekeyA); ++i){ |
1779 | dkeyA[i] = (mifarekeyA[i] << 1) & 0xff; | |
1780 | dkeyA[6] |= ((mifarekeyA[i] >> 7) & 1) << (i+1); | |
1781 | } | |
4be9f36e | 1782 | |
f168b263 | 1783 | for (int i=0; i < sizeof(mifarekeyB); ++i){ |
1784 | dkeyB[1] |= ((mifarekeyB[i] >> 7) & 1) << (i+1); | |
1785 | dkeyB[2+i] = (mifarekeyB[i] << 1) & 0xff; | |
1786 | } | |
4be9f36e | 1787 | |
f168b263 | 1788 | uint8_t zeros[8] = {0x00}; |
1789 | uint8_t newpwd[8] = {0x00}; | |
1790 | uint8_t dmkey[24] = {0x00}; | |
1791 | memcpy(dmkey, dkeyA, 8); | |
1792 | memcpy(dmkey+8, dkeyB, 8); | |
1793 | memcpy(dmkey+16, dkeyA, 8); | |
1794 | memset(iv, 0x00, 8); | |
79d7bcbb | 1795 | |
700d8687 | 1796 | mbedtls_des3_set3key_enc(&ctx, dmkey); |
f168b263 | 1797 | |
700d8687 OM |
1798 | mbedtls_des3_crypt_cbc(&ctx // des3_context |
1799 | , MBEDTLS_DES_ENCRYPT // int mode | |
f168b263 | 1800 | , sizeof(newpwd) // length |
1801 | , iv // iv[8] | |
1802 | , zeros // input | |
1803 | , newpwd // output | |
1804 | ); | |
4be9f36e | 1805 | |
1806 | PrintAndLogEx(NORMAL, "\n-- DES version"); | |
1807 | PrintAndLogEx(NORMAL, "Mifare dkeyA :\t %s", sprint_hex(dkeyA, sizeof(dkeyA))); | |
1808 | PrintAndLogEx(NORMAL, "Mifare dkeyB :\t %s", sprint_hex(dkeyB, sizeof(dkeyB))); | |
1809 | PrintAndLogEx(NORMAL, "Mifare ABA :\t %s", sprint_hex(dmkey, sizeof(dmkey))); | |
1810 | PrintAndLogEx(NORMAL, "Mifare Pwd :\t %s", sprint_hex(newpwd, sizeof(newpwd))); | |
1811 | ||
f168b263 | 1812 | return 0; |
1813 | } | |
1814 | ||
1815 | // static uint8_t * diversify_key(uint8_t * key){ | |
4be9f36e | 1816 | |
f168b263 | 1817 | // for(int i=0; i<16; i++){ |
1818 | // if(i<=6) key[i]^=cuid[i]; | |
1819 | // if(i>6) key[i]^=cuid[i%7]; | |
1820 | // } | |
1821 | // return key; | |
1822 | // } | |
1823 | ||
1824 | // static void GenerateUIDe( uint8_t *uid, uint8_t len){ | |
1825 | // for (int i=0; i<len; ++i){ | |
4be9f36e | 1826 | |
f168b263 | 1827 | // } |
1828 | // return; | |
1829 | // } | |
1830 | ||
81740aa5 | 1831 | //------------------------------------ |
1832 | // Menu Stuff | |
1833 | //------------------------------------ | |
4be9f36e | 1834 | static int CmdHelp(const char *Cmd); |
1835 | ||
81740aa5 | 1836 | static command_t CommandTable[] = |
1837 | { | |
4be9f36e | 1838 | {"help", CmdHelp, 1, "This help"}, |
1839 | {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"}, | |
1840 | {"info", CmdHF14AMfUInfo, 0, "Tag information"}, | |
1841 | {"dump", CmdHF14AMfUDump, 0, "Dump Ultralight / Ultralight-C / NTAG tag to binary file"}, | |
b8dd1ef6 | 1842 | // {"restore", CmdHF14AMfURestore, 0, "Restore a dump onto a MFU MAGIC tag"}, |
4be9f36e | 1843 | {"rdbl", CmdHF14AMfURdBl, 0, "Read block"}, |
1844 | {"wrbl", CmdHF14AMfUWrBl, 0, "Write block"}, | |
1845 | {"cauth", CmdHF14AMfucAuth, 0, "Authentication - Ultralight C"}, | |
1846 | {"setpwd", CmdHF14AMfucSetPwd, 0, "Set 3des password - Ultralight-C"}, | |
1847 | {"setuid", CmdHF14AMfucSetUid, 0, "Set UID - MAGIC tags only"}, | |
1848 | {"gen", CmdHF14AMfuGenDiverseKeys, 1, "Generate 3des mifare diversified keys"}, | |
afceaf40 | 1849 | {NULL, NULL, 0, NULL} |
81740aa5 | 1850 | }; |
1851 | ||
1852 | int CmdHFMFUltra(const char *Cmd){ | |
44964fd1 | 1853 | (void)WaitForResponseTimeout(CMD_ACK,NULL,100); |
afceaf40 MHS |
1854 | CmdsParse(CommandTable, Cmd); |
1855 | return 0; | |
81740aa5 | 1856 | } |
1857 | ||
4be9f36e | 1858 | static int CmdHelp(const char *Cmd){ |
afceaf40 MHS |
1859 | CmdsHelp(CommandTable); |
1860 | return 0; | |
f168b263 | 1861 | } |