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 #define MAX_ULTRA_BLOCKS 0x0f
17 #define MAX_ULTRAC_BLOCKS 0x2f
18 //#define MAX_ULTRAC_BLOCKS 0x2c
20 static int CmdHelp(const char *Cmd
);
22 int CmdHF14AMfUInfo(const char *Cmd
){
24 uint8_t datatemp
[7] = {0x00};
26 uint8_t data
[16] = {0x00};
28 UsbCommand c
= {CMD_MIFAREU_READCARD
, {0, 4}};
32 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
33 isOK
= resp
.arg
[0] & 0xff;
34 memcpy(data
, resp
.d
.asBytes
, sizeof(data
));
37 PrintAndLog("Error reading from tag");
41 PrintAndLog("Command execute timed out");
45 PrintAndLog("\n-- Tag Information ---------");
46 PrintAndLog("-------------------------------------------------------------");
48 UsbCommand cmd
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_RAW
| ISO14A_APPEND_CRC
, 1, 0}};
49 cmd
.d
.asBytes
[0] = 0x60;
51 WaitForResponse(CMD_ACK
, &resp
);
54 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
56 uint8_t version
[8] = {0,0,0,0,0,0,0,0};
57 memcpy(&version
, resp
.d
.asBytes
, sizeof(version
));
58 uint8_t len
= resp
.arg
[0] & 0xff;
60 // todo, identify "Magic UL-C tags". // they usually have a static nonce response to 0x1A command.
61 // UL-EV1, size, check version[6] == 0x0b (smaller) 0x0b * 4 == 48
62 case 0x0A:PrintAndLog(" TYPE : NXP MIFARE Ultralight EV1 %d bytes", (version
[6] == 0xB) ? 48 : 128); break;
63 case 0x01:PrintAndLog(" TYPE : NXP MIFARE Ultralight C");break;
64 case 0x00:PrintAndLog(" TYPE : NXP MIFARE Ultralight");break;
69 // Answers to 0x1A == UL-C
70 // NONCE seems to be fixed for my Magic UL-C. How to detect a Magic UL?
71 // maybe read Block0, change 1 byte, write back, and see if it failes or not. If magic, revert changes made?
74 memcpy( datatemp
, data
, 3);
75 memcpy( datatemp
+3, data
+4, 4);
77 PrintAndLog("MANUFACTURER : %s", getTagInfo(datatemp
[0]));
78 PrintAndLog(" UID : %s ", sprint_hex(datatemp
, 7));
80 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
81 int crc0
= 0x88 ^ data
[0] ^ data
[1] ^data
[2];
82 if ( data
[3] == crc0
)
83 PrintAndLog(" BCC0 : %02x - Ok", data
[3]);
85 PrintAndLog(" BCC0 : %02x - crc should be %02x", data
[3], crc0
);
87 int crc1
= data
[4] ^ data
[5] ^ data
[6] ^data
[7];
88 if ( data
[8] == crc1
)
89 PrintAndLog(" BCC1 : %02x - Ok", data
[8]);
91 PrintAndLog(" BCC1 : %02x - crc should be %02x", data
[8], crc1
);
93 PrintAndLog(" Internal : %s ", sprint_hex(data
+ 9, 1));
95 memcpy(datatemp
, data
+10, 2);
96 PrintAndLog(" Lock : %s - %s", sprint_hex(datatemp
, 2),printBits( 2, &datatemp
) );
97 PrintAndLog(" OneTimePad : %s ", sprint_hex(data
+ 3*4, 4));
103 // Mifare Ultralight Write Single Block
105 int CmdHF14AMfUWrBl(const char *Cmd
){
106 uint8_t blockNo
= -1;
107 bool chinese_card
= FALSE
;
108 uint8_t bldata
[16] = {0x00};
111 char cmdp
= param_getchar(Cmd
, 0);
112 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
113 PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]");
114 PrintAndLog(" [block number]");
115 PrintAndLog(" [block data] - (8 hex symbols)");
116 PrintAndLog(" [w] - Chinese magic ultralight tag");
118 PrintAndLog(" sample: hf mfu wrbl 0 01020304");
123 blockNo
= param_get8(Cmd
, 0);
125 if (blockNo
> MAX_ULTRA_BLOCKS
){
126 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
130 if (param_gethex(Cmd
, 1, bldata
, 8)) {
131 PrintAndLog("Block data must include 8 HEX symbols");
135 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
141 PrintAndLog("Access Denied");
143 PrintAndLog("--specialblock no:%02x", blockNo
);
144 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
145 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
146 memcpy(d
.d
.asBytes
,bldata
, 4);
148 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
149 uint8_t isOK
= resp
.arg
[0] & 0xff;
150 PrintAndLog("isOk:%02x", isOK
);
152 PrintAndLog("Command execute timeout");
156 PrintAndLog("--block no:%02x", blockNo
);
157 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
158 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
159 memcpy(e
.d
.asBytes
,bldata
, 4);
161 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
162 uint8_t isOK
= resp
.arg
[0] & 0xff;
163 PrintAndLog("isOk:%02x", isOK
);
165 PrintAndLog("Command execute timeout");
172 // Mifare Ultralight Read Single Block
174 int CmdHF14AMfURdBl(const char *Cmd
){
177 uint8_t blockNo
= -1;
178 char cmdp
= param_getchar(Cmd
, 0);
180 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
181 PrintAndLog("Usage: hf mfu rdbl <block number>");
182 PrintAndLog(" sample: hfu mfu rdbl 0");
186 blockNo
= param_get8(Cmd
, 0);
188 if (blockNo
> MAX_ULTRA_BLOCKS
){
189 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight");
193 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
197 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
198 uint8_t isOK
= resp
.arg
[0] & 0xff;
200 uint8_t *data
= resp
.d
.asBytes
;
201 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
204 PrintAndLog("Failed reading block: (%02x)", isOK
);
207 PrintAndLog("Command execute time-out");
214 // Mifare Ultralight / Ultralight-C; Read and Dump Card Contents
216 int CmdHF14AMfUDump(const char *Cmd
){
219 char filename
[FILE_PATH_SIZE
] = {0x00};
220 char * fnameptr
= filename
;
222 uint8_t *lockbytes_t
= NULL
;
223 uint8_t lockbytes
[2] = {0x00};
225 uint8_t *lockbytes_t2
= NULL
;
226 uint8_t lockbytes2
[2] = {0x00};
228 bool bit
[16] = {0x00};
229 bool bit2
[16] = {0x00};
235 bool tmplockbit
= false;
237 uint8_t *data
= NULL
;
239 char cmdp
= param_getchar(Cmd
, 0);
241 if (cmdp
== 'h' || cmdp
== 'H') {
242 PrintAndLog("Reads all pages from Mifare Ultralight or Ultralight-C tag.");
243 PrintAndLog("It saves binary dump into the file `filename.bin` or `cardUID.bin`");
244 PrintAndLog("Usage: hf mfu dump <c> <filename w/o .bin>");
245 PrintAndLog(" <c> optional cardtype c == Ultralight-C, Defaults to Ultralight");
246 PrintAndLog(" sample: hf mfu dump");
247 PrintAndLog(" : hf mfu dump myfile");
248 PrintAndLog(" : hf mfu dump c myfile");
253 Pages
= (cmdp
== 'c' || cmdp
== 'C') ? 44 : 16;
255 PrintAndLog("Dumping Ultralight%s Card Data...", (Pages
==16)?"":"-C");
257 UsbCommand c
= {CMD_MIFAREU_READCARD
, {BlockNo
,Pages
}};
261 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
262 isOK
= resp
.arg
[0] & 0xff;
264 PrintAndLog("Command error");
267 data
= resp
.d
.asBytes
;
269 PrintAndLog("Command execute time-out");
276 lockbytes_t
= data
+ 8;
277 lockbytes
[0] = lockbytes_t
[2];
278 lockbytes
[1] = lockbytes_t
[3];
279 for(j
= 0; j
< 16; j
++){
280 bit
[j
] = lockbytes
[j
/8] & ( 1 <<(7-j
%8));
283 // Load bottom lockbytes if available
286 lockbytes_t2
= data
+ (40*4);
287 lockbytes2
[0] = lockbytes_t2
[2];
288 lockbytes2
[1] = lockbytes_t2
[3];
289 for (j
= 0; j
< 16; j
++) {
290 bit2
[j
] = lockbytes2
[j
/8] & ( 1 <<(7-j
%8));
294 for (i
= 0; i
< Pages
; ++i
) {
297 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
302 case 3: tmplockbit
= bit
[4]; break;
303 case 4: tmplockbit
= bit
[3]; break;
304 case 5: tmplockbit
= bit
[2]; break;
305 case 6: tmplockbit
= bit
[1]; break;
306 case 7: tmplockbit
= bit
[0]; break;
307 case 8: tmplockbit
= bit
[15]; break;
308 case 9: tmplockbit
= bit
[14]; break;
309 case 10: tmplockbit
= bit
[13]; break;
310 case 11: tmplockbit
= bit
[12]; break;
311 case 12: tmplockbit
= bit
[11]; break;
312 case 13: tmplockbit
= bit
[10]; break;
313 case 14: tmplockbit
= bit
[9]; break;
314 case 15: tmplockbit
= bit
[8]; break;
318 case 19: tmplockbit
= bit2
[6]; break;
322 case 23: tmplockbit
= bit2
[5]; break;
326 case 27: tmplockbit
= bit2
[4]; break;
330 case 31: tmplockbit
= bit2
[2]; break;
334 case 35: tmplockbit
= bit2
[1]; break;
338 case 39: tmplockbit
= bit2
[0]; break;
339 case 40: tmplockbit
= bit2
[12]; break;
340 case 41: tmplockbit
= bit2
[11]; break;
341 case 42: tmplockbit
= bit2
[10]; break; //auth0
342 case 43: tmplockbit
= bit2
[9]; break; //auth1
345 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),tmplockbit
);
350 len
= param_getstr(Cmd
,0,filename
);
352 len
= param_getstr(Cmd
,1,filename
);
354 if (len
> FILE_PATH_SIZE
-5) len
= FILE_PATH_SIZE
-5;
356 // user supplied filename?
359 // UID = data 0-1-2 4-5-6-7 (skips a beat)
360 sprintf(fnameptr
,"%02X%02X%02X%02X%02X%02X%02X.bin",
361 data
[0],data
[1], data
[2], data
[4],data
[5],data
[6], data
[7]);
364 sprintf(fnameptr
+ len
," .bin");
368 if ((fout
= fopen(filename
,"wb")) == NULL
) {
369 PrintAndLog("Could not create file name %s", filename
);
372 fwrite( data
, 1, Pages
*4, fout
);
375 PrintAndLog("Dumped %d pages, wrote %d bytes to %s", Pages
, Pages
*4, filename
);
379 // Needed to Authenticate to Ultralight C tags
380 void rol (uint8_t *data
, const size_t len
){
381 uint8_t first
= data
[0];
382 for (size_t i
= 0; i
< len
-1; i
++) {
388 //-------------------------------------------------------------------------------
389 // Ultralight C Methods
390 //-------------------------------------------------------------------------------
393 // Ultralight C Authentication Demo {currently uses hard-coded key}
395 int CmdHF14AMfucAuth(const char *Cmd
){
397 uint8_t default_keys
[7][16] = {
398 { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 },// 3des std key
399 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },// all zeroes
400 { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f },// 0x00-0x0F
401 { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 },// NFC-key
402 { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },// all ones
403 { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF },// all FF
404 { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF } // 11 22 33
407 char cmdp
= param_getchar(Cmd
, 0);
411 //Change key to user defined one
412 if (cmdp
== 'k' || cmdp
== 'K'){
413 keyNo
= param_get8(Cmd
, 1);
414 if(keyNo
> 6) errors
= true;
417 if (cmdp
== 'h' || cmdp
== 'H') {
422 PrintAndLog("Usage: hf mfu cauth k <key number>");
423 PrintAndLog(" 0 (default): 3DES standard key");
424 PrintAndLog(" 1 : all 0x00 key");
425 PrintAndLog(" 2 : 0x00-0x0F key");
426 PrintAndLog(" 3 : nfc key");
427 PrintAndLog(" 4 : all 0x01 key");
428 PrintAndLog(" 5 : all 0xff key");
429 PrintAndLog(" 6 : 0x00-0xFF key");
430 PrintAndLog("\n sample : hf mfu cauth k");
431 PrintAndLog(" : hf mfu cauth k 3");
435 uint8_t random_a
[8] = { 1,1,1,1,1,1,1,1 };
436 //uint8_t enc_random_a[8] = { 0 };
437 uint8_t random_b
[8] = { 0 };
438 uint8_t enc_random_b
[8] = { 0 };
439 uint8_t random_a_and_b
[16] = { 0 };
440 des3_context ctx
= { 0 };
441 uint8_t *key
= default_keys
[keyNo
];
446 UsbCommand c
= {CMD_MIFAREUC_AUTH1
, {blockNo
}};
449 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
450 uint8_t isOK
= resp
.arg
[0] & 0xff;
452 uint8_t * data
= resp
.d
.asBytes
;
455 memcpy(enc_random_b
,data
+1,8);
457 PrintAndLog("Auth failed");
461 PrintAndLog("Command execute timeout");
464 uint8_t iv
[8] = { 0 };
466 PrintAndLog(" RndA :%s",sprint_hex(random_a
, 8));
467 PrintAndLog(" enc(RndB):%s",sprint_hex(enc_random_b
, 8));
469 des3_set2key_dec(&ctx
, key
);
471 des3_crypt_cbc(&ctx
// des3_context *ctx
472 , DES_DECRYPT
// int mode
473 , sizeof(random_b
) // size_t length
474 , iv
// unsigned char iv[8]
475 , enc_random_b
// const unsigned char *input
476 , random_b
// unsigned char *output
479 PrintAndLog(" RndB:%s",sprint_hex(random_b
, 8));
482 memcpy(random_a_and_b
,random_a
,8);
483 memcpy(random_a_and_b
+8,random_b
,8);
485 PrintAndLog(" A+B:%s",sprint_hex(random_a_and_b
, 16));
487 des3_set2key_enc(&ctx
, key
);
489 des3_crypt_cbc(&ctx
// des3_context *ctx
490 , DES_ENCRYPT
// int mode
491 , sizeof(random_a_and_b
) // size_t length
492 , enc_random_b
// unsigned char iv[8]
493 , random_a_and_b
// const unsigned char *input
494 , random_a_and_b
// unsigned char *output
497 PrintAndLog("enc(A+B):%s",sprint_hex(random_a_and_b
, 16));
500 UsbCommand d
= {CMD_MIFAREUC_AUTH2
, {cuid
}};
501 memcpy(d
.d
.asBytes
,random_a_and_b
, 16);
505 if (WaitForResponseTimeout(CMD_ACK
,&respb
,1500)) {
506 uint8_t isOK
= respb
.arg
[0] & 0xff;
507 uint8_t * data2
= respb
.d
.asBytes
;
510 PrintAndLog("enc(RndA'):%s", sprint_hex(data2
+1, 8));
512 uint8_t foo
[8] = { 0 };
513 uint8_t bar
[8] = { 0 };
514 memcpy(foo
, data2
+1, 8);
515 des3_set2key_dec(&ctx
, key
);
517 des3_crypt_cbc(&ctx
// des3_context *ctx
518 , DES_DECRYPT
// int mode
520 , enc_random_b
// unsigned char iv[8]
521 , foo
// const unsigned char *input
522 , bar
// unsigned char *output
525 PrintAndLog("--> : %s : <-- Should be equal to our RndA",sprint_hex(bar
, 8));
533 PrintAndLog("Command execute timeout");
539 A test function to validate that the polarssl-function works the same
540 was as the openssl-implementation.
541 Commented out, since it requires openssl
543 int CmdTestDES(const char * cmd)
545 uint8_t key[16] = {0x00};
547 memcpy(key,key3_3des_data,16);
548 DES_cblock RndA, RndB;
550 PrintAndLog("----------OpenSSL DES implementation----------");
552 uint8_t e_RndB[8] = {0x00};
553 unsigned char RndARndB[16] = {0x00};
555 DES_cblock iv = { 0 };
556 DES_key_schedule ks1,ks2;
557 DES_cblock key1,key2;
559 memcpy(key,key3_3des_data,16);
561 memcpy(key2,key+8,8);
564 DES_set_key((DES_cblock *)key1,&ks1);
565 DES_set_key((DES_cblock *)key2,&ks2);
567 DES_random_key(&RndA);
568 PrintAndLog(" RndA:%s",sprint_hex(RndA, 8));
569 PrintAndLog(" e_RndB:%s",sprint_hex(e_RndB, 8));
570 //void DES_ede2_cbc_encrypt(const unsigned char *input,
571 // unsigned char *output, long length, DES_key_schedule *ks1,
572 // DES_key_schedule *ks2, DES_cblock *ivec, int enc);
573 DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0);
575 PrintAndLog(" RndB:%s",sprint_hex(RndB, 8));
577 memcpy(RndARndB,RndA,8);
578 memcpy(RndARndB+8,RndB,8);
579 PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16));
580 DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1);
581 PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16));
584 PrintAndLog("----------PolarSSL implementation----------");
586 uint8_t random_a[8] = { 0 };
587 uint8_t enc_random_a[8] = { 0 };
588 uint8_t random_b[8] = { 0 };
589 uint8_t enc_random_b[8] = { 0 };
590 uint8_t random_a_and_b[16] = { 0 };
591 des3_context ctx = { 0 };
593 memcpy(random_a, RndA,8);
595 uint8_t output[8] = { 0 };
596 uint8_t iv[8] = { 0 };
598 PrintAndLog(" RndA :%s",sprint_hex(random_a, 8));
599 PrintAndLog(" e_RndB:%s",sprint_hex(enc_random_b, 8));
601 des3_set2key_dec(&ctx, key);
603 des3_crypt_cbc(&ctx // des3_context *ctx
604 , DES_DECRYPT // int mode
605 , sizeof(random_b) // size_t length
606 , iv // unsigned char iv[8]
607 , enc_random_b // const unsigned char *input
608 , random_b // unsigned char *output
611 PrintAndLog(" RndB:%s",sprint_hex(random_b, 8));
614 memcpy(random_a_and_b ,random_a,8);
615 memcpy(random_a_and_b+8,random_b,8);
617 PrintAndLog(" RA+B:%s",sprint_hex(random_a_and_b, 16));
619 des3_set2key_enc(&ctx, key);
621 des3_crypt_cbc(&ctx // des3_context *ctx
622 , DES_ENCRYPT // int mode
623 , sizeof(random_a_and_b) // size_t length
624 , enc_random_b // unsigned char iv[8]
625 , random_a_and_b // const unsigned char *input
626 , random_a_and_b // unsigned char *output
629 PrintAndLog("enc(RA+B):%s",sprint_hex(random_a_and_b, 16));
635 // Ultralight C Read Single Block
637 int CmdHF14AMfUCRdBl(const char *Cmd
)
641 uint8_t blockNo
= -1;
642 unsigned char key
[16];
643 char cmdp
= param_getchar(Cmd
, 0);
645 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
646 PrintAndLog("Usage: hf mfu crdbl <block number> <password>");
648 PrintAndLog("sample: hf mfu crdbl 0");
649 PrintAndLog(" hf mfu crdbl 0 112233445566778899AABBCCDDEEFF");
653 blockNo
= param_get8(Cmd
, 0);
655 PrintAndLog("Wrong block number");
659 if (blockNo
> MAX_ULTRAC_BLOCKS
){
660 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C");
665 if ( strlen(Cmd
) > 3){
666 if (param_gethex(Cmd
, 1, key
, 32)) {
667 PrintAndLog("Key must include %d HEX symbols", 32);
675 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
678 memcpy(c
.d
.asBytes
,key
,16);
682 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
683 uint8_t isOK
= resp
.arg
[0] & 0xff;
685 uint8_t *data
= resp
.d
.asBytes
;
686 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
689 PrintAndLog("Failed reading block: (%02x)", isOK
);
692 PrintAndLog("Command execute time-out");
698 // Mifare Ultralight C Write Single Block
700 int CmdHF14AMfUCWrBl(const char *Cmd
){
702 uint8_t blockNo
= -1;
703 bool chinese_card
= FALSE
;
704 uint8_t bldata
[16] = {0x00};
707 char cmdp
= param_getchar(Cmd
, 0);
709 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
710 PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]");
711 PrintAndLog(" [block number]");
712 PrintAndLog(" [block data] - (8 hex symbols)");
713 PrintAndLog(" [w] - Chinese magic ultralight tag");
715 PrintAndLog(" sample: hf mfu cwrbl 0 01020304");
720 blockNo
= param_get8(Cmd
, 0);
721 if (blockNo
> MAX_ULTRAC_BLOCKS
){
722 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!");
726 if (param_gethex(Cmd
, 1, bldata
, 8)) {
727 PrintAndLog("Block data must include 8 HEX symbols");
731 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
735 if ( blockNo
<= 3 ) {
737 PrintAndLog("Access Denied");
739 PrintAndLog("--Special block no: 0x%02x", blockNo
);
740 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
741 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
742 memcpy(d
.d
.asBytes
,bldata
, 4);
744 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
745 uint8_t isOK
= resp
.arg
[0] & 0xff;
746 PrintAndLog("isOk:%02x", isOK
);
748 PrintAndLog("Command execute timeout");
752 PrintAndLog("--Block no : 0x%02x", blockNo
);
753 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
754 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
755 memcpy(e
.d
.asBytes
,bldata
, 4);
757 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
758 uint8_t isOK
= resp
.arg
[0] & 0xff;
759 PrintAndLog("isOk : %02x", isOK
);
761 PrintAndLog("Command execute timeout");
768 // Mifare Ultralight C - Set password
770 int CmdHF14AMfucSetPwd(const char *Cmd
){
772 uint8_t pwd
[16] = {0x00};
774 char cmdp
= param_getchar(Cmd
, 0);
776 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
777 PrintAndLog("Usage: hf mfu setpwd <password (32 hex symbols)>");
778 PrintAndLog(" [password] - (32 hex symbols)");
780 PrintAndLog("sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f");
785 if (param_gethex(Cmd
, 0, pwd
, 32)) {
786 PrintAndLog("Password must include 32 HEX symbols");
790 UsbCommand c
= {CMD_MIFAREUC_SETPWD
};
791 memcpy( c
.d
.asBytes
, pwd
, 16);
796 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
797 if ( (resp
.arg
[0] & 0xff) == 1)
798 PrintAndLog("Ultralight-C new password: %s", sprint_hex(pwd
,16));
800 PrintAndLog("Failed writing at block %d", resp
.arg
[1] & 0xff);
805 PrintAndLog("command execution time out");
813 // Mifare Ultraligh - Set UID
815 int CmdHF14AMfucSetUid(const char *Cmd
){
819 uint8_t uid
[7] = {0x00};
820 char cmdp
= param_getchar(Cmd
, 0);
822 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
823 PrintAndLog("Usage: hf mfu setuid <uid (14 hex symbols)>");
824 PrintAndLog(" [uid] - (14 hex symbols)");
825 PrintAndLog("\nThis only works for Magic Ultralight tags.");
827 PrintAndLog("sample: hf mfu setuid 11223344556677");
832 if (param_gethex(Cmd
, 0, uid
, 14)) {
833 PrintAndLog("UID must include 14 HEX symbols");
838 c
.cmd
= CMD_MIFAREU_READBL
;
841 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
842 PrintAndLog("Command execute timeout");
847 uint8_t oldblock2
[4] = {0x00};
848 memcpy(resp
.d
.asBytes
, oldblock2
, 4);
851 c
.cmd
= CMD_MIFAREU_WRITEBL
;
853 c
.d
.asBytes
[0] = uid
[0];
854 c
.d
.asBytes
[1] = uid
[1];
855 c
.d
.asBytes
[2] = uid
[2];
856 c
.d
.asBytes
[3] = 0x88 ^ uid
[0] ^ uid
[1] ^ uid
[2];
858 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
859 PrintAndLog("Command execute timeout");
865 c
.d
.asBytes
[0] = uid
[3];
866 c
.d
.asBytes
[1] = uid
[4];
867 c
.d
.asBytes
[2] = uid
[5];
868 c
.d
.asBytes
[3] = uid
[6];
870 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
871 PrintAndLog("Command execute timeout");
877 c
.d
.asBytes
[0] = uid
[3] ^ uid
[4] ^ uid
[5] ^ uid
[6];
878 c
.d
.asBytes
[1] = oldblock2
[1];
879 c
.d
.asBytes
[2] = oldblock2
[2];
880 c
.d
.asBytes
[3] = oldblock2
[3];
882 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
883 PrintAndLog("Command execute timeout");
890 int CmdHF14AMfuGenDiverseKeys(const char *Cmd
){
892 uint8_t iv
[8] = { 0x00 };
893 uint8_t block
= 0x07;
896 //04 57 b6 e2 05 3f 80 UID
898 uint8_t uid
[] = { 0xF4,0xEA, 0x54, 0x8E };
899 uint8_t mifarekeyA
[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 };
900 uint8_t mifarekeyB
[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 };
901 uint8_t dkeyA
[8] = { 0x00 };
902 uint8_t dkeyB
[8] = { 0x00 };
904 uint8_t masterkey
[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff };
906 uint8_t mix
[8] = { 0x00 };
907 uint8_t divkey
[8] = { 0x00 };
909 memcpy(mix
, mifarekeyA
, 4);
911 mix
[4] = mifarekeyA
[4] ^ uid
[0];
912 mix
[5] = mifarekeyA
[5] ^ uid
[1];
913 mix
[6] = block
^ uid
[2];
916 des3_context ctx
= { 0x00 };
917 des3_set2key_enc(&ctx
, masterkey
);
919 des3_crypt_cbc(&ctx
// des3_context
920 , DES_ENCRYPT
// int mode
921 , sizeof(mix
) // length
927 PrintAndLog("3DES version");
928 PrintAndLog("Masterkey :\t %s", sprint_hex(masterkey
,sizeof(masterkey
)));
929 PrintAndLog("UID :\t %s", sprint_hex(uid
, sizeof(uid
)));
930 PrintAndLog("Sector :\t %0d", block
);
931 PrintAndLog("Mifare key :\t %s", sprint_hex(mifarekeyA
, sizeof(mifarekeyA
)));
932 PrintAndLog("Message :\t %s", sprint_hex(mix
, sizeof(mix
)));
933 PrintAndLog("Diversified key: %s", sprint_hex(divkey
+1, 6));
935 PrintAndLog("\n DES version");
937 for (int i
=0; i
< sizeof(mifarekeyA
); ++i
){
938 dkeyA
[i
] = (mifarekeyA
[i
] << 1) & 0xff;
939 dkeyA
[6] |= ((mifarekeyA
[i
] >> 7) & 1) << (i
+1);
942 for (int i
=0; i
< sizeof(mifarekeyB
); ++i
){
943 dkeyB
[1] |= ((mifarekeyB
[i
] >> 7) & 1) << (i
+1);
944 dkeyB
[2+i
] = (mifarekeyB
[i
] << 1) & 0xff;
947 uint8_t zeros
[8] = {0x00};
948 uint8_t newpwd
[8] = {0x00};
949 uint8_t dmkey
[24] = {0x00};
950 memcpy(dmkey
, dkeyA
, 8);
951 memcpy(dmkey
+8, dkeyB
, 8);
952 memcpy(dmkey
+16, dkeyA
, 8);
955 des3_set3key_enc(&ctx
, dmkey
);
957 des3_crypt_cbc(&ctx
// des3_context
958 , DES_ENCRYPT
// int mode
959 , sizeof(newpwd
) // length
965 PrintAndLog("Mifare dkeyA :\t %s", sprint_hex(dkeyA
, sizeof(dkeyA
)));
966 PrintAndLog("Mifare dkeyB :\t %s", sprint_hex(dkeyB
, sizeof(dkeyB
)));
967 PrintAndLog("Mifare ABA :\t %s", sprint_hex(dmkey
, sizeof(dmkey
)));
968 PrintAndLog("Mifare Pwd :\t %s", sprint_hex(newpwd
, sizeof(newpwd
)));
973 // static uint8_t * diversify_key(uint8_t * key){
975 // for(int i=0; i<16; i++){
976 // if(i<=6) key[i]^=cuid[i];
977 // if(i>6) key[i]^=cuid[i%7];
982 // static void GenerateUIDe( uint8_t *uid, uint8_t len){
983 // for (int i=0; i<len; ++i){
989 //------------------------------------
991 //------------------------------------
992 static command_t CommandTable
[] =
994 {"help", CmdHelp
, 1,"This help"},
995 {"dbg", CmdHF14AMfDbg
, 0,"Set default debug mode"},
996 {"info", CmdHF14AMfUInfo
, 0,"Taginfo"},
997 {"dump", CmdHF14AMfUDump
, 0,"Dump MIFARE Ultralight / Ultralight-C tag to binary file"},
998 {"rdbl", CmdHF14AMfURdBl
, 0,"Read block - MIFARE Ultralight"},
999 {"wrbl", CmdHF14AMfUWrBl
, 0,"Write block - MIFARE Ultralight"},
1000 {"crdbl", CmdHF14AMfUCRdBl
, 0,"Read block - MIFARE Ultralight C"},
1001 {"cwrbl", CmdHF14AMfUCWrBl
, 0,"Write block - MIFARE Ultralight C"},
1002 {"cauth", CmdHF14AMfucAuth
, 0,"Ultralight C Authentication"},
1003 //{"testdes", CmdTestDES , 1, "Test DES"},
1004 {"setpwd", CmdHF14AMfucSetPwd
, 1, "Set 3des password [Ultralight-C only]"},
1005 {"setuid", CmdHF14AMfucSetUid
, 1, "Set UID"},
1006 {"gen", CmdHF14AMfuGenDiverseKeys
, 1, "Generate 3des mifare diversified keys"},
1007 {NULL
, NULL
, 0, NULL
}
1010 int CmdHFMFUltra(const char *Cmd
){
1011 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1012 CmdsParse(CommandTable
, Cmd
);
1016 int CmdHelp(const char *Cmd
){
1017 CmdsHelp(CommandTable
);