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
{
39 MY_D_MOVE_NFC
= 0x0800,
41 UL_MAGIC
= UL
| MAGIC
,
42 UL_C_MAGIC
= UL_C
| MAGIC
,
46 #define KEYS_3DES_COUNT 7
47 uint8_t default_3des_keys
[KEYS_3DES_COUNT
][16] = {
48 { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 },// 3des std key
49 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },// all zeroes
50 { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f },// 0x00-0x0F
51 { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 },// NFC-key
52 { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },// all ones
53 { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF },// all FF
54 { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF } // 11 22 33
57 #define KEYS_PWD_COUNT 8
58 uint8_t default_pwd_pack
[KEYS_PWD_COUNT
][4] = {
59 {0xFF,0xFF,0xFF,0xFF}, // PACK 0x00,0x00 -- factory default
60 {0x4A,0xF8,0x4B,0x19}, // PACK 0xE5,0xBE -- italian bus (sniffed)
61 {0x33,0x6B,0xA1,0x19}, // PACK 0x9c,0x2d -- italian bus (sniffed)
62 {0xFF,0x90,0x6C,0xB2}, // PACK 0x12,0x9e -- italian bus (sniffed)
63 {0x05,0x22,0xE6,0xB4}, // PACK 0x80,0x80 -- Amiiboo (sniffed) pikachu-b UID:
64 {0x7E,0x22,0xE6,0xB4}, // PACK 0x80,0x80 -- AMiiboo (sniffed)
65 {0x02,0xE1,0xEE,0x36}, // PACK 0x80,0x80 -- AMiiboo (sniffed) sonic UID: 04d257 7ae33e8027
66 {0x32,0x0C,0x16,0x17}, // PACK 0x80,0x80 -- AMiiboo (sniffed)
69 static int CmdHelp(const char *Cmd
);
71 char* getProductTypeStr( uint8_t id
){
78 sprintf(retStr
, "0x%02X %s", id
, "(Ultralight)");
81 sprintf(retStr
, "0x%02X %s", id
, "(NTAG)");
84 sprintf(retStr
, "0x%02X %s", id
, "(unknown)");
91 The 7 MSBits (=n) code the storage size itself based on 2^n,
92 the LSBit is set to '0' if the size is exactly 2^n
93 and set to '1' if the storage size is between 2^n and 2^(n+1).
95 char* getUlev1CardSizeStr( uint8_t fsize
){
100 uint8_t usize
= 1 << ((fsize
>>1) + 1);
101 uint8_t lsize
= 1 << (fsize
>>1);
105 sprintf(retStr
, "%02X (%u <-> %u bytes)",fsize
, usize
, lsize
);
107 sprintf(retStr
, "%02X (%u bytes)", fsize
, lsize
);
111 static void ul_switch_on_field(void) {
112 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_NO_DISCONNECT
, 0, 0}};
116 static void ul_switch_off_field(void) {
117 UsbCommand c
= {CMD_READER_ISO_14443a
, {0, 0, 0}};
121 static int ul_send_cmd_raw( uint8_t *cmd
, uint8_t cmdlen
, uint8_t *response
, uint16_t responseLength
) {
122 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_RAW
| ISO14A_NO_DISCONNECT
| ISO14A_APPEND_CRC
, cmdlen
, 0}};
123 memcpy(c
.d
.asBytes
, cmd
, cmdlen
);
126 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
127 if (!resp
.arg
[0] && responseLength
) return -1;
129 uint16_t resplen
= (resp
.arg
[0] < responseLength
) ? resp
.arg
[0] : responseLength
;
130 memcpy(response
, resp
.d
.asBytes
, resplen
);
134 static int ul_send_cmd_raw_crc( uint8_t *cmd, uint8_t cmdlen, uint8_t *response, uint16_t responseLength, bool append_crc ) {
135 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT , cmdlen, 0}};
137 c.arg[0] |= ISO14A_APPEND_CRC;
139 memcpy(c.d.asBytes, cmd, cmdlen);
142 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
143 if (!resp.arg[0] && responseLength) return -1;
145 uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength;
146 memcpy(response, resp.d.asBytes, resplen);
150 static int ul_select( iso14a_card_select_t
*card
){
152 ul_switch_on_field();
155 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
156 if (resp
.arg
[0] < 1) return -1;
158 memcpy(card
, resp
.d
.asBytes
, sizeof(iso14a_card_select_t
));
162 // This read command will at least return 16bytes.
163 static int ul_read( uint8_t page
, uint8_t *response
, uint16_t responseLength
){
165 uint8_t cmd
[] = {ISO14443A_CMD_READBLOCK
, page
};
166 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
168 ul_switch_off_field();
172 static int ul_comp_write( uint8_t page
, uint8_t *data
, uint8_t datalen
){
175 memset(cmd
, 0x00, sizeof(cmd
));
176 datalen
= ( datalen
> 16) ? 16 : datalen
;
178 cmd
[0] = ISO14443A_CMD_WRITEBLOCK
;
180 memcpy(cmd
+2, data
, datalen
);
182 uint8_t response
[1] = {0xff};
183 int len
= ul_send_cmd_raw(cmd
, 2+datalen
, response
, sizeof(response
));
185 ul_switch_off_field();
187 if ( response
[0] == 0x0a ) return 0;
192 static int ulc_requestAuthentication( uint8_t blockNo
, uint8_t *nonce
, uint16_t nonceLength
){
194 uint8_t cmd
[] = {MIFARE_ULC_AUTH_1
, blockNo
};
195 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), nonce
, nonceLength
);
197 ul_switch_off_field();
201 static int ulev1_requestAuthentication( uint8_t *pwd
, uint8_t *pack
, uint16_t packLength
){
203 uint8_t cmd
[] = {MIFARE_ULEV1_AUTH
, pwd
[0], pwd
[1], pwd
[2], pwd
[3]};
204 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), pack
, packLength
);
206 ul_switch_off_field();
210 static int ulev1_getVersion( uint8_t *response
, uint16_t responseLength
){
212 uint8_t cmd
[] = {MIFARE_ULEV1_VERSION
};
213 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
215 ul_switch_off_field();
219 // static int ulev1_fastRead( uint8_t startblock, uint8_t endblock, uint8_t *response ){
221 // uint8_t cmd[] = {MIFARE_ULEV1_FASTREAD, startblock, endblock};
223 // if ( !ul_send_cmd_raw(cmd, sizeof(cmd), response)){
224 // ul_switch_off_field();
230 static int ulev1_readCounter( uint8_t counter
, uint8_t *response
, uint16_t responseLength
){
232 uint8_t cmd
[] = {MIFARE_ULEV1_READ_CNT
, counter
};
233 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
235 ul_switch_off_field();
239 static int ulev1_readSignature( uint8_t *response
, uint16_t responseLength
){
241 uint8_t cmd
[] = {MIFARE_ULEV1_READSIG
, 0x00};
242 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
244 ul_switch_off_field();
248 static int ul_print_default( uint8_t *data
){
260 PrintAndLog(" UID : %s ", sprint_hex(uid
, 7));
261 PrintAndLog(" UID[0] : %02X, Manufacturer: %s", uid
[0], getTagInfo(uid
[0]) );
262 if ( uid
[0] == 0x05 ) {
263 uint8_t chip
= (data
[8] & 0xC7); // 11000111 mask, bit 3,4,5 RFU
265 case 0xc2: PrintAndLog(" IC type : SLE 66R04P"); break;
266 case 0xc4: PrintAndLog(" IC type : SLE 66R16P"); break;
267 case 0xc6: PrintAndLog(" IC type : SLE 66R32P"); break;
270 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
271 int crc0
= 0x88 ^ data
[0] ^ data
[1] ^data
[2];
272 if ( data
[3] == crc0
)
273 PrintAndLog(" BCC0 : %02X - Ok", data
[3]);
275 PrintAndLog(" BCC0 : %02X - crc should be %02X", data
[3], crc0
);
277 int crc1
= data
[4] ^ data
[5] ^ data
[6] ^data
[7];
278 if ( data
[8] == crc1
)
279 PrintAndLog(" BCC1 : %02X - Ok", data
[8]);
281 PrintAndLog(" BCC1 : %02X - crc should be %02X", data
[8], crc1
);
283 PrintAndLog(" Internal : %02X - %s default", data
[9], (data
[9]==0x48)?"":"not" );
284 PrintAndLog(" Lock : %s - %s", sprint_hex(data
+10, 2),printBits( 2, data
+10) );
285 PrintAndLog("OneTimePad : %s ", sprint_hex(data
+ 12, 4));
290 static int ntag_print_CC(uint8_t *data
) {
292 PrintAndLog("\n--- NTAG NDEF Message");
294 if(data
[0] != 0xe1) {
295 PrintAndLog("no NDEF message");
296 return -1; // no NDEF message
299 PrintAndLog("Capability Container: %s", sprint_hex(data
,4) );
300 PrintAndLog(" %02X: NDEF Magic Number", data
[0]);
301 PrintAndLog(" %02X: version %d.%d supported by tag", data
[1], (data
[1] & 0xF0) >> 4, data
[1] & 0x0f);
302 PrintAndLog(" %02X: Physical Memory Size: %d bytes", data
[2], (data
[2] + 1) * 8);
303 if ( data
[2] == 0x12 )
304 PrintAndLog(" %02X: NDEF Memory Size: %d bytes", data
[2], 144);
305 else if ( data
[2] == 0x3e )
306 PrintAndLog(" %02X: NDEF Memory Size: %d bytes", data
[2], 496);
307 else if ( data
[2] == 0x6d )
308 PrintAndLog(" %02X: NDEF Memory Size: %d bytes", data
[2], 872);
310 PrintAndLog(" %02X: %s / %s", data
[3],
311 (data
[3] & 0xF0) ? "(RFU)" : "Read access granted without any security",
312 (data
[3] & 0x0F)==0 ? "Write access granted without any security" : (data
[3] & 0x0F)==0x0F ? "No write access granted at all" : "(RFU)");
316 static int ul_print_type(uint16_t tagtype
){
318 PrintAndLog(" TYPE : MIFARE Ultralight (MF0ICU1) %s [%x]", (tagtype
& MAGIC
)?"<magic>":"", tagtype
);
319 else if ( tagtype
& UL_C
)
320 PrintAndLog(" TYPE : MIFARE Ultralight C (MF0ULC) %s [%x]", (tagtype
& MAGIC
)?"<magic>":"", tagtype
);
321 else if ( tagtype
& UL_EV1_48
)
322 PrintAndLog(" TYPE : MIFARE Ultralight EV1 48bytes (MF0UL1101)");
323 else if ( tagtype
& UL_EV1_128
)
324 PrintAndLog(" TYPE : MIFARE Ultralight EV1 128bytes (MF0UL2101)");
325 else if ( tagtype
& NTAG_213
)
326 PrintAndLog(" TYPE : MIFARE NTAG 213 144bytes (NT2H1311G0DU)");
327 else if ( tagtype
& NTAG_215
)
328 PrintAndLog(" TYPE : MIFARE NTAG 215 504bytes (NT2H1511G0DU)");
329 else if ( tagtype
& NTAG_216
)
330 PrintAndLog(" TYPE : MIFARE NTAG 216 888bytes (NT2H1611G0DU)");
331 else if ( tagtype
& MY_D
)
332 PrintAndLog(" TYPE : INFINEON my-d\x99");
333 else if ( tagtype
& MY_D_NFC
)
334 PrintAndLog(" TYPE : INFINEON my-d\x99 NFC");
335 else if ( tagtype
& MY_D_MOVE
)
336 PrintAndLog(" TYPE : INFINEON my-d\x99 move");
337 else if ( tagtype
& MY_D_MOVE_NFC
)
338 PrintAndLog(" TYPE : INFINEON my-d\x99 move NFC");
340 PrintAndLog(" TYPE : Unknown %04x",tagtype
);
344 static int ulc_print_3deskey( uint8_t *data
){
345 PrintAndLog(" deskey1 [44/0x2C]: %s [%.4s]", sprint_hex(data
,4),data
);
346 PrintAndLog(" deskey1 [45/0x2D]: %s [%.4s]", sprint_hex(data
+4 ,4),data
+4);
347 PrintAndLog(" deskey2 [46/0x2E]: %s [%.4s]", sprint_hex(data
+8 ,4),data
+8);
348 PrintAndLog(" deskey2 [47/0x2F]: %s [%.4s]", sprint_hex(data
+12,4),data
+12);
349 PrintAndLog(" 3des key : %s", sprint_hex(SwapEndian64(data
, 16), 16));
353 static int ulc_print_configuration( uint8_t *data
){
355 PrintAndLog("--- UL-C Configuration");
356 PrintAndLog(" Higher Lockbits [40/0x28]: %s - %s", sprint_hex(data
, 4), printBits(2, data
));
357 PrintAndLog(" Counter [41/0x29]: %s - %s", sprint_hex(data
+4, 4), printBits(2, data
+4));
359 bool validAuth
= (data
[8] >= 0x03 && data
[8] <= 0x30);
361 PrintAndLog(" Auth0 [42/0x2A]: %s Pages above %d needs authentication", sprint_hex(data
+8, 4), data
[8] );
364 PrintAndLog(" Auth0 [42/0x2A]: %s default", sprint_hex(data
+8, 4) );
366 PrintAndLog(" Auth0 [42/0x2A]: %s auth byte is out-of-range", sprint_hex(data
+8, 4) );
369 PrintAndLog(" Auth1 [43/0x2B]: %s %s",
370 sprint_hex(data
+12, 4),
371 (data
[12] & 1) ? "write access restricted": "read and write access restricted"
376 static int ulev1_print_configuration( uint8_t *data
){
378 PrintAndLog("\n--- UL-EV1 Configuration");
380 bool strg_mod_en
= (data
[0] & 2);
381 uint8_t authlim
= (data
[4] & 0x07);
382 bool cfglck
= (data
[4] & 0x40);
383 bool prot
= (data
[4] & 0x80);
384 uint8_t vctid
= data
[5];
386 PrintAndLog(" cfg0 [16/0x10]: %s", sprint_hex(data
, 4));
387 if ( data
[3] < 0xff )
388 PrintAndLog(" - pages above %d needs authentication",data
[3]);
390 PrintAndLog(" - pages don't need authentication");
391 PrintAndLog(" - strong modulation mode %s", (strg_mod_en
) ? "enabled":"disabled");
392 PrintAndLog(" cfg1 [17/0x11]: %s", sprint_hex(data
+4, 4) );
394 PrintAndLog(" - Unlimited password attempts");
396 PrintAndLog(" - Max number of password attempts is %d", authlim
);
397 PrintAndLog(" - user configuration %s", cfglck
? "permanently locked":"writeable");
398 PrintAndLog(" - %s access is protected with password", prot
? "read and write":"write");
399 PrintAndLog(" %02X - Virtual Card Type Identifier is %s default", vctid
, (vctid
==0x05)? "":"not");
400 PrintAndLog(" PWD [18/0x12]: %s", sprint_hex(data
+8, 4));
401 PrintAndLog(" PACK [19/0x13]: %s", sprint_hex(data
+12, 4));
405 static int ulev1_print_counters(){
406 PrintAndLog("--- UL-EV1 Counters");
407 uint8_t counter
[3] = {0,0,0};
408 for ( uint8_t i
= 0; i
<3; ++i
) {
409 ulev1_readCounter(i
,counter
, sizeof(counter
) );
410 PrintAndLog(" [%0d] : %s", i
, sprint_hex(counter
,3));
415 static int ulev1_print_signature( uint8_t *data
, uint8_t len
){
416 PrintAndLog("\n--- UL-EV1 Signature");
417 PrintAndLog("IC signature public key name : NXP NTAG21x 2013");
418 PrintAndLog("IC signature public key value : 04494e1a386d3d3cfe3dc10e5de68a499b1c202db5b132393e89ed19fe5be8bc61");
419 PrintAndLog(" Elliptic curve parameters : secp128r1");
420 PrintAndLog(" Tag ECC Signature : %s", sprint_hex(data
, len
));
421 //to do: verify if signature is valid
422 //PrintAndLog("IC signature status: %s valid", (iseccvalid() )?"":"not");
426 static int ulev1_print_version(uint8_t *data
){
427 PrintAndLog("\n--- UL-EV1 / NTAG Version");
428 PrintAndLog(" Raw bytes : %s", sprint_hex(data
, 8) );
429 PrintAndLog(" Vendor ID : %02X, Manufacturer: %s", data
[1], getTagInfo(data
[1]));
430 PrintAndLog(" Product type : %s", getProductTypeStr(data
[2]));
431 PrintAndLog(" Product subtype : %02X %s", data
[3], (data
[3]==1) ?"17 pF":"50pF");
432 PrintAndLog(" Major version : %02X", data
[4]);
433 PrintAndLog(" Minor version : %02X", data
[5]);
434 PrintAndLog(" Size : %s", getUlev1CardSizeStr(data
[6]));
435 PrintAndLog(" Protocol type : %02X", data
[7]);
440 static int ulc_magic_test(){
441 // Magic Ultralight test
442 // Magic UL-C, by observation,
443 // 1) it seems to have a static nonce response to 0x1A command.
444 // 2) the deskey bytes is not-zero:d out on as datasheet states.
445 // 3) UID - changeable, not only, but pages 0-1-2-3.
446 // 4) use the ul_magic_test ! magic tags answers specially!
447 int returnValue = UL_ERROR;
448 iso14a_card_select_t card;
449 uint8_t nonce1[11] = {0x00};
450 uint8_t nonce2[11] = {0x00};
451 int status = ul_select(&card);
453 PrintAndLog("Error: couldn't select ulc_magic_test");
454 ul_switch_off_field();
457 status = ulc_requestAuthentication(0, nonce1, sizeof(nonce1));
459 status = ulc_requestAuthentication(0, nonce2, sizeof(nonce2));
460 returnValue = ( !memcmp(nonce1, nonce2, 11) ) ? UL_C_MAGIC : UL_C;
464 ul_switch_off_field();
468 static int ul_magic_test(){
470 // Magic Ultralight tests
471 // 1) take present UID, and try to write it back. OBSOLETE
472 // 2) make a wrong length write to page0, and see if tag answers with ACK/NACK:
473 iso14a_card_select_t card
;
474 int status
= ul_select(&card
);
476 PrintAndLog("Error: couldn't select ul_magic_test");
477 ul_switch_off_field();
480 status
= ul_comp_write(0, NULL
, 0);
481 ul_switch_off_field();
487 uint16_t GetHF14AMfU_Type(void){
489 TagTypeUL_t tagtype
= UNKNOWN
;
490 iso14a_card_select_t card
;
491 uint8_t version
[10] = {0x00};
495 status
= ul_select(&card
);
497 PrintAndLog("Error: couldn't select");
498 ul_switch_off_field();
501 // Ultralight - ATQA / SAK
502 if ( card
.atqa
[1] != 0x00 || card
.atqa
[0] != 0x44 || card
.sak
!= 0x00 ) {
503 PrintAndLog("Tag is not Ultralight | NTAG | MY-D [ATQA: %02X %02X SAK: %02X]\n", card
.atqa
[1], card
.atqa
[0], card
.sak
);
504 ul_switch_off_field();
508 if ( card
.uid
[0] != 0x05) {
510 len
= ulev1_getVersion(version
, sizeof(version
));
511 if (len
> -1) ul_switch_off_field(); //if -1 it is already off
516 if ( version
[2] == 0x03 && version
[6] == 0x0B )
518 else if ( version
[2] == 0x03 && version
[6] != 0x0B )
519 tagtype
= UL_EV1_128
;
520 else if ( version
[2] == 0x04 && version
[6] == 0x0F )
522 else if ( version
[2] == 0x04 && version
[6] == 0x11 )
524 else if ( version
[2] == 0x04 && version
[6] == 0x13 )
526 else if ( version
[2] == 0x04 )
531 case 0x01: tagtype
= UL_C
; break;
532 case 0x00: tagtype
= UL
; break;
533 case -1 : tagtype
= (UL
| UL_C
); break; //when does this happen?
534 default : tagtype
= UNKNOWN
; break;
536 if (tagtype
== (UL
| UL_C
)) {
537 status
= ul_select(&card
);
539 PrintAndLog("Error: couldn't select 2");
540 ul_switch_off_field();
543 uint8_t nonce1
[11] = {0x00};
544 status
= ulc_requestAuthentication(0, nonce1
, sizeof(nonce1
));
550 if (status
!= -1) ul_switch_off_field();
553 // Infinition MY-D tests Exam high nibble
554 uint8_t nib
= (card
.uid
[1] & 0xf0) >> 4;
556 case 1: tagtype
= MY_D
; break;
557 case 2: tagtype
= (MY_D
| MY_D_NFC
); break;
558 case 3: tagtype
= (MY_D_MOVE
| MY_D_MOVE_NFC
); break;
562 tagtype
= (ul_magic_test() == UL_MAGIC
) ? (tagtype
| MAGIC
) : tagtype
;
563 //if ((tagtype & UL)) tagtype = ul_magic_test();
568 int CmdHF14AMfUInfo(const char *Cmd
){
570 uint8_t authlim
= 0xff;
571 uint8_t data
[16] = {0x00};
572 iso14a_card_select_t card
;
576 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
577 if (tagtype
== UL_ERROR
) return -1;
579 PrintAndLog("\n--- Tag Information ---------");
580 PrintAndLog("-------------------------------------------------------------");
581 ul_print_type(tagtype
);
583 status
= ul_select(&card
);
585 PrintAndLog("Error: couldn't select");
586 ul_switch_off_field();
590 // read pages 0,1,2,4 (should read 4pages)
591 status
= ul_read(0, data
, sizeof(data
));
593 PrintAndLog("Error: tag didn't answer to READ");
597 ul_print_default(data
);
599 if ((tagtype
& UL_C
)){
601 // read pages 0x28, 0x29, 0x2A, 0x2B
602 uint8_t ulc_conf
[16] = {0x00};
603 status
= ul_read(0x28, ulc_conf
, sizeof(ulc_conf
));
605 PrintAndLog("Error: tag didn't answer to READ - possibly locked");
608 ulc_print_configuration(ulc_conf
);
610 if ((tagtype
& MAGIC
)){
612 uint8_t ulc_deskey
[16] = {0x00};
613 status
= ul_read(0x2C, ulc_deskey
, sizeof(ulc_deskey
));
615 PrintAndLog("Error: tag didn't answer to READ magic");
618 ulc_print_3deskey(ulc_deskey
);
621 PrintAndLog("Trying some default 3des keys");
622 ul_switch_off_field();
623 for (uint8_t i
= 0; i
< 7; ++i
){
624 key
= default_3des_keys
[i
];
625 if (try3DesAuthentication(key
) == 1){
626 PrintAndLog("Found default 3des key: "); //%s", sprint_hex(key,16));
627 ulc_print_3deskey(SwapEndian64(key
,16));
634 if ((tagtype
& (UL_EV1_48
| UL_EV1_128
))) {
635 //do counters and signature first (don't neet auth)
636 ulev1_print_counters();
638 uint8_t ulev1_signature
[32] = {0x00};
639 status
= ulev1_readSignature( ulev1_signature
, sizeof(ulev1_signature
));
641 PrintAndLog("Error: tag didn't answer to READ SIGNATURE");
644 ulev1_print_signature( ulev1_signature
, sizeof(ulev1_signature
));
646 uint8_t startconfigblock
= (tagtype
& UL_EV1_48
) ? 0x10 : 0x25;
647 uint8_t ulev1_conf
[16] = {0x00};
648 status
= ul_read(startconfigblock
, ulev1_conf
, sizeof(ulev1_conf
));
650 PrintAndLog("Error: tag didn't answer to READ EV1");
653 // save AUTHENTICATION LIMITS for later:
654 authlim
= (ulev1_conf
[4] & 0x07);
655 ulev1_print_configuration(ulev1_conf
);
658 if ((tagtype
& (UL_EV1_48
| UL_EV1_128
| NTAG_213
| NTAG_215
| NTAG_216
))) {
660 uint8_t version
[10] = {0x00};
661 status
= ulev1_getVersion(version
, sizeof(version
));
663 PrintAndLog("Error: tag didn't answer to GETVERSION");
666 ulev1_print_version(version
);
668 // AUTHLIMIT, (number of failed authentications)
670 // 1-7 = ... should we even try then?
672 PrintAndLog("\n--- Known EV1/NTAG passwords.");
674 uint8_t pack
[4] = {0,0,0,0};
675 int len
=0; //if len goes to -1 the connection will be turned off.
676 for (uint8_t i
= 0; i
< 3; ++i
){
677 key
= default_pwd_pack
[i
];
679 len
= ulev1_requestAuthentication(key
, pack
, sizeof(pack
));
680 PrintAndLog("Found a default password: %s || Pack: %02X %02X",sprint_hex(key
, 4), pack
[0], pack
[1]);
684 if (len
> -1) ul_switch_off_field();
688 if ((tagtype
& (NTAG_213
| NTAG_215
| NTAG_216
))){
690 uint8_t cc
[16] = {0x00};
691 status
= ul_read(3, cc
, sizeof(cc
));
693 PrintAndLog("Error: tag didn't answer to READ ntag");
699 ul_switch_off_field();
705 // Mifare Ultralight Write Single Block
707 int CmdHF14AMfUWrBl(const char *Cmd
){
708 uint8_t blockNo
= -1;
709 bool chinese_card
= FALSE
;
710 uint8_t bldata
[16] = {0x00};
713 char cmdp
= param_getchar(Cmd
, 0);
714 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
715 PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]");
716 PrintAndLog(" [block number]");
717 PrintAndLog(" [block data] - (8 hex symbols)");
718 PrintAndLog(" [w] - Chinese magic ultralight tag");
720 PrintAndLog(" sample: hf mfu wrbl 0 01020304");
725 blockNo
= param_get8(Cmd
, 0);
727 if (blockNo
> MAX_UL_BLOCKS
){
728 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
732 if (param_gethex(Cmd
, 1, bldata
, 8)) {
733 PrintAndLog("Block data must include 8 HEX symbols");
737 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
743 PrintAndLog("Access Denied");
745 PrintAndLog("--specialblock no:%02x", blockNo
);
746 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
747 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
748 memcpy(d
.d
.asBytes
,bldata
, 4);
750 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
751 uint8_t isOK
= resp
.arg
[0] & 0xff;
752 PrintAndLog("isOk:%02x", isOK
);
754 PrintAndLog("Command execute timeout");
758 PrintAndLog("--block no:%02x", blockNo
);
759 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
760 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
761 memcpy(e
.d
.asBytes
,bldata
, 4);
763 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
764 uint8_t isOK
= resp
.arg
[0] & 0xff;
765 PrintAndLog("isOk:%02x", isOK
);
767 PrintAndLog("Command execute timeout");
774 // Mifare Ultralight Read Single Block
776 int CmdHF14AMfURdBl(const char *Cmd
){
779 uint8_t blockNo
= -1;
780 char cmdp
= param_getchar(Cmd
, 0);
782 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
783 PrintAndLog("Usage: hf mfu rdbl <block number>");
784 PrintAndLog(" sample: hfu mfu rdbl 0");
788 blockNo
= param_get8(Cmd
, 0);
790 if (blockNo
> MAX_UL_BLOCKS
){
791 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight");
795 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
799 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
800 uint8_t isOK
= resp
.arg
[0] & 0xff;
802 uint8_t *data
= resp
.d
.asBytes
;
803 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
806 PrintAndLog("Failed reading block: (%02x)", isOK
);
809 PrintAndLog("Command execute time-out");
815 int usage_hf_mfu_dump(void)
817 PrintAndLog("Reads all pages from Ultralight, Ultralight-C, Ultralight EV1");
818 PrintAndLog("and saves binary dump into the file `filename.bin` or `cardUID.bin`");
819 PrintAndLog("It autodetects card type.\n");
820 PrintAndLog("Usage: hf mfu dump k <key> n <filename w/o .bin>");
821 PrintAndLog(" Options : ");
822 PrintAndLog(" k <key> : Enter key for authentication");
823 PrintAndLog(" n <FN > : Enter filename w/o .bin to save the dump as");
824 PrintAndLog(" s : Swap entered key's endianness for auth");
826 PrintAndLog(" sample : hf mfu dump");
827 PrintAndLog(" : hf mfu dump n myfile");
831 // Mifare Ultralight / Ultralight-C / Ultralight-EV1
832 // Read and Dump Card Contents, using auto detection of tag size.
834 // TODO: take a password to read UL-C / UL-EV1 tags.
835 int CmdHF14AMfUDump(const char *Cmd
){
838 char filename
[FILE_PATH_SIZE
] = {0x00};
839 char *fnameptr
= filename
;
840 char *str
= "Dumping Ultralight%s%s Card Data...";
841 uint8_t *lockbytes_t
= NULL
;
842 uint8_t lockbytes
[2] = {0x00};
843 uint8_t *lockbytes_t2
= NULL
;
844 uint8_t lockbytes2
[2] = {0x00};
845 bool bit
[16] = {0x00};
846 bool bit2
[16] = {0x00};
847 uint8_t data
[1024] = {0x00};
851 bool tmplockbit
= false;
854 uint8_t key
[16] = {0x00};
855 uint8_t *keyPtr
= key
;
858 bool swapEndian
= false;
860 while(param_getchar(Cmd
, cmdp
) != 0x00)
862 switch(param_getchar(Cmd
, cmdp
))
866 return usage_hf_mfu_dump();
869 dataLen
= param_gethex(Cmd
, cmdp
+1, data
, 32);
873 memcpy(key
, data
, 16);
880 fileNlen
= param_getstr(Cmd
, cmdp
+1, filename
);
881 if (!fileNlen
) errors
= true;
882 if (fileNlen
> FILE_PATH_SIZE
-5) fileNlen
= FILE_PATH_SIZE
-5;
889 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
897 if(errors
) return usage_hf_mfu_dump();
900 keyPtr
= SwapEndian64(data
, 16);
902 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
903 if (tagtype
== UL_ERROR
) return -1;
905 if ( tagtype
& UL
) {
907 PrintAndLog(str
,"", (tagtype
& MAGIC
)?" (magic)":"" );
909 else if ( tagtype
& UL_C
) {
911 PrintAndLog(str
,"-C", (tagtype
& MAGIC
)?" (magic)":"" );
913 else if ( tagtype
& UL_EV1_48
) {
915 PrintAndLog(str
," EV1_48","");
917 else if ( tagtype
& UL_EV1_128
) {
919 PrintAndLog(str
," EV1_128","");
922 PrintAndLog("Dumping unknown Ultralight, using default values.");
925 UsbCommand c
= {CMD_MIFAREUC_READCARD
, {0,Pages
}};
928 memcpy(c
.d
.asBytes
, key
, 16);
932 if (!WaitForResponseTimeout(CMD_ACK
, &resp
,1500)) {
933 PrintAndLog("Command execute time-out");
936 PrintAndLog ("%u,%u",resp
.arg
[0],resp
.arg
[1]);
937 uint8_t isOK
= resp
.arg
[0] & 0xff;
939 memcpy(data
, resp
.d
.asBytes
, resp
.arg
[1]);
941 PrintAndLog("Failed reading block: (%02x)", i
);
948 lockbytes_t
= data
+ 8;
949 lockbytes
[0] = lockbytes_t
[2];
950 lockbytes
[1] = lockbytes_t
[3];
951 for(j
= 0; j
< 16; j
++){
952 bit
[j
] = lockbytes
[j
/8] & ( 1 <<(7-j
%8));
955 // Load bottom lockbytes if available
957 lockbytes_t2
= data
+ (40*4);
958 lockbytes2
[0] = lockbytes_t2
[2];
959 lockbytes2
[1] = lockbytes_t2
[3];
960 for (j
= 0; j
< 16; j
++) {
961 bit2
[j
] = lockbytes2
[j
/8] & ( 1 <<(7-j
%8));
967 memcpy(data
+ Pages
*4, key
, 16);
970 for (i
= 0; i
< Pages
; ++i
) {
972 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
976 case 3: tmplockbit
= bit
[4]; break;
977 case 4: tmplockbit
= bit
[3]; break;
978 case 5: tmplockbit
= bit
[2]; break;
979 case 6: tmplockbit
= bit
[1]; break;
980 case 7: tmplockbit
= bit
[0]; break;
981 case 8: tmplockbit
= bit
[15]; break;
982 case 9: tmplockbit
= bit
[14]; break;
983 case 10: tmplockbit
= bit
[13]; break;
984 case 11: tmplockbit
= bit
[12]; break;
985 case 12: tmplockbit
= bit
[11]; break;
986 case 13: tmplockbit
= bit
[10]; break;
987 case 14: tmplockbit
= bit
[9]; break;
988 case 15: tmplockbit
= bit
[8]; break;
992 case 19: tmplockbit
= bit2
[6]; break;
996 case 23: tmplockbit
= bit2
[5]; break;
1000 case 27: tmplockbit
= bit2
[4]; break;
1004 case 31: tmplockbit
= bit2
[2]; break;
1008 case 35: tmplockbit
= bit2
[1]; break;
1012 case 39: tmplockbit
= bit2
[0]; break;
1013 case 40: tmplockbit
= bit2
[12]; break;
1014 case 41: tmplockbit
= bit2
[11]; break;
1015 case 42: tmplockbit
= bit2
[10]; break; //auth0
1016 case 43: tmplockbit
= bit2
[9]; break; //auth1
1019 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),tmplockbit
);
1022 // user supplied filename?
1024 // UID = data 0-1-2 4-5-6-7 (skips a beat)
1025 sprintf(fnameptr
,"%02X%02X%02X%02X%02X%02X%02X.bin",
1026 data
[0],data
[1], data
[2], data
[4],data
[5],data
[6], data
[7]);
1028 sprintf(fnameptr
+ fileNlen
," .bin");
1031 if ((fout
= fopen(filename
,"wb")) == NULL
) {
1032 PrintAndLog("Could not create file name %s", filename
);
1035 fwrite( data
, 1, Pages
*4, fout
);
1038 PrintAndLog("Dumped %d pages, wrote %d bytes to %s", Pages
, Pages
*4, filename
);
1042 // Needed to Authenticate to Ultralight C tags
1043 void rol (uint8_t *data
, const size_t len
){
1044 uint8_t first
= data
[0];
1045 for (size_t i
= 0; i
< len
-1; i
++) {
1046 data
[i
] = data
[i
+1];
1048 data
[len
-1] = first
;
1051 //-------------------------------------------------------------------------------
1052 // Ultralight C Methods
1053 //-------------------------------------------------------------------------------
1056 // Ultralight C Authentication Demo {currently uses hard-coded key}
1058 int CmdHF14AMfucAuth(const char *Cmd
){
1061 bool errors
= false;
1063 char cmdp
= param_getchar(Cmd
, 0);
1065 //Change key to user defined one
1066 if (cmdp
== 'k' || cmdp
== 'K'){
1067 keyNo
= param_get8(Cmd
, 1);
1072 if (cmdp
== 'h' || cmdp
== 'H')
1076 PrintAndLog("Usage: hf mfu cauth k <key number>");
1077 PrintAndLog(" 0 (default): 3DES standard key");
1078 PrintAndLog(" 1 : all 0x00 key");
1079 PrintAndLog(" 2 : 0x00-0x0F key");
1080 PrintAndLog(" 3 : nfc key");
1081 PrintAndLog(" 4 : all 0x01 key");
1082 PrintAndLog(" 5 : all 0xff key");
1083 PrintAndLog(" 6 : 0x00-0xFF key");
1084 PrintAndLog("\n sample : hf mfu cauth k");
1085 PrintAndLog(" : hf mfu cauth k 3");
1089 uint8_t *key
= default_3des_keys
[keyNo
];
1090 if (try3DesAuthentication(key
)>0)
1091 PrintAndLog("Authentication successful. 3des key: %s",sprint_hex(key
, 16));
1093 PrintAndLog("Authentication failed");
1098 int try3DesAuthentication( uint8_t *key
){
1100 uint8_t blockNo
= 0;
1103 des3_context ctx
= { 0 };
1105 uint8_t random_a
[8] = { 1,1,1,1,1,1,1,1 };
1106 uint8_t random_b
[8] = { 0 };
1107 uint8_t enc_random_b
[8] = { 0 };
1108 uint8_t rnd_ab
[16] = { 0 };
1109 uint8_t iv
[8] = { 0 };
1111 UsbCommand c
= {CMD_MIFAREUC_AUTH1
, {blockNo
}};
1114 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) return -1;
1115 if ( !(resp
.arg
[0] & 0xff) ) return -2;
1118 memcpy(enc_random_b
,resp
.d
.asBytes
+1,8);
1120 des3_set2key_dec(&ctx
, key
);
1121 // context, mode, length, IV, input, output
1122 des3_crypt_cbc( &ctx
, DES_DECRYPT
, sizeof(random_b
), iv
, enc_random_b
, random_b
);
1125 memcpy(rnd_ab
,random_a
,8);
1126 memcpy(rnd_ab
+8,random_b
,8);
1128 des3_set2key_enc(&ctx
, key
);
1129 // context, mode, length, IV, input, output
1130 des3_crypt_cbc(&ctx
, DES_ENCRYPT
, sizeof(rnd_ab
), enc_random_b
, rnd_ab
, rnd_ab
);
1133 c
.cmd
= CMD_MIFAREUC_AUTH2
;
1135 memcpy(c
.d
.asBytes
, rnd_ab
, 16);
1138 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
1139 if ( !(resp
.arg
[0] & 0xff)) return -2;
1141 uint8_t enc_resp
[8] = { 0 };
1142 uint8_t resp_random_a
[8] = { 0 };
1143 memcpy(enc_resp
, resp
.d
.asBytes
+1, 8);
1145 des3_set2key_dec(&ctx
, key
);
1146 // context, mode, length, IV, input, output
1147 des3_crypt_cbc( &ctx
, DES_DECRYPT
, 8, enc_random_b
, enc_resp
, resp_random_a
);
1149 if ( !memcmp(resp_random_a
, random_a
, 8))
1153 //PrintAndLog(" RndA :%s", sprint_hex(random_a, 8));
1154 //PrintAndLog(" enc(RndB) :%s", sprint_hex(enc_random_b, 8));
1155 //PrintAndLog(" RndB :%s", sprint_hex(random_b, 8));
1156 //PrintAndLog(" A+B :%s", sprint_hex(random_a_and_b, 16));
1157 //PrintAndLog(" enc(A+B) :%s", sprint_hex(random_a_and_b, 16));
1158 //PrintAndLog(" enc(RndA') :%s", sprint_hex(data2+1, 8));
1162 A test function to validate that the polarssl-function works the same
1163 was as the openssl-implementation.
1164 Commented out, since it requires openssl
1166 int CmdTestDES(const char * cmd)
1168 uint8_t key[16] = {0x00};
1170 memcpy(key,key3_3des_data,16);
1171 DES_cblock RndA, RndB;
1173 PrintAndLog("----------OpenSSL DES implementation----------");
1175 uint8_t e_RndB[8] = {0x00};
1176 unsigned char RndARndB[16] = {0x00};
1178 DES_cblock iv = { 0 };
1179 DES_key_schedule ks1,ks2;
1180 DES_cblock key1,key2;
1182 memcpy(key,key3_3des_data,16);
1184 memcpy(key2,key+8,8);
1187 DES_set_key((DES_cblock *)key1,&ks1);
1188 DES_set_key((DES_cblock *)key2,&ks2);
1190 DES_random_key(&RndA);
1191 PrintAndLog(" RndA:%s",sprint_hex(RndA, 8));
1192 PrintAndLog(" e_RndB:%s",sprint_hex(e_RndB, 8));
1193 //void DES_ede2_cbc_encrypt(const unsigned char *input,
1194 // unsigned char *output, long length, DES_key_schedule *ks1,
1195 // DES_key_schedule *ks2, DES_cblock *ivec, int enc);
1196 DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0);
1198 PrintAndLog(" RndB:%s",sprint_hex(RndB, 8));
1200 memcpy(RndARndB,RndA,8);
1201 memcpy(RndARndB+8,RndB,8);
1202 PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16));
1203 DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1);
1204 PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16));
1207 PrintAndLog("----------PolarSSL implementation----------");
1209 uint8_t random_a[8] = { 0 };
1210 uint8_t enc_random_a[8] = { 0 };
1211 uint8_t random_b[8] = { 0 };
1212 uint8_t enc_random_b[8] = { 0 };
1213 uint8_t random_a_and_b[16] = { 0 };
1214 des3_context ctx = { 0 };
1216 memcpy(random_a, RndA,8);
1218 uint8_t output[8] = { 0 };
1219 uint8_t iv[8] = { 0 };
1221 PrintAndLog(" RndA :%s",sprint_hex(random_a, 8));
1222 PrintAndLog(" e_RndB:%s",sprint_hex(enc_random_b, 8));
1224 des3_set2key_dec(&ctx, key);
1226 des3_crypt_cbc(&ctx // des3_context *ctx
1227 , DES_DECRYPT // int mode
1228 , sizeof(random_b) // size_t length
1229 , iv // unsigned char iv[8]
1230 , enc_random_b // const unsigned char *input
1231 , random_b // unsigned char *output
1234 PrintAndLog(" RndB:%s",sprint_hex(random_b, 8));
1237 memcpy(random_a_and_b ,random_a,8);
1238 memcpy(random_a_and_b+8,random_b,8);
1240 PrintAndLog(" RA+B:%s",sprint_hex(random_a_and_b, 16));
1242 des3_set2key_enc(&ctx, key);
1244 des3_crypt_cbc(&ctx // des3_context *ctx
1245 , DES_ENCRYPT // int mode
1246 , sizeof(random_a_and_b) // size_t length
1247 , enc_random_b // unsigned char iv[8]
1248 , random_a_and_b // const unsigned char *input
1249 , random_a_and_b // unsigned char *output
1252 PrintAndLog("enc(RA+B):%s",sprint_hex(random_a_and_b, 16));
1259 // Ultralight C Read Single Block
1261 int CmdHF14AMfUCRdBl(const char *Cmd
)
1264 bool hasPwd
= FALSE
;
1265 uint8_t blockNo
= -1;
1267 char cmdp
= param_getchar(Cmd
, 0);
1269 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
1270 PrintAndLog("Usage: hf mfu crdbl <block number> <key>");
1272 PrintAndLog("sample: hf mfu crdbl 0");
1273 PrintAndLog(" hf mfu crdbl 0 00112233445566778899AABBCCDDEEFF");
1277 blockNo
= param_get8(Cmd
, 0);
1279 PrintAndLog("Wrong block number");
1283 if (blockNo
> MAX_ULC_BLOCKS
){
1284 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C");
1289 if ( strlen(Cmd
) > 3){
1290 if (param_gethex(Cmd
, 1, key
, 32)) {
1291 PrintAndLog("Key must include %d HEX symbols", 32);
1297 //uint8_t *key2 = SwapEndian64(key, 16);
1300 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
1303 memcpy(c
.d
.asBytes
,key
,16);
1307 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1308 uint8_t isOK
= resp
.arg
[0] & 0xff;
1310 uint8_t *data
= resp
.d
.asBytes
;
1311 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
1314 PrintAndLog("Failed reading block: (%02x)", isOK
);
1317 PrintAndLog("Command execute time-out");
1323 // Mifare Ultralight C Write Single Block
1325 int CmdHF14AMfUCWrBl(const char *Cmd
){
1327 uint8_t blockNo
= -1;
1328 bool chinese_card
= FALSE
;
1329 uint8_t bldata
[16] = {0x00};
1332 char cmdp
= param_getchar(Cmd
, 0);
1334 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
1335 PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]");
1336 PrintAndLog(" [block number]");
1337 PrintAndLog(" [block data] - (8 hex symbols)");
1338 PrintAndLog(" [w] - Chinese magic ultralight tag");
1340 PrintAndLog(" sample: hf mfu cwrbl 0 01020304");
1345 blockNo
= param_get8(Cmd
, 0);
1346 if (blockNo
> MAX_ULC_BLOCKS
){
1347 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!");
1351 if (param_gethex(Cmd
, 1, bldata
, 8)) {
1352 PrintAndLog("Block data must include 8 HEX symbols");
1356 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
1357 chinese_card
= TRUE
;
1360 if ( blockNo
<= 3 ) {
1362 PrintAndLog("Access Denied");
1365 PrintAndLog("--Special block no: 0x%02x", blockNo
);
1366 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
1367 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1368 memcpy(d
.d
.asBytes
,bldata
, 4);
1370 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1371 uint8_t isOK
= resp
.arg
[0] & 0xff;
1372 PrintAndLog("isOk:%02x", isOK
);
1374 PrintAndLog("Command execute timeout");
1379 PrintAndLog("--Block no : 0x%02x", blockNo
);
1380 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
1381 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1382 memcpy(e
.d
.asBytes
,bldata
, 4);
1384 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1385 uint8_t isOK
= resp
.arg
[0] & 0xff;
1386 PrintAndLog("isOk : %02x", isOK
);
1388 PrintAndLog("Command execute timeout");
1396 // Mifare Ultralight C - Set password
1398 int CmdHF14AMfucSetPwd(const char *Cmd
){
1400 uint8_t pwd
[16] = {0x00};
1402 char cmdp
= param_getchar(Cmd
, 0);
1404 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
1405 PrintAndLog("Usage: hf mfu setpwd <password (32 hex symbols)>");
1406 PrintAndLog(" [password] - (32 hex symbols)");
1408 PrintAndLog("sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f");
1413 if (param_gethex(Cmd
, 0, pwd
, 32)) {
1414 PrintAndLog("Password must include 32 HEX symbols");
1418 UsbCommand c
= {CMD_MIFAREUC_SETPWD
};
1419 memcpy( c
.d
.asBytes
, pwd
, 16);
1424 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1425 if ( (resp
.arg
[0] & 0xff) == 1)
1426 PrintAndLog("Ultralight-C new password: %s", sprint_hex(pwd
,16));
1428 PrintAndLog("Failed writing at block %d", resp
.arg
[1] & 0xff);
1433 PrintAndLog("command execution time out");
1441 // Magic UL / UL-C tags - Set UID
1443 int CmdHF14AMfucSetUid(const char *Cmd
){
1447 uint8_t uid
[7] = {0x00};
1448 char cmdp
= param_getchar(Cmd
, 0);
1450 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
1451 PrintAndLog("Usage: hf mfu setuid <uid (14 hex symbols)>");
1452 PrintAndLog(" [uid] - (14 hex symbols)");
1453 PrintAndLog("\nThis only works for Magic Ultralight tags.");
1455 PrintAndLog("sample: hf mfu setuid 11223344556677");
1460 if (param_gethex(Cmd
, 0, uid
, 14)) {
1461 PrintAndLog("UID must include 14 HEX symbols");
1466 c
.cmd
= CMD_MIFAREU_READBL
;
1469 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1470 PrintAndLog("Command execute timeout");
1475 uint8_t oldblock2
[4] = {0x00};
1476 memcpy(resp
.d
.asBytes
, oldblock2
, 4);
1479 c
.cmd
= CMD_MIFAREU_WRITEBL
;
1481 c
.d
.asBytes
[0] = uid
[0];
1482 c
.d
.asBytes
[1] = uid
[1];
1483 c
.d
.asBytes
[2] = uid
[2];
1484 c
.d
.asBytes
[3] = 0x88 ^ uid
[0] ^ uid
[1] ^ uid
[2];
1486 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1487 PrintAndLog("Command execute timeout");
1493 c
.d
.asBytes
[0] = uid
[3];
1494 c
.d
.asBytes
[1] = uid
[4];
1495 c
.d
.asBytes
[2] = uid
[5];
1496 c
.d
.asBytes
[3] = uid
[6];
1498 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1499 PrintAndLog("Command execute timeout");
1505 c
.d
.asBytes
[0] = uid
[3] ^ uid
[4] ^ uid
[5] ^ uid
[6];
1506 c
.d
.asBytes
[1] = oldblock2
[1];
1507 c
.d
.asBytes
[2] = oldblock2
[2];
1508 c
.d
.asBytes
[3] = oldblock2
[3];
1510 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1511 PrintAndLog("Command execute timeout");
1518 int CmdHF14AMfuGenDiverseKeys(const char *Cmd
){
1520 uint8_t iv
[8] = { 0x00 };
1521 uint8_t block
= 0x07;
1524 //04 57 b6 e2 05 3f 80 UID
1526 uint8_t uid
[] = { 0xF4,0xEA, 0x54, 0x8E };
1527 uint8_t mifarekeyA
[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 };
1528 uint8_t mifarekeyB
[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 };
1529 uint8_t dkeyA
[8] = { 0x00 };
1530 uint8_t dkeyB
[8] = { 0x00 };
1532 uint8_t masterkey
[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff };
1534 uint8_t mix
[8] = { 0x00 };
1535 uint8_t divkey
[8] = { 0x00 };
1537 memcpy(mix
, mifarekeyA
, 4);
1539 mix
[4] = mifarekeyA
[4] ^ uid
[0];
1540 mix
[5] = mifarekeyA
[5] ^ uid
[1];
1541 mix
[6] = block
^ uid
[2];
1544 des3_context ctx
= { 0x00 };
1545 des3_set2key_enc(&ctx
, masterkey
);
1547 des3_crypt_cbc(&ctx
// des3_context
1548 , DES_ENCRYPT
// int mode
1549 , sizeof(mix
) // length
1555 PrintAndLog("3DES version");
1556 PrintAndLog("Masterkey :\t %s", sprint_hex(masterkey
,sizeof(masterkey
)));
1557 PrintAndLog("UID :\t %s", sprint_hex(uid
, sizeof(uid
)));
1558 PrintAndLog("Sector :\t %0d", block
);
1559 PrintAndLog("Mifare key :\t %s", sprint_hex(mifarekeyA
, sizeof(mifarekeyA
)));
1560 PrintAndLog("Message :\t %s", sprint_hex(mix
, sizeof(mix
)));
1561 PrintAndLog("Diversified key: %s", sprint_hex(divkey
+1, 6));
1563 PrintAndLog("\n DES version");
1565 for (int i
=0; i
< sizeof(mifarekeyA
); ++i
){
1566 dkeyA
[i
] = (mifarekeyA
[i
] << 1) & 0xff;
1567 dkeyA
[6] |= ((mifarekeyA
[i
] >> 7) & 1) << (i
+1);
1570 for (int i
=0; i
< sizeof(mifarekeyB
); ++i
){
1571 dkeyB
[1] |= ((mifarekeyB
[i
] >> 7) & 1) << (i
+1);
1572 dkeyB
[2+i
] = (mifarekeyB
[i
] << 1) & 0xff;
1575 uint8_t zeros
[8] = {0x00};
1576 uint8_t newpwd
[8] = {0x00};
1577 uint8_t dmkey
[24] = {0x00};
1578 memcpy(dmkey
, dkeyA
, 8);
1579 memcpy(dmkey
+8, dkeyB
, 8);
1580 memcpy(dmkey
+16, dkeyA
, 8);
1581 memset(iv
, 0x00, 8);
1583 des3_set3key_enc(&ctx
, dmkey
);
1585 des3_crypt_cbc(&ctx
// des3_context
1586 , DES_ENCRYPT
// int mode
1587 , sizeof(newpwd
) // length
1593 PrintAndLog("Mifare dkeyA :\t %s", sprint_hex(dkeyA
, sizeof(dkeyA
)));
1594 PrintAndLog("Mifare dkeyB :\t %s", sprint_hex(dkeyB
, sizeof(dkeyB
)));
1595 PrintAndLog("Mifare ABA :\t %s", sprint_hex(dmkey
, sizeof(dmkey
)));
1596 PrintAndLog("Mifare Pwd :\t %s", sprint_hex(newpwd
, sizeof(newpwd
)));
1601 // static uint8_t * diversify_key(uint8_t * key){
1603 // for(int i=0; i<16; i++){
1604 // if(i<=6) key[i]^=cuid[i];
1605 // if(i>6) key[i]^=cuid[i%7];
1610 // static void GenerateUIDe( uint8_t *uid, uint8_t len){
1611 // for (int i=0; i<len; ++i){
1617 //------------------------------------
1619 //------------------------------------
1620 static command_t CommandTable
[] =
1622 {"help", CmdHelp
, 1, "This help"},
1623 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1624 {"info", CmdHF14AMfUInfo
, 0, "Tag information"},
1625 {"dump", CmdHF14AMfUDump
, 0, "Dump Ultralight / Ultralight-C tag to binary file"},
1626 {"rdbl", CmdHF14AMfURdBl
, 0, "Read block - Ultralight"},
1627 {"wrbl", CmdHF14AMfUWrBl
, 0, "Write block - Ultralight"},
1628 {"crdbl", CmdHF14AMfUCRdBl
, 0, "Read block - Ultralight C"},
1629 {"cwrbl", CmdHF14AMfUCWrBl
, 0, "Write block - Ultralight C"},
1630 {"cauth", CmdHF14AMfucAuth
, 0, "Authentication - Ultralight C"},
1631 {"setpwd", CmdHF14AMfucSetPwd
, 1, "Set 3des password - Ultralight-C"},
1632 {"setuid", CmdHF14AMfucSetUid
, 1, "Set UID - MAGIC tags only"},
1633 {"gen", CmdHF14AMfuGenDiverseKeys
, 1, "Generate 3des mifare diversified keys"},
1634 {NULL
, NULL
, 0, NULL
}
1637 int CmdHFMFUltra(const char *Cmd
){
1638 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1639 CmdsParse(CommandTable
, Cmd
);
1643 int CmdHelp(const char *Cmd
){
1644 CmdsHelp(CommandTable
);