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