1 //-----------------------------------------------------------------------------
2 // Ultralight Code (c) 2013,2014 Midnitesnake & Andy Davies of Pentura
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
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE ULTRALIGHT (C) commands
9 //-----------------------------------------------------------------------------
10 #include "loclass/des.h"
16 #include "protocols.h"
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
26 typedef enum TAGTYPE_UL
{
37 UL_MAGIC
= UL
| MAGIC
,
38 UL_C_MAGIC
= UL_C
| MAGIC
,
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
52 uint8_t default_pwd_pack
[3][4] = {
53 {0xFF,0xFF,0xFF,0xFF}, // PACK 0x00,0x00 -- factory default
54 {0x4A,0xF8,0x4B,0x19}, // PACK 0xE5,0xBE -- italian bus (sniffed)
55 {0x05,0x22,0xE6,0xB4} // PACK 0x80,0x80 -- Amiiboo (sniffed)
58 static int CmdHelp(const char *Cmd
);
60 char* getProductTypeStr( uint8_t id
){
67 sprintf(retStr
, "0x%02X %s", id
, "(Ultralight)");
70 sprintf(retStr
, "0x%02X %s", id
, "(NTAG)");
73 sprintf(retStr
, "0x%02X %s", id
, "(unknown)");
80 The 7 MSBits (=n) code the storage size itself based on 2^n,
81 the LSBit is set to '0' if the size is exactly 2^n
82 and set to '1' if the storage size is between 2^n and 2^(n+1).
84 char* getUlev1CardSizeStr( uint8_t fsize
){
89 uint8_t usize
= 1 << ((fsize
>>1) + 1);
90 uint8_t lsize
= 1 << (fsize
>>1);
94 sprintf(retStr
, "0x%02X (%u - %u bytes)",fsize
, usize
, lsize
);
96 sprintf(retStr
, "0x%02X (%u bytes)", fsize
, lsize
);
100 static void ul_switch_on_field(void) {
101 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_NO_DISCONNECT
, 0, 0}};
105 static void ul_switch_off_field(void) {
106 UsbCommand c
= {CMD_READER_ISO_14443a
, {0, 0, 0}};
110 static int ul_send_cmd_raw( uint8_t *cmd
, uint8_t cmdlen
, uint8_t *response
, uint16_t responseLength
) {
111 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_RAW
| ISO14A_NO_DISCONNECT
| ISO14A_APPEND_CRC
, cmdlen
, 0}};
112 memcpy(c
.d
.asBytes
, cmd
, cmdlen
);
115 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
116 if (!resp
.arg
[0] && responseLength
) return -1;
118 uint16_t resplen
= (resp
.arg
[0] < responseLength
) ? resp
.arg
[0] : responseLength
;
119 memcpy(response
, resp
.d
.asBytes
, resplen
);
123 static int ul_send_cmd_raw_crc( uint8_t *cmd, uint8_t cmdlen, uint8_t *response, uint16_t responseLength, bool append_crc ) {
124 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT , cmdlen, 0}};
126 c.arg[0] |= ISO14A_APPEND_CRC;
128 memcpy(c.d.asBytes, cmd, cmdlen);
131 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
132 if (!resp.arg[0] && responseLength) return -1;
134 uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength;
135 memcpy(response, resp.d.asBytes, resplen);
139 static int ul_select( iso14a_card_select_t
*card
){
141 ul_switch_on_field();
144 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
145 if (resp
.arg
[0] < 1) return -1;
147 memcpy(card
, resp
.d
.asBytes
, sizeof(iso14a_card_select_t
));
151 // This read command will at least return 16bytes.
152 static int ul_read( uint8_t page
, uint8_t *response
, uint16_t responseLength
){
154 uint8_t cmd
[] = {ISO14443A_CMD_READBLOCK
, page
};
155 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
157 ul_switch_off_field();
161 static int ul_comp_write( uint8_t page
, uint8_t *data
, uint8_t datalen
){
164 memset(cmd
, 0x00, sizeof(cmd
));
165 datalen
= ( datalen
> 16) ? 16 : datalen
;
167 cmd
[0] = ISO14443A_CMD_WRITEBLOCK
;
169 memcpy(cmd
+2, data
, datalen
);
171 uint8_t response
[1] = {0xff};
172 int len
= ul_send_cmd_raw(cmd
, 2+datalen
, response
, sizeof(response
));
174 ul_switch_off_field();
176 if ( response
[0] == 0x0a ) return 0;
181 static int ulc_requestAuthentication( uint8_t blockNo
, uint8_t *nonce
, uint16_t nonceLength
){
183 uint8_t cmd
[] = {MIFARE_ULC_AUTH_1
, blockNo
};
184 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), nonce
, nonceLength
);
186 ul_switch_off_field();
190 static int ulev1_requestAuthentication( uint8_t *pwd
, uint8_t *pack
, uint16_t packLength
){
192 uint8_t cmd
[] = {MIFARE_ULEV1_AUTH
, pwd
[0], pwd
[1], pwd
[2], pwd
[3]};
193 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), pack
, packLength
);
195 ul_switch_off_field();
199 static int ulev1_getVersion( uint8_t *response
, uint16_t responseLength
){
201 uint8_t cmd
[] = {MIFARE_ULEV1_VERSION
};
202 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
204 ul_switch_off_field();
208 // static int ulev1_fastRead( uint8_t startblock, uint8_t endblock, uint8_t *response ){
210 // uint8_t cmd[] = {MIFARE_ULEV1_FASTREAD, startblock, endblock};
212 // if ( !ul_send_cmd_raw(cmd, sizeof(cmd), response)){
213 // ul_switch_off_field();
219 static int ulev1_readCounter( uint8_t counter
, uint8_t *response
, uint16_t responseLength
){
221 uint8_t cmd
[] = {MIFARE_ULEV1_READ_CNT
, counter
};
222 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
224 ul_switch_off_field();
228 static int ulev1_readSignature( uint8_t *response
, uint16_t responseLength
){
230 uint8_t cmd
[] = {MIFARE_ULEV1_READSIG
, 0x00};
231 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
233 ul_switch_off_field();
237 static int ul_print_default( uint8_t *data
){
249 PrintAndLog(" UID : %s ", sprint_hex(uid
, 7));
250 PrintAndLog(" UID[0] : (Manufacturer Byte) = %02x, Manufacturer: %s", uid
[0], getTagInfo(uid
[0]) );
253 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
254 int crc0
= 0x88 ^ data
[0] ^ data
[1] ^data
[2];
255 if ( data
[3] == crc0
)
256 PrintAndLog(" BCC0 : 0x%02X - Ok", data
[3]);
258 PrintAndLog(" BCC0 : 0x%02X - crc should be %02x", data
[3], crc0
);
260 int crc1
= data
[4] ^ data
[5] ^ data
[6] ^data
[7];
261 if ( data
[8] == crc1
)
262 PrintAndLog(" BCC1 : 0x%02X - Ok", data
[8]);
264 PrintAndLog(" BCC1 : 0x%02X - crc should be 0x%02X", data
[8], crc1
);
266 PrintAndLog(" Internal : 0x%02X - %s default", data
[9], (data
[9]==0x48)?"":"not" );
267 PrintAndLog(" Lock : %s - %s", sprint_hex(data
+10, 2),printBits( 2, data
+10) );
268 PrintAndLog("OneTimePad : %s ", sprint_hex(data
+ 12, 4));
273 static int ntag_print_CC(uint8_t *data
) {
274 if(data
[0] != 0xe1) {
275 PrintAndLog("no NDEF message");
276 return -1; // no NDEF message
279 PrintAndLog("Capability Container: %s", sprint_hex(data
,4) );
280 PrintAndLog(" %02X: NDEF Magic Number", data
[0]);
281 PrintAndLog(" %02X: version %d.%d supported by tag", data
[1], (data
[1] & 0xF0) >> 4, data
[1] & 0x0f);
282 PrintAndLog(" %02X: Physical Memory Size: %d bytes", data
[2], (data
[2] + 1) * 8);
283 if ( data
[2] == 0x12 )
284 PrintAndLog(" %02X: NDEF Memory Size: &d bytes", data
[2], 144);
285 else if ( data
[2] == 0x3e )
286 PrintAndLog(" %02X: NDEF Memory Size: &d bytes", data
[2], 496);
287 else if ( data
[2] == 0x6d )
288 PrintAndLog(" %02X: NDEF Memory Size: &d bytes", data
[2], 872);
290 PrintAndLog(" %02X: %s / %s", data
[3],
291 (data
[3] & 0xF0) ? "(RFU)" : "Read access granted without any security",
292 (data
[3] & 0x0F)==0 ? "Write access granted without any security" : (data
[3] & 0x0F)==0x0F ? "No write access granted at all" : "(RFU)");
296 static int ulev1_print_version(uint8_t *data
){
297 PrintAndLog("\n--- UL-EV1 / NTAG Version");
298 PrintAndLog(" Raw bytes : %s", sprint_hex(data
, 8) );
299 PrintAndLog(" Vendor ID : 0x%02X, Manufacturer: %s", data
[1], getTagInfo(data
[1]));
300 PrintAndLog(" Product type : %s" , getProductTypeStr(data
[2]));
301 PrintAndLog(" Product subtype : 0x%02X %s" , data
[3], (data
[3]==1) ?"17 pF":"50pF");
302 PrintAndLog(" Major version : 0x%02X" , data
[4]);
303 PrintAndLog(" Minor version : 0x%02X" , data
[5]);
304 PrintAndLog(" Size : %s", getUlev1CardSizeStr(data
[6]));
305 PrintAndLog(" Protocol type : 0x%02X" , data
[7]);
309 static int ul_print_type(uint16_t tagtype
){
311 PrintAndLog(" TYPE : MIFARE Ultralight (MF0ICU1) %s", (tagtype
& MAGIC
)?"<magic>":"");
312 else if ( tagtype
& UL_C
)
313 PrintAndLog(" TYPE : MIFARE Ultralight C (MF0ULC) %s [%x]", (tagtype
& MAGIC
)?"<magic>":"", tagtype
);
314 else if ( tagtype
& UL_EV1_48
)
315 PrintAndLog(" TYPE : MIFARE Ultralight EV1 48bytes (MF0UL1101)");
316 else if ( tagtype
& UL_EV1_128
)
317 PrintAndLog(" TYPE : MIFARE Ultralight EV1 128bytes (MF0UL2101)");
318 else if ( tagtype
& NTAG_213
)
319 PrintAndLog(" TYPE : MIFARE NTAG 213 144bytes (NT2H1311G0DU)");
320 else if ( tagtype
& NTAG_215
)
321 PrintAndLog(" TYPE : MIFARE NTAG 215 504bytes (NT2H1511G0DU)");
322 else if ( tagtype
& NTAG_216
)
323 PrintAndLog(" TYPE : MIFARE NTAG 216 888bytes (NT2H1611G0DU)");
325 PrintAndLog(" TYPE : Unknown %04x",tagtype
);
329 static int ulc_print_3deskey( uint8_t *data
){
330 PrintAndLog(" deskey1 [44/0x2C]: %s [%.4s]", sprint_hex(data
,4),data
);
331 PrintAndLog(" deskey1 [45/0x2D]: %s [%.4s]", sprint_hex(data
+4 ,4),data
+4);
332 PrintAndLog(" deskey2 [46/0x2E]: %s [%.4s]", sprint_hex(data
+8 ,4),data
+8);
333 PrintAndLog(" deskey2 [47/0x2F]: %s [%.4s]", sprint_hex(data
+12,4),data
+12);
334 PrintAndLog(" 3des key : %s", sprint_hex(SwapEndian64(data
, 16), 16));
338 static int ulc_print_configuration( uint8_t *data
){
340 PrintAndLog("--- UL-C Configuration");
341 PrintAndLog(" Higher Lockbits [40/0x28]: %s %s", sprint_hex(data
, 4), printBits(2, data
));
342 PrintAndLog(" Counter [41/0x29]: %s %s", sprint_hex(data
+4, 4), printBits(2, data
+4));
344 bool validAuth
= (data
[8] >= 0x03 && data
[8] <= 0x30);
346 PrintAndLog(" Auth0 [42/0x2A]: %s - Pages above %d needs authentication", sprint_hex(data
+8, 4), data
[8] );
349 PrintAndLog(" Auth0 [42/0x2A]: %s - default", sprint_hex(data
+8, 4) );
351 PrintAndLog(" Auth0 [42/0x2A]: %s - auth byte is out-of-range", sprint_hex(data
+8, 4) );
354 PrintAndLog(" Auth1 [43/0x2B]: %s - %s",
355 sprint_hex(data
+12, 4),
356 (data
[12] & 1) ? "write access restricted": "read and write access restricted"
361 static int ulev1_print_configuration( uint8_t *data
){
363 PrintAndLog("\n--- UL-EV1 Configuration");
365 bool strg_mod_en
= (data
[0] & 2);
366 uint8_t authlim
= (data
[4] & 0x07);
367 bool cfglck
= (data
[4] & 0x40);
368 bool prot
= (data
[4] & 0x80);
369 uint8_t vctid
= data
[5];
371 PrintAndLog(" cfg0 [16/0x10]: %s", sprint_hex(data
, 4));
372 PrintAndLog(" - pages above %d needs authentication",data
[3]);
373 PrintAndLog(" - strong modulation mode %s", (strg_mod_en
) ? "enabled":"disabled");
374 PrintAndLog(" cfg1 [17/0x11]: %s", sprint_hex(data
+4, 4) );
376 PrintAndLog(" - Max number of password attempts is unlimited");
378 PrintAndLog(" - Max number of password attempts is %d", authlim
);
379 PrintAndLog(" - user configuration %s", cfglck
? "permanently locked":"writeable");
380 PrintAndLog(" - %s access is protected with password", prot
? "read and write":"write");
381 PrintAndLog(" 0x%02X - Virtual Card Type Identifier is %s default", vctid
, (vctid
==0x05)? "":"not");
382 PrintAndLog(" PWD [18/0x12]: %s", sprint_hex(data
+8, 4));
383 PrintAndLog(" PACK [19/0x13]: %s", sprint_hex(data
+12, 4));
387 static int ulev1_print_counters(){
388 PrintAndLog("--- UL-EV1 Counters");
389 uint8_t counter
[3] = {0,0,0};
390 for ( uint8_t i
= 0; i
<3; ++i
) {
391 ulev1_readCounter(i
,counter
, sizeof(counter
) );
392 PrintAndLog("Counter [%d] : %s", i
, sprint_hex(counter
,3));
397 static int ulev1_print_signature( uint8_t *data
, uint8_t len
){
398 PrintAndLog("\n--- UL-EV1 Signature");
399 PrintAndLog("IC signature public key name : NXP NTAG21x 2013");
400 PrintAndLog("IC signature public key value : 04494e1a386d3d3cfe3dc10e5de68a499b1c202db5b132393e89ed19fe5be8bc61");
401 PrintAndLog(" Elliptic curve parameters : secp128r1");
402 PrintAndLog(" Tag ECC Signature : %s", sprint_hex(data
, len
));
403 //to do: verify if signature is valid
404 //PrintAndLog("IC signature status: %s valid", (iseccvalid() )?"":"not");
408 static int ulc_magic_test(){
409 // Magic Ultralight test
410 // Magic UL-C, by observation,
411 // 1) it seems to have a static nonce response to 0x1A command.
412 // 2) the deskey bytes is not-zero:d out on as datasheet states.
413 // 3) UID - changeable, not only, but pages 0-1-2-3.
414 // 4) use the ul_magic_test ! magic tags answers specially!
415 int returnValue
= UL_ERROR
;
416 iso14a_card_select_t card
;
417 uint8_t nonce1
[11] = {0x00};
418 uint8_t nonce2
[11] = {0x00};
419 int status
= ul_select(&card
);
421 PrintAndLog("Error: couldn't select ulc_magic_test");
422 ul_switch_off_field();
425 status
= ulc_requestAuthentication(0, nonce1
, sizeof(nonce1
));
427 status
= ulc_requestAuthentication(0, nonce2
, sizeof(nonce2
));
428 returnValue
= ( !memcmp(nonce1
, nonce2
, 11) ) ? UL_C_MAGIC
: UL_C
;
432 ul_switch_off_field();
436 static int ul_magic_test(){
438 // Magic Ultralight tests
439 // 1) take present UID, and try to write it back. OBSOLETE
440 // 2) make a wrong length write to page0, and see if tag answers with ACK/NACK:
441 iso14a_card_select_t card
;
442 int status
= ul_select(&card
);
444 PrintAndLog("Error: couldn't select ul_magic_test");
445 ul_switch_off_field();
448 status
= ul_comp_write(0, NULL
, 0);
449 ul_switch_off_field();
455 uint16_t GetHF14AMfU_Type(void){
457 TagTypeUL_t tagtype
= UNKNOWN
;
458 iso14a_card_select_t card
;
459 uint8_t version
[10] = {0x00};
463 status
= ul_select(&card
);
465 PrintAndLog("Error: couldn't select");
466 ul_switch_off_field();
469 // Ultralight - ATQA / SAK
470 if ( card
.atqa
[1] != 0x00 || card
.atqa
[0] != 0x44 || card
.sak
!= 0x00 ) {
471 PrintAndLog ("Tag is not UL or NTAG, ATQA1: %x, ATQA0: %x, SAK: %d", card
.atqa
[1],card
.atqa
[0],card
.sak
);
472 ul_switch_off_field();
476 len
= ulev1_getVersion(version
, sizeof(version
));
477 ul_switch_off_field();
482 if ( version
[2] == 0x03 && version
[6] == 0x0B )
484 else if ( version
[2] == 0x03 && version
[6] != 0x0B )
485 tagtype
= UL_EV1_128
;
486 else if ( version
[2] == 0x04 && version
[6] == 0x0F )
488 else if ( version
[2] == 0x04 && version
[6] != 0x11 )
490 else if ( version
[2] == 0x04 && version
[6] == 0x13 )
492 else if ( version
[2] == 0x04 )
497 case 0x01: tagtype
= UL_C
; break;
498 case 0x00: tagtype
= UL
; break;
499 case -1 : tagtype
= (UL
| UL_C
); break;
500 default : tagtype
= UNKNOWN
; break;
503 if ((tagtype
& ( UL_C
| UL
))) tagtype
= ulc_magic_test();
504 if ((tagtype
& UL
)) tagtype
= ul_magic_test();
509 int CmdHF14AMfUInfo(const char *Cmd
){
511 uint8_t authlim
= 0xff;
512 uint8_t data
[16] = {0x00};
513 iso14a_card_select_t card
;
517 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
518 if (tagtype
== UL_ERROR
) return -1;
520 PrintAndLog("\n--- Tag Information ---------");
521 PrintAndLog("-------------------------------------------------------------");
522 ul_print_type(tagtype
);
524 status
= ul_select(&card
);
526 PrintAndLog("Error: couldn't select");
527 ul_switch_off_field();
531 // read pages 0,1,2,4 (should read 4pages)
532 status
= ul_read(0, data
, sizeof(data
));
534 PrintAndLog("Error: tag didn't answer to READ A");
535 ul_switch_off_field();
539 ul_print_default(data
);
541 if ((tagtype
& UL_C
)){
543 // read pages 0x28, 0x29, 0x2A, 0x2B
544 uint8_t ulc_conf
[16] = {0x00};
545 status
= ul_read(0x28, ulc_conf
, sizeof(ulc_conf
));
547 PrintAndLog("Error: tag didn't answer to READ - possibly locked");
548 ul_switch_off_field();
552 ulc_print_configuration(ulc_conf
);
554 if ((tagtype
& MAGIC
)){
556 uint8_t ulc_deskey
[16] = {0x00};
557 status
= ul_read(0x2C, ulc_deskey
, sizeof(ulc_deskey
));
559 PrintAndLog("Error: tag didn't answer to READ B");
560 ul_switch_off_field();
563 ulc_print_3deskey(ulc_deskey
);
566 PrintAndLog("Trying some default 3des keys");
567 ul_switch_off_field();
568 for (uint8_t i
= 0; i
< 7; ++i
){
569 key
= default_3des_keys
[i
];
570 if (try3DesAuthentication(key
) == 1){
571 PrintAndLog("Found default 3des key: "); //%s", sprint_hex(key,16));
572 ulc_print_3deskey(SwapEndian64(key
,16));
579 if ((tagtype
& (UL_EV1_48
| UL_EV1_128
))) {
581 uint8_t startconfigblock
= (tagtype
& UL_EV1_48
) ? 0x10 : 0x25;
582 uint8_t ulev1_conf
[16] = {0x00};
583 status
= ul_read(startconfigblock
, ulev1_conf
, sizeof(ulev1_conf
));
585 PrintAndLog("Error: tag didn't answer to READ C");
586 ul_switch_off_field();
589 // save AUTHENTICATION LIMITS for later:
590 authlim
= (ulev1_conf
[4] & 0x07);
592 ulev1_print_configuration(ulev1_conf
);
594 uint8_t ulev1_signature
[32] = {0x00};
595 status
= ulev1_readSignature( ulev1_signature
, sizeof(ulev1_signature
));
597 PrintAndLog("Error: tag didn't answer to READ SIGNATURE");
598 ul_switch_off_field();
601 ulev1_print_signature( ulev1_signature
, sizeof(ulev1_signature
));
603 ulev1_print_counters();
606 if ((tagtype
& (UL_EV1_48
| UL_EV1_128
| NTAG_213
| NTAG_215
| NTAG_216
))) {
608 uint8_t version
[10] = {0x00};
609 status
= ulev1_getVersion(version
, sizeof(version
));
611 PrintAndLog("Error: tag didn't answer to GETVERSION");
612 ul_switch_off_field();
615 ulev1_print_version(version
);
617 // AUTHLIMIT, (number of failed authentications)
619 // 1-7 = ... should we even try then?
621 PrintAndLog("\n--- Known EV1/NTAG passwords.");
623 uint8_t pack
[4] = {0,0,0,0};
625 for (uint8_t i
= 0; i
< 3; ++i
){
626 key
= default_pwd_pack
[i
];
627 if ( ulev1_requestAuthentication(key
, pack
, sizeof(pack
)) > -1 ){
628 PrintAndLog("Found a default password: %s || Pack: %02X %02X",sprint_hex(key
, 4), pack
[0], pack
[1]);
631 ul_switch_off_field();
635 if ((tagtype
& (NTAG_213
| NTAG_215
| NTAG_216
))){
637 PrintAndLog("\n--- NTAG NDEF Message");
638 uint8_t cc
[16] = {0x00};
639 status
= ul_read(2, cc
, sizeof(cc
));
641 PrintAndLog("Error: tag didn't answer to READ D");
642 ul_switch_off_field();
648 ul_switch_off_field();
653 // Mifare Ultralight Write Single Block
655 int CmdHF14AMfUWrBl(const char *Cmd
){
656 uint8_t blockNo
= -1;
657 bool chinese_card
= FALSE
;
658 uint8_t bldata
[16] = {0x00};
661 char cmdp
= param_getchar(Cmd
, 0);
662 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
663 PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]");
664 PrintAndLog(" [block number]");
665 PrintAndLog(" [block data] - (8 hex symbols)");
666 PrintAndLog(" [w] - Chinese magic ultralight tag");
668 PrintAndLog(" sample: hf mfu wrbl 0 01020304");
673 blockNo
= param_get8(Cmd
, 0);
675 if (blockNo
> MAX_UL_BLOCKS
){
676 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
680 if (param_gethex(Cmd
, 1, bldata
, 8)) {
681 PrintAndLog("Block data must include 8 HEX symbols");
685 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
691 PrintAndLog("Access Denied");
693 PrintAndLog("--specialblock no:%02x", blockNo
);
694 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
695 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
696 memcpy(d
.d
.asBytes
,bldata
, 4);
698 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
699 uint8_t isOK
= resp
.arg
[0] & 0xff;
700 PrintAndLog("isOk:%02x", isOK
);
702 PrintAndLog("Command execute timeout");
706 PrintAndLog("--block no:%02x", blockNo
);
707 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
708 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
709 memcpy(e
.d
.asBytes
,bldata
, 4);
711 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
712 uint8_t isOK
= resp
.arg
[0] & 0xff;
713 PrintAndLog("isOk:%02x", isOK
);
715 PrintAndLog("Command execute timeout");
722 // Mifare Ultralight Read Single Block
724 int CmdHF14AMfURdBl(const char *Cmd
){
727 uint8_t blockNo
= -1;
728 char cmdp
= param_getchar(Cmd
, 0);
730 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
731 PrintAndLog("Usage: hf mfu rdbl <block number>");
732 PrintAndLog(" sample: hfu mfu rdbl 0");
736 blockNo
= param_get8(Cmd
, 0);
738 if (blockNo
> MAX_UL_BLOCKS
){
739 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight");
743 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
747 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
748 uint8_t isOK
= resp
.arg
[0] & 0xff;
750 uint8_t *data
= resp
.d
.asBytes
;
751 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
754 PrintAndLog("Failed reading block: (%02x)", isOK
);
757 PrintAndLog("Command execute time-out");
763 int usage_hf_mfu_dump(void)
765 PrintAndLog("Reads all pages from Ultralight, Ultralight-C, Ultralight EV1");
766 PrintAndLog("and saves binary dump into the file `filename.bin` or `cardUID.bin`");
767 PrintAndLog("It autodetects card type.\n");
768 PrintAndLog("Usage: hf mfu dump k <key> n <filename w/o .bin>");
769 PrintAndLog(" Options : ");
770 PrintAndLog(" k <key> : Enter key for authentication");
771 PrintAndLog(" n <FN > : Enter filename w/o .bin to save the dump as");
772 PrintAndLog(" s : Swap entered key's endianness for auth");
774 PrintAndLog(" sample : hf mfu dump");
775 PrintAndLog(" : hf mfu dump n myfile");
779 // Mifare Ultralight / Ultralight-C / Ultralight-EV1
780 // Read and Dump Card Contents, using auto detection of tag size.
782 // TODO: take a password to read UL-C / UL-EV1 tags.
783 int CmdHF14AMfUDump(const char *Cmd
){
786 char filename
[FILE_PATH_SIZE
] = {0x00};
787 char *fnameptr
= filename
;
788 char *str
= "Dumping Ultralight%s%s Card Data...";
789 uint8_t *lockbytes_t
= NULL
;
790 uint8_t lockbytes
[2] = {0x00};
791 uint8_t *lockbytes_t2
= NULL
;
792 uint8_t lockbytes2
[2] = {0x00};
793 bool bit
[16] = {0x00};
794 bool bit2
[16] = {0x00};
795 uint8_t data
[1024] = {0x00};
799 bool tmplockbit
= false;
802 uint8_t key
[16] = {0x00};
803 uint8_t *keyPtr
= key
;
806 bool swapEndian
= false;
808 while(param_getchar(Cmd
, cmdp
) != 0x00)
810 switch(param_getchar(Cmd
, cmdp
))
814 return usage_hf_mfu_dump();
817 dataLen
= param_gethex(Cmd
, cmdp
+1, data
, 32);
821 memcpy(key
, data
, 16);
828 fileNlen
= param_getstr(Cmd
, cmdp
+1, filename
);
829 if (!fileNlen
) errors
= true;
830 if (fileNlen
> FILE_PATH_SIZE
-5) fileNlen
= FILE_PATH_SIZE
-5;
837 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
845 if(errors
) return usage_hf_mfu_dump();
848 keyPtr
= SwapEndian64(data
, 16);
850 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
851 if (tagtype
== UL_ERROR
) return -1;
853 if ( tagtype
& UL
) {
855 PrintAndLog(str
,"", (tagtype
& MAGIC
)?" (magic)":"" );
857 else if ( tagtype
& UL_C
) {
859 PrintAndLog(str
,"-C", (tagtype
& MAGIC
)?" (magic)":"" );
861 else if ( tagtype
& UL_EV1_48
) {
863 PrintAndLog(str
," EV1_48","");
865 else if ( tagtype
& UL_EV1_128
) {
867 PrintAndLog(str
," EV1_128","");
870 PrintAndLog("Dumping unknown Ultralight, using default values.");
873 UsbCommand c
= {CMD_MIFAREUC_READCARD
, {0,Pages
}};
876 memcpy(c
.d
.asBytes
, key
, 16);
880 if (!WaitForResponseTimeout(CMD_ACK
, &resp
,1500)) {
881 PrintAndLog("Command execute time-out");
884 PrintAndLog ("%u,%u",resp
.arg
[0],resp
.arg
[1]);
885 uint8_t isOK
= resp
.arg
[0] & 0xff;
887 memcpy(data
, resp
.d
.asBytes
, resp
.arg
[1]);
889 PrintAndLog("Failed reading block: (%02x)", i
);
896 lockbytes_t
= data
+ 8;
897 lockbytes
[0] = lockbytes_t
[2];
898 lockbytes
[1] = lockbytes_t
[3];
899 for(j
= 0; j
< 16; j
++){
900 bit
[j
] = lockbytes
[j
/8] & ( 1 <<(7-j
%8));
903 // Load bottom lockbytes if available
905 lockbytes_t2
= data
+ (40*4);
906 lockbytes2
[0] = lockbytes_t2
[2];
907 lockbytes2
[1] = lockbytes_t2
[3];
908 for (j
= 0; j
< 16; j
++) {
909 bit2
[j
] = lockbytes2
[j
/8] & ( 1 <<(7-j
%8));
915 memcpy(data
+ Pages
*4, key
, 16);
918 for (i
= 0; i
< Pages
; ++i
) {
920 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
924 case 3: tmplockbit
= bit
[4]; break;
925 case 4: tmplockbit
= bit
[3]; break;
926 case 5: tmplockbit
= bit
[2]; break;
927 case 6: tmplockbit
= bit
[1]; break;
928 case 7: tmplockbit
= bit
[0]; break;
929 case 8: tmplockbit
= bit
[15]; break;
930 case 9: tmplockbit
= bit
[14]; break;
931 case 10: tmplockbit
= bit
[13]; break;
932 case 11: tmplockbit
= bit
[12]; break;
933 case 12: tmplockbit
= bit
[11]; break;
934 case 13: tmplockbit
= bit
[10]; break;
935 case 14: tmplockbit
= bit
[9]; break;
936 case 15: tmplockbit
= bit
[8]; break;
940 case 19: tmplockbit
= bit2
[6]; break;
944 case 23: tmplockbit
= bit2
[5]; break;
948 case 27: tmplockbit
= bit2
[4]; break;
952 case 31: tmplockbit
= bit2
[2]; break;
956 case 35: tmplockbit
= bit2
[1]; break;
960 case 39: tmplockbit
= bit2
[0]; break;
961 case 40: tmplockbit
= bit2
[12]; break;
962 case 41: tmplockbit
= bit2
[11]; break;
963 case 42: tmplockbit
= bit2
[10]; break; //auth0
964 case 43: tmplockbit
= bit2
[9]; break; //auth1
967 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),tmplockbit
);
970 // user supplied filename?
972 // UID = data 0-1-2 4-5-6-7 (skips a beat)
973 sprintf(fnameptr
,"%02X%02X%02X%02X%02X%02X%02X.bin",
974 data
[0],data
[1], data
[2], data
[4],data
[5],data
[6], data
[7]);
976 sprintf(fnameptr
+ fileNlen
," .bin");
979 if ((fout
= fopen(filename
,"wb")) == NULL
) {
980 PrintAndLog("Could not create file name %s", filename
);
983 fwrite( data
, 1, Pages
*4, fout
);
986 PrintAndLog("Dumped %d pages, wrote %d bytes to %s", Pages
, Pages
*4, filename
);
990 // Needed to Authenticate to Ultralight C tags
991 void rol (uint8_t *data
, const size_t len
){
992 uint8_t first
= data
[0];
993 for (size_t i
= 0; i
< len
-1; i
++) {
999 //-------------------------------------------------------------------------------
1000 // Ultralight C Methods
1001 //-------------------------------------------------------------------------------
1004 // Ultralight C Authentication Demo {currently uses hard-coded key}
1006 int CmdHF14AMfucAuth(const char *Cmd
){
1009 bool errors
= false;
1011 char cmdp
= param_getchar(Cmd
, 0);
1013 //Change key to user defined one
1014 if (cmdp
== 'k' || cmdp
== 'K'){
1015 keyNo
= param_get8(Cmd
, 1);
1020 if (cmdp
== 'h' || cmdp
== 'H')
1024 PrintAndLog("Usage: hf mfu cauth k <key number>");
1025 PrintAndLog(" 0 (default): 3DES standard key");
1026 PrintAndLog(" 1 : all 0x00 key");
1027 PrintAndLog(" 2 : 0x00-0x0F key");
1028 PrintAndLog(" 3 : nfc key");
1029 PrintAndLog(" 4 : all 0x01 key");
1030 PrintAndLog(" 5 : all 0xff key");
1031 PrintAndLog(" 6 : 0x00-0xFF key");
1032 PrintAndLog("\n sample : hf mfu cauth k");
1033 PrintAndLog(" : hf mfu cauth k 3");
1037 uint8_t *key
= default_3des_keys
[keyNo
];
1038 if (try3DesAuthentication(key
)>0)
1039 PrintAndLog("Authentication successful. 3des key: %s",sprint_hex(key
, 16));
1041 PrintAndLog("Authentication failed");
1046 int try3DesAuthentication( uint8_t *key
){
1048 uint8_t blockNo
= 0;
1051 des3_context ctx
= { 0 };
1053 uint8_t random_a
[8] = { 1,1,1,1,1,1,1,1 };
1054 uint8_t random_b
[8] = { 0 };
1055 uint8_t enc_random_b
[8] = { 0 };
1056 uint8_t rnd_ab
[16] = { 0 };
1057 uint8_t iv
[8] = { 0 };
1059 UsbCommand c
= {CMD_MIFAREUC_AUTH1
, {blockNo
}};
1062 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) return -1;
1063 if ( !(resp
.arg
[0] & 0xff) ) return -2;
1066 memcpy(enc_random_b
,resp
.d
.asBytes
+1,8);
1068 des3_set2key_dec(&ctx
, key
);
1069 // context, mode, length, IV, input, output
1070 des3_crypt_cbc( &ctx
, DES_DECRYPT
, sizeof(random_b
), iv
, enc_random_b
, random_b
);
1073 memcpy(rnd_ab
,random_a
,8);
1074 memcpy(rnd_ab
+8,random_b
,8);
1076 des3_set2key_enc(&ctx
, key
);
1077 // context, mode, length, IV, input, output
1078 des3_crypt_cbc(&ctx
, DES_ENCRYPT
, sizeof(rnd_ab
), enc_random_b
, rnd_ab
, rnd_ab
);
1081 c
.cmd
= CMD_MIFAREUC_AUTH2
;
1083 memcpy(c
.d
.asBytes
, rnd_ab
, 16);
1086 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
1087 if ( !(resp
.arg
[0] & 0xff)) return -2;
1089 uint8_t enc_resp
[8] = { 0 };
1090 uint8_t resp_random_a
[8] = { 0 };
1091 memcpy(enc_resp
, resp
.d
.asBytes
+1, 8);
1093 des3_set2key_dec(&ctx
, key
);
1094 // context, mode, length, IV, input, output
1095 des3_crypt_cbc( &ctx
, DES_DECRYPT
, 8, enc_random_b
, enc_resp
, resp_random_a
);
1097 if ( !memcmp(resp_random_a
, random_a
, 8))
1101 //PrintAndLog(" RndA :%s", sprint_hex(random_a, 8));
1102 //PrintAndLog(" enc(RndB) :%s", sprint_hex(enc_random_b, 8));
1103 //PrintAndLog(" RndB :%s", sprint_hex(random_b, 8));
1104 //PrintAndLog(" A+B :%s", sprint_hex(random_a_and_b, 16));
1105 //PrintAndLog(" enc(A+B) :%s", sprint_hex(random_a_and_b, 16));
1106 //PrintAndLog(" enc(RndA') :%s", sprint_hex(data2+1, 8));
1110 A test function to validate that the polarssl-function works the same
1111 was as the openssl-implementation.
1112 Commented out, since it requires openssl
1114 int CmdTestDES(const char * cmd)
1116 uint8_t key[16] = {0x00};
1118 memcpy(key,key3_3des_data,16);
1119 DES_cblock RndA, RndB;
1121 PrintAndLog("----------OpenSSL DES implementation----------");
1123 uint8_t e_RndB[8] = {0x00};
1124 unsigned char RndARndB[16] = {0x00};
1126 DES_cblock iv = { 0 };
1127 DES_key_schedule ks1,ks2;
1128 DES_cblock key1,key2;
1130 memcpy(key,key3_3des_data,16);
1132 memcpy(key2,key+8,8);
1135 DES_set_key((DES_cblock *)key1,&ks1);
1136 DES_set_key((DES_cblock *)key2,&ks2);
1138 DES_random_key(&RndA);
1139 PrintAndLog(" RndA:%s",sprint_hex(RndA, 8));
1140 PrintAndLog(" e_RndB:%s",sprint_hex(e_RndB, 8));
1141 //void DES_ede2_cbc_encrypt(const unsigned char *input,
1142 // unsigned char *output, long length, DES_key_schedule *ks1,
1143 // DES_key_schedule *ks2, DES_cblock *ivec, int enc);
1144 DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0);
1146 PrintAndLog(" RndB:%s",sprint_hex(RndB, 8));
1148 memcpy(RndARndB,RndA,8);
1149 memcpy(RndARndB+8,RndB,8);
1150 PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16));
1151 DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1);
1152 PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16));
1155 PrintAndLog("----------PolarSSL implementation----------");
1157 uint8_t random_a[8] = { 0 };
1158 uint8_t enc_random_a[8] = { 0 };
1159 uint8_t random_b[8] = { 0 };
1160 uint8_t enc_random_b[8] = { 0 };
1161 uint8_t random_a_and_b[16] = { 0 };
1162 des3_context ctx = { 0 };
1164 memcpy(random_a, RndA,8);
1166 uint8_t output[8] = { 0 };
1167 uint8_t iv[8] = { 0 };
1169 PrintAndLog(" RndA :%s",sprint_hex(random_a, 8));
1170 PrintAndLog(" e_RndB:%s",sprint_hex(enc_random_b, 8));
1172 des3_set2key_dec(&ctx, key);
1174 des3_crypt_cbc(&ctx // des3_context *ctx
1175 , DES_DECRYPT // int mode
1176 , sizeof(random_b) // size_t length
1177 , iv // unsigned char iv[8]
1178 , enc_random_b // const unsigned char *input
1179 , random_b // unsigned char *output
1182 PrintAndLog(" RndB:%s",sprint_hex(random_b, 8));
1185 memcpy(random_a_and_b ,random_a,8);
1186 memcpy(random_a_and_b+8,random_b,8);
1188 PrintAndLog(" RA+B:%s",sprint_hex(random_a_and_b, 16));
1190 des3_set2key_enc(&ctx, key);
1192 des3_crypt_cbc(&ctx // des3_context *ctx
1193 , DES_ENCRYPT // int mode
1194 , sizeof(random_a_and_b) // size_t length
1195 , enc_random_b // unsigned char iv[8]
1196 , random_a_and_b // const unsigned char *input
1197 , random_a_and_b // unsigned char *output
1200 PrintAndLog("enc(RA+B):%s",sprint_hex(random_a_and_b, 16));
1207 // Ultralight C Read Single Block
1209 int CmdHF14AMfUCRdBl(const char *Cmd
)
1212 bool hasPwd
= FALSE
;
1213 uint8_t blockNo
= -1;
1215 char cmdp
= param_getchar(Cmd
, 0);
1217 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
1218 PrintAndLog("Usage: hf mfu crdbl <block number> <key>");
1220 PrintAndLog("sample: hf mfu crdbl 0");
1221 PrintAndLog(" hf mfu crdbl 0 00112233445566778899AABBCCDDEEFF");
1225 blockNo
= param_get8(Cmd
, 0);
1227 PrintAndLog("Wrong block number");
1231 if (blockNo
> MAX_ULC_BLOCKS
){
1232 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C");
1237 if ( strlen(Cmd
) > 3){
1238 if (param_gethex(Cmd
, 1, key
, 32)) {
1239 PrintAndLog("Key must include %d HEX symbols", 32);
1245 //uint8_t *key2 = SwapEndian64(key, 16);
1248 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
1251 memcpy(c
.d
.asBytes
,key
,16);
1255 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1256 uint8_t isOK
= resp
.arg
[0] & 0xff;
1258 uint8_t *data
= resp
.d
.asBytes
;
1259 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
1262 PrintAndLog("Failed reading block: (%02x)", isOK
);
1265 PrintAndLog("Command execute time-out");
1271 // Mifare Ultralight C Write Single Block
1273 int CmdHF14AMfUCWrBl(const char *Cmd
){
1275 uint8_t blockNo
= -1;
1276 bool chinese_card
= FALSE
;
1277 uint8_t bldata
[16] = {0x00};
1280 char cmdp
= param_getchar(Cmd
, 0);
1282 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
1283 PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]");
1284 PrintAndLog(" [block number]");
1285 PrintAndLog(" [block data] - (8 hex symbols)");
1286 PrintAndLog(" [w] - Chinese magic ultralight tag");
1288 PrintAndLog(" sample: hf mfu cwrbl 0 01020304");
1293 blockNo
= param_get8(Cmd
, 0);
1294 if (blockNo
> MAX_ULC_BLOCKS
){
1295 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!");
1299 if (param_gethex(Cmd
, 1, bldata
, 8)) {
1300 PrintAndLog("Block data must include 8 HEX symbols");
1304 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
1305 chinese_card
= TRUE
;
1308 if ( blockNo
<= 3 ) {
1310 PrintAndLog("Access Denied");
1313 PrintAndLog("--Special block no: 0x%02x", blockNo
);
1314 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
1315 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1316 memcpy(d
.d
.asBytes
,bldata
, 4);
1318 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1319 uint8_t isOK
= resp
.arg
[0] & 0xff;
1320 PrintAndLog("isOk:%02x", isOK
);
1322 PrintAndLog("Command execute timeout");
1327 PrintAndLog("--Block no : 0x%02x", blockNo
);
1328 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
1329 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1330 memcpy(e
.d
.asBytes
,bldata
, 4);
1332 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1333 uint8_t isOK
= resp
.arg
[0] & 0xff;
1334 PrintAndLog("isOk : %02x", isOK
);
1336 PrintAndLog("Command execute timeout");
1344 // Mifare Ultralight C - Set password
1346 int CmdHF14AMfucSetPwd(const char *Cmd
){
1348 uint8_t pwd
[16] = {0x00};
1350 char cmdp
= param_getchar(Cmd
, 0);
1352 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
1353 PrintAndLog("Usage: hf mfu setpwd <password (32 hex symbols)>");
1354 PrintAndLog(" [password] - (32 hex symbols)");
1356 PrintAndLog("sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f");
1361 if (param_gethex(Cmd
, 0, pwd
, 32)) {
1362 PrintAndLog("Password must include 32 HEX symbols");
1366 UsbCommand c
= {CMD_MIFAREUC_SETPWD
};
1367 memcpy( c
.d
.asBytes
, pwd
, 16);
1372 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1373 if ( (resp
.arg
[0] & 0xff) == 1)
1374 PrintAndLog("Ultralight-C new password: %s", sprint_hex(pwd
,16));
1376 PrintAndLog("Failed writing at block %d", resp
.arg
[1] & 0xff);
1381 PrintAndLog("command execution time out");
1389 // Magic UL / UL-C tags - Set UID
1391 int CmdHF14AMfucSetUid(const char *Cmd
){
1395 uint8_t uid
[7] = {0x00};
1396 char cmdp
= param_getchar(Cmd
, 0);
1398 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
1399 PrintAndLog("Usage: hf mfu setuid <uid (14 hex symbols)>");
1400 PrintAndLog(" [uid] - (14 hex symbols)");
1401 PrintAndLog("\nThis only works for Magic Ultralight tags.");
1403 PrintAndLog("sample: hf mfu setuid 11223344556677");
1408 if (param_gethex(Cmd
, 0, uid
, 14)) {
1409 PrintAndLog("UID must include 14 HEX symbols");
1414 c
.cmd
= CMD_MIFAREU_READBL
;
1417 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1418 PrintAndLog("Command execute timeout");
1423 uint8_t oldblock2
[4] = {0x00};
1424 memcpy(resp
.d
.asBytes
, oldblock2
, 4);
1427 c
.cmd
= CMD_MIFAREU_WRITEBL
;
1429 c
.d
.asBytes
[0] = uid
[0];
1430 c
.d
.asBytes
[1] = uid
[1];
1431 c
.d
.asBytes
[2] = uid
[2];
1432 c
.d
.asBytes
[3] = 0x88 ^ uid
[0] ^ uid
[1] ^ uid
[2];
1434 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1435 PrintAndLog("Command execute timeout");
1441 c
.d
.asBytes
[0] = uid
[3];
1442 c
.d
.asBytes
[1] = uid
[4];
1443 c
.d
.asBytes
[2] = uid
[5];
1444 c
.d
.asBytes
[3] = uid
[6];
1446 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1447 PrintAndLog("Command execute timeout");
1453 c
.d
.asBytes
[0] = uid
[3] ^ uid
[4] ^ uid
[5] ^ uid
[6];
1454 c
.d
.asBytes
[1] = oldblock2
[1];
1455 c
.d
.asBytes
[2] = oldblock2
[2];
1456 c
.d
.asBytes
[3] = oldblock2
[3];
1458 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1459 PrintAndLog("Command execute timeout");
1466 int CmdHF14AMfuGenDiverseKeys(const char *Cmd
){
1468 uint8_t iv
[8] = { 0x00 };
1469 uint8_t block
= 0x07;
1472 //04 57 b6 e2 05 3f 80 UID
1474 uint8_t uid
[] = { 0xF4,0xEA, 0x54, 0x8E };
1475 uint8_t mifarekeyA
[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 };
1476 uint8_t mifarekeyB
[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 };
1477 uint8_t dkeyA
[8] = { 0x00 };
1478 uint8_t dkeyB
[8] = { 0x00 };
1480 uint8_t masterkey
[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff };
1482 uint8_t mix
[8] = { 0x00 };
1483 uint8_t divkey
[8] = { 0x00 };
1485 memcpy(mix
, mifarekeyA
, 4);
1487 mix
[4] = mifarekeyA
[4] ^ uid
[0];
1488 mix
[5] = mifarekeyA
[5] ^ uid
[1];
1489 mix
[6] = block
^ uid
[2];
1492 des3_context ctx
= { 0x00 };
1493 des3_set2key_enc(&ctx
, masterkey
);
1495 des3_crypt_cbc(&ctx
// des3_context
1496 , DES_ENCRYPT
// int mode
1497 , sizeof(mix
) // length
1503 PrintAndLog("3DES version");
1504 PrintAndLog("Masterkey :\t %s", sprint_hex(masterkey
,sizeof(masterkey
)));
1505 PrintAndLog("UID :\t %s", sprint_hex(uid
, sizeof(uid
)));
1506 PrintAndLog("Sector :\t %0d", block
);
1507 PrintAndLog("Mifare key :\t %s", sprint_hex(mifarekeyA
, sizeof(mifarekeyA
)));
1508 PrintAndLog("Message :\t %s", sprint_hex(mix
, sizeof(mix
)));
1509 PrintAndLog("Diversified key: %s", sprint_hex(divkey
+1, 6));
1511 PrintAndLog("\n DES version");
1513 for (int i
=0; i
< sizeof(mifarekeyA
); ++i
){
1514 dkeyA
[i
] = (mifarekeyA
[i
] << 1) & 0xff;
1515 dkeyA
[6] |= ((mifarekeyA
[i
] >> 7) & 1) << (i
+1);
1518 for (int i
=0; i
< sizeof(mifarekeyB
); ++i
){
1519 dkeyB
[1] |= ((mifarekeyB
[i
] >> 7) & 1) << (i
+1);
1520 dkeyB
[2+i
] = (mifarekeyB
[i
] << 1) & 0xff;
1523 uint8_t zeros
[8] = {0x00};
1524 uint8_t newpwd
[8] = {0x00};
1525 uint8_t dmkey
[24] = {0x00};
1526 memcpy(dmkey
, dkeyA
, 8);
1527 memcpy(dmkey
+8, dkeyB
, 8);
1528 memcpy(dmkey
+16, dkeyA
, 8);
1529 memset(iv
, 0x00, 8);
1531 des3_set3key_enc(&ctx
, dmkey
);
1533 des3_crypt_cbc(&ctx
// des3_context
1534 , DES_ENCRYPT
// int mode
1535 , sizeof(newpwd
) // length
1541 PrintAndLog("Mifare dkeyA :\t %s", sprint_hex(dkeyA
, sizeof(dkeyA
)));
1542 PrintAndLog("Mifare dkeyB :\t %s", sprint_hex(dkeyB
, sizeof(dkeyB
)));
1543 PrintAndLog("Mifare ABA :\t %s", sprint_hex(dmkey
, sizeof(dmkey
)));
1544 PrintAndLog("Mifare Pwd :\t %s", sprint_hex(newpwd
, sizeof(newpwd
)));
1549 // static uint8_t * diversify_key(uint8_t * key){
1551 // for(int i=0; i<16; i++){
1552 // if(i<=6) key[i]^=cuid[i];
1553 // if(i>6) key[i]^=cuid[i%7];
1558 // static void GenerateUIDe( uint8_t *uid, uint8_t len){
1559 // for (int i=0; i<len; ++i){
1565 //------------------------------------
1567 //------------------------------------
1568 static command_t CommandTable
[] =
1570 {"help", CmdHelp
, 1, "This help"},
1571 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1572 {"info", CmdHF14AMfUInfo
, 0, "Tag information"},
1573 {"dump", CmdHF14AMfUDump
, 0, "Dump Ultralight / Ultralight-C tag to binary file"},
1574 {"rdbl", CmdHF14AMfURdBl
, 0, "Read block - Ultralight"},
1575 {"wrbl", CmdHF14AMfUWrBl
, 0, "Write block - Ultralight"},
1576 {"crdbl", CmdHF14AMfUCRdBl
, 0, "Read block - Ultralight C"},
1577 {"cwrbl", CmdHF14AMfUCWrBl
, 0, "Write block - Ultralight C"},
1578 {"cauth", CmdHF14AMfucAuth
, 0, "Authentication - Ultralight C"},
1579 {"setpwd", CmdHF14AMfucSetPwd
, 1, "Set 3des password - Ultralight-C"},
1580 {"setuid", CmdHF14AMfucSetUid
, 1, "Set UID - MAGIC tags only"},
1581 {"gen", CmdHF14AMfuGenDiverseKeys
, 1, "Generate 3des mifare diversified keys"},
1582 {NULL
, NULL
, 0, NULL
}
1585 int CmdHFMFUltra(const char *Cmd
){
1586 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1587 CmdsParse(CommandTable
, Cmd
);
1591 int CmdHelp(const char *Cmd
){
1592 CmdsHelp(CommandTable
);