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