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