]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmfu.c
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 <openssl/des.h>
13 uint8_t MAX_ULTRA_BLOCKS
= 0x0f;
14 uint8_t MAX_ULTRAC_BLOCKS
= 0x2c;
15 uint8_t key1_blnk_data
[16] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
16 uint8_t key2_defa_data
[16] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f };
17 uint8_t key3_3des_data
[16] = { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 };
18 uint8_t key4_nfc_data
[16] = { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 };
19 uint8_t key5_ones_data
[16] = { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 };
21 static int CmdHelp(const char *Cmd
);
23 int CmdHF14AMfUInfo(const char *Cmd
){
25 uint8_t datatemp
[7] = {0x00};
29 UsbCommand c
= {CMD_MIFAREU_READCARD
, {0, 4}};
33 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
34 isOK
= resp
.arg
[0] & 0xff;
35 data
= resp
.d
.asBytes
;
38 PrintAndLog("Error reading from tag");
42 PrintAndLog("Command execute timed out");
47 memcpy( datatemp
, data
,3);
48 memcpy( datatemp
+3, data
+4, 4);
49 PrintAndLog(" UID :%s ", sprint_hex(datatemp
, 7));
51 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
52 int crc0
= 0x88 ^ data
[0] ^ data
[1] ^data
[2];
53 if ( data
[3] == crc0
)
54 PrintAndLog(" BCC0 :%02x - Ok", data
[3]);
56 PrintAndLog(" BCC0 :%02x - crc should be %02x", data
[3], crc0
);
58 int crc1
= data
[4] ^ data
[5] ^ data
[6] ^data
[7];
59 if ( data
[8] == crc1
)
60 PrintAndLog(" BCC1 :%02x - Ok", data
[8]);
62 PrintAndLog(" BCC1 :%02x - crc should be %02x", data
[8], crc1
);
64 PrintAndLog(" Internal :%s ", sprint_hex(data
+ 9, 1));
66 memcpy(datatemp
, data
+10, 2);
67 PrintAndLog(" Lock :%s - %s", sprint_hex(datatemp
, 2),printBits( 2, &datatemp
) );
68 PrintAndLog(" OneTimePad :%s ", sprint_hex(data
+ 3*4, 4));
75 // Mifare Ultralight Write Single Block
77 int CmdHF14AMfUWrBl(const char *Cmd
){
79 bool chinese_card
= 0;
80 uint8_t bldata
[16] = {0x00};
84 PrintAndLog("Usage: hf mfu uwrbl <block number> <block data (8 hex symbols)> [w]");
85 PrintAndLog(" sample: hf mfu uwrbl 0 01020304");
88 blockNo
= param_get8(Cmd
, 0);
89 if (blockNo
>MAX_ULTRA_BLOCKS
){
90 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
93 if (param_gethex(Cmd
, 1, bldata
, 8)) {
94 PrintAndLog("Block data must include 8 HEX symbols");
97 if (strchr(Cmd
,'w') != 0) {
103 PrintAndLog("Access Denied");
105 PrintAndLog("--specialblock no:%02x", blockNo
);
106 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
107 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
108 memcpy(d
.d
.asBytes
,bldata
, 4);
110 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
111 uint8_t isOK
= resp
.arg
[0] & 0xff;
112 PrintAndLog("isOk:%02x", isOK
);
114 PrintAndLog("Command execute timeout");
120 PrintAndLog("Access Denied");
122 PrintAndLog("--specialblock no:%02x", blockNo
);
123 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
124 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
125 memcpy(d
.d
.asBytes
,bldata
, 4);
127 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
128 uint8_t isOK
= resp
.arg
[0] & 0xff;
129 PrintAndLog("isOk:%02x", isOK
);
131 PrintAndLog("Command execute timeout");
137 PrintAndLog("Access Denied");
139 PrintAndLog("--specialblock no:%02x", blockNo
);
140 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
141 UsbCommand c
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
142 memcpy(c
.d
.asBytes
, bldata
, 4);
144 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
145 uint8_t isOK
= resp
.arg
[0] & 0xff;
146 PrintAndLog("isOk:%02x", isOK
);
148 PrintAndLog("Command execute timeout");
153 PrintAndLog("--specialblock no:%02x", blockNo
);
154 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
155 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
156 memcpy(d
.d
.asBytes
,bldata
, 4);
158 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
159 uint8_t isOK
= resp
.arg
[0] & 0xff;
160 PrintAndLog("isOk:%02x", isOK
);
162 PrintAndLog("Command execute timeout");
166 PrintAndLog("--block no:%02x", blockNo
);
167 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
168 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
169 memcpy(e
.d
.asBytes
,bldata
, 4);
171 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
172 uint8_t isOK
= resp
.arg
[0] & 0xff;
173 PrintAndLog("isOk:%02x", isOK
);
175 PrintAndLog("Command execute timeout");
183 // Mifare Ultralight Read Single Block
185 int CmdHF14AMfURdBl(const char *Cmd
){
190 PrintAndLog("Usage: hf mfu urdbl <block number>");
191 PrintAndLog(" sample: hfu mfu urdbl 0");
195 blockNo
= param_get8(Cmd
, 0);
196 // if (blockNo>MAX_ULTRA_BLOCKS){
197 // PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
200 PrintAndLog("--block no:%02x", (int)blockNo
);
201 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
205 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
206 uint8_t isOK
= resp
.arg
[0] & 0xff;
207 uint8_t * data
= resp
.d
.asBytes
;
210 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 4));
212 PrintAndLog("isOk:%02x", isOK
);
215 PrintAndLog("Command execute timeout");
221 // Mifare Ultralight Read (Dump) Card Contents
223 int CmdHF14AMfURdCard(const char *Cmd
){
227 uint8_t *lockbytes_t
= NULL
;
228 uint8_t lockbytes
[2] = {0x00};
229 bool bit
[16] = {0x00};
231 uint8_t datatemp
[7] = {0x00};
233 uint8_t * data
= NULL
;
236 if (strchr(Cmd
,'x') != 0){
238 if ((fout
= fopen("dump_ultralight_data.bin","wb")) == NULL
) {
239 PrintAndLog("Could not create file name dumpdata.bin");
242 PrintAndLog("Dumping Ultralight Card Data...");
244 PrintAndLog("Attempting to Read Ultralight... ");
245 UsbCommand c
= {CMD_MIFAREU_READCARD
, {BlockNo
, pages
}};
249 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
250 isOK
= resp
.arg
[0] & 0xff;
251 data
= resp
.d
.asBytes
;
252 PrintAndLog("isOk:%02x", isOK
);
255 for (i
= 0; i
< pages
; i
++) {
259 lockbytes_t
=data
+(i
*4);
260 lockbytes
[0]=lockbytes_t
[2];
261 lockbytes
[1]=lockbytes_t
[3];
262 for(int j
=0; j
<16; j
++){
263 bit
[j
]=lockbytes
[j
/8] & ( 1 <<(7-j
%8));
265 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
266 memcpy(datatemp
,data
+ i
* 4,4);
267 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
270 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[4]);
271 memcpy(datatemp
,data
+ i
* 4,4);
272 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
275 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[3]);
276 memcpy(datatemp
,data
+ i
* 4,4);
277 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
280 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[2]);
281 memcpy(datatemp
,data
+ i
* 4,4);
282 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
285 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[1]);
286 memcpy(datatemp
,data
+ i
* 4,4);
287 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
290 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[0]);
291 memcpy(datatemp
,data
+ i
* 4,4);
292 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
295 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[15]);
296 memcpy(datatemp
,data
+ i
* 4,4);
297 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
300 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[14]);
301 memcpy(datatemp
,data
+ i
* 4,4);
302 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
305 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[13]);
306 memcpy(datatemp
,data
+ i
* 4,4);
307 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
310 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[12]);
311 memcpy(datatemp
,data
+ i
* 4,4);
312 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
315 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[11]);
316 memcpy(datatemp
,data
+ i
* 4,4);
317 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
320 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[10]);
321 memcpy(datatemp
,data
+ i
* 4,4);
322 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
325 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[9]);
326 memcpy(datatemp
,data
+ i
* 4,4);
327 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
330 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[8]);
331 memcpy(datatemp
,data
+ i
* 4,4);
332 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
335 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
336 memcpy(datatemp
,data
+ i
* 4,4);
337 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
343 PrintAndLog("Command execute timeout");
345 if (dump
) fclose(fout
);
349 int CmdHF14AMfUDump(const char *Cmd
){
353 uint8_t *lockbytes_t
= NULL
;
354 uint8_t lockbytes
[2] = {0x00};
355 bool bit
[16] = {0x00};
356 uint8_t datatemp
[5] = {0x00};
359 uint8_t * data
= NULL
;
362 if ((fout
= fopen("dump_ultralight_data.bin","wb")) == NULL
) {
363 PrintAndLog("Could not create file name dumpdata.bin");
366 PrintAndLog("Dumping Ultralight Card Data...");
368 PrintAndLog("Attempting to Read Ultralight... ");
369 UsbCommand c
= {CMD_MIFAREU_READCARD
, {BlockNo
,Pages
}};
373 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
374 isOK
= resp
.arg
[0] & 0xff;
375 data
= resp
.d
.asBytes
;
376 PrintAndLog("isOk:%02x", isOK
);
378 for (i
= 0; i
< Pages
; i
++) {
382 lockbytes_t
=data
+(i
*4);
383 lockbytes
[0]=lockbytes_t
[2];
384 lockbytes
[1]=lockbytes_t
[3];
385 for(int j
=0; j
<16; j
++){
386 bit
[j
]=lockbytes
[j
/8] & ( 1 <<(7-j
%8));
388 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
389 memcpy(datatemp
,data
+ i
* 4,4);
390 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
393 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[4]);
394 memcpy(datatemp
,data
+ i
* 4,4);
395 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
398 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[3]);
399 memcpy(datatemp
,data
+ i
* 4,4);
400 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
403 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[2]);
404 memcpy(datatemp
,data
+ i
* 4,4);
405 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
408 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[1]);
409 memcpy(datatemp
,data
+ i
* 4,4);
410 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
413 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[0]);
414 memcpy(datatemp
,data
+ i
* 4,4);
415 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
418 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[15]);
419 memcpy(datatemp
,data
+ i
* 4,4);
420 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
423 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[14]);
424 memcpy(datatemp
,data
+ i
* 4,4);
425 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
428 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[13]);
429 memcpy(datatemp
,data
+ i
* 4,4);
430 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
433 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[12]);
434 memcpy(datatemp
,data
+ i
* 4,4);
435 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
438 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[11]);
439 memcpy(datatemp
,data
+ i
* 4,4);
440 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
443 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[10]);
444 memcpy(datatemp
,data
+ i
* 4,4);
445 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
448 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[9]);
449 memcpy(datatemp
,data
+ i
* 4,4);
450 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
453 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[8]);
454 memcpy(datatemp
,data
+ i
* 4,4);
455 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
458 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
459 memcpy(datatemp
,data
+ i
* 4,4);
460 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
465 PrintAndLog("Command1 execute timeout");
467 if (dump
) fclose(fout
);
471 // Needed to Authenticate to Ultralight C tags
472 void rol (uint8_t *data
, const size_t len
){
473 uint8_t first
= data
[0];
474 for (size_t i
= 0; i
< len
-1; i
++) {
480 //-------------------------------------------------------------------------------
481 // Ultralight C Methods
482 //-------------------------------------------------------------------------------
485 // Ultralight C Authentication Demo {currently uses hard-coded key}
487 int CmdHF14AMfucAuth(const char *Cmd
){
489 uint8_t blockNo
= 0, keyNo
=0;
490 uint8_t e_RndB
[8] = {0x00};
492 unsigned char RndARndB
[16] = {0x00};
493 uint8_t key
[16] = {0x00};
494 DES_cblock RndA
, RndB
;
496 DES_key_schedule ks1
,ks2
;
497 DES_cblock key1
,key2
;
503 PrintAndLog("Usage: hf mfu auth k <key number>");
504 PrintAndLog(" sample: hf mfu auth k 0");
508 //Change key to user defined one
509 if (strchr(Cmd
,'k') != 0){
511 keyNo
= param_get8(Cmd
, 1);
514 memcpy(key
,key1_blnk_data
,16);
517 memcpy(key
,key2_defa_data
,16);
520 memcpy(key
,key4_nfc_data
,16);
523 memcpy(key
,key5_ones_data
,16);
526 memcpy(key
,key3_3des_data
,16);
530 memcpy(key
,key3_3des_data
,16);
533 memcpy(key2
,key
+8,8);
534 DES_set_key((DES_cblock
*)key1
,&ks1
);
535 DES_set_key((DES_cblock
*)key2
,&ks2
);
538 UsbCommand c
= {CMD_MIFAREUC_AUTH1
, {blockNo
}};
541 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
542 uint8_t isOK
= resp
.arg
[0] & 0xff;
544 uint8_t * data
= resp
.d
.asBytes
;
547 PrintAndLog("enc(RndB):%s", sprint_hex(data
+1, 8));
548 memcpy(e_RndB
,data
+1,8);
551 PrintAndLog("Command execute timeout");
555 DES_random_key(&RndA
);
556 DES_ede2_cbc_encrypt(e_RndB
,RndB
,sizeof(e_RndB
),&ks1
,&ks2
,&iv
,0);
557 PrintAndLog(" RndB:%s",sprint_hex(RndB
, 8));
558 PrintAndLog(" RndA:%s",sprint_hex(RndA
, 8));
560 memcpy(RndARndB
,RndA
,8);
561 memcpy(RndARndB
+8,RndB
,8);
562 PrintAndLog(" RA+B:%s",sprint_hex(RndARndB
, 16));
563 DES_ede2_cbc_encrypt(RndARndB
,RndARndB
,sizeof(RndARndB
),&ks1
,&ks2
,&e_RndB
,1);
564 PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB
, 16));
567 UsbCommand d
= {CMD_MIFAREUC_AUTH2
, {cuid
}};
568 memcpy(d
.d
.asBytes
,RndARndB
, 16);
572 if (WaitForResponseTimeout(CMD_ACK
,&respb
,1500)) {
573 uint8_t isOK
= respb
.arg
[0] & 0xff;
574 uint8_t * data2
= respb
.d
.asBytes
;
577 PrintAndLog("enc(RndA'):%s", sprint_hex(data2
+1, 8));
581 PrintAndLog("Command execute timeout");
587 // Ultralight C Read Single Block
589 int CmdHF14AMfUCRdBl(const char *Cmd
)
594 PrintAndLog("Usage: hf mfu ucrdbl <block number>");
595 PrintAndLog(" sample: hf mfu ucrdbl 0");
599 blockNo
= param_get8(Cmd
, 0);
600 if (blockNo
>MAX_ULTRAC_BLOCKS
){
601 PrintAndLog("Error: Maximum number of readable blocks is 44 for Ultralight Cards!");
604 PrintAndLog("--block no:%02x", (int)blockNo
);
607 UsbCommand e
= {CMD_MIFAREU_READBL
, {blockNo
}};
610 if (WaitForResponseTimeout(CMD_ACK
,&resp_c
,1500)) {
611 uint8_t isOK
= resp_c
.arg
[0] & 0xff;
612 uint8_t * data
= resp_c
.d
.asBytes
;
614 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 4));
616 PrintAndLog("isOk:%02x", isOK
);
618 PrintAndLog("Command execute timeout");
624 // Ultralight C Read (or Dump) Card Contents
626 int CmdHF14AMfUCRdCard(const char *Cmd
){
630 uint8_t *lockbytes_t
=NULL
;
631 uint8_t lockbytes
[2]={0x00};
632 uint8_t *lockbytes_t2
=NULL
;
633 uint8_t lockbytes2
[2]={0x00};
635 bool bit2
[16]={0x00};
637 uint8_t datatemp
[5]={0x00};
639 uint8_t * data
= NULL
;
642 if (strchr(Cmd
,'x') != 0){
644 if ((fout
= fopen("dump_ultralightc_data.bin","wb")) == NULL
) {
645 PrintAndLog("Could not create file name dumpdata.bin");
648 PrintAndLog("Dumping Ultralight C Card Data...");
650 PrintAndLog("Attempting to Read Ultralight C... ");
651 UsbCommand c
= {CMD_MIFAREUC_READCARD
, {BlockNo
, Pages
}};
655 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
656 isOK
= resp
.arg
[0] & 0xff;
657 data
= resp
.d
.asBytes
;
659 PrintAndLog("isOk:%02x", isOK
);
661 for (i
= 0; i
< Pages
; i
++) {
665 lockbytes_t
=data
+(i
*4);
666 lockbytes
[0]=lockbytes_t
[2];
667 lockbytes
[1]=lockbytes_t
[3];
668 for(int j
=0; j
<16; j
++){
669 bit
[j
]=lockbytes
[j
/8] & ( 1 <<(7-j
%8));
671 //might as well read bottom lockbytes too
672 lockbytes_t2
=data
+(40*4);
673 lockbytes2
[0]=lockbytes_t2
[2];
674 lockbytes2
[1]=lockbytes_t2
[3];
675 for(int j
=0; j
<16; j
++){
676 bit2
[j
]=lockbytes2
[j
/8] & ( 1 <<(7-j
%8));
678 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
679 memcpy(datatemp
,data
+ i
* 4,4);
680 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
683 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[4]);
684 memcpy(datatemp
,data
+ i
* 4,4);
685 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
688 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[3]);
689 memcpy(datatemp
,data
+ i
* 4,4);
690 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
693 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[2]);
694 memcpy(datatemp
,data
+ i
* 4,4);
695 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
698 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[1]);
699 memcpy(datatemp
,data
+ i
* 4,4);
700 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
703 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[0]);
704 memcpy(datatemp
,data
+ i
* 4,4);
705 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
708 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[15]);
709 memcpy(datatemp
,data
+ i
* 4,4);
710 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
713 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[14]);
714 memcpy(datatemp
,data
+ i
* 4,4);
715 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
718 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[13]);
719 memcpy(datatemp
,data
+ i
* 4,4);
720 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
723 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[12]);
724 memcpy(datatemp
,data
+ i
* 4,4);
725 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
728 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[11]);
729 memcpy(datatemp
,data
+ i
* 4,4);
730 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
733 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[10]);
734 memcpy(datatemp
,data
+ i
* 4,4);
735 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
738 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[9]);
739 memcpy(datatemp
,data
+ i
* 4,4);
740 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
743 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[8]);
744 memcpy(datatemp
,data
+ i
* 4,4);
745 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
751 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[6]);
752 memcpy(datatemp
,data
+ i
* 4,4);
753 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
759 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[5]);
760 memcpy(datatemp
,data
+ i
* 4,4);
761 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
767 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[4]);
768 memcpy(datatemp
,data
+ i
* 4,4);
769 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
775 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[2]);
776 memcpy(datatemp
,data
+ i
* 4,4);
777 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
783 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[1]);
784 memcpy(datatemp
,data
+ i
* 4,4);
785 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
791 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[0]);
792 memcpy(datatemp
,data
+ i
* 4,4);
793 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
796 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[12]);
797 memcpy(datatemp
,data
+ i
* 4,4);
798 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
801 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[11]);
802 memcpy(datatemp
,data
+ i
* 4,4);
803 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
807 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[10]);
808 memcpy(datatemp
,data
+ i
* 4,4);
809 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
813 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[9]);
814 memcpy(datatemp
,data
+ i
* 4,4);
815 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
818 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
819 memcpy(datatemp
,data
+ i
* 4,4);
820 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
826 PrintAndLog("Command1 execute timeout");
828 if (dump
) fclose(fout
);
833 // Ultralight C Dump Card Contents to file
835 int CmdHF14AMfUCDump(const char *Cmd
){
839 uint8_t *lockbytes_t
=NULL
;
840 uint8_t lockbytes
[2]={0x00};
841 uint8_t *lockbytes_t2
=NULL
;
842 uint8_t lockbytes2
[2]={0x00};
844 bool bit2
[16]={0x00};
846 uint8_t datatemp
[5]={0x00};
849 uint8_t * data
= NULL
;
852 if ((fout
= fopen("dump_ultralightc_data.bin","wb")) == NULL
) {
853 PrintAndLog("Could not create file name dumpdata.bin");
856 PrintAndLog("Dumping Ultralight C Card Data...");
857 PrintAndLog("Attempting to Read Ultralight C... ");
858 UsbCommand c
= {CMD_MIFAREU_READCARD
, {BlockNo
,Pages
}};
862 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
863 isOK
= resp
.arg
[0] & 0xff;
864 data
= resp
.d
.asBytes
;
865 PrintAndLog("isOk:%02x", isOK
);
867 for (i
= 0; i
< Pages
; i
++) {
871 lockbytes_t
=data
+(i
*4);
872 lockbytes
[0]=lockbytes_t
[2];
873 lockbytes
[1]=lockbytes_t
[3];
874 for(int j
=0; j
<16; j
++){
875 bit
[j
]=lockbytes
[j
/8] & ( 1 <<(7-j
%8));
878 //might as well read bottom lockbytes too
879 lockbytes_t2
=data
+(40*4);
880 lockbytes2
[0]=lockbytes_t2
[2];
881 lockbytes2
[1]=lockbytes_t2
[3];
882 for(int j
=0; j
<16; j
++){
883 bit2
[j
]=lockbytes2
[j
/8] & ( 1 <<(7-j
%8));
886 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
887 memcpy(datatemp
,data
+ i
* 4,4);
888 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
891 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[4]);
892 memcpy(datatemp
,data
+ i
* 4,4);
893 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
896 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[3]);
897 memcpy(datatemp
,data
+ i
* 4,4);
898 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
901 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[2]);
902 memcpy(datatemp
,data
+ i
* 4,4);
903 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
906 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[1]);
907 memcpy(datatemp
,data
+ i
* 4,4);
908 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
911 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[0]);
912 memcpy(datatemp
,data
+ i
* 4,4);
913 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
916 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[15]);
917 memcpy(datatemp
,data
+ i
* 4,4);
918 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
921 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[14]);
922 memcpy(datatemp
,data
+ i
* 4,4);
923 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
926 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[13]);
927 memcpy(datatemp
,data
+ i
* 4,4);
928 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
931 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[12]);
932 memcpy(datatemp
,data
+ i
* 4,4);
933 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
936 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[11]);
937 memcpy(datatemp
,data
+ i
* 4,4);
938 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
941 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[10]);
942 memcpy(datatemp
,data
+ i
* 4,4);
943 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
946 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[9]);
947 memcpy(datatemp
,data
+ i
* 4,4);
948 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
951 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[8]);
952 memcpy(datatemp
,data
+ i
* 4,4);
953 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
959 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[6]);
960 memcpy(datatemp
,data
+ i
* 4,4);
961 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
967 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[5]);
968 memcpy(datatemp
,data
+ i
* 4,4);
969 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
975 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[4]);
976 memcpy(datatemp
,data
+ i
* 4,4);
977 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
983 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[2]);
984 memcpy(datatemp
,data
+ i
* 4,4);
985 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
991 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[1]);
992 memcpy(datatemp
,data
+ i
* 4,4);
993 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
999 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[0]);
1000 memcpy(datatemp
,data
+ i
* 4,4);
1001 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
1004 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[12]);
1005 memcpy(datatemp
,data
+ i
* 4,4);
1006 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
1009 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[11]);
1010 memcpy(datatemp
,data
+ i
* 4,4);
1011 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
1015 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[10]);
1016 memcpy(datatemp
,data
+ i
* 4,4);
1017 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
1021 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[9]);
1022 memcpy(datatemp
,data
+ i
* 4,4);
1023 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
1026 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
1027 memcpy(datatemp
,data
+ i
* 4,4);
1028 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
1034 PrintAndLog("Command1 execute timeout");
1036 if (dump
) fclose(fout
);
1041 // Mifare Ultralight C Write Single Block
1043 int CmdHF14AMfUCWrBl(const char *Cmd
){
1045 uint8_t blockNo
= 0;
1046 bool chinese_card
= 0;
1047 uint8_t bldata
[16] = {0x00};
1050 if (strlen(Cmd
)<3) {
1051 PrintAndLog("Usage: hf mfu ucwrbl <block number> <block data (8 hex symbols)> [w]");
1052 PrintAndLog(" sample: hf mfu uwrbl 0 01020304");
1055 blockNo
= param_get8(Cmd
, 0);
1056 if (blockNo
>(MAX_ULTRAC_BLOCKS
+4)){
1057 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight Cards!");
1060 if (param_gethex(Cmd
, 1, bldata
, 8)) {
1061 PrintAndLog("Block data must include 8 HEX symbols");
1064 if (strchr(Cmd
,'w') != 0) {
1070 PrintAndLog("Access Denied");
1072 PrintAndLog("--specialblock no:%02x", blockNo
);
1073 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
1074 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1075 memcpy(d
.d
.asBytes
,bldata
, 4);
1077 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1078 uint8_t isOK
= resp
.arg
[0] & 0xff;
1079 PrintAndLog("isOk:%02x", isOK
);
1081 PrintAndLog("Command execute timeout");
1087 PrintAndLog("Access Denied");
1089 PrintAndLog("--specialblock no:%02x", blockNo
);
1090 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
1091 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1092 memcpy(d
.d
.asBytes
,bldata
, 4);
1094 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1095 uint8_t isOK
= resp
.arg
[0] & 0xff;
1096 PrintAndLog("isOk:%02x", isOK
);
1098 PrintAndLog("Command execute timeout");
1104 PrintAndLog("Access Denied");
1106 PrintAndLog("--specialblock no:%02x", blockNo
);
1107 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
1108 UsbCommand c
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1109 memcpy(c
.d
.asBytes
, bldata
, 4);
1111 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1112 uint8_t isOK
= resp
.arg
[0] & 0xff;
1113 PrintAndLog("isOk:%02x", isOK
);
1115 PrintAndLog("Command execute timeout");
1120 PrintAndLog("--specialblock no:%02x", blockNo
);
1121 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
1122 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1123 memcpy(d
.d
.asBytes
,bldata
, 4);
1125 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1126 uint8_t isOK
= resp
.arg
[0] & 0xff;
1127 PrintAndLog("isOk:%02x", isOK
);
1129 PrintAndLog("Command execute timeout");
1133 PrintAndLog("--block no:%02x", blockNo
);
1134 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
1135 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1136 memcpy(e
.d
.asBytes
,bldata
, 4);
1138 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1139 uint8_t isOK
= resp
.arg
[0] & 0xff;
1140 PrintAndLog("isOk:%02x", isOK
);
1142 PrintAndLog("Command execute timeout");
1149 //------------------------------------
1151 //------------------------------------
1152 static command_t CommandTable
[] =
1154 {"help", CmdHelp
, 1,"This help"},
1155 {"dbg", CmdHF14AMfDbg
, 0,"Set default debug mode"},
1156 {"info", CmdHF14AMfUInfo
, 0,"Taginfo"},
1157 {"rdbl", CmdHF14AMfURdBl
, 0,"Read block - MIFARE Ultralight"},
1158 {"rdcard", CmdHF14AMfURdCard
, 0,"Read card - MIFARE Ultralight"},
1159 {"dump", CmdHF14AMfUDump
, 0,"Dump MIFARE Ultralight tag to binary file"},
1160 {"wrbl", CmdHF14AMfUWrBl
, 0,"Write block - MIFARE Ultralight"},
1161 {"crdbl", CmdHF14AMfUCRdBl
, 0,"Read block - MIFARE Ultralight C"},
1162 {"crdcard", CmdHF14AMfUCRdCard
, 0,"Read card - MIFARE Ultralight C"},
1163 {"cdump", CmdHF14AMfUCDump
, 0,"Dump MIFARE Ultralight C tag to binary file"},
1164 {"cwrbl", CmdHF14AMfUCWrBl
, 0,"Write MIFARE Ultralight C block"},
1165 {"cauth", CmdHF14AMfucAuth
, 0,"try a Ultralight C Authentication"},
1166 {NULL
, NULL
, 0, NULL
}
1169 int CmdHFMFUltra(const char *Cmd
){
1170 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1171 CmdsParse(CommandTable
, Cmd
);
1175 int CmdHelp(const char *Cmd
){
1176 CmdsHelp(CommandTable
);