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