]>
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 | //----------------------------------------------------------------------------- | |
afceaf40 | 10 | #include "loclass/des.h" |
81740aa5 | 11 | #include "cmdhfmfu.h" |
12 | #include "cmdhfmf.h" | |
13 | #include "cmdhf14a.h" | |
5d554ea6 | 14 | #include "mifare.h" |
2c74558d | 15 | #include "util.h" |
16 | #include "../common/protocols.h" | |
81740aa5 | 17 | |
2c74558d | 18 | #define MAX_UL_BLOCKS 0x0f |
19 | #define MAX_ULC_BLOCKS 0x2f | |
20 | #define MAX_ULEV1a_BLOCKS 0x0b | |
21 | #define MAX_ULEV1b_BLOCKS 0x20 | |
22 | #define MAX_NTAG_213 0x2c | |
23 | #define MAX_NTAG_215 0x86 | |
2b03dea7 | 24 | #define MAX_NTAG_216 0xe6 |
2c74558d | 25 | |
26 | typedef enum TAGTYPE_UL { | |
2b03dea7 | 27 | UNKNOWN = 0x0000, |
28 | UL = 0x0001, | |
29 | UL_C = 0x0002, | |
30 | UL_EV1_48 = 0x0004, | |
31 | UL_EV1_128 = 0x0008, | |
32 | NTAG = 0x0010, | |
33 | NTAG_213 = 0x0020, | |
34 | NTAG_215 = 0x0040, | |
35 | NTAG_216 = 0x0080, | |
36 | MAGIC = 0x0100, | |
2c74558d | 37 | UL_MAGIC = UL | MAGIC, |
38 | UL_C_MAGIC = UL_C | MAGIC, | |
39 | UL_ERROR = 0xFFFF, | |
2c74558d | 40 | } TagTypeUL_t; |
afceaf40 | 41 | |
ebd7412d | 42 | #define KEYS_3DES_COUNT 7 |
43 | uint8_t default_3des_keys[KEYS_3DES_COUNT][16] = { | |
a8be77af | 44 | { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 },// 3des std key |
45 | { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },// all zeroes | |
46 | { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f },// 0x00-0x0F | |
47 | { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 },// NFC-key | |
48 | { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },// all ones | |
49 | { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF },// all FF | |
50 | { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF } // 11 22 33 | |
51 | }; | |
aebe7790 | 52 | |
ebd7412d | 53 | #define KEYS_PWD_COUNT 8 |
54 | uint8_t default_pwd_pack[KEYS_PWD_COUNT][4] = { | |
aebe7790 | 55 | {0xFF,0xFF,0xFF,0xFF}, // PACK 0x00,0x00 -- factory default |
802319a3 | 56 | {0x4A,0xF8,0x4B,0x19}, // PACK 0xE5,0xBE -- italian bus (sniffed) |
ebd7412d | 57 | {0x33,0x6B,0xA1,0x19}, // PACK 0x9c,0x2d -- italian bus (sniffed) |
58 | {0xFF,0x90,0x6C,0xB2}, // PACK 0x12,0x9e -- italian bus (sniffed) | |
59 | {0x05,0x22,0xE6,0xB4}, // PACK 0x80,0x80 -- Amiiboo (sniffed) pikachu-b UID: | |
60 | {0x7E,0x22,0xE6,0xB4}, // PACK 0x80,0x80 -- AMiiboo (sniffed) | |
61 | {0x02,0xE1,0xEE,0x36}, // PACK 0x80,0x80 -- AMiiboo (sniffed) sonic UID: 04d257 7ae33e8027 | |
62 | {0x32,0x0C,0x16,0x17}, // PACK 0x80,0x80 -- AMiiboo (sniffed) | |
63 | ||
aebe7790 | 64 | }; |
a8be77af | 65 | |
81740aa5 | 66 | static int CmdHelp(const char *Cmd); |
67 | ||
2c74558d | 68 | char* getProductTypeStr( uint8_t id){ |
69 | ||
70 | static char buf[20]; | |
71 | char *retStr = buf; | |
72 | ||
73 | switch(id) { | |
74 | case 3: | |
75 | sprintf(retStr, "0x%02X %s", id, "(Ultralight)"); | |
76 | break; | |
77 | case 4: | |
78 | sprintf(retStr, "0x%02X %s", id, "(NTAG)"); | |
79 | break; | |
80 | default: | |
996fda30 | 81 | sprintf(retStr, "0x%02X %s", id, "(unknown)"); |
2c74558d | 82 | break; |
83 | } | |
84 | return buf; | |
85 | } | |
a8be77af | 86 | |
2c74558d | 87 | /* |
88 | The 7 MSBits (=n) code the storage size itself based on 2^n, | |
89 | the LSBit is set to '0' if the size is exactly 2^n | |
90 | and set to '1' if the storage size is between 2^n and 2^(n+1). | |
91 | */ | |
92 | char* getUlev1CardSizeStr( uint8_t fsize ){ | |
93 | ||
94 | static char buf[30]; | |
95 | char *retStr = buf; | |
96 | ||
97 | uint8_t usize = 1 << ((fsize >>1) + 1); | |
98 | uint8_t lsize = 1 << (fsize >>1); | |
99 | ||
100 | // is LSB set? | |
101 | if ( fsize & 1 ) | |
2b03dea7 | 102 | sprintf(retStr, "0x%02X (%u - %u bytes)",fsize, usize, lsize); |
2c74558d | 103 | else |
2b03dea7 | 104 | sprintf(retStr, "0x%02X (%u bytes)", fsize, lsize); |
2c74558d | 105 | return buf; |
106 | } | |
107 | ||
108 | static void ul_switch_on_field(void) { | |
109 | UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}}; | |
a8be77af | 110 | SendCommand(&c); |
2c74558d | 111 | } |
a8be77af | 112 | |
2c74558d | 113 | static void ul_switch_off_field(void) { |
114 | UsbCommand c = {CMD_READER_ISO_14443a, {0, 0, 0}}; | |
115 | SendCommand(&c); | |
a8be77af | 116 | } |
117 | ||
8297860e | 118 | static int ul_send_cmd_raw( uint8_t *cmd, uint8_t cmdlen, uint8_t *response, uint16_t responseLength ) { |
119 | UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT | ISO14A_APPEND_CRC, cmdlen, 0}}; | |
120 | memcpy(c.d.asBytes, cmd, cmdlen); | |
2c74558d | 121 | SendCommand(&c); |
122 | UsbCommand resp; | |
aebe7790 | 123 | if ( !WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) return -1; |
124 | if ( !resp.arg[0] && responseLength) return -1; | |
2b03dea7 | 125 | |
802319a3 | 126 | uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength; |
aebe7790 | 127 | memcpy(response, resp.d.asBytes, resplen); |
128 | return resplen; | |
2c74558d | 129 | } |
2b03dea7 | 130 | /* |
8297860e | 131 | static int ul_send_cmd_raw_crc( uint8_t *cmd, uint8_t cmdlen, uint8_t *response, uint16_t responseLength, bool append_crc ) { |
8297860e | 132 | UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT , cmdlen, 0}}; |
2c74558d | 133 | if (append_crc) |
134 | c.arg[0] |= ISO14A_APPEND_CRC; | |
135 | ||
8297860e | 136 | memcpy(c.d.asBytes, cmd, cmdlen); |
1ec21089 | 137 | SendCommand(&c); |
138 | UsbCommand resp; | |
2c74558d | 139 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1; |
ebd7412d | 140 | if (!resp.arg[0] && responseLength) return -1; |
1ec21089 | 141 | |
8297860e | 142 | uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength; |
8297860e | 143 | memcpy(response, resp.d.asBytes, resplen ); |
8297860e | 144 | return resplen; |
2c74558d | 145 | } |
2b03dea7 | 146 | */ |
2c74558d | 147 | static int ul_select( iso14a_card_select_t *card ){ |
1ec21089 | 148 | |
2c74558d | 149 | ul_switch_on_field(); |
150 | ||
151 | UsbCommand resp; | |
2b03dea7 | 152 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1; |
ebd7412d | 153 | if (resp.arg[0] < 1) return -1; |
2b03dea7 | 154 | |
155 | memcpy(card, resp.d.asBytes, sizeof(iso14a_card_select_t)); | |
2c74558d | 156 | return resp.arg[0]; |
157 | } | |
158 | ||
996fda30 | 159 | // This read command will at least return 16bytes. |
8297860e | 160 | static int ul_read( uint8_t page, uint8_t *response, uint16_t responseLength ){ |
2c74558d | 161 | |
162 | uint8_t cmd[] = {ISO14443A_CMD_READBLOCK, page}; | |
8297860e | 163 | int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength); |
2c74558d | 164 | if ( len == -1 ) |
165 | ul_switch_off_field(); | |
166 | return len; | |
167 | } | |
168 | ||
802319a3 | 169 | static int ul_comp_write( uint8_t page, uint8_t *data, uint8_t datalen ){ |
170 | ||
171 | uint8_t cmd[18]; | |
172 | memset(cmd, 0x00, sizeof(cmd)); | |
173 | datalen = ( datalen > 16) ? 16 : datalen; | |
174 | ||
175 | cmd[0] = ISO14443A_CMD_WRITEBLOCK; | |
176 | cmd[1] = page; | |
aebe7790 | 177 | memcpy(cmd+2, data, datalen); |
802319a3 | 178 | |
aebe7790 | 179 | uint8_t response[1] = {0xff}; |
802319a3 | 180 | int len = ul_send_cmd_raw(cmd, 2+datalen, response, sizeof(response)); |
aebe7790 | 181 | if ( len == -1 ) |
182 | ul_switch_off_field(); | |
aebe7790 | 183 | // ACK |
184 | if ( response[0] == 0x0a ) return 0; | |
185 | // NACK | |
186 | return -1; | |
187 | } | |
188 | ||
8297860e | 189 | static int ulc_requestAuthentication( uint8_t blockNo, uint8_t *nonce, uint16_t nonceLength ){ |
2c74558d | 190 | |
191 | uint8_t cmd[] = {MIFARE_ULC_AUTH_1, blockNo}; | |
8297860e | 192 | int len = ul_send_cmd_raw(cmd, sizeof(cmd), nonce, nonceLength); |
2c74558d | 193 | if ( len == -1 ) |
194 | ul_switch_off_field(); | |
195 | return len; | |
196 | } | |
197 | ||
8297860e | 198 | static int ulev1_requestAuthentication( uint8_t *pwd, uint8_t *pack, uint16_t packLength ){ |
2c74558d | 199 | |
200 | uint8_t cmd[] = {MIFARE_ULEV1_AUTH, pwd[0], pwd[1], pwd[2], pwd[3]}; | |
8297860e | 201 | int len = ul_send_cmd_raw(cmd, sizeof(cmd), pack, packLength); |
2c74558d | 202 | if ( len == -1) |
203 | ul_switch_off_field(); | |
204 | return len; | |
205 | } | |
1ec21089 | 206 | |
8297860e | 207 | static int ulev1_getVersion( uint8_t *response, uint16_t responseLength ){ |
2c74558d | 208 | |
209 | uint8_t cmd[] = {MIFARE_ULEV1_VERSION}; | |
8297860e | 210 | int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength); |
2c74558d | 211 | if ( len == -1 ) |
212 | ul_switch_off_field(); | |
213 | return len; | |
214 | } | |
215 | ||
216 | // static int ulev1_fastRead( uint8_t startblock, uint8_t endblock, uint8_t *response ){ | |
217 | ||
218 | // uint8_t cmd[] = {MIFARE_ULEV1_FASTREAD, startblock, endblock}; | |
219 | ||
220 | // if ( !ul_send_cmd_raw(cmd, sizeof(cmd), response)){ | |
221 | // ul_switch_off_field(); | |
222 | // return -1; | |
223 | // } | |
224 | // return 0; | |
225 | // } | |
226 | ||
8297860e | 227 | static int ulev1_readCounter( uint8_t counter, uint8_t *response, uint16_t responseLength ){ |
2c74558d | 228 | |
229 | uint8_t cmd[] = {MIFARE_ULEV1_READ_CNT, counter}; | |
8297860e | 230 | int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength); |
2c74558d | 231 | if (len == -1) |
232 | ul_switch_off_field(); | |
233 | return len; | |
234 | } | |
235 | ||
69a29536 | 236 | static int ulev1_readSignature( uint8_t *response, uint16_t responseLength ){ |
237 | ||
238 | uint8_t cmd[] = {MIFARE_ULEV1_READSIG, 0x00}; | |
239 | int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength); | |
240 | if (len == -1) | |
241 | ul_switch_off_field(); | |
242 | return len; | |
243 | } | |
244 | ||
a903be43 | 245 | static int ul_print_default( uint8_t *data){ |
246 | ||
247 | uint8_t uid[7]; | |
248 | ||
249 | uid[0] = data[0]; | |
250 | uid[1] = data[1]; | |
251 | uid[2] = data[2]; | |
252 | uid[3] = data[4]; | |
253 | uid[4] = data[5]; | |
254 | uid[5] = data[6]; | |
255 | uid[6] = data[7]; | |
256 | ||
257 | PrintAndLog(" UID : %s ", sprint_hex(uid, 7)); | |
258 | PrintAndLog(" UID[0] : (Manufacturer Byte) = %02x, Manufacturer: %s", uid[0], getTagInfo(uid[0]) ); | |
259 | ||
260 | // BBC | |
261 | // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2 | |
262 | int crc0 = 0x88 ^ data[0] ^ data[1] ^data[2]; | |
263 | if ( data[3] == crc0 ) | |
264 | PrintAndLog(" BCC0 : 0x%02X - Ok", data[3]); | |
265 | else | |
266 | PrintAndLog(" BCC0 : 0x%02X - crc should be %02x", data[3], crc0); | |
267 | ||
268 | int crc1 = data[4] ^ data[5] ^ data[6] ^data[7]; | |
269 | if ( data[8] == crc1 ) | |
270 | PrintAndLog(" BCC1 : 0x%02X - Ok", data[8]); | |
271 | else | |
272 | PrintAndLog(" BCC1 : 0x%02X - crc should be 0x%02X", data[8], crc1 ); | |
273 | ||
274 | PrintAndLog(" Internal : 0x%02X - %s default", data[9], (data[9]==0x48)?"":"not" ); | |
275 | PrintAndLog(" Lock : %s - %s", sprint_hex(data+10, 2),printBits( 2, data+10) ); | |
276 | PrintAndLog("OneTimePad : %s ", sprint_hex(data + 12, 4)); | |
277 | PrintAndLog(""); | |
278 | return 0; | |
279 | } | |
280 | ||
69a29536 | 281 | static int ntag_print_CC(uint8_t *data) { |
2c74558d | 282 | if(data[0] != 0xe1) { |
283 | PrintAndLog("no NDEF message"); | |
284 | return -1; // no NDEF message | |
285 | } | |
286 | ||
287 | PrintAndLog("Capability Container: %s", sprint_hex(data,4) ); | |
288 | PrintAndLog(" %02X: NDEF Magic Number", data[0]); | |
289 | PrintAndLog(" %02X: version %d.%d supported by tag", data[1], (data[1] & 0xF0) >> 4, data[1] & 0x0f); | |
69a29536 | 290 | PrintAndLog(" %02X: Physical Memory Size: %d bytes", data[2], (data[2] + 1) * 8); |
291 | if ( data[2] == 0x12 ) | |
292 | PrintAndLog(" %02X: NDEF Memory Size: &d bytes", data[2], 144); | |
293 | else if ( data[2] == 0x3e ) | |
294 | PrintAndLog(" %02X: NDEF Memory Size: &d bytes", data[2], 496); | |
295 | else if ( data[2] == 0x6d ) | |
296 | PrintAndLog(" %02X: NDEF Memory Size: &d bytes", data[2], 872); | |
297 | ||
2c74558d | 298 | PrintAndLog(" %02X: %s / %s", data[3], |
299 | (data[3] & 0xF0) ? "(RFU)" : "Read access granted without any security", | |
300 | (data[3] & 0x0F)==0 ? "Write access granted without any security" : (data[3] & 0x0F)==0x0F ? "No write access granted at all" : "(RFU)"); | |
301 | return 0; | |
302 | } | |
1ec21089 | 303 | |
aebe7790 | 304 | static int ulev1_print_version(uint8_t *data){ |
a903be43 | 305 | PrintAndLog("\n--- UL-EV1 / NTAG Version"); |
aebe7790 | 306 | PrintAndLog(" Raw bytes : %s", sprint_hex(data, 8) ); |
8297860e | 307 | PrintAndLog(" Vendor ID : 0x%02X, Manufacturer: %s", data[1], getTagInfo(data[1])); |
308 | PrintAndLog(" Product type : %s" , getProductTypeStr(data[2])); | |
309 | PrintAndLog(" Product subtype : 0x%02X %s" , data[3], (data[3]==1) ?"17 pF":"50pF"); | |
310 | PrintAndLog(" Major version : 0x%02X" , data[4]); | |
311 | PrintAndLog(" Minor version : 0x%02X" , data[5]); | |
312 | PrintAndLog(" Size : %s", getUlev1CardSizeStr(data[6])); | |
313 | PrintAndLog(" Protocol type : 0x%02X" , data[7]); | |
2c74558d | 314 | return 0; |
315 | } | |
316 | ||
317 | static int ul_print_type(uint16_t tagtype){ | |
318 | if ( tagtype & UL ) | |
319 | PrintAndLog(" TYPE : MIFARE Ultralight (MF0ICU1) %s", (tagtype & MAGIC)?"<magic>":""); | |
320 | else if ( tagtype & UL_C) | |
2b03dea7 | 321 | PrintAndLog(" TYPE : MIFARE Ultralight C (MF0ULC) %s [%x]", (tagtype & MAGIC)?"<magic>":"", tagtype ); |
2c74558d | 322 | else if ( tagtype & UL_EV1_48) |
323 | PrintAndLog(" TYPE : MIFARE Ultralight EV1 48bytes (MF0UL1101)"); | |
324 | else if ( tagtype & UL_EV1_128) | |
2b03dea7 | 325 | PrintAndLog(" TYPE : MIFARE Ultralight EV1 128bytes (MF0UL2101)"); |
2c74558d | 326 | else if ( tagtype & NTAG_213 ) |
327 | PrintAndLog(" TYPE : MIFARE NTAG 213 144bytes (NT2H1311G0DU)"); | |
328 | else if ( tagtype & NTAG_215 ) | |
329 | PrintAndLog(" TYPE : MIFARE NTAG 215 504bytes (NT2H1511G0DU)"); | |
330 | else if ( tagtype & NTAG_216 ) | |
331 | PrintAndLog(" TYPE : MIFARE NTAG 216 888bytes (NT2H1611G0DU)"); | |
332 | else | |
8297860e | 333 | PrintAndLog(" TYPE : Unknown %04x",tagtype); |
2c74558d | 334 | return 0; |
335 | } | |
336 | ||
b9a3c864 | 337 | static int ulc_print_3deskey( uint8_t *data){ |
ebd7412d | 338 | PrintAndLog(" deskey1 [44/0x2C]: %s [%.4s]", sprint_hex(data ,4),data); |
339 | PrintAndLog(" deskey1 [45/0x2D]: %s [%.4s]", sprint_hex(data+4 ,4),data+4); | |
340 | PrintAndLog(" deskey2 [46/0x2E]: %s [%.4s]", sprint_hex(data+8 ,4),data+8); | |
341 | PrintAndLog(" deskey2 [47/0x2F]: %s [%.4s]", sprint_hex(data+12,4),data+12); | |
b9a3c864 | 342 | PrintAndLog(" 3des key : %s", sprint_hex(SwapEndian64(data, 16), 16)); |
343 | return 0; | |
344 | } | |
345 | ||
346 | static int ulc_print_configuration( uint8_t *data){ | |
347 | ||
348 | PrintAndLog("--- UL-C Configuration"); | |
349 | PrintAndLog(" Higher Lockbits [40/0x28]: %s %s", sprint_hex(data, 4), printBits(2, data)); | |
350 | PrintAndLog(" Counter [41/0x29]: %s %s", sprint_hex(data+4, 4), printBits(2, data+4)); | |
351 | ||
352 | bool validAuth = (data[8] >= 0x03 && data[8] <= 0x30); | |
353 | if ( validAuth ) | |
354 | PrintAndLog(" Auth0 [42/0x2A]: %s - Pages above %d needs authentication", sprint_hex(data+8, 4), data[8] ); | |
355 | else{ | |
356 | if ( data[8] == 0){ | |
357 | PrintAndLog(" Auth0 [42/0x2A]: %s - default", sprint_hex(data+8, 4) ); | |
358 | } else { | |
359 | PrintAndLog(" Auth0 [42/0x2A]: %s - auth byte is out-of-range", sprint_hex(data+8, 4) ); | |
360 | } | |
361 | } | |
362 | PrintAndLog(" Auth1 [43/0x2B]: %s - %s", | |
363 | sprint_hex(data+12, 4), | |
364 | (data[12] & 1) ? "write access restricted": "read and write access restricted" | |
365 | ); | |
366 | return 0; | |
367 | } | |
368 | ||
369 | static int ulev1_print_configuration( uint8_t *data){ | |
370 | ||
371 | PrintAndLog("\n--- UL-EV1 Configuration"); | |
372 | ||
373 | bool strg_mod_en = (data[0] & 2); | |
374 | uint8_t authlim = (data[4] & 0x07); | |
375 | bool cfglck = (data[4] & 0x40); | |
376 | bool prot = (data[4] & 0x80); | |
377 | uint8_t vctid = data[5]; | |
378 | ||
379 | PrintAndLog(" cfg0 [16/0x10]: %s", sprint_hex(data, 4)); | |
380 | PrintAndLog(" - pages above %d needs authentication",data[3]); | |
381 | PrintAndLog(" - strong modulation mode %s", (strg_mod_en) ? "enabled":"disabled"); | |
382 | PrintAndLog(" cfg1 [17/0x11]: %s", sprint_hex(data+4, 4) ); | |
383 | if ( authlim == 0) | |
384 | PrintAndLog(" - Max number of password attempts is unlimited"); | |
385 | else | |
386 | PrintAndLog(" - Max number of password attempts is %d", authlim); | |
387 | PrintAndLog(" - user configuration %s", cfglck ? "permanently locked":"writeable"); | |
388 | PrintAndLog(" - %s access is protected with password", prot ? "read and write":"write"); | |
389 | PrintAndLog(" 0x%02X - Virtual Card Type Identifier is %s default", vctid, (vctid==0x05)? "":"not"); | |
390 | PrintAndLog(" PWD [18/0x12]: %s", sprint_hex(data+8, 4)); | |
391 | PrintAndLog(" PACK [19/0x13]: %s", sprint_hex(data+12, 4)); | |
392 | return 0; | |
393 | } | |
394 | ||
a903be43 | 395 | static int ulev1_print_counters(){ |
396 | PrintAndLog("--- UL-EV1 Counters"); | |
397 | uint8_t counter[3] = {0,0,0}; | |
398 | for ( uint8_t i = 0; i<3; ++i) { | |
399 | ulev1_readCounter(i,counter, sizeof(counter) ); | |
aebe7790 | 400 | PrintAndLog(" [0x%0d] : %s", i, sprint_hex(counter,3)); |
a903be43 | 401 | } |
402 | return 0; | |
403 | } | |
404 | ||
2b03dea7 | 405 | static int ulev1_print_signature( uint8_t *data, uint8_t len){ |
aebe7790 | 406 | PrintAndLog("\n--- UL-EV1 Signature"); |
2b03dea7 | 407 | PrintAndLog("IC signature public key name : NXP NTAG21x 2013"); |
408 | PrintAndLog("IC signature public key value : 04494e1a386d3d3cfe3dc10e5de68a499b1c202db5b132393e89ed19fe5be8bc61"); | |
409 | PrintAndLog(" Elliptic curve parameters : secp128r1"); | |
410 | PrintAndLog(" Tag ECC Signature : %s", sprint_hex(data, len)); | |
411 | //to do: verify if signature is valid | |
412 | //PrintAndLog("IC signature status: %s valid", (iseccvalid() )?"":"not"); | |
413 | return 0; | |
414 | } | |
415 | ||
aebe7790 | 416 | static int ulc_magic_test(){ |
417 | // Magic Ultralight test | |
418 | // Magic UL-C, by observation, | |
419 | // 1) it seems to have a static nonce response to 0x1A command. | |
420 | // 2) the deskey bytes is not-zero:d out on as datasheet states. | |
421 | // 3) UID - changeable, not only, but pages 0-1-2-3. | |
802319a3 | 422 | // 4) use the ul_magic_test ! magic tags answers specially! |
aebe7790 | 423 | int returnValue = UL_ERROR; |
424 | iso14a_card_select_t card; | |
425 | uint8_t nonce1[11] = {0x00}; | |
426 | uint8_t nonce2[11] = {0x00}; | |
427 | int status = ul_select(&card); | |
428 | if ( status < 1 ){ | |
429 | PrintAndLog("Error: couldn't select ulc_magic_test"); | |
430 | ul_switch_off_field(); | |
431 | return UL_ERROR; | |
432 | } | |
433 | status = ulc_requestAuthentication(0, nonce1, sizeof(nonce1)); | |
434 | if ( status > 0 ) { | |
435 | status = ulc_requestAuthentication(0, nonce2, sizeof(nonce2)); | |
436 | returnValue = ( !memcmp(nonce1, nonce2, 11) ) ? UL_C_MAGIC : UL_C; | |
437 | } else { | |
438 | returnValue = UL; | |
439 | } | |
440 | ul_switch_off_field(); | |
441 | return returnValue; | |
442 | } | |
443 | ||
444 | static int ul_magic_test(){ | |
445 | ||
ebd7412d | 446 | // Magic Ultralight tests |
447 | // 1) take present UID, and try to write it back. OBSOLETE | |
802319a3 | 448 | // 2) make a wrong length write to page0, and see if tag answers with ACK/NACK: |
aebe7790 | 449 | iso14a_card_select_t card; |
450 | int status = ul_select(&card); | |
451 | if ( status < 1 ){ | |
452 | PrintAndLog("Error: couldn't select ul_magic_test"); | |
453 | ul_switch_off_field(); | |
454 | return UL_ERROR; | |
455 | } | |
802319a3 | 456 | status = ul_comp_write(0, NULL, 0); |
aebe7790 | 457 | ul_switch_off_field(); |
458 | if ( status == 0) | |
459 | return UL_MAGIC; | |
460 | return UL; | |
461 | } | |
462 | ||
2c74558d | 463 | uint16_t GetHF14AMfU_Type(void){ |
464 | ||
465 | TagTypeUL_t tagtype = UNKNOWN; | |
466 | iso14a_card_select_t card; | |
467 | uint8_t version[10] = {0x00}; | |
2c74558d | 468 | int status = 0; |
469 | int len; | |
470 | ||
471 | status = ul_select(&card); | |
472 | if ( status < 1 ){ | |
473 | PrintAndLog("Error: couldn't select"); | |
2b03dea7 | 474 | ul_switch_off_field(); |
2c74558d | 475 | return UL_ERROR; |
476 | } | |
2c74558d | 477 | // Ultralight - ATQA / SAK |
aebe7790 | 478 | if ( card.atqa[1] != 0x00 || card.atqa[0] != 0x44 || card.sak != 0x00 ) { |
479 | PrintAndLog("Tag is not UL or NTAG. [ATQA: %02X %02x SAK: %02X]", card.atqa[1],card.atqa[0],card.sak); | |
2c74558d | 480 | ul_switch_off_field(); |
481 | return UL_ERROR; | |
482 | } | |
483 | ||
8297860e | 484 | len = ulev1_getVersion(version, sizeof(version)); |
2b03dea7 | 485 | ul_switch_off_field(); |
2c74558d | 486 | |
487 | switch (len) { | |
2c74558d | 488 | case 0x0A: { |
1ec21089 | 489 | |
2c74558d | 490 | if ( version[2] == 0x03 && version[6] == 0x0B ) |
491 | tagtype = UL_EV1_48; | |
492 | else if ( version[2] == 0x03 && version[6] != 0x0B ) | |
ebd7412d | 493 | tagtype = UL_EV1_128; |
2c74558d | 494 | else if ( version[2] == 0x04 && version[6] == 0x0F ) |
495 | tagtype = NTAG_213; | |
ebd7412d | 496 | else if ( version[2] == 0x04 && version[6] == 0x11 ) |
2c74558d | 497 | tagtype = NTAG_215; |
498 | else if ( version[2] == 0x04 && version[6] == 0x13 ) | |
499 | tagtype = NTAG_216; | |
500 | else if ( version[2] == 0x04 ) | |
501 | tagtype = NTAG; | |
502 | ||
503 | break; | |
504 | } | |
aebe7790 | 505 | case 0x01: tagtype = UL_C; break; |
506 | case 0x00: tagtype = UL; break; | |
507 | case -1: tagtype = (UL | UL_C); break; | |
508 | default: tagtype = UNKNOWN; break; | |
1ec21089 | 509 | } |
2c74558d | 510 | |
aebe7790 | 511 | if ((tagtype & ( UL_C | UL ))) tagtype = ulc_magic_test(); |
512 | if ( (tagtype & UL) ) tagtype = ul_magic_test(); | |
2c74558d | 513 | |
1ec21089 | 514 | return tagtype; |
515 | } | |
516 | ||
517 | int CmdHF14AMfUInfo(const char *Cmd){ | |
2b03dea7 | 518 | |
aebe7790 | 519 | uint8_t authlim = 0xff; |
5d554ea6 | 520 | uint8_t data[16] = {0x00}; |
2c74558d | 521 | iso14a_card_select_t card; |
1ec21089 | 522 | uint8_t *key; |
2c74558d | 523 | int status; |
524 | ||
1ec21089 | 525 | TagTypeUL_t tagtype = GetHF14AMfU_Type(); |
526 | if (tagtype == UL_ERROR) return -1; | |
aebe7790 | 527 | |
528 | PrintAndLog("\n--- Tag Information ---------"); | |
529 | PrintAndLog("-------------------------------------------------------------"); | |
2c74558d | 530 | ul_print_type(tagtype); |
531 | ||
532 | status = ul_select(&card); | |
533 | if ( status < 1 ){ | |
534 | PrintAndLog("Error: couldn't select"); | |
535 | ul_switch_off_field(); | |
536 | return status; | |
537 | } | |
538 | ||
539 | // read pages 0,1,2,4 (should read 4pages) | |
8297860e | 540 | status = ul_read(0, data, sizeof(data)); |
2c74558d | 541 | if ( status == -1 ){ |
2b03dea7 | 542 | PrintAndLog("Error: tag didn't answer to READ A"); |
2c74558d | 543 | ul_switch_off_field(); |
544 | return status; | |
81740aa5 | 545 | } |
5d554ea6 | 546 | |
a903be43 | 547 | ul_print_default(data); |
548 | ||
1c1c5f4c | 549 | if ((tagtype & UL_C)){ |
8297860e | 550 | |
551 | // read pages 0x28, 0x29, 0x2A, 0x2B | |
2c74558d | 552 | uint8_t ulc_conf[16] = {0x00}; |
8297860e | 553 | status = ul_read(0x28, ulc_conf, sizeof(ulc_conf)); |
2c74558d | 554 | if ( status == -1 ){ |
2b03dea7 | 555 | PrintAndLog("Error: tag didn't answer to READ - possibly locked"); |
ebd7412d | 556 | ul_switch_off_field(); |
557 | return status; | |
558 | } | |
559 | ||
560 | ulc_print_configuration(ulc_conf); | |
b9a3c864 | 561 | |
2b03dea7 | 562 | if ((tagtype & MAGIC)){ |
8297860e | 563 | |
b9a3c864 | 564 | uint8_t ulc_deskey[16] = {0x00}; |
565 | status = ul_read(0x2C, ulc_deskey, sizeof(ulc_deskey)); | |
2c74558d | 566 | if ( status == -1 ){ |
2b03dea7 | 567 | PrintAndLog("Error: tag didn't answer to READ B"); |
2c74558d | 568 | ul_switch_off_field(); |
569 | return status; | |
570 | } | |
b9a3c864 | 571 | ulc_print_3deskey(ulc_deskey); |
572 | ||
ebd7412d | 573 | } else { |
8297860e | 574 | PrintAndLog("Trying some default 3des keys"); |
2b03dea7 | 575 | ul_switch_off_field(); |
ebd7412d | 576 | for (uint8_t i = 0; i < KEYS_3DES_COUNT; ++i ){ |
8297860e | 577 | key = default_3des_keys[i]; |
578 | if (try3DesAuthentication(key) == 1){ | |
ebd7412d | 579 | PrintAndLog("Found default 3des key: "); //%s", sprint_hex(key,16)); |
580 | ulc_print_3deskey(SwapEndian64(key,16)); | |
8297860e | 581 | return 0; |
582 | } | |
a8be77af | 583 | } |
8297860e | 584 | } |
a8be77af | 585 | } |
2c74558d | 586 | |
587 | if ((tagtype & (UL_EV1_48 | UL_EV1_128))) { | |
ebd7412d | 588 | |
589 | ulev1_print_counters(); | |
69a29536 | 590 | |
591 | uint8_t ulev1_signature[32] = {0x00}; | |
592 | status = ulev1_readSignature( ulev1_signature, sizeof(ulev1_signature)); | |
593 | if ( status == -1 ){ | |
594 | PrintAndLog("Error: tag didn't answer to READ SIGNATURE"); | |
595 | ul_switch_off_field(); | |
596 | return status; | |
aebe7790 | 597 | } |
2b03dea7 | 598 | ulev1_print_signature( ulev1_signature, sizeof(ulev1_signature)); |
aebe7790 | 599 | |
ebd7412d | 600 | uint8_t startconfigblock = (tagtype & UL_EV1_48) ? 0x10 : 0x25; |
601 | uint8_t ulev1_conf[16] = {0x00}; | |
602 | status = ul_read(startconfigblock, ulev1_conf, sizeof(ulev1_conf)); | |
603 | if ( status == -1 ){ | |
604 | PrintAndLog("Error: tag didn't answer to READ - possibly locked"); | |
605 | ul_switch_off_field(); | |
606 | //return status; | |
607 | } | |
608 | // save AUTHENTICATION LIMITS for later: | |
609 | authlim = (ulev1_conf[4] & 0x07); | |
610 | ulev1_print_configuration(ulev1_conf); | |
2c74558d | 611 | } |
612 | ||
613 | if ((tagtype & (UL_EV1_48 | UL_EV1_128 | NTAG_213 | NTAG_215 | NTAG_216))) { | |
614 | ||
2c74558d | 615 | uint8_t version[10] = {0x00}; |
8297860e | 616 | status = ulev1_getVersion(version, sizeof(version)); |
2c74558d | 617 | if ( status == -1 ){ |
618 | PrintAndLog("Error: tag didn't answer to GETVERSION"); | |
619 | ul_switch_off_field(); | |
620 | return status; | |
621 | } | |
aebe7790 | 622 | ulev1_print_version(version); |
2c74558d | 623 | |
aebe7790 | 624 | // AUTHLIMIT, (number of failed authentications) |
625 | // 0 = limitless. | |
626 | // 1-7 = ... should we even try then? | |
ebd7412d | 627 | authlim = 1; |
aebe7790 | 628 | if ( authlim == 0 ){ |
629 | PrintAndLog("\n--- Known EV1/NTAG passwords."); | |
630 | ||
631 | uint8_t pack[4] = {0,0,0,0}; | |
632 | ||
ebd7412d | 633 | for (uint8_t i = 0; i < KEYS_PWD_COUNT; ++i ){ |
aebe7790 | 634 | key = default_pwd_pack[i]; |
635 | if ( ulev1_requestAuthentication(key, pack, sizeof(pack)) > -1 ){ | |
636 | PrintAndLog("Found a default password: %s || Pack: %02X %02X",sprint_hex(key, 4), pack[0], pack[1]); | |
637 | } | |
638 | } | |
2c74558d | 639 | ul_switch_off_field(); |
2c74558d | 640 | } |
2c74558d | 641 | } |
642 | ||
643 | if ((tagtype & (NTAG_213 | NTAG_215 | NTAG_216))){ | |
644 | ||
8297860e | 645 | PrintAndLog("\n--- NTAG NDEF Message"); |
996fda30 | 646 | uint8_t cc[16] = {0x00}; |
ebd7412d | 647 | status = ul_read(3, cc, sizeof(cc)); |
2c74558d | 648 | if ( status == -1 ){ |
2b03dea7 | 649 | PrintAndLog("Error: tag didn't answer to READ D"); |
2c74558d | 650 | ul_switch_off_field(); |
651 | return status; | |
a903be43 | 652 | } |
69a29536 | 653 | ntag_print_CC(cc); |
a8be77af | 654 | } |
1ec21089 | 655 | |
2c74558d | 656 | ul_switch_off_field(); |
81740aa5 | 657 | return 0; |
658 | } | |
659 | ||
660 | // | |
661 | // Mifare Ultralight Write Single Block | |
662 | // | |
663 | int CmdHF14AMfUWrBl(const char *Cmd){ | |
afceaf40 MHS |
664 | uint8_t blockNo = -1; |
665 | bool chinese_card = FALSE; | |
666 | uint8_t bldata[16] = {0x00}; | |
667 | UsbCommand resp; | |
81740aa5 | 668 | |
afceaf40 | 669 | char cmdp = param_getchar(Cmd, 0); |
81740aa5 | 670 | if (strlen(Cmd) < 3 || cmdp == 'h' || cmdp == 'H') { |
afceaf40 | 671 | PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]"); |
81740aa5 | 672 | PrintAndLog(" [block number]"); |
673 | PrintAndLog(" [block data] - (8 hex symbols)"); | |
674 | PrintAndLog(" [w] - Chinese magic ultralight tag"); | |
675 | PrintAndLog(""); | |
afceaf40 | 676 | PrintAndLog(" sample: hf mfu wrbl 0 01020304"); |
81740aa5 | 677 | PrintAndLog(""); |
afceaf40 MHS |
678 | return 0; |
679 | } | |
680 | ||
81740aa5 | 681 | blockNo = param_get8(Cmd, 0); |
682 | ||
a8be77af | 683 | if (blockNo > MAX_UL_BLOCKS){ |
afceaf40 MHS |
684 | PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!"); |
685 | return 1; | |
686 | } | |
81740aa5 | 687 | |
afceaf40 MHS |
688 | if (param_gethex(Cmd, 1, bldata, 8)) { |
689 | PrintAndLog("Block data must include 8 HEX symbols"); | |
690 | return 1; | |
691 | } | |
81740aa5 | 692 | |
afceaf40 MHS |
693 | if (strchr(Cmd,'w') != 0 || strchr(Cmd,'W') != 0 ) { |
694 | chinese_card = TRUE; | |
695 | } | |
81740aa5 | 696 | |
afceaf40 | 697 | if ( blockNo <= 3) { |
81740aa5 | 698 | if (!chinese_card){ |
699 | PrintAndLog("Access Denied"); | |
700 | } else { | |
701 | PrintAndLog("--specialblock no:%02x", blockNo); | |
702 | PrintAndLog("--data: %s", sprint_hex(bldata, 4)); | |
703 | UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}}; | |
704 | memcpy(d.d.asBytes,bldata, 4); | |
705 | SendCommand(&d); | |
706 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
707 | uint8_t isOK = resp.arg[0] & 0xff; | |
708 | PrintAndLog("isOk:%02x", isOK); | |
709 | } else { | |
710 | PrintAndLog("Command execute timeout"); | |
711 | } | |
712 | } | |
713 | } else { | |
afceaf40 MHS |
714 | PrintAndLog("--block no:%02x", blockNo); |
715 | PrintAndLog("--data: %s", sprint_hex(bldata, 4)); | |
716 | UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}}; | |
81740aa5 | 717 | memcpy(e.d.asBytes,bldata, 4); |
718 | SendCommand(&e); | |
afceaf40 MHS |
719 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { |
720 | uint8_t isOK = resp.arg[0] & 0xff; | |
721 | PrintAndLog("isOk:%02x", isOK); | |
722 | } else { | |
723 | PrintAndLog("Command execute timeout"); | |
724 | } | |
725 | } | |
726 | return 0; | |
81740aa5 | 727 | } |
728 | ||
729 | // | |
730 | // Mifare Ultralight Read Single Block | |
731 | // | |
732 | int CmdHF14AMfURdBl(const char *Cmd){ | |
81740aa5 | 733 | |
aa60d156 | 734 | UsbCommand resp; |
735 | uint8_t blockNo = -1; | |
afceaf40 | 736 | char cmdp = param_getchar(Cmd, 0); |
81740aa5 | 737 | |
738 | if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') { | |
afceaf40 MHS |
739 | PrintAndLog("Usage: hf mfu rdbl <block number>"); |
740 | PrintAndLog(" sample: hfu mfu rdbl 0"); | |
741 | return 0; | |
742 | } | |
743 | ||
744 | blockNo = param_get8(Cmd, 0); | |
745 | ||
a8be77af | 746 | if (blockNo > MAX_UL_BLOCKS){ |
aa60d156 | 747 | PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight"); |
afceaf40 MHS |
748 | return 1; |
749 | } | |
aa60d156 | 750 | |
afceaf40 MHS |
751 | UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}}; |
752 | SendCommand(&c); | |
753 | ||
aa60d156 | 754 | |
afceaf40 | 755 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { |
aa60d156 | 756 | uint8_t isOK = resp.arg[0] & 0xff; |
757 | if (isOK) { | |
758 | uint8_t *data = resp.d.asBytes; | |
759 | PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo, blockNo, sprint_hex(data, 4)); | |
760 | } | |
761 | else { | |
762 | PrintAndLog("Failed reading block: (%02x)", isOK); | |
763 | } | |
81740aa5 | 764 | } else { |
aa60d156 | 765 | PrintAndLog("Command execute time-out"); |
afceaf40 | 766 | } |
aa60d156 | 767 | |
afceaf40 | 768 | return 0; |
81740aa5 | 769 | } |
770 | ||
1c1c5f4c | 771 | int usage_hf_mfu_dump(void) |
1ec21089 | 772 | { |
773 | PrintAndLog("Reads all pages from Ultralight, Ultralight-C, Ultralight EV1"); | |
774 | PrintAndLog("and saves binary dump into the file `filename.bin` or `cardUID.bin`"); | |
775 | PrintAndLog("It autodetects card type.\n"); | |
2c74558d | 776 | PrintAndLog("Usage: hf mfu dump k <key> n <filename w/o .bin>"); |
2b03dea7 | 777 | PrintAndLog(" Options : "); |
778 | PrintAndLog(" k <key> : Enter key for authentication"); | |
779 | PrintAndLog(" n <FN > : Enter filename w/o .bin to save the dump as"); | |
780 | PrintAndLog(" s : Swap entered key's endianness for auth"); | |
781 | PrintAndLog(""); | |
1ec21089 | 782 | PrintAndLog(" sample : hf mfu dump"); |
2c74558d | 783 | PrintAndLog(" : hf mfu dump n myfile"); |
1ec21089 | 784 | return 0; |
785 | } | |
81740aa5 | 786 | // |
1ec21089 | 787 | // Mifare Ultralight / Ultralight-C / Ultralight-EV1 |
788 | // Read and Dump Card Contents, using auto detection of tag size. | |
81740aa5 | 789 | // |
1ec21089 | 790 | // TODO: take a password to read UL-C / UL-EV1 tags. |
81740aa5 | 791 | int CmdHF14AMfUDump(const char *Cmd){ |
792 | ||
afceaf40 | 793 | FILE *fout; |
81740aa5 | 794 | char filename[FILE_PATH_SIZE] = {0x00}; |
795 | char * fnameptr = filename; | |
1ec21089 | 796 | char* str = "Dumping Ultralight%s%s Card Data..."; |
afceaf40 MHS |
797 | uint8_t *lockbytes_t = NULL; |
798 | uint8_t lockbytes[2] = {0x00}; | |
afceaf40 MHS |
799 | uint8_t *lockbytes_t2 = NULL; |
800 | uint8_t lockbytes2[2] = {0x00}; | |
afceaf40 | 801 | bool bit[16] = {0x00}; |
81740aa5 | 802 | bool bit2[16] = {0x00}; |
2c74558d | 803 | uint8_t data[1024] = {0x00}; |
804 | bool hasPwd = false; | |
1ec21089 | 805 | int i = 0; |
806 | int Pages = 16; | |
807 | bool tmplockbit = false; | |
2c74558d | 808 | uint8_t dataLen=0; |
809 | uint8_t cmdp =0; | |
2b03dea7 | 810 | uint8_t key[16] = {0x00}; |
811 | uint8_t *keyPtr = key; | |
2c74558d | 812 | size_t fileNlen = 0; |
2b03dea7 | 813 | bool errors = false; |
814 | bool swapEndian = false; | |
2c74558d | 815 | |
816 | while(param_getchar(Cmd, cmdp) != 0x00) | |
817 | { | |
818 | switch(param_getchar(Cmd, cmdp)) | |
819 | { | |
820 | case 'h': | |
821 | case 'H': | |
822 | return usage_hf_mfu_dump(); | |
823 | case 'k': | |
824 | case 'K': | |
825 | dataLen = param_gethex(Cmd, cmdp+1, data, 32); | |
826 | if (dataLen) { | |
827 | errors = true; | |
828 | } else { | |
2b03dea7 | 829 | memcpy(key, data, 16); |
2c74558d | 830 | } |
831 | cmdp += 2; | |
832 | hasPwd = true; | |
833 | break; | |
834 | case 'n': | |
835 | case 'N': | |
836 | fileNlen = param_getstr(Cmd, cmdp+1, filename); | |
837 | if (!fileNlen) errors = true; | |
838 | if (fileNlen > FILE_PATH_SIZE-5) fileNlen = FILE_PATH_SIZE-5; | |
839 | cmdp += 2; | |
840 | break; | |
2b03dea7 | 841 | case 's': |
842 | swapEndian = true; | |
843 | cmdp++; | |
ebd7412d | 844 | break; |
2c74558d | 845 | default: |
846 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
847 | errors = true; | |
848 | break; | |
849 | } | |
850 | if(errors) break; | |
851 | } | |
852 | ||
853 | //Validations | |
854 | if(errors) return usage_hf_mfu_dump(); | |
81740aa5 | 855 | |
2b03dea7 | 856 | if (swapEndian) |
857 | keyPtr = SwapEndian64(data, 16); | |
858 | ||
1ec21089 | 859 | TagTypeUL_t tagtype = GetHF14AMfU_Type(); |
860 | if (tagtype == UL_ERROR) return -1; | |
861 | ||
862 | if ( tagtype & UL ) { | |
863 | Pages = 16; | |
864 | PrintAndLog(str,"", (tagtype & MAGIC)?" (magic)":"" ); | |
865 | } | |
866 | else if ( tagtype & UL_C ) { | |
867 | Pages = 44; | |
868 | PrintAndLog(str,"-C", (tagtype & MAGIC)?" (magic)":"" ); | |
869 | } | |
870 | else if ( tagtype & UL_EV1_48 ) { | |
871 | Pages = 18; | |
872 | PrintAndLog(str," EV1_48",""); | |
873 | } | |
874 | else if ( tagtype & UL_EV1_128 ) { | |
875 | Pages = 32; | |
876 | PrintAndLog(str," EV1_128",""); | |
877 | } else { | |
878 | Pages = 16; | |
879 | PrintAndLog("Dumping unknown Ultralight, using default values."); | |
81740aa5 | 880 | } |
81740aa5 | 881 | |
2c74558d | 882 | UsbCommand c = {CMD_MIFAREUC_READCARD, {0,Pages}}; |
883 | if ( hasPwd ) { | |
884 | c.arg[2] = 1; | |
885 | memcpy(c.d.asBytes, key, 16); | |
886 | } | |
afceaf40 MHS |
887 | SendCommand(&c); |
888 | UsbCommand resp; | |
1ec21089 | 889 | if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) { |
aa60d156 | 890 | PrintAndLog("Command execute time-out"); |
2c74558d | 891 | return 1; |
81740aa5 | 892 | } |
2b03dea7 | 893 | PrintAndLog ("%u,%u",resp.arg[0],resp.arg[1]); |
2c74558d | 894 | uint8_t isOK = resp.arg[0] & 0xff; |
895 | if (isOK) { | |
896 | memcpy(data, resp.d.asBytes, resp.arg[1]); | |
897 | } else { | |
898 | PrintAndLog("Failed reading block: (%02x)", i); | |
899 | return 1; | |
900 | } | |
901 | ||
81740aa5 | 902 | // Load lock bytes. |
903 | int j = 0; | |
904 | ||
905 | lockbytes_t = data + 8; | |
906 | lockbytes[0] = lockbytes_t[2]; | |
907 | lockbytes[1] = lockbytes_t[3]; | |
908 | for(j = 0; j < 16; j++){ | |
909 | bit[j] = lockbytes[j/8] & ( 1 <<(7-j%8)); | |
910 | } | |
911 | ||
912 | // Load bottom lockbytes if available | |
913 | if ( Pages == 44 ) { | |
81740aa5 | 914 | lockbytes_t2 = data + (40*4); |
915 | lockbytes2[0] = lockbytes_t2[2]; | |
916 | lockbytes2[1] = lockbytes_t2[3]; | |
917 | for (j = 0; j < 16; j++) { | |
918 | bit2[j] = lockbytes2[j/8] & ( 1 <<(7-j%8)); | |
919 | } | |
920 | } | |
921 | ||
2c74558d | 922 | // add keys |
923 | if (hasPwd){ | |
924 | memcpy(data + Pages*4, key, 16); | |
925 | Pages += 4; | |
926 | } | |
81740aa5 | 927 | for (i = 0; i < Pages; ++i) { |
81740aa5 | 928 | if ( i < 3 ) { |
929 | PrintAndLog("Block %02x:%s ", i,sprint_hex(data + i * 4, 4)); | |
930 | continue; | |
931 | } | |
81740aa5 | 932 | switch(i){ |
933 | case 3: tmplockbit = bit[4]; break; | |
934 | case 4: tmplockbit = bit[3]; break; | |
935 | case 5: tmplockbit = bit[2]; break; | |
936 | case 6: tmplockbit = bit[1]; break; | |
937 | case 7: tmplockbit = bit[0]; break; | |
938 | case 8: tmplockbit = bit[15]; break; | |
939 | case 9: tmplockbit = bit[14]; break; | |
940 | case 10: tmplockbit = bit[13]; break; | |
941 | case 11: tmplockbit = bit[12]; break; | |
942 | case 12: tmplockbit = bit[11]; break; | |
943 | case 13: tmplockbit = bit[10]; break; | |
944 | case 14: tmplockbit = bit[9]; break; | |
945 | case 15: tmplockbit = bit[8]; break; | |
946 | case 16: | |
947 | case 17: | |
948 | case 18: | |
949 | case 19: tmplockbit = bit2[6]; break; | |
950 | case 20: | |
951 | case 21: | |
952 | case 22: | |
953 | case 23: tmplockbit = bit2[5]; break; | |
954 | case 24: | |
955 | case 25: | |
956 | case 26: | |
957 | case 27: tmplockbit = bit2[4]; break; | |
958 | case 28: | |
959 | case 29: | |
960 | case 30: | |
961 | case 31: tmplockbit = bit2[2]; break; | |
962 | case 32: | |
963 | case 33: | |
964 | case 34: | |
965 | case 35: tmplockbit = bit2[1]; break; | |
966 | case 36: | |
967 | case 37: | |
968 | case 38: | |
969 | case 39: tmplockbit = bit2[0]; break; | |
970 | case 40: tmplockbit = bit2[12]; break; | |
971 | case 41: tmplockbit = bit2[11]; break; | |
972 | case 42: tmplockbit = bit2[10]; break; //auth0 | |
973 | case 43: tmplockbit = bit2[9]; break; //auth1 | |
974 | default: break; | |
975 | } | |
976 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),tmplockbit); | |
977 | } | |
978 | ||
2c74558d | 979 | // user supplied filename? |
980 | if (fileNlen < 1) { | |
981 | // UID = data 0-1-2 4-5-6-7 (skips a beat) | |
afceaf40 | 982 | sprintf(fnameptr,"%02X%02X%02X%02X%02X%02X%02X.bin", |
1ec21089 | 983 | data[0], data[1], data[2], data[4], data[5], data[6], data[7]); |
81740aa5 | 984 | } else { |
2c74558d | 985 | sprintf(fnameptr + fileNlen," .bin"); |
81740aa5 | 986 | } |
987 | ||
81740aa5 | 988 | if ((fout = fopen(filename,"wb")) == NULL) { |
989 | PrintAndLog("Could not create file name %s", filename); | |
990 | return 1; | |
991 | } | |
992 | fwrite( data, 1, Pages*4, fout ); | |
993 | fclose(fout); | |
994 | ||
995 | PrintAndLog("Dumped %d pages, wrote %d bytes to %s", Pages, Pages*4, filename); | |
afceaf40 | 996 | return 0; |
81740aa5 | 997 | } |
998 | ||
999 | // Needed to Authenticate to Ultralight C tags | |
1000 | void rol (uint8_t *data, const size_t len){ | |
afceaf40 MHS |
1001 | uint8_t first = data[0]; |
1002 | for (size_t i = 0; i < len-1; i++) { | |
1003 | data[i] = data[i+1]; | |
1004 | } | |
1005 | data[len-1] = first; | |
81740aa5 | 1006 | } |
1007 | ||
1008 | //------------------------------------------------------------------------------- | |
1009 | // Ultralight C Methods | |
1010 | //------------------------------------------------------------------------------- | |
1011 | ||
1012 | // | |
1013 | // Ultralight C Authentication Demo {currently uses hard-coded key} | |
1014 | // | |
1015 | int CmdHF14AMfucAuth(const char *Cmd){ | |
afceaf40 | 1016 | |
afceaf40 MHS |
1017 | uint8_t keyNo = 0; |
1018 | bool errors = false; | |
a8be77af | 1019 | |
1020 | char cmdp = param_getchar(Cmd, 0); | |
1021 | ||
afceaf40 MHS |
1022 | //Change key to user defined one |
1023 | if (cmdp == 'k' || cmdp == 'K'){ | |
1024 | keyNo = param_get8(Cmd, 1); | |
ebd7412d | 1025 | if(keyNo > KEYS_3DES_COUNT) |
a8be77af | 1026 | errors = true; |
afceaf40 MHS |
1027 | } |
1028 | ||
a8be77af | 1029 | if (cmdp == 'h' || cmdp == 'H') |
afceaf40 | 1030 | errors = true; |
a8be77af | 1031 | |
afceaf40 MHS |
1032 | if (errors) { |
1033 | PrintAndLog("Usage: hf mfu cauth k <key number>"); | |
1034 | PrintAndLog(" 0 (default): 3DES standard key"); | |
aa60d156 | 1035 | PrintAndLog(" 1 : all 0x00 key"); |
afceaf40 MHS |
1036 | PrintAndLog(" 2 : 0x00-0x0F key"); |
1037 | PrintAndLog(" 3 : nfc key"); | |
aa60d156 | 1038 | PrintAndLog(" 4 : all 0x01 key"); |
1039 | PrintAndLog(" 5 : all 0xff key"); | |
1040 | PrintAndLog(" 6 : 0x00-0xFF key"); | |
1041 | PrintAndLog("\n sample : hf mfu cauth k"); | |
81740aa5 | 1042 | PrintAndLog(" : hf mfu cauth k 3"); |
afceaf40 MHS |
1043 | return 0; |
1044 | } | |
1045 | ||
a8be77af | 1046 | uint8_t *key = default_3des_keys[keyNo]; |
1c1c5f4c | 1047 | if (try3DesAuthentication(key)>0) |
1048 | PrintAndLog("Authentication successful. 3des key: %s",sprint_hex(key, 16)); | |
a8be77af | 1049 | else |
1050 | PrintAndLog("Authentication failed"); | |
1051 | ||
1052 | return 0; | |
1053 | } | |
1054 | ||
1055 | int try3DesAuthentication( uint8_t *key){ | |
1056 | ||
afceaf40 MHS |
1057 | uint8_t blockNo = 0; |
1058 | uint32_t cuid = 0; | |
81740aa5 | 1059 | |
a8be77af | 1060 | des3_context ctx = { 0 }; |
1061 | ||
1062 | uint8_t random_a[8] = { 1,1,1,1,1,1,1,1 }; | |
1063 | uint8_t random_b[8] = { 0 }; | |
1064 | uint8_t enc_random_b[8] = { 0 }; | |
1065 | uint8_t rnd_ab[16] = { 0 }; | |
1066 | uint8_t iv[8] = { 0 }; | |
1067 | ||
81740aa5 | 1068 | UsbCommand c = {CMD_MIFAREUC_AUTH1, {blockNo}}; |
1069 | SendCommand(&c); | |
1070 | UsbCommand resp; | |
a8be77af | 1071 | if ( !WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) return -1; |
1072 | if ( !(resp.arg[0] & 0xff) ) return -2; | |
1073 | ||
1074 | cuid = resp.arg[1]; | |
1075 | memcpy(enc_random_b,resp.d.asBytes+1,8); | |
afceaf40 MHS |
1076 | |
1077 | des3_set2key_dec(&ctx, key); | |
a8be77af | 1078 | // context, mode, length, IV, input, output |
1079 | des3_crypt_cbc( &ctx, DES_DECRYPT, sizeof(random_b), iv , enc_random_b , random_b); | |
afceaf40 MHS |
1080 | |
1081 | rol(random_b,8); | |
a8be77af | 1082 | memcpy(rnd_ab ,random_a,8); |
1083 | memcpy(rnd_ab+8,random_b,8); | |
1c1c5f4c | 1084 | |
1085 | //PrintAndLog(" RndA :%s", sprint_hex(random_a, 8)); | |
1086 | //PrintAndLog(" enc(RndB) :%s", sprint_hex(enc_random_b, 8)); | |
1087 | //PrintAndLog(" RndB :%s", sprint_hex(random_b, 8)); | |
1088 | //PrintAndLog(" A+B :%s", sprint_hex(rnd_ab, 16)); | |
1089 | ||
afceaf40 | 1090 | des3_set2key_enc(&ctx, key); |
a8be77af | 1091 | // context, mode, length, IV, input, output |
1092 | des3_crypt_cbc(&ctx, DES_ENCRYPT, sizeof(rnd_ab), enc_random_b, rnd_ab, rnd_ab); | |
afceaf40 MHS |
1093 | |
1094 | //Auth2 | |
a8be77af | 1095 | c.cmd = CMD_MIFAREUC_AUTH2; |
1096 | c.arg[0] = cuid; | |
1097 | memcpy(c.d.asBytes, rnd_ab, 16); | |
1098 | SendCommand(&c); | |
81740aa5 | 1099 | |
a8be77af | 1100 | if ( !WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1; |
1101 | if ( !(resp.arg[0] & 0xff)) return -2; | |
1102 | ||
1103 | uint8_t enc_resp[8] = { 0 }; | |
1104 | uint8_t resp_random_a[8] = { 0 }; | |
1105 | memcpy(enc_resp, resp.d.asBytes+1, 8); | |
0ec548dc | 1106 | |
a8be77af | 1107 | des3_set2key_dec(&ctx, key); |
1108 | // context, mode, length, IV, input, output | |
1109 | des3_crypt_cbc( &ctx, DES_DECRYPT, 8, enc_random_b, enc_resp, resp_random_a); | |
0ec548dc | 1110 | |
1c1c5f4c | 1111 | //PrintAndLog(" enc(A+B) :%s", sprint_hex(rnd_ab, 16)); |
1112 | //PrintAndLog(" enc(RndA') :%s", sprint_hex(enc_resp, 8)); | |
1113 | ||
a8be77af | 1114 | if ( !memcmp(resp_random_a, random_a, 8)) |
1115 | return 1; | |
afceaf40 | 1116 | return 0; |
81740aa5 | 1117 | } |
a8be77af | 1118 | |
afceaf40 MHS |
1119 | /** |
1120 | A test function to validate that the polarssl-function works the same | |
1121 | was as the openssl-implementation. | |
1122 | Commented out, since it requires openssl | |
1123 | ||
1124 | int CmdTestDES(const char * cmd) | |
1125 | { | |
1126 | uint8_t key[16] = {0x00}; | |
1127 | ||
1128 | memcpy(key,key3_3des_data,16); | |
1129 | DES_cblock RndA, RndB; | |
1130 | ||
1131 | PrintAndLog("----------OpenSSL DES implementation----------"); | |
1132 | { | |
1133 | uint8_t e_RndB[8] = {0x00}; | |
1134 | unsigned char RndARndB[16] = {0x00}; | |
1135 | ||
1136 | DES_cblock iv = { 0 }; | |
1137 | DES_key_schedule ks1,ks2; | |
1138 | DES_cblock key1,key2; | |
1139 | ||
1140 | memcpy(key,key3_3des_data,16); | |
1141 | memcpy(key1,key,8); | |
1142 | memcpy(key2,key+8,8); | |
1143 | ||
1144 | ||
1145 | DES_set_key((DES_cblock *)key1,&ks1); | |
1146 | DES_set_key((DES_cblock *)key2,&ks2); | |
1147 | ||
1148 | DES_random_key(&RndA); | |
1149 | PrintAndLog(" RndA:%s",sprint_hex(RndA, 8)); | |
1150 | PrintAndLog(" e_RndB:%s",sprint_hex(e_RndB, 8)); | |
1151 | //void DES_ede2_cbc_encrypt(const unsigned char *input, | |
1152 | // unsigned char *output, long length, DES_key_schedule *ks1, | |
1153 | // DES_key_schedule *ks2, DES_cblock *ivec, int enc); | |
1154 | DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0); | |
1155 | ||
1156 | PrintAndLog(" RndB:%s",sprint_hex(RndB, 8)); | |
1157 | rol(RndB,8); | |
1158 | memcpy(RndARndB,RndA,8); | |
1159 | memcpy(RndARndB+8,RndB,8); | |
1160 | PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16)); | |
1161 | DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1); | |
1162 | PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16)); | |
1163 | ||
1164 | } | |
1165 | PrintAndLog("----------PolarSSL implementation----------"); | |
1166 | { | |
1167 | uint8_t random_a[8] = { 0 }; | |
1168 | uint8_t enc_random_a[8] = { 0 }; | |
1169 | uint8_t random_b[8] = { 0 }; | |
1170 | uint8_t enc_random_b[8] = { 0 }; | |
1171 | uint8_t random_a_and_b[16] = { 0 }; | |
1172 | des3_context ctx = { 0 }; | |
1173 | ||
1174 | memcpy(random_a, RndA,8); | |
1175 | ||
1176 | uint8_t output[8] = { 0 }; | |
1177 | uint8_t iv[8] = { 0 }; | |
1178 | ||
1179 | PrintAndLog(" RndA :%s",sprint_hex(random_a, 8)); | |
1180 | PrintAndLog(" e_RndB:%s",sprint_hex(enc_random_b, 8)); | |
1181 | ||
1182 | des3_set2key_dec(&ctx, key); | |
1183 | ||
1184 | des3_crypt_cbc(&ctx // des3_context *ctx | |
1185 | , DES_DECRYPT // int mode | |
1186 | , sizeof(random_b) // size_t length | |
1187 | , iv // unsigned char iv[8] | |
1188 | , enc_random_b // const unsigned char *input | |
1189 | , random_b // unsigned char *output | |
1190 | ); | |
1191 | ||
1192 | PrintAndLog(" RndB:%s",sprint_hex(random_b, 8)); | |
1193 | ||
1194 | rol(random_b,8); | |
1195 | memcpy(random_a_and_b ,random_a,8); | |
1196 | memcpy(random_a_and_b+8,random_b,8); | |
1197 | ||
1198 | PrintAndLog(" RA+B:%s",sprint_hex(random_a_and_b, 16)); | |
1199 | ||
1200 | des3_set2key_enc(&ctx, key); | |
81740aa5 | 1201 | |
afceaf40 MHS |
1202 | des3_crypt_cbc(&ctx // des3_context *ctx |
1203 | , DES_ENCRYPT // int mode | |
1204 | , sizeof(random_a_and_b) // size_t length | |
1205 | , enc_random_b // unsigned char iv[8] | |
1206 | , random_a_and_b // const unsigned char *input | |
1207 | , random_a_and_b // unsigned char *output | |
1208 | ); | |
1209 | ||
1210 | PrintAndLog("enc(RA+B):%s",sprint_hex(random_a_and_b, 16)); | |
1211 | } | |
1212 | return 0; | |
1213 | } | |
1214 | **/ | |
2c74558d | 1215 | |
81740aa5 | 1216 | // |
1217 | // Ultralight C Read Single Block | |
1218 | // | |
1219 | int CmdHF14AMfUCRdBl(const char *Cmd) | |
1220 | { | |
aa60d156 | 1221 | UsbCommand resp; |
f1170fa7 | 1222 | bool hasPwd = FALSE; |
afceaf40 | 1223 | uint8_t blockNo = -1; |
2b03dea7 | 1224 | uint8_t key[16]; |
afceaf40 | 1225 | char cmdp = param_getchar(Cmd, 0); |
81740aa5 | 1226 | |
1227 | if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') { | |
2b03dea7 | 1228 | PrintAndLog("Usage: hf mfu crdbl <block number> <key>"); |
f1170fa7 | 1229 | PrintAndLog(""); |
1230 | PrintAndLog("sample: hf mfu crdbl 0"); | |
1c1c5f4c | 1231 | PrintAndLog(" hf mfu crdbl 0 00112233445566778899AABBCCDDEEFF"); |
afceaf40 MHS |
1232 | return 0; |
1233 | } | |
1234 | ||
1235 | blockNo = param_get8(Cmd, 0); | |
81740aa5 | 1236 | if (blockNo < 0) { |
1237 | PrintAndLog("Wrong block number"); | |
1238 | return 1; | |
1239 | } | |
1240 | ||
a8be77af | 1241 | if (blockNo > MAX_ULC_BLOCKS ){ |
aa60d156 | 1242 | PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C"); |
afceaf40 MHS |
1243 | return 1; |
1244 | } | |
81740aa5 | 1245 | |
f1170fa7 | 1246 | // key |
1247 | if ( strlen(Cmd) > 3){ | |
aa60d156 | 1248 | if (param_gethex(Cmd, 1, key, 32)) { |
1249 | PrintAndLog("Key must include %d HEX symbols", 32); | |
f1170fa7 | 1250 | return 1; |
1251 | } else { | |
1252 | hasPwd = TRUE; | |
1253 | } | |
aa60d156 | 1254 | } |
afceaf40 MHS |
1255 | |
1256 | //Read Block | |
f1170fa7 | 1257 | UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}}; |
1258 | if ( hasPwd ) { | |
1259 | c.arg[1] = 1; | |
1260 | memcpy(c.d.asBytes,key,16); | |
1261 | } | |
1262 | SendCommand(&c); | |
aa60d156 | 1263 | |
f1170fa7 | 1264 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { |
1265 | uint8_t isOK = resp.arg[0] & 0xff; | |
aa60d156 | 1266 | if (isOK) { |
1267 | uint8_t *data = resp.d.asBytes; | |
1268 | PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo, blockNo, sprint_hex(data, 4)); | |
1269 | } | |
1270 | else { | |
1271 | PrintAndLog("Failed reading block: (%02x)", isOK); | |
1272 | } | |
81740aa5 | 1273 | } else { |
aa60d156 | 1274 | PrintAndLog("Command execute time-out"); |
81740aa5 | 1275 | } |
afceaf40 | 1276 | return 0; |
81740aa5 | 1277 | } |
1278 | ||
1279 | // | |
1280 | // Mifare Ultralight C Write Single Block | |
1281 | // | |
1282 | int CmdHF14AMfUCWrBl(const char *Cmd){ | |
afceaf40 MHS |
1283 | |
1284 | uint8_t blockNo = -1; | |
1285 | bool chinese_card = FALSE; | |
1286 | uint8_t bldata[16] = {0x00}; | |
1287 | UsbCommand resp; | |
81740aa5 | 1288 | |
afceaf40 | 1289 | char cmdp = param_getchar(Cmd, 0); |
81740aa5 | 1290 | |
1291 | if (strlen(Cmd) < 3 || cmdp == 'h' || cmdp == 'H') { | |
afceaf40 | 1292 | PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]"); |
81740aa5 | 1293 | PrintAndLog(" [block number]"); |
1294 | PrintAndLog(" [block data] - (8 hex symbols)"); | |
1295 | PrintAndLog(" [w] - Chinese magic ultralight tag"); | |
1296 | PrintAndLog(""); | |
afceaf40 | 1297 | PrintAndLog(" sample: hf mfu cwrbl 0 01020304"); |
81740aa5 | 1298 | PrintAndLog(""); |
afceaf40 MHS |
1299 | return 0; |
1300 | } | |
81740aa5 | 1301 | |
afceaf40 | 1302 | blockNo = param_get8(Cmd, 0); |
a8be77af | 1303 | if (blockNo > MAX_ULC_BLOCKS ){ |
afceaf40 MHS |
1304 | PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!"); |
1305 | return 1; | |
1306 | } | |
81740aa5 | 1307 | |
afceaf40 MHS |
1308 | if (param_gethex(Cmd, 1, bldata, 8)) { |
1309 | PrintAndLog("Block data must include 8 HEX symbols"); | |
1310 | return 1; | |
1311 | } | |
81740aa5 | 1312 | |
afceaf40 MHS |
1313 | if (strchr(Cmd,'w') != 0 || strchr(Cmd,'W') != 0 ) { |
1314 | chinese_card = TRUE; | |
1315 | } | |
81740aa5 | 1316 | |
1317 | if ( blockNo <= 3 ) { | |
1318 | if (!chinese_card){ | |
1319 | PrintAndLog("Access Denied"); | |
2b03dea7 | 1320 | return 1; |
81740aa5 | 1321 | } else { |
1322 | PrintAndLog("--Special block no: 0x%02x", blockNo); | |
1323 | PrintAndLog("--Data: %s", sprint_hex(bldata, 4)); | |
1324 | UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}}; | |
1325 | memcpy(d.d.asBytes,bldata, 4); | |
1326 | SendCommand(&d); | |
1327 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
1328 | uint8_t isOK = resp.arg[0] & 0xff; | |
1329 | PrintAndLog("isOk:%02x", isOK); | |
1330 | } else { | |
1331 | PrintAndLog("Command execute timeout"); | |
2b03dea7 | 1332 | return 1; |
81740aa5 | 1333 | } |
1334 | } | |
1335 | } else { | |
afceaf40 MHS |
1336 | PrintAndLog("--Block no : 0x%02x", blockNo); |
1337 | PrintAndLog("--Data: %s", sprint_hex(bldata, 4)); | |
1338 | UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}}; | |
1339 | memcpy(e.d.asBytes,bldata, 4); | |
1340 | SendCommand(&e); | |
1341 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
1342 | uint8_t isOK = resp.arg[0] & 0xff; | |
1343 | PrintAndLog("isOk : %02x", isOK); | |
1344 | } else { | |
1345 | PrintAndLog("Command execute timeout"); | |
2b03dea7 | 1346 | return 1; |
afceaf40 | 1347 | } |
81740aa5 | 1348 | } |
1349 | return 0; | |
1350 | } | |
1351 | ||
aa60d156 | 1352 | // |
1353 | // Mifare Ultralight C - Set password | |
1354 | // | |
1355 | int CmdHF14AMfucSetPwd(const char *Cmd){ | |
1356 | ||
1ec21089 | 1357 | uint8_t pwd[16] = {0x00}; |
1c1c5f4c | 1358 | |
aa60d156 | 1359 | char cmdp = param_getchar(Cmd, 0); |
1360 | ||
1361 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') { | |
1362 | PrintAndLog("Usage: hf mfu setpwd <password (32 hex symbols)>"); | |
1363 | PrintAndLog(" [password] - (32 hex symbols)"); | |
1364 | PrintAndLog(""); | |
1365 | PrintAndLog("sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f"); | |
1366 | PrintAndLog(""); | |
1367 | return 0; | |
1368 | } | |
1369 | ||
1370 | if (param_gethex(Cmd, 0, pwd, 32)) { | |
1371 | PrintAndLog("Password must include 32 HEX symbols"); | |
1372 | return 1; | |
1373 | } | |
1374 | ||
1375 | UsbCommand c = {CMD_MIFAREUC_SETPWD}; | |
1376 | memcpy( c.d.asBytes, pwd, 16); | |
1377 | SendCommand(&c); | |
1378 | ||
1379 | UsbCommand resp; | |
1380 | ||
1381 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500) ) { | |
1382 | if ( (resp.arg[0] & 0xff) == 1) | |
1383 | PrintAndLog("Ultralight-C new password: %s", sprint_hex(pwd,16)); | |
1384 | else{ | |
1385 | PrintAndLog("Failed writing at block %d", resp.arg[1] & 0xff); | |
1386 | return 1; | |
1387 | } | |
1388 | } | |
1389 | else { | |
1390 | PrintAndLog("command execution time out"); | |
1c1c5f4c | 1391 | return 1; |
aa60d156 | 1392 | } |
1393 | ||
1394 | return 0; | |
1395 | } | |
1396 | ||
1397 | // | |
1ec21089 | 1398 | // Magic UL / UL-C tags - Set UID |
aa60d156 | 1399 | // |
1400 | int CmdHF14AMfucSetUid(const char *Cmd){ | |
1401 | ||
5d554ea6 | 1402 | UsbCommand c; |
1403 | UsbCommand resp; | |
aa60d156 | 1404 | uint8_t uid[7] = {0x00}; |
1405 | char cmdp = param_getchar(Cmd, 0); | |
1406 | ||
1407 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') { | |
1408 | PrintAndLog("Usage: hf mfu setuid <uid (14 hex symbols)>"); | |
1409 | PrintAndLog(" [uid] - (14 hex symbols)"); | |
5d554ea6 | 1410 | PrintAndLog("\nThis only works for Magic Ultralight tags."); |
aa60d156 | 1411 | PrintAndLog(""); |
1412 | PrintAndLog("sample: hf mfu setuid 11223344556677"); | |
1413 | PrintAndLog(""); | |
1414 | return 0; | |
1415 | } | |
1416 | ||
1417 | if (param_gethex(Cmd, 0, uid, 14)) { | |
5d554ea6 | 1418 | PrintAndLog("UID must include 14 HEX symbols"); |
aa60d156 | 1419 | return 1; |
1420 | } | |
1421 | ||
5d554ea6 | 1422 | // read block2. |
1423 | c.cmd = CMD_MIFAREU_READBL; | |
1424 | c.arg[0] = 2; | |
aa60d156 | 1425 | SendCommand(&c); |
5d554ea6 | 1426 | if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) { |
1427 | PrintAndLog("Command execute timeout"); | |
1428 | return 2; | |
aa60d156 | 1429 | } |
5d554ea6 | 1430 | |
1431 | // save old block2. | |
1432 | uint8_t oldblock2[4] = {0x00}; | |
1433 | memcpy(resp.d.asBytes, oldblock2, 4); | |
1434 | ||
1435 | // block 0. | |
1436 | c.cmd = CMD_MIFAREU_WRITEBL; | |
1437 | c.arg[0] = 0; | |
1438 | c.d.asBytes[0] = uid[0]; | |
1439 | c.d.asBytes[1] = uid[1]; | |
1440 | c.d.asBytes[2] = uid[2]; | |
1441 | c.d.asBytes[3] = 0x88 ^ uid[0] ^ uid[1] ^ uid[2]; | |
1442 | SendCommand(&c); | |
1443 | if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
1444 | PrintAndLog("Command execute timeout"); | |
1445 | return 3; | |
aa60d156 | 1446 | } |
5d554ea6 | 1447 | |
1448 | // block 1. | |
1449 | c.arg[0] = 1; | |
1450 | c.d.asBytes[0] = uid[3]; | |
1451 | c.d.asBytes[1] = uid[4]; | |
1452 | c.d.asBytes[2] = uid[5]; | |
1453 | c.d.asBytes[3] = uid[6]; | |
1454 | SendCommand(&c); | |
1455 | if (!WaitForResponseTimeout(CMD_ACK,&resp,1500) ) { | |
1456 | PrintAndLog("Command execute timeout"); | |
1457 | return 4; | |
1458 | } | |
1459 | ||
1460 | // block 2. | |
1461 | c.arg[0] = 2; | |
1462 | c.d.asBytes[0] = uid[3] ^ uid[4] ^ uid[5] ^ uid[6]; | |
1463 | c.d.asBytes[1] = oldblock2[1]; | |
1464 | c.d.asBytes[2] = oldblock2[2]; | |
1465 | c.d.asBytes[3] = oldblock2[3]; | |
1466 | SendCommand(&c); | |
1467 | if (!WaitForResponseTimeout(CMD_ACK,&resp,1500) ) { | |
1468 | PrintAndLog("Command execute timeout"); | |
1469 | return 5; | |
1470 | } | |
1471 | ||
aa60d156 | 1472 | return 0; |
1473 | } | |
1474 | ||
1475 | int CmdHF14AMfuGenDiverseKeys(const char *Cmd){ | |
1476 | ||
1477 | uint8_t iv[8] = { 0x00 }; | |
1478 | uint8_t block = 0x07; | |
1479 | ||
5d554ea6 | 1480 | // UL-EV1 |
1481 | //04 57 b6 e2 05 3f 80 UID | |
1482 | //4a f8 4b 19 PWD | |
aa60d156 | 1483 | uint8_t uid[] = { 0xF4,0xEA, 0x54, 0x8E }; |
395f6a81 | 1484 | uint8_t mifarekeyA[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 }; |
1485 | uint8_t mifarekeyB[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 }; | |
1486 | uint8_t dkeyA[8] = { 0x00 }; | |
1487 | uint8_t dkeyB[8] = { 0x00 }; | |
1488 | ||
aa60d156 | 1489 | uint8_t masterkey[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff }; |
1490 | ||
1491 | uint8_t mix[8] = { 0x00 }; | |
1492 | uint8_t divkey[8] = { 0x00 }; | |
1493 | ||
395f6a81 | 1494 | memcpy(mix, mifarekeyA, 4); |
aa60d156 | 1495 | |
395f6a81 | 1496 | mix[4] = mifarekeyA[4] ^ uid[0]; |
1497 | mix[5] = mifarekeyA[5] ^ uid[1]; | |
aa60d156 | 1498 | mix[6] = block ^ uid[2]; |
1499 | mix[7] = uid[3]; | |
1500 | ||
1501 | des3_context ctx = { 0x00 }; | |
1502 | des3_set2key_enc(&ctx, masterkey); | |
1503 | ||
f2019c77 | 1504 | des3_crypt_cbc(&ctx // des3_context |
1505 | , DES_ENCRYPT // int mode | |
1506 | , sizeof(mix) // length | |
1507 | , iv // iv[8] | |
1508 | , mix // input | |
1509 | , divkey // output | |
aa60d156 | 1510 | ); |
1511 | ||
1512 | PrintAndLog("3DES version"); | |
1513 | PrintAndLog("Masterkey :\t %s", sprint_hex(masterkey,sizeof(masterkey))); | |
1514 | PrintAndLog("UID :\t %s", sprint_hex(uid, sizeof(uid))); | |
1515 | PrintAndLog("Sector :\t %0d", block); | |
395f6a81 | 1516 | PrintAndLog("Mifare key :\t %s", sprint_hex(mifarekeyA, sizeof(mifarekeyA))); |
aa60d156 | 1517 | PrintAndLog("Message :\t %s", sprint_hex(mix, sizeof(mix))); |
1518 | PrintAndLog("Diversified key: %s", sprint_hex(divkey+1, 6)); | |
1519 | ||
395f6a81 | 1520 | PrintAndLog("\n DES version"); |
1521 | ||
1522 | for (int i=0; i < sizeof(mifarekeyA); ++i){ | |
1523 | dkeyA[i] = (mifarekeyA[i] << 1) & 0xff; | |
1524 | dkeyA[6] |= ((mifarekeyA[i] >> 7) & 1) << (i+1); | |
1525 | } | |
1526 | ||
1527 | for (int i=0; i < sizeof(mifarekeyB); ++i){ | |
1528 | dkeyB[1] |= ((mifarekeyB[i] >> 7) & 1) << (i+1); | |
1529 | dkeyB[2+i] = (mifarekeyB[i] << 1) & 0xff; | |
1530 | } | |
1531 | ||
1532 | uint8_t zeros[8] = {0x00}; | |
1533 | uint8_t newpwd[8] = {0x00}; | |
1534 | uint8_t dmkey[24] = {0x00}; | |
1535 | memcpy(dmkey, dkeyA, 8); | |
1536 | memcpy(dmkey+8, dkeyB, 8); | |
1537 | memcpy(dmkey+16, dkeyA, 8); | |
1538 | memset(iv, 0x00, 8); | |
1539 | ||
1540 | des3_set3key_enc(&ctx, dmkey); | |
1541 | ||
1542 | des3_crypt_cbc(&ctx // des3_context | |
1543 | , DES_ENCRYPT // int mode | |
1544 | , sizeof(newpwd) // length | |
1545 | , iv // iv[8] | |
1546 | , zeros // input | |
1547 | , newpwd // output | |
1548 | ); | |
1549 | ||
1550 | PrintAndLog("Mifare dkeyA :\t %s", sprint_hex(dkeyA, sizeof(dkeyA))); | |
1551 | PrintAndLog("Mifare dkeyB :\t %s", sprint_hex(dkeyB, sizeof(dkeyB))); | |
1552 | PrintAndLog("Mifare ABA :\t %s", sprint_hex(dmkey, sizeof(dmkey))); | |
1553 | PrintAndLog("Mifare Pwd :\t %s", sprint_hex(newpwd, sizeof(newpwd))); | |
1554 | ||
aa60d156 | 1555 | return 0; |
1556 | } | |
1557 | ||
395f6a81 | 1558 | // static uint8_t * diversify_key(uint8_t * key){ |
1559 | ||
aa60d156 | 1560 | // for(int i=0; i<16; i++){ |
1561 | // if(i<=6) key[i]^=cuid[i]; | |
1562 | // if(i>6) key[i]^=cuid[i%7]; | |
1563 | // } | |
1564 | // return key; | |
1565 | // } | |
1566 | ||
1567 | // static void GenerateUIDe( uint8_t *uid, uint8_t len){ | |
1568 | // for (int i=0; i<len; ++i){ | |
1569 | ||
1570 | // } | |
1571 | // return; | |
1572 | // } | |
1573 | ||
81740aa5 | 1574 | static command_t CommandTable[] = |
1575 | { | |
1ec21089 | 1576 | {"help", CmdHelp, 1, "This help"}, |
1577 | {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"}, | |
1578 | {"info", CmdHF14AMfUInfo, 0, "Tag information"}, | |
1579 | {"dump", CmdHF14AMfUDump, 0, "Dump Ultralight / Ultralight-C tag to binary file"}, | |
1580 | {"rdbl", CmdHF14AMfURdBl, 0, "Read block - Ultralight"}, | |
1581 | {"wrbl", CmdHF14AMfUWrBl, 0, "Write block - Ultralight"}, | |
1582 | {"crdbl", CmdHF14AMfUCRdBl, 0, "Read block - Ultralight C"}, | |
1583 | {"cwrbl", CmdHF14AMfUCWrBl, 0, "Write block - Ultralight C"}, | |
1584 | {"cauth", CmdHF14AMfucAuth, 0, "Authentication - Ultralight C"}, | |
1585 | {"setpwd", CmdHF14AMfucSetPwd, 1, "Set 3des password - Ultralight-C"}, | |
1586 | {"setuid", CmdHF14AMfucSetUid, 1, "Set UID - MAGIC tags only"}, | |
a8be77af | 1587 | {"gen", CmdHF14AMfuGenDiverseKeys , 1, "Generate 3des mifare diversified keys"}, |
afceaf40 | 1588 | {NULL, NULL, 0, NULL} |
81740aa5 | 1589 | }; |
1590 | ||
1591 | int CmdHFMFUltra(const char *Cmd){ | |
afceaf40 MHS |
1592 | WaitForResponseTimeout(CMD_ACK,NULL,100); |
1593 | CmdsParse(CommandTable, Cmd); | |
1594 | return 0; | |
81740aa5 | 1595 | } |
1596 | ||
1597 | int CmdHelp(const char *Cmd){ | |
afceaf40 MHS |
1598 | CmdsHelp(CommandTable); |
1599 | return 0; | |
2b03dea7 | 1600 | } |