]>
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 //-----------------------------------------------------------------------------
13 #include <openssl/des.h>
15 uint8_t MAX_ULTRA_BLOCKS
= 0x0f;
16 uint8_t MAX_ULTRAC_BLOCKS
= 0x2c;
17 uint8_t key1_blnk_data
[16] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
18 uint8_t key2_defa_data
[16] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f };
19 uint8_t key3_3des_data
[16] = { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 };
20 uint8_t key4_nfc_data
[16] = { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 };
22 static int CmdHelp(const char *Cmd
);
25 // Mifare Ultralight Write Single Block
27 int CmdHF14AMfUWrBl(const char *Cmd
){
30 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
34 PrintAndLog("Usage: hf mfu uwrbl <block number> <block data (8 hex symbols)> [w]");
35 PrintAndLog(" sample: hf mfu uwrbl 0 01020304");
38 blockNo
= param_get8(Cmd
, 0);
39 if (blockNo
>MAX_ULTRA_BLOCKS
){
40 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
43 if (param_gethex(Cmd
, 1, bldata
, 8)) {
44 PrintAndLog("Block data must include 8 HEX symbols");
47 if (strchr(Cmd
,'w') != 0) {
53 PrintAndLog("Access Denied");
55 PrintAndLog("--specialblock no:%02x", blockNo
);
56 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
57 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
58 memcpy(d
.d
.asBytes
,bldata
, 4);
60 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
61 uint8_t isOK
= resp
.arg
[0] & 0xff;
62 PrintAndLog("isOk:%02x", isOK
);
64 PrintAndLog("Command execute timeout");
70 PrintAndLog("Access Denied");
72 PrintAndLog("--specialblock no:%02x", blockNo
);
73 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
74 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
75 memcpy(d
.d
.asBytes
,bldata
, 4);
77 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
78 uint8_t isOK
= resp
.arg
[0] & 0xff;
79 PrintAndLog("isOk:%02x", isOK
);
81 PrintAndLog("Command execute timeout");
87 PrintAndLog("Access Denied");
89 PrintAndLog("--specialblock no:%02x", blockNo
);
90 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
91 UsbCommand c
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
92 memcpy(c
.d
.asBytes
, bldata
, 4);
94 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
95 uint8_t isOK
= resp
.arg
[0] & 0xff;
96 PrintAndLog("isOk:%02x", isOK
);
98 PrintAndLog("Command execute timeout");
103 PrintAndLog("--specialblock no:%02x", blockNo
);
104 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
105 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
106 memcpy(d
.d
.asBytes
,bldata
, 4);
108 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
109 uint8_t isOK
= resp
.arg
[0] & 0xff;
110 PrintAndLog("isOk:%02x", isOK
);
112 PrintAndLog("Command execute timeout");
116 PrintAndLog("--block no:%02x", blockNo
);
117 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
118 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
119 memcpy(e
.d
.asBytes
,bldata
, 4);
121 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
122 uint8_t isOK
= resp
.arg
[0] & 0xff;
123 PrintAndLog("isOk:%02x", isOK
);
125 PrintAndLog("Command execute timeout");
133 // Mifare Ultralight Read Single Block
135 int CmdHF14AMfURdBl(const char *Cmd
){
140 PrintAndLog("Usage: hf mfu urdbl <block number>");
141 PrintAndLog(" sample: hfu mf urdbl 0");
145 blockNo
= param_get8(Cmd
, 0);
146 if (blockNo
>MAX_ULTRA_BLOCKS
){
147 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
150 PrintAndLog("--block no:%02x", (int)blockNo
);
151 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
155 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
156 uint8_t isOK
= resp
.arg
[0] & 0xff;
157 uint8_t * data
= resp
.d
.asBytes
;
160 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 4));
162 PrintAndLog("isOk:%02x", isOK
);
164 PrintAndLog("Command execute timeout");
170 // Mifare Ultralight Read (Dump) Card Contents
172 int CmdHF14AMfURdCard(const char *Cmd
){
176 uint8_t *lockbytes_t
=NULL
;
177 uint8_t lockbytes
[2]={0,0};
178 bool bit
[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
180 uint8_t datatemp
[5]={0,0,0,0,0};
183 uint8_t * data
= NULL
;
186 if (strchr(Cmd
,'x') != 0){
188 if ((fout
= fopen("dump_ultralight_data.bin","wb")) == NULL
) {
189 PrintAndLog("Could not create file name dumpdata.bin");
192 PrintAndLog("Dumping Ultralight Card Data...");
194 PrintAndLog("Attempting to Read Ultralight... ");
195 UsbCommand c
= {CMD_MIFAREU_READCARD
, {BlockNo
, Pages
}};
199 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
200 isOK
= resp
.arg
[0] & 0xff;
201 data
= resp
.d
.asBytes
;
202 PrintAndLog("isOk:%02x", isOK
);
204 for (i
= 0; i
< Pages
; i
++) {
208 lockbytes_t
=data
+(i
*4);
209 lockbytes
[0]=lockbytes_t
[2];
210 lockbytes
[1]=lockbytes_t
[3];
211 for(int j
=0; j
<16; j
++){
212 bit
[j
]=lockbytes
[j
/8] & ( 1 <<(7-j
%8));
214 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
215 memcpy(datatemp
,data
+ i
* 4,4);
216 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
219 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[4]);
220 memcpy(datatemp
,data
+ i
* 4,4);
221 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
224 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[3]);
225 memcpy(datatemp
,data
+ i
* 4,4);
226 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
229 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[2]);
230 memcpy(datatemp
,data
+ i
* 4,4);
231 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
234 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[1]);
235 memcpy(datatemp
,data
+ i
* 4,4);
236 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
239 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[0]);
240 memcpy(datatemp
,data
+ i
* 4,4);
241 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
244 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[15]);
245 memcpy(datatemp
,data
+ i
* 4,4);
246 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
249 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[14]);
250 memcpy(datatemp
,data
+ i
* 4,4);
251 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
254 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[13]);
255 memcpy(datatemp
,data
+ i
* 4,4);
256 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
259 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[12]);
260 memcpy(datatemp
,data
+ i
* 4,4);
261 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
264 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[11]);
265 memcpy(datatemp
,data
+ i
* 4,4);
266 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
269 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[10]);
270 memcpy(datatemp
,data
+ i
* 4,4);
271 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
274 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[9]);
275 memcpy(datatemp
,data
+ i
* 4,4);
276 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
279 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[8]);
280 memcpy(datatemp
,data
+ i
* 4,4);
281 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
284 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
285 memcpy(datatemp
,data
+ i
* 4,4);
286 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
291 PrintAndLog("Command1 execute timeout");
293 if (dump
) fclose(fout
);
297 int CmdHF14AMfUDump(const char *Cmd
){
301 uint8_t *lockbytes_t
=NULL
;
302 uint8_t lockbytes
[2]={0,0};
303 bool bit
[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
305 uint8_t datatemp
[5]={0,0,0,0,0};
308 uint8_t * data
= NULL
;
311 //if (strchr(Cmd,'x') != 0){
313 if ((fout
= fopen("dump_ultralight_data.bin","wb")) == NULL
) {
314 PrintAndLog("Could not create file name dumpdata.bin");
317 PrintAndLog("Dumping Ultralight Card Data...");
319 PrintAndLog("Attempting to Read Ultralight... ");
320 UsbCommand c
= {CMD_MIFAREU_READCARD
, {BlockNo
,Pages
}};
324 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
325 isOK
= resp
.arg
[0] & 0xff;
326 data
= resp
.d
.asBytes
;
327 PrintAndLog("isOk:%02x", isOK
);
329 for (i
= 0; i
< Pages
; i
++) {
333 lockbytes_t
=data
+(i
*4);
334 lockbytes
[0]=lockbytes_t
[2];
335 lockbytes
[1]=lockbytes_t
[3];
336 for(int j
=0; j
<16; j
++){
337 bit
[j
]=lockbytes
[j
/8] & ( 1 <<(7-j
%8));
339 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
340 memcpy(datatemp
,data
+ i
* 4,4);
341 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
344 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[4]);
345 memcpy(datatemp
,data
+ i
* 4,4);
346 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
349 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[3]);
350 memcpy(datatemp
,data
+ i
* 4,4);
351 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
354 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[2]);
355 memcpy(datatemp
,data
+ i
* 4,4);
356 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
359 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[1]);
360 memcpy(datatemp
,data
+ i
* 4,4);
361 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
364 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[0]);
365 memcpy(datatemp
,data
+ i
* 4,4);
366 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
369 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[15]);
370 memcpy(datatemp
,data
+ i
* 4,4);
371 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
374 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[14]);
375 memcpy(datatemp
,data
+ i
* 4,4);
376 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
379 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[13]);
380 memcpy(datatemp
,data
+ i
* 4,4);
381 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
384 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[12]);
385 memcpy(datatemp
,data
+ i
* 4,4);
386 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
389 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[11]);
390 memcpy(datatemp
,data
+ i
* 4,4);
391 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
394 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[10]);
395 memcpy(datatemp
,data
+ i
* 4,4);
396 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
399 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[9]);
400 memcpy(datatemp
,data
+ i
* 4,4);
401 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
404 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[8]);
405 memcpy(datatemp
,data
+ i
* 4,4);
406 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
409 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
410 memcpy(datatemp
,data
+ i
* 4,4);
411 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
416 PrintAndLog("Command1 execute timeout");
418 if (dump
) fclose(fout
);
422 //-------------------------------------------------------------------------------
423 // Ultralight C Methods
424 //-------------------------------------------------------------------------------
427 // Ultralight C Authentication Demo {currently uses hard-coded key}
429 int CmdHF14AMfucAuth(const char *Cmd
){
431 uint8_t blockNo
= 0, keyNo
=0;
434 unsigned char RndARndB
[16];
436 DES_cblock RndA
, RndB
;
437 DES_cblock iv
={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
438 DES_key_schedule ks1
,ks2
;
439 DES_cblock key1
,key2
;
442 PrintAndLog("Usage: hf mfu auth k <key number>");
443 PrintAndLog(" sample: hf mfu auth k 0");
447 //Change key to user defined one
448 if (strchr(Cmd
,'k') != 0){
450 keyNo
= param_get8(Cmd
, 1);
453 memcpy(key
,key1_blnk_data
,16);
456 memcpy(key
,key2_defa_data
,16);
459 memcpy(key
,key4_nfc_data
,16);
462 memcpy(key
,key3_3des_data
,16);
466 memcpy(key
,key3_3des_data
,16);
469 memcpy(key2
,key
+8,8);
470 DES_set_key((DES_cblock
*)key1
,&ks1
);
471 DES_set_key((DES_cblock
*)key2
,&ks2
);
474 UsbCommand c
= {CMD_MIFAREUC_AUTH1
, {blockNo
}};
477 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
478 uint8_t isOK
= resp
.arg
[0] & 0xff;
480 uint8_t * data
= resp
.d
.asBytes
;
483 PrintAndLog("enc(RndB):%s", sprint_hex(data
+1, 8));
484 memcpy(e_RndB
,data
+1,8);
487 PrintAndLog("Command execute timeout");
491 DES_random_key(&RndA
);
492 DES_ede2_cbc_encrypt(e_RndB
,RndB
,sizeof(e_RndB
),&ks1
,&ks2
,&iv
,0);
493 PrintAndLog(" RndB:%s",sprint_hex(RndB
, 8));
494 PrintAndLog(" RndA:%s",sprint_hex(RndA
, 8));
496 memcpy(RndARndB
,RndA
,8);
497 memcpy(RndARndB
+8,RndB
,8);
498 PrintAndLog(" RA+B:%s",sprint_hex(RndARndB
, 16));
499 DES_ede2_cbc_encrypt(RndARndB
,RndARndB
,sizeof(RndARndB
),&ks1
,&ks2
,&e_RndB
,1);
500 PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB
, 16));
503 UsbCommand d
= {CMD_MIFAREUC_AUTH2
, {cuid
}};
504 memcpy(d
.d
.asBytes
,RndARndB
, 16);
508 if (WaitForResponseTimeout(CMD_ACK
,&respb
,1500)) {
509 uint8_t isOK
= respb
.arg
[0] & 0xff;
510 uint8_t * data2
= respb
.d
.asBytes
;
513 PrintAndLog("enc(RndA'):%s", sprint_hex(data2
+1, 8));
517 PrintAndLog("Command execute timeout");
523 // Ultralight C Read Single Block
525 int CmdHF14AMfUCRdBl(const char *Cmd
)
531 PrintAndLog("Usage: hf mfu ucrdbl <block number>");
532 PrintAndLog(" sample: hf mfu ucrdbl 0");
536 blockNo
= param_get8(Cmd
, 0);
537 if (blockNo
>MAX_ULTRAC_BLOCKS
){
538 PrintAndLog("Error: Maximum number of readable blocks is 44 for Ultralight Cards!");
541 PrintAndLog("--block no:%02x", (int)blockNo
);
544 UsbCommand e
= {CMD_MIFAREU_READBL
, {blockNo
}};
547 if (WaitForResponseTimeout(CMD_ACK
,&resp_c
,1500)) {
548 uint8_t isOK
= resp_c
.arg
[0] & 0xff;
549 uint8_t * data
= resp_c
.d
.asBytes
;
551 PrintAndLog("isOk:%02x data:%s", isOK
, sprint_hex(data
, 4));
553 PrintAndLog("isOk:%02x", isOK
);
555 PrintAndLog("Command execute timeout");
561 // Ultralight C Read (or Dump) Card Contents
563 int CmdHF14AMfUCRdCard(const char *Cmd
){
567 uint8_t *lockbytes_t
=NULL
;
568 uint8_t lockbytes
[2]={0,0};
569 uint8_t *lockbytes_t2
=NULL
;
570 uint8_t lockbytes2
[2]={0,0};
571 bool bit
[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
572 bool bit2
[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
574 uint8_t datatemp
[5]={0,0,0,0,0};
577 uint8_t * data
= NULL
;
580 if (strchr(Cmd
,'x') != 0){
582 if ((fout
= fopen("dump_ultralightc_data.bin","wb")) == NULL
) {
583 PrintAndLog("Could not create file name dumpdata.bin");
586 PrintAndLog("Dumping Ultralight C Card Data...");
588 PrintAndLog("Attempting to Read Ultralight C... ");
589 UsbCommand c
= {CMD_MIFAREUC_READCARD
, {BlockNo
, Pages
}};
593 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
594 isOK
= resp
.arg
[0] & 0xff;
595 data
= resp
.d
.asBytes
;
596 //Pages=sizeof(data)/sizeof(data[0]);
597 PrintAndLog("isOk:%02x", isOK
);
599 for (i
= 0; i
< Pages
; i
++) {
603 lockbytes_t
=data
+(i
*4);
604 lockbytes
[0]=lockbytes_t
[2];
605 lockbytes
[1]=lockbytes_t
[3];
606 for(int j
=0; j
<16; j
++){
607 bit
[j
]=lockbytes
[j
/8] & ( 1 <<(7-j
%8));
609 //might as well read bottom lockbytes too
610 lockbytes_t2
=data
+(40*4);
611 lockbytes2
[0]=lockbytes_t2
[2];
612 lockbytes2
[1]=lockbytes_t2
[3];
613 for(int j
=0; j
<16; j
++){
614 bit2
[j
]=lockbytes2
[j
/8] & ( 1 <<(7-j
%8));
616 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
617 memcpy(datatemp
,data
+ i
* 4,4);
618 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
621 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[4]);
622 memcpy(datatemp
,data
+ i
* 4,4);
623 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
626 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[3]);
627 memcpy(datatemp
,data
+ i
* 4,4);
628 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
631 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[2]);
632 memcpy(datatemp
,data
+ i
* 4,4);
633 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
636 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[1]);
637 memcpy(datatemp
,data
+ i
* 4,4);
638 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
641 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[0]);
642 memcpy(datatemp
,data
+ i
* 4,4);
643 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
646 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[15]);
647 memcpy(datatemp
,data
+ i
* 4,4);
648 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
651 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[14]);
652 memcpy(datatemp
,data
+ i
* 4,4);
653 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
656 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[13]);
657 memcpy(datatemp
,data
+ i
* 4,4);
658 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
661 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[12]);
662 memcpy(datatemp
,data
+ i
* 4,4);
663 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
666 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[11]);
667 memcpy(datatemp
,data
+ i
* 4,4);
668 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
671 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[10]);
672 memcpy(datatemp
,data
+ i
* 4,4);
673 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
676 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[9]);
677 memcpy(datatemp
,data
+ i
* 4,4);
678 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
681 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[8]);
682 memcpy(datatemp
,data
+ i
* 4,4);
683 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
689 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[6]);
690 memcpy(datatemp
,data
+ i
* 4,4);
691 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
697 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[5]);
698 memcpy(datatemp
,data
+ i
* 4,4);
699 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
705 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[4]);
706 memcpy(datatemp
,data
+ i
* 4,4);
707 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
713 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[2]);
714 memcpy(datatemp
,data
+ i
* 4,4);
715 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
721 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[1]);
722 memcpy(datatemp
,data
+ i
* 4,4);
723 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
729 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[0]);
730 memcpy(datatemp
,data
+ i
* 4,4);
731 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
734 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[12]);
735 memcpy(datatemp
,data
+ i
* 4,4);
736 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
739 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[11]);
740 memcpy(datatemp
,data
+ i
* 4,4);
741 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
745 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[10]);
746 memcpy(datatemp
,data
+ i
* 4,4);
747 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
751 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[9]);
752 memcpy(datatemp
,data
+ i
* 4,4);
753 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
756 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
757 memcpy(datatemp
,data
+ i
* 4,4);
758 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
764 PrintAndLog("Command1 execute timeout");
766 if (dump
) fclose(fout
);
771 // Ultralight C Dump Card Contents to file
773 int CmdHF14AMfUCDump(const char *Cmd
){
777 uint8_t *lockbytes_t
=NULL
;
778 uint8_t lockbytes
[2]={0,0};
779 uint8_t *lockbytes_t2
=NULL
;
780 uint8_t lockbytes2
[2]={0,0};
781 bool bit
[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
782 bool bit2
[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
784 uint8_t datatemp
[5]={0,0,0,0,0};
787 uint8_t * data
= NULL
;
790 //if (strchr(Cmd,'x') != 0){
792 if ((fout
= fopen("dump_ultralightc_data.bin","wb")) == NULL
) {
793 PrintAndLog("Could not create file name dumpdata.bin");
796 PrintAndLog("Dumping Ultralight C Card Data...");
798 PrintAndLog("Attempting to Read Ultralight C... ");
799 UsbCommand c
= {CMD_MIFAREU_READCARD
, {BlockNo
,Pages
}};
803 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
804 isOK
= resp
.arg
[0] & 0xff;
805 data
= resp
.d
.asBytes
;
806 PrintAndLog("isOk:%02x", isOK
);
808 for (i
= 0; i
< Pages
; i
++) {
812 lockbytes_t
=data
+(i
*4);
813 lockbytes
[0]=lockbytes_t
[2];
814 lockbytes
[1]=lockbytes_t
[3];
815 for(int j
=0; j
<16; j
++){
816 bit
[j
]=lockbytes
[j
/8] & ( 1 <<(7-j
%8));
819 //might as well read bottom lockbytes too
820 lockbytes_t2
=data
+(40*4);
821 lockbytes2
[0]=lockbytes_t2
[2];
822 lockbytes2
[1]=lockbytes_t2
[3];
823 for(int j
=0; j
<16; j
++){
824 bit2
[j
]=lockbytes2
[j
/8] & ( 1 <<(7-j
%8));
827 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
828 memcpy(datatemp
,data
+ i
* 4,4);
829 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
832 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[4]);
833 memcpy(datatemp
,data
+ i
* 4,4);
834 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
837 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[3]);
838 memcpy(datatemp
,data
+ i
* 4,4);
839 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
842 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[2]);
843 memcpy(datatemp
,data
+ i
* 4,4);
844 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
847 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[1]);
848 memcpy(datatemp
,data
+ i
* 4,4);
849 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
852 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[0]);
853 memcpy(datatemp
,data
+ i
* 4,4);
854 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
857 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[15]);
858 memcpy(datatemp
,data
+ i
* 4,4);
859 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
862 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[14]);
863 memcpy(datatemp
,data
+ i
* 4,4);
864 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
867 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[13]);
868 memcpy(datatemp
,data
+ i
* 4,4);
869 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
872 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[12]);
873 memcpy(datatemp
,data
+ i
* 4,4);
874 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
877 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[11]);
878 memcpy(datatemp
,data
+ i
* 4,4);
879 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
882 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[10]);
883 memcpy(datatemp
,data
+ i
* 4,4);
884 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
887 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[9]);
888 memcpy(datatemp
,data
+ i
* 4,4);
889 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
892 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit
[8]);
893 memcpy(datatemp
,data
+ i
* 4,4);
894 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
900 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[6]);
901 memcpy(datatemp
,data
+ i
* 4,4);
902 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
908 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[5]);
909 memcpy(datatemp
,data
+ i
* 4,4);
910 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
916 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[4]);
917 memcpy(datatemp
,data
+ i
* 4,4);
918 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
924 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[2]);
925 memcpy(datatemp
,data
+ i
* 4,4);
926 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
932 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[1]);
933 memcpy(datatemp
,data
+ i
* 4,4);
934 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
940 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[0]);
941 memcpy(datatemp
,data
+ i
* 4,4);
942 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
945 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[12]);
946 memcpy(datatemp
,data
+ i
* 4,4);
947 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
950 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[11]);
951 memcpy(datatemp
,data
+ i
* 4,4);
952 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
956 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[10]);
957 memcpy(datatemp
,data
+ i
* 4,4);
958 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
962 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),bit2
[9]);
963 memcpy(datatemp
,data
+ i
* 4,4);
964 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
967 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
968 memcpy(datatemp
,data
+ i
* 4,4);
969 if (dump
) fwrite ( datatemp
, 1, 4, fout
);
975 PrintAndLog("Command1 execute timeout");
977 if (dump
) fclose(fout
);
982 // Mifare Ultralight C Write Single Block
984 int CmdHF14AMfUCWrBl(const char *Cmd
){
988 uint8_t bldata
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
992 PrintAndLog("Usage: hf mfu ucwrbl <block number> <block data (8 hex symbols)> [w]");
993 PrintAndLog(" sample: hf mfu uwrbl 0 01020304");
996 blockNo
= param_get8(Cmd
, 0);
997 if (blockNo
>(MAX_ULTRAC_BLOCKS
+4)){
998 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight Cards!");
1001 if (param_gethex(Cmd
, 1, bldata
, 8)) {
1002 PrintAndLog("Block data must include 8 HEX symbols");
1005 if (strchr(Cmd
,'w') != 0) {
1011 PrintAndLog("Access Denied");
1013 PrintAndLog("--specialblock no:%02x", blockNo
);
1014 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
1015 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1016 memcpy(d
.d
.asBytes
,bldata
, 4);
1018 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1019 uint8_t isOK
= resp
.arg
[0] & 0xff;
1020 PrintAndLog("isOk:%02x", isOK
);
1022 PrintAndLog("Command execute timeout");
1028 PrintAndLog("Access Denied");
1030 PrintAndLog("--specialblock no:%02x", blockNo
);
1031 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
1032 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1033 memcpy(d
.d
.asBytes
,bldata
, 4);
1035 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1036 uint8_t isOK
= resp
.arg
[0] & 0xff;
1037 PrintAndLog("isOk:%02x", isOK
);
1039 PrintAndLog("Command execute timeout");
1045 PrintAndLog("Access Denied");
1047 PrintAndLog("--specialblock no:%02x", blockNo
);
1048 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
1049 UsbCommand c
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1050 memcpy(c
.d
.asBytes
, bldata
, 4);
1052 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1053 uint8_t isOK
= resp
.arg
[0] & 0xff;
1054 PrintAndLog("isOk:%02x", isOK
);
1056 PrintAndLog("Command execute timeout");
1061 PrintAndLog("--specialblock no:%02x", blockNo
);
1062 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
1063 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1064 memcpy(d
.d
.asBytes
,bldata
, 4);
1066 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1067 uint8_t isOK
= resp
.arg
[0] & 0xff;
1068 PrintAndLog("isOk:%02x", isOK
);
1070 PrintAndLog("Command execute timeout");
1074 PrintAndLog("--block no:%02x", blockNo
);
1075 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
1076 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1077 memcpy(e
.d
.asBytes
,bldata
, 4);
1079 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1080 uint8_t isOK
= resp
.arg
[0] & 0xff;
1081 PrintAndLog("isOk:%02x", isOK
);
1083 PrintAndLog("Command execute timeout");
1090 //------------------------------------
1092 //------------------------------------
1093 static command_t CommandTable
[] =
1095 {"help", CmdHelp
, 1,"This help"},
1096 {"dbg", CmdHF14AMfDbg
, 0,"Set default debug mode"},
1097 {"urdbl", CmdHF14AMfURdBl
, 0,"Read MIFARE Ultralight block"},
1098 {"urdcard", CmdHF14AMfURdCard
, 0,"Read MIFARE Ultralight Card"},
1099 {"udump", CmdHF14AMfUDump
, 0,"Dump MIFARE Ultralight tag to binary file"},
1100 {"uwrbl", CmdHF14AMfUWrBl
, 0,"Write MIFARE Ultralight block"},
1101 {"ucrdbl", CmdHF14AMfUCRdBl
, 0,"Read MIFARE Ultralight C block"},
1102 {"ucrdcard",CmdHF14AMfUCRdCard
, 0,"Read MIFARE Ultralight C Card"},
1103 {"ucdump", CmdHF14AMfUCDump
, 0,"Dump MIFARE Ultralight C tag to binary file"},
1104 {"ucwrbl", CmdHF14AMfUCWrBl
, 0,"Write MIFARE Ultralight C block"},
1105 {"auth", CmdHF14AMfucAuth
, 0,"Ultralight C Authentication"},
1106 {NULL
, NULL
, 0, NULL
}
1109 int CmdHFMFUltra(const char *Cmd
){
1111 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1112 CmdsParse(CommandTable
, Cmd
);
1116 int CmdHelp(const char *Cmd
){
1117 CmdsHelp(CommandTable
);