]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmfu.c
ADD: "HF MFU INFO" Reading and printing of UL-EV1 configuration pages.
[proxmark3-svn] / client / cmdhfmfu.c
1 //-----------------------------------------------------------------------------
2 // Ultralight Code (c) 2013,2014 Midnitesnake & Andy Davies of Pentura
3 //
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
6 // the license.
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE ULTRALIGHT (C) commands
9 //-----------------------------------------------------------------------------
10 #include "loclass/des.h"
11 #include "cmdhfmfu.h"
12 #include "cmdhfmf.h"
13 #include "cmdhf14a.h"
14 #include "mifare.h"
15 #include "util.h"
16 #include "../common/protocols.h"
17
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
25
26 typedef enum TAGTYPE_UL {
27 UNKNOWN = 0,
28 UL = 1,
29 UL_C = 2,
30 UL_EV1_48 = 4,
31 UL_EV1_128 = 8,
32 NTAG = 16,
33 NTAG_213 = 32,
34 NTAG_215 = 64,
35 NTAG_216 = 128,
36 MAGIC = 256,
37 UL_MAGIC = UL | MAGIC,
38 UL_C_MAGIC = UL_C | MAGIC,
39 UL_ERROR = 0xFFFF,
40
41 } TagTypeUL_t;
42
43 uint8_t default_3des_keys[7][16] = {
44 { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 },// 3des std key
45 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },// all zeroes
46 { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f },// 0x00-0x0F
47 { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 },// NFC-key
48 { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },// all ones
49 { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF },// all FF
50 { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF } // 11 22 33
51 };
52
53 static int CmdHelp(const char *Cmd);
54
55 char* getProductTypeStr( uint8_t id){
56
57 static char buf[20];
58 char *retStr = buf;
59
60 switch(id) {
61 case 3:
62 sprintf(retStr, "0x%02X %s", id, "(Ultralight)");
63 break;
64 case 4:
65 sprintf(retStr, "0x%02X %s", id, "(NTAG)");
66 break;
67 default:
68 sprintf(retStr, "0x%02X %s", id, "(unknown)");
69 break;
70 }
71 return buf;
72 }
73
74 /*
75 The 7 MSBits (=n) code the storage size itself based on 2^n,
76 the LSBit is set to '0' if the size is exactly 2^n
77 and set to '1' if the storage size is between 2^n and 2^(n+1).
78 */
79 char* getUlev1CardSizeStr( uint8_t fsize ){
80
81 static char buf[30];
82 char *retStr = buf;
83
84 uint8_t usize = 1 << ((fsize >>1) + 1);
85 uint8_t lsize = 1 << (fsize >>1);
86
87 // is LSB set?
88 if ( fsize & 1 )
89 sprintf(retStr, "0x%02X (%d - %d bytes)",fsize, usize, lsize);
90 else
91 sprintf(retStr, "0x%02X (%d bytes)", fsize, lsize);
92 return buf;
93 }
94
95 static void ul_switch_on_field(void) {
96 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}};
97 SendCommand(&c);
98 }
99
100 static void ul_switch_off_field(void) {
101 UsbCommand c = {CMD_READER_ISO_14443a, {0, 0, 0}};
102 SendCommand(&c);
103 }
104
105 static int ul_send_cmd_raw( uint8_t *cmd, uint8_t cmdlen, uint8_t *response, uint16_t responseLength ) {
106 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT | ISO14A_APPEND_CRC, cmdlen, 0}};
107 memcpy(c.d.asBytes, cmd, cmdlen);
108 SendCommand(&c);
109 UsbCommand resp;
110 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
111
112 uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength;
113 if (resp.arg[0] > 0)
114 memcpy(response, resp.d.asBytes, resplen);
115
116 return resplen;
117 }
118
119 static int ul_send_cmd_raw_crc( uint8_t *cmd, uint8_t cmdlen, uint8_t *response, uint16_t responseLength, bool append_crc ) {
120
121 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT , cmdlen, 0}};
122 if (append_crc)
123 c.arg[0] |= ISO14A_APPEND_CRC;
124
125 memcpy(c.d.asBytes, cmd, cmdlen);
126 SendCommand(&c);
127 UsbCommand resp;
128 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
129
130 uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength;
131 if (resp.arg[0] > 0)
132 memcpy(response, resp.d.asBytes, resplen );
133
134 return resplen;
135 }
136
137 static int ul_select( iso14a_card_select_t *card ){
138
139 ul_switch_on_field();
140
141 UsbCommand resp;
142 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
143 ul_switch_off_field();
144 return -1;
145 }
146
147 if (resp.arg[0] > 0) {
148 memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));
149 }
150 return resp.arg[0];
151 }
152
153 // This read command will at least return 16bytes.
154 static int ul_read( uint8_t page, uint8_t *response, uint16_t responseLength ){
155
156 uint8_t cmd[] = {ISO14443A_CMD_READBLOCK, page};
157 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
158 if ( len == -1 )
159 ul_switch_off_field();
160 return len;
161 }
162
163 static int ulc_requestAuthentication( uint8_t blockNo, uint8_t *nonce, uint16_t nonceLength ){
164
165 uint8_t cmd[] = {MIFARE_ULC_AUTH_1, blockNo};
166 int len = ul_send_cmd_raw(cmd, sizeof(cmd), nonce, nonceLength);
167 if ( len == -1 )
168 ul_switch_off_field();
169 return len;
170 }
171
172 static int ulev1_requestAuthentication( uint8_t *pwd, uint8_t *pack, uint16_t packLength ){
173
174 uint8_t cmd[] = {MIFARE_ULEV1_AUTH, pwd[0], pwd[1], pwd[2], pwd[3]};
175 int len = ul_send_cmd_raw(cmd, sizeof(cmd), pack, packLength);
176 if ( len == -1)
177 ul_switch_off_field();
178 return len;
179 }
180
181 static int ulev1_getVersion( uint8_t *response, uint16_t responseLength ){
182
183 uint8_t cmd[] = {MIFARE_ULEV1_VERSION};
184 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
185 if ( len == -1 )
186 ul_switch_off_field();
187 return len;
188 }
189
190 // static int ulev1_fastRead( uint8_t startblock, uint8_t endblock, uint8_t *response ){
191
192 // uint8_t cmd[] = {MIFARE_ULEV1_FASTREAD, startblock, endblock};
193
194 // if ( !ul_send_cmd_raw(cmd, sizeof(cmd), response)){
195 // ul_switch_off_field();
196 // return -1;
197 // }
198 // return 0;
199 // }
200
201 static int ulev1_readCounter( uint8_t counter, uint8_t *response, uint16_t responseLength ){
202
203 uint8_t cmd[] = {MIFARE_ULEV1_READ_CNT, counter};
204 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
205 if (len == -1)
206 ul_switch_off_field();
207 return len;
208 }
209
210 static int ul_print_CC(uint8_t *data) {
211 if(data[0] != 0xe1) {
212 PrintAndLog("no NDEF message");
213 return -1; // no NDEF message
214 }
215
216 PrintAndLog("Capability Container: %s", sprint_hex(data,4) );
217 PrintAndLog(" %02X: NDEF Magic Number", data[0]);
218 PrintAndLog(" %02X: version %d.%d supported by tag", data[1], (data[1] & 0xF0) >> 4, data[1] & 0x0f);
219 PrintAndLog(" %02X: Physical Memory Size of this tag: %d bytes", data[2], (data[2] + 1) * 8);
220 PrintAndLog(" %02X: %s / %s", data[3],
221 (data[3] & 0xF0) ? "(RFU)" : "Read access granted without any security",
222 (data[3] & 0x0F)==0 ? "Write access granted without any security" : (data[3] & 0x0F)==0x0F ? "No write access granted at all" : "(RFU)");
223 return 0;
224 }
225
226 static int ul_print_version(uint8_t *data){
227 PrintAndLog("Raw version bytes: %s", sprint_hex(data, 8) );
228 PrintAndLog(" Vendor ID : 0x%02X, Manufacturer: %s", data[1], getTagInfo(data[1]));
229 PrintAndLog(" Product type : %s" , getProductTypeStr(data[2]));
230 PrintAndLog(" Product subtype : 0x%02X %s" , data[3], (data[3]==1) ?"17 pF":"50pF");
231 PrintAndLog(" Major version : 0x%02X" , data[4]);
232 PrintAndLog(" Minor version : 0x%02X" , data[5]);
233 PrintAndLog(" Size : %s", getUlev1CardSizeStr(data[6]));
234 PrintAndLog(" Protocol type : 0x%02X" , data[7]);
235 return 0;
236 }
237
238 static int ul_print_type(uint16_t tagtype){
239 if ( tagtype & UL )
240 PrintAndLog(" TYPE : MIFARE Ultralight (MF0ICU1) %s", (tagtype & MAGIC)?"<magic>":"");
241 else if ( tagtype & UL_C)
242 PrintAndLog(" TYPE : MIFARE Ultralight C (MF0ULC) %s", (tagtype & MAGIC)?"<magic>":"" );
243 else if ( tagtype & UL_EV1_48)
244 PrintAndLog(" TYPE : MIFARE Ultralight EV1 48bytes (MF0UL1101)");
245 else if ( tagtype & UL_EV1_128)
246 PrintAndLog(" TYPE : MIFARE Ultralight EV1 128bytes (MF0UL2101");
247 else if ( tagtype & NTAG_213 )
248 PrintAndLog(" TYPE : MIFARE NTAG 213 144bytes (NT2H1311G0DU)");
249 else if ( tagtype & NTAG_215 )
250 PrintAndLog(" TYPE : MIFARE NTAG 215 504bytes (NT2H1511G0DU)");
251 else if ( tagtype & NTAG_216 )
252 PrintAndLog(" TYPE : MIFARE NTAG 216 888bytes (NT2H1611G0DU)");
253 else
254 PrintAndLog(" TYPE : Unknown %04x",tagtype);
255 return 0;
256 }
257
258 static int ulc_print_3deskey( uint8_t *data){
259 PrintAndLog(" deskey1 [44/0x2C]: %s", sprint_hex(data ,4));
260 PrintAndLog(" deskey1 [45/0x2D]: %s", sprint_hex(data+4 ,4));
261 PrintAndLog(" deskey2 [46/0x2E]: %s", sprint_hex(data+8 ,4));
262 PrintAndLog(" deskey2 [47/0x2F]: %s", sprint_hex(data+12,4));
263 PrintAndLog(" 3des key : %s", sprint_hex(SwapEndian64(data, 16), 16));
264 return 0;
265 }
266
267 static int ulc_print_configuration( uint8_t *data){
268
269 PrintAndLog("--- UL-C Configuration");
270 PrintAndLog(" Higher Lockbits [40/0x28]: %s %s", sprint_hex(data, 4), printBits(2, data));
271 PrintAndLog(" Counter [41/0x29]: %s %s", sprint_hex(data+4, 4), printBits(2, data+4));
272
273 bool validAuth = (data[8] >= 0x03 && data[8] <= 0x30);
274 if ( validAuth )
275 PrintAndLog(" Auth0 [42/0x2A]: %s - Pages above %d needs authentication", sprint_hex(data+8, 4), data[8] );
276 else{
277 if ( data[8] == 0){
278 PrintAndLog(" Auth0 [42/0x2A]: %s - default", sprint_hex(data+8, 4) );
279 } else {
280 PrintAndLog(" Auth0 [42/0x2A]: %s - auth byte is out-of-range", sprint_hex(data+8, 4) );
281 }
282 }
283 PrintAndLog(" Auth1 [43/0x2B]: %s - %s",
284 sprint_hex(data+12, 4),
285 (data[12] & 1) ? "write access restricted": "read and write access restricted"
286 );
287 return 0;
288 }
289
290 static int ulev1_print_configuration( uint8_t *data){
291
292 PrintAndLog("\n--- UL-EV1 Configuration");
293
294 bool strg_mod_en = (data[0] & 2);
295 uint8_t authlim = (data[4] & 0x07);
296 bool cfglck = (data[4] & 0x40);
297 bool prot = (data[4] & 0x80);
298 uint8_t vctid = data[5];
299
300 PrintAndLog(" cfg0 [16/0x10]: %s", sprint_hex(data, 4));
301 PrintAndLog(" - pages above %d needs authentication",data[3]);
302 PrintAndLog(" - strong modulation mode %s", (strg_mod_en) ? "enabled":"disabled");
303 PrintAndLog(" cfg1 [17/0x11]: %s", sprint_hex(data+4, 4) );
304 if ( authlim == 0)
305 PrintAndLog(" - Max number of password attempts is unlimited");
306 else
307 PrintAndLog(" - Max number of password attempts is %d", authlim);
308 PrintAndLog(" - user configuration %s", cfglck ? "permanently locked":"writeable");
309 PrintAndLog(" - %s access is protected with password", prot ? "read and write":"write");
310 PrintAndLog(" 0x%02X - Virtual Card Type Identifier is %s default", vctid, (vctid==0x05)? "":"not");
311 PrintAndLog(" PWD [18/0x12]: %s", sprint_hex(data+8, 4));
312 PrintAndLog(" PACK [19/0x13]: %s", sprint_hex(data+12, 4));
313 return 0;
314 }
315
316 uint16_t GetHF14AMfU_Type(void){
317
318 TagTypeUL_t tagtype = UNKNOWN;
319 iso14a_card_select_t card;
320 uint8_t version[10] = {0x00};
321 uint8_t nonce1[11] = {0x00};
322 uint8_t nonce2[11] = {0x00};
323 int status = 0;
324 int len;
325
326 status = ul_select(&card);
327 if ( status < 1 ){
328 PrintAndLog("Error: couldn't select");
329 return UL_ERROR;
330 }
331
332 // Ultralight - ATQA / SAK
333 if ( card.atqa[1] != 0x00 && card.atqa[0] != 0x44 && card.sak != 0x00 ) {
334 ul_switch_off_field();
335 return UL_ERROR;
336 }
337
338 len = ulev1_getVersion(version, sizeof(version));
339
340 switch (len) {
341 case -1:
342 tagtype = (UL | UL_C);
343 break;
344 case 0x0A: {
345
346 if ( version[2] == 0x03 && version[6] == 0x0B )
347 tagtype = UL_EV1_48;
348 else if ( version[2] == 0x03 && version[6] != 0x0B )
349 tagtype = UL_EV1_128;
350 else if ( version[2] == 0x04 && version[6] == 0x0F )
351 tagtype = NTAG_213;
352 else if ( version[2] == 0x04 && version[6] != 0x11 )
353 tagtype = NTAG_215;
354 else if ( version[2] == 0x04 && version[6] == 0x13 )
355 tagtype = NTAG_216;
356 else if ( version[2] == 0x04 )
357 tagtype = NTAG;
358
359 break;
360 }
361 case 0x01:{
362 tagtype = UL_C;
363 break;
364 }
365 case 0x00: {
366 tagtype = UL;
367 break;
368 }
369 default :{
370 tagtype = UNKNOWN;
371 break;
372 }
373 }
374
375 if ((tagtype & ( UL_C | UL ))) {
376 // Magic UL-C, by observation,
377 // it seems to have a static nonce response to 0x1A command.
378 status = ul_select(&card);
379 status = ulc_requestAuthentication(0, nonce1, sizeof(nonce1));
380 if ( status > 0 ) {
381
382 status = ulc_requestAuthentication(0, nonce2, sizeof(nonce2));
383
384 tagtype =( !memcmp(nonce1, nonce2, 11) ) ? UL_C_MAGIC : UL_C;
385
386 } else {
387 tagtype = UL;
388 }
389 }
390
391 //PrintAndLog("ICE %d", tagtype);
392 //Magic Ultralight test here. It takes present UID, and tries to write it back.
393 if ( (tagtype & UL) ){
394 // read 3des key or PWD,
395 // if response bytes == all zeros its a NORMAL tag.
396 //return UL_MAGIC;
397 }
398
399 ul_switch_off_field();
400 return tagtype;
401 }
402
403 int CmdHF14AMfUInfo(const char *Cmd){
404
405 uint8_t datatemp[7] = {0x00};
406 uint8_t data[16] = {0x00};
407 iso14a_card_select_t card;
408 uint8_t *key;
409 int status;
410
411 PrintAndLog("\n--- Tag Information ---------");
412 PrintAndLog("-------------------------------------------------------------");
413
414 TagTypeUL_t tagtype = GetHF14AMfU_Type();
415 if (tagtype == UL_ERROR) return -1;
416
417 ul_print_type(tagtype);
418
419 status = ul_select(&card);
420 if ( status < 1 ){
421 PrintAndLog("Error: couldn't select");
422 ul_switch_off_field();
423 return status;
424 }
425
426 // read pages 0,1,2,4 (should read 4pages)
427 status = ul_read(0, data, sizeof(data));
428 if ( status == -1 ){
429 PrintAndLog("Error: tag didn't answer to READ");
430 ul_switch_off_field();
431 return status;
432 }
433
434 // UID
435 memcpy( datatemp, data, 3);
436 memcpy( datatemp+3, data+4, 4);
437
438 PrintAndLog(" UID : %s ", sprint_hex(datatemp, 7));
439 PrintAndLog(" UID[0] : (Manufacturer Byte) = %02x, Manufacturer: %s", datatemp[0], getTagInfo(datatemp[0]) );
440
441 // BBC
442 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
443 int crc0 = 0x88 ^ data[0] ^ data[1] ^data[2];
444 if ( data[3] == crc0 )
445 PrintAndLog(" BCC0 : 0x%02X - Ok", data[3]);
446 else
447 PrintAndLog(" BCC0 : 0x%02X - crc should be %02x", data[3], crc0);
448
449 int crc1 = data[4] ^ data[5] ^ data[6] ^data[7];
450 if ( data[8] == crc1 )
451 PrintAndLog(" BCC1 : 0x%02X - Ok", data[8]);
452 else
453 PrintAndLog(" BCC1 : 0x%02X - crc should be 0x%02X", data[8], crc1 );
454
455 PrintAndLog(" Internal : 0x%02X - %s default", data[9], (data[9]==0x48)?"":"not" );
456
457 memcpy(datatemp, data+10, 2);
458 PrintAndLog(" Lock : %s - %s", sprint_hex(datatemp, 2),printBits( 2, &datatemp) );
459 PrintAndLog("OneTimePad : %s ", sprint_hex(data + 3*4, 4));
460 PrintAndLog("");
461
462 if ((tagtype & UL_C)){
463
464 // read pages 0x28, 0x29, 0x2A, 0x2B
465 uint8_t ulc_conf[16] = {0x00};
466 status = ul_read(0x28, ulc_conf, sizeof(ulc_conf));
467 if ( status == -1 ){
468 PrintAndLog("Error: tag didn't answer to READ");
469 ul_switch_off_field();
470 return status;
471 }
472
473 ulc_print_configuration(ulc_conf);
474
475 if ((tagtype & UL_C_MAGIC)){
476
477 uint8_t ulc_deskey[16] = {0x00};
478 status = ul_read(0x2C, ulc_deskey, sizeof(ulc_deskey));
479 if ( status == -1 ){
480 PrintAndLog("Error: tag didn't answer to READ");
481 ul_switch_off_field();
482 return status;
483 }
484
485 ulc_print_3deskey(ulc_deskey);
486
487 }
488 else {
489 PrintAndLog("Trying some default 3des keys");
490 for (uint8_t i = 0; i < 7; ++i ){
491 key = default_3des_keys[i];
492 if (try3DesAuthentication(key) == 1){
493 PrintAndLog("Found default 3des key: %s", sprint_hex(key,16));
494 return 0;
495 }
496 }
497 }
498 }
499
500 if ((tagtype & (UL_EV1_48 | UL_EV1_128))) {
501
502 PrintAndLog("--- UL-EV1 Counters");
503 uint8_t counter[3] = {0,0,0};
504 for ( uint8_t i = 0; i<3; ++i) {
505 ulev1_readCounter(i,counter, sizeof(counter) );
506 PrintAndLog("Counter[%d] :: %s", i, sprint_hex(counter,3));
507 }
508
509
510
511 uint8_t startconfigblock = (tagtype & UL_EV1_48) ? 0x10 : 0x24;
512 uint8_t ulev1_conf[16] = {0x00};
513 status = ul_read(startconfigblock, ulev1_conf, sizeof(ulev1_conf));
514 if ( status == -1 ){
515 PrintAndLog("Error: tag didn't answer to READ");
516 ul_switch_off_field();
517 return status;
518 }
519
520 ulev1_print_configuration(ulev1_conf);
521 }
522
523 if ((tagtype & (UL_EV1_48 | UL_EV1_128 | NTAG_213 | NTAG_215 | NTAG_216))) {
524
525 PrintAndLog("\n--- UL-EV1 / NTAG Version");
526
527 uint8_t version[10] = {0x00};
528 status = ulev1_getVersion(version, sizeof(version));
529 if ( status == -1 ){
530 PrintAndLog("Error: tag didn't answer to GETVERSION");
531 ul_switch_off_field();
532 return status;
533 }
534 ul_print_version(version);
535
536 //********** TODO ********************************
537 // --problem, there is a failed pwd tries counter in UL-EV1
538 PrintAndLog("\nTrying some known EV1/NTAG passwords.");
539
540 uint8_t password[4] ={0xff,0xff,0xff,0xff};
541 uint8_t pack[4] = {0,0,0,0};
542 status = ulev1_requestAuthentication(password, pack, sizeof(pack));
543 if ( status == -1 ){
544 PrintAndLog("Error: tag didn't answer to AUTHENTICATE");
545 ul_switch_off_field();
546 return status;
547 }
548 PrintAndLog("Found default password: %s",sprint_hex(password, sizeof(password)));
549 PrintAndLog("Got PACK : %s", sprint_hex(pack,sizeof(pack)));
550 }
551
552 if ((tagtype & (NTAG_213 | NTAG_215 | NTAG_216))){
553
554 PrintAndLog("\n--- NTAG NDEF Message");
555 uint8_t cc[16] = {0x00};
556 status = ul_read(2, cc, sizeof(cc));
557 if ( status == -1 ){
558 PrintAndLog("Error: tag didn't answer to READ");
559 ul_switch_off_field();
560 return status;
561 }
562 ul_print_CC(cc);
563 }
564
565 ul_switch_off_field();
566 return 0;
567 }
568
569 //
570 // Mifare Ultralight Write Single Block
571 //
572 int CmdHF14AMfUWrBl(const char *Cmd){
573 uint8_t blockNo = -1;
574 bool chinese_card = FALSE;
575 uint8_t bldata[16] = {0x00};
576 UsbCommand resp;
577
578 char cmdp = param_getchar(Cmd, 0);
579 if (strlen(Cmd) < 3 || cmdp == 'h' || cmdp == 'H') {
580 PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]");
581 PrintAndLog(" [block number]");
582 PrintAndLog(" [block data] - (8 hex symbols)");
583 PrintAndLog(" [w] - Chinese magic ultralight tag");
584 PrintAndLog("");
585 PrintAndLog(" sample: hf mfu wrbl 0 01020304");
586 PrintAndLog("");
587 return 0;
588 }
589
590 blockNo = param_get8(Cmd, 0);
591
592 if (blockNo > MAX_UL_BLOCKS){
593 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
594 return 1;
595 }
596
597 if (param_gethex(Cmd, 1, bldata, 8)) {
598 PrintAndLog("Block data must include 8 HEX symbols");
599 return 1;
600 }
601
602 if (strchr(Cmd,'w') != 0 || strchr(Cmd,'W') != 0 ) {
603 chinese_card = TRUE;
604 }
605
606 if ( blockNo <= 3) {
607 if (!chinese_card){
608 PrintAndLog("Access Denied");
609 } else {
610 PrintAndLog("--specialblock no:%02x", blockNo);
611 PrintAndLog("--data: %s", sprint_hex(bldata, 4));
612 UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};
613 memcpy(d.d.asBytes,bldata, 4);
614 SendCommand(&d);
615 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
616 uint8_t isOK = resp.arg[0] & 0xff;
617 PrintAndLog("isOk:%02x", isOK);
618 } else {
619 PrintAndLog("Command execute timeout");
620 }
621 }
622 } else {
623 PrintAndLog("--block no:%02x", blockNo);
624 PrintAndLog("--data: %s", sprint_hex(bldata, 4));
625 UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}};
626 memcpy(e.d.asBytes,bldata, 4);
627 SendCommand(&e);
628 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
629 uint8_t isOK = resp.arg[0] & 0xff;
630 PrintAndLog("isOk:%02x", isOK);
631 } else {
632 PrintAndLog("Command execute timeout");
633 }
634 }
635 return 0;
636 }
637
638 //
639 // Mifare Ultralight Read Single Block
640 //
641 int CmdHF14AMfURdBl(const char *Cmd){
642
643 UsbCommand resp;
644 uint8_t blockNo = -1;
645 char cmdp = param_getchar(Cmd, 0);
646
647 if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') {
648 PrintAndLog("Usage: hf mfu rdbl <block number>");
649 PrintAndLog(" sample: hfu mfu rdbl 0");
650 return 0;
651 }
652
653 blockNo = param_get8(Cmd, 0);
654
655 if (blockNo > MAX_UL_BLOCKS){
656 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight");
657 return 1;
658 }
659
660 UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}};
661 SendCommand(&c);
662
663
664 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
665 uint8_t isOK = resp.arg[0] & 0xff;
666 if (isOK) {
667 uint8_t *data = resp.d.asBytes;
668 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo, blockNo, sprint_hex(data, 4));
669 }
670 else {
671 PrintAndLog("Failed reading block: (%02x)", isOK);
672 }
673 } else {
674 PrintAndLog("Command execute time-out");
675 }
676
677 return 0;
678 }
679
680 int usage_hf_mfu_dump(void)
681 {
682 PrintAndLog("Reads all pages from Ultralight, Ultralight-C, Ultralight EV1");
683 PrintAndLog("and saves binary dump into the file `filename.bin` or `cardUID.bin`");
684 PrintAndLog("It autodetects card type.\n");
685 PrintAndLog("Usage: hf mfu dump k <key> n <filename w/o .bin>");
686 PrintAndLog(" sample : hf mfu dump");
687 PrintAndLog(" : hf mfu dump n myfile");
688 return 0;
689 }
690 //
691 // Mifare Ultralight / Ultralight-C / Ultralight-EV1
692 // Read and Dump Card Contents, using auto detection of tag size.
693 //
694 // TODO: take a password to read UL-C / UL-EV1 tags.
695 int CmdHF14AMfUDump(const char *Cmd){
696
697 FILE *fout;
698 char filename[FILE_PATH_SIZE] = {0x00};
699 char * fnameptr = filename;
700 char* str = "Dumping Ultralight%s%s Card Data...";
701 uint8_t *lockbytes_t = NULL;
702 uint8_t lockbytes[2] = {0x00};
703 uint8_t *lockbytes_t2 = NULL;
704 uint8_t lockbytes2[2] = {0x00};
705 bool bit[16] = {0x00};
706 bool bit2[16] = {0x00};
707 uint8_t data[1024] = {0x00};
708 bool hasPwd = false;
709 int i = 0;
710 int Pages = 16;
711 bool tmplockbit = false;
712 uint8_t dataLen=0;
713 uint8_t cmdp =0;
714 //uint8_t key[16]= {0x00};
715 uint8_t *key = NULL;
716 size_t fileNlen = 0;
717 bool errors = FALSE;
718
719 while(param_getchar(Cmd, cmdp) != 0x00)
720 {
721 switch(param_getchar(Cmd, cmdp))
722 {
723 case 'h':
724 case 'H':
725 return usage_hf_mfu_dump();
726 case 'k':
727 case 'K':
728 dataLen = param_gethex(Cmd, cmdp+1, data, 32);
729 if (dataLen) {
730 errors = true;
731 } else {
732 //memcpy(key, data, 16);
733 key = SwapEndian64(data, 16);
734 PrintAndLog("3des key: %s",sprint_hex(key, 16));
735 }
736 cmdp += 2;
737 hasPwd = true;
738 break;
739 case 'n':
740 case 'N':
741 fileNlen = param_getstr(Cmd, cmdp+1, filename);
742 if (!fileNlen) errors = true;
743 if (fileNlen > FILE_PATH_SIZE-5) fileNlen = FILE_PATH_SIZE-5;
744 cmdp += 2;
745 break;
746 default:
747 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
748 errors = true;
749 break;
750 }
751 if(errors) break;
752 }
753
754 //Validations
755 if(errors) return usage_hf_mfu_dump();
756
757 TagTypeUL_t tagtype = GetHF14AMfU_Type();
758 if (tagtype == UL_ERROR) return -1;
759
760 if ( tagtype & UL ) {
761 Pages = 16;
762 PrintAndLog(str,"", (tagtype & MAGIC)?" (magic)":"" );
763 }
764 else if ( tagtype & UL_C ) {
765 Pages = 44;
766 PrintAndLog(str,"-C", (tagtype & MAGIC)?" (magic)":"" );
767 }
768 else if ( tagtype & UL_EV1_48 ) {
769 Pages = 18;
770 PrintAndLog(str," EV1_48","");
771 }
772 else if ( tagtype & UL_EV1_128 ) {
773 Pages = 32;
774 PrintAndLog(str," EV1_128","");
775 } else {
776 Pages = 16;
777 PrintAndLog("Dumping unknown Ultralight, using default values.");
778 }
779
780 UsbCommand c = {CMD_MIFAREUC_READCARD, {0,Pages}};
781 if ( hasPwd ) {
782 c.arg[2] = 1;
783 memcpy(c.d.asBytes, key, 16);
784 }
785 SendCommand(&c);
786 UsbCommand resp;
787 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
788 PrintAndLog("Command execute time-out");
789 return 1;
790 }
791
792 uint8_t isOK = resp.arg[0] & 0xff;
793 if (isOK) {
794 memcpy(data, resp.d.asBytes, resp.arg[1]);
795 } else {
796 PrintAndLog("Failed reading block: (%02x)", i);
797 return 1;
798 }
799
800 // Load lock bytes.
801 int j = 0;
802
803 lockbytes_t = data + 8;
804 lockbytes[0] = lockbytes_t[2];
805 lockbytes[1] = lockbytes_t[3];
806 for(j = 0; j < 16; j++){
807 bit[j] = lockbytes[j/8] & ( 1 <<(7-j%8));
808 }
809
810 // Load bottom lockbytes if available
811 if ( Pages == 44 ) {
812 lockbytes_t2 = data + (40*4);
813 lockbytes2[0] = lockbytes_t2[2];
814 lockbytes2[1] = lockbytes_t2[3];
815 for (j = 0; j < 16; j++) {
816 bit2[j] = lockbytes2[j/8] & ( 1 <<(7-j%8));
817 }
818 }
819
820 // add keys
821 if (hasPwd){
822 memcpy(data + Pages*4, key, 16);
823 Pages += 4;
824 }
825 for (i = 0; i < Pages; ++i) {
826 if ( i < 3 ) {
827 PrintAndLog("Block %02x:%s ", i,sprint_hex(data + i * 4, 4));
828 continue;
829 }
830 switch(i){
831 case 3: tmplockbit = bit[4]; break;
832 case 4: tmplockbit = bit[3]; break;
833 case 5: tmplockbit = bit[2]; break;
834 case 6: tmplockbit = bit[1]; break;
835 case 7: tmplockbit = bit[0]; break;
836 case 8: tmplockbit = bit[15]; break;
837 case 9: tmplockbit = bit[14]; break;
838 case 10: tmplockbit = bit[13]; break;
839 case 11: tmplockbit = bit[12]; break;
840 case 12: tmplockbit = bit[11]; break;
841 case 13: tmplockbit = bit[10]; break;
842 case 14: tmplockbit = bit[9]; break;
843 case 15: tmplockbit = bit[8]; break;
844 case 16:
845 case 17:
846 case 18:
847 case 19: tmplockbit = bit2[6]; break;
848 case 20:
849 case 21:
850 case 22:
851 case 23: tmplockbit = bit2[5]; break;
852 case 24:
853 case 25:
854 case 26:
855 case 27: tmplockbit = bit2[4]; break;
856 case 28:
857 case 29:
858 case 30:
859 case 31: tmplockbit = bit2[2]; break;
860 case 32:
861 case 33:
862 case 34:
863 case 35: tmplockbit = bit2[1]; break;
864 case 36:
865 case 37:
866 case 38:
867 case 39: tmplockbit = bit2[0]; break;
868 case 40: tmplockbit = bit2[12]; break;
869 case 41: tmplockbit = bit2[11]; break;
870 case 42: tmplockbit = bit2[10]; break; //auth0
871 case 43: tmplockbit = bit2[9]; break; //auth1
872 default: break;
873 }
874 PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),tmplockbit);
875 }
876
877 // user supplied filename?
878 if (fileNlen < 1) {
879 // UID = data 0-1-2 4-5-6-7 (skips a beat)
880 sprintf(fnameptr,"%02X%02X%02X%02X%02X%02X%02X.bin",
881 data[0], data[1], data[2], data[4], data[5], data[6], data[7]);
882 } else {
883 sprintf(fnameptr + fileNlen," .bin");
884 }
885
886 if ((fout = fopen(filename,"wb")) == NULL) {
887 PrintAndLog("Could not create file name %s", filename);
888 return 1;
889 }
890 fwrite( data, 1, Pages*4, fout );
891 fclose(fout);
892
893 PrintAndLog("Dumped %d pages, wrote %d bytes to %s", Pages, Pages*4, filename);
894 return 0;
895 }
896
897 // Needed to Authenticate to Ultralight C tags
898 void rol (uint8_t *data, const size_t len){
899 uint8_t first = data[0];
900 for (size_t i = 0; i < len-1; i++) {
901 data[i] = data[i+1];
902 }
903 data[len-1] = first;
904 }
905
906 //-------------------------------------------------------------------------------
907 // Ultralight C Methods
908 //-------------------------------------------------------------------------------
909
910 //
911 // Ultralight C Authentication Demo {currently uses hard-coded key}
912 //
913 int CmdHF14AMfucAuth(const char *Cmd){
914
915 uint8_t keyNo = 0;
916 bool errors = false;
917
918 char cmdp = param_getchar(Cmd, 0);
919
920 //Change key to user defined one
921 if (cmdp == 'k' || cmdp == 'K'){
922 keyNo = param_get8(Cmd, 1);
923 if(keyNo > 6)
924 errors = true;
925 }
926
927 if (cmdp == 'h' || cmdp == 'H')
928 errors = true;
929
930 if (errors) {
931 PrintAndLog("Usage: hf mfu cauth k <key number>");
932 PrintAndLog(" 0 (default): 3DES standard key");
933 PrintAndLog(" 1 : all 0x00 key");
934 PrintAndLog(" 2 : 0x00-0x0F key");
935 PrintAndLog(" 3 : nfc key");
936 PrintAndLog(" 4 : all 0x01 key");
937 PrintAndLog(" 5 : all 0xff key");
938 PrintAndLog(" 6 : 0x00-0xFF key");
939 PrintAndLog("\n sample : hf mfu cauth k");
940 PrintAndLog(" : hf mfu cauth k 3");
941 return 0;
942 }
943
944 uint8_t *key = default_3des_keys[keyNo];
945 if (try3DesAuthentication(key)>0)
946 PrintAndLog("Authentication successful. 3des key: %s",sprint_hex(key, 16));
947 else
948 PrintAndLog("Authentication failed");
949
950 return 0;
951 }
952
953 int try3DesAuthentication( uint8_t *key){
954
955 uint8_t blockNo = 0;
956 uint32_t cuid = 0;
957
958 des3_context ctx = { 0 };
959
960 uint8_t random_a[8] = { 1,1,1,1,1,1,1,1 };
961 uint8_t random_b[8] = { 0 };
962 uint8_t enc_random_b[8] = { 0 };
963 uint8_t rnd_ab[16] = { 0 };
964 uint8_t iv[8] = { 0 };
965
966 UsbCommand c = {CMD_MIFAREUC_AUTH1, {blockNo}};
967 SendCommand(&c);
968 UsbCommand resp;
969 if ( !WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) return -1;
970 if ( !(resp.arg[0] & 0xff) ) return -2;
971
972 cuid = resp.arg[1];
973 memcpy(enc_random_b,resp.d.asBytes+1,8);
974
975 des3_set2key_dec(&ctx, key);
976 // context, mode, length, IV, input, output
977 des3_crypt_cbc( &ctx, DES_DECRYPT, sizeof(random_b), iv , enc_random_b , random_b);
978
979 rol(random_b,8);
980 memcpy(rnd_ab ,random_a,8);
981 memcpy(rnd_ab+8,random_b,8);
982
983 //PrintAndLog(" RndA :%s", sprint_hex(random_a, 8));
984 //PrintAndLog(" enc(RndB) :%s", sprint_hex(enc_random_b, 8));
985 //PrintAndLog(" RndB :%s", sprint_hex(random_b, 8));
986 //PrintAndLog(" A+B :%s", sprint_hex(rnd_ab, 16));
987
988 des3_set2key_enc(&ctx, key);
989 // context, mode, length, IV, input, output
990 des3_crypt_cbc(&ctx, DES_ENCRYPT, sizeof(rnd_ab), enc_random_b, rnd_ab, rnd_ab);
991
992 //Auth2
993 c.cmd = CMD_MIFAREUC_AUTH2;
994 c.arg[0] = cuid;
995 memcpy(c.d.asBytes, rnd_ab, 16);
996 SendCommand(&c);
997
998 if ( !WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
999 if ( !(resp.arg[0] & 0xff)) return -2;
1000
1001 uint8_t enc_resp[8] = { 0 };
1002 uint8_t resp_random_a[8] = { 0 };
1003 memcpy(enc_resp, resp.d.asBytes+1, 8);
1004
1005 des3_set2key_dec(&ctx, key);
1006 // context, mode, length, IV, input, output
1007 des3_crypt_cbc( &ctx, DES_DECRYPT, 8, enc_random_b, enc_resp, resp_random_a);
1008
1009 //PrintAndLog(" enc(A+B) :%s", sprint_hex(rnd_ab, 16));
1010 //PrintAndLog(" enc(RndA') :%s", sprint_hex(enc_resp, 8));
1011
1012 if ( !memcmp(resp_random_a, random_a, 8))
1013 return 1;
1014 return 0;
1015 }
1016
1017 /**
1018 A test function to validate that the polarssl-function works the same
1019 was as the openssl-implementation.
1020 Commented out, since it requires openssl
1021
1022 int CmdTestDES(const char * cmd)
1023 {
1024 uint8_t key[16] = {0x00};
1025
1026 memcpy(key,key3_3des_data,16);
1027 DES_cblock RndA, RndB;
1028
1029 PrintAndLog("----------OpenSSL DES implementation----------");
1030 {
1031 uint8_t e_RndB[8] = {0x00};
1032 unsigned char RndARndB[16] = {0x00};
1033
1034 DES_cblock iv = { 0 };
1035 DES_key_schedule ks1,ks2;
1036 DES_cblock key1,key2;
1037
1038 memcpy(key,key3_3des_data,16);
1039 memcpy(key1,key,8);
1040 memcpy(key2,key+8,8);
1041
1042
1043 DES_set_key((DES_cblock *)key1,&ks1);
1044 DES_set_key((DES_cblock *)key2,&ks2);
1045
1046 DES_random_key(&RndA);
1047 PrintAndLog(" RndA:%s",sprint_hex(RndA, 8));
1048 PrintAndLog(" e_RndB:%s",sprint_hex(e_RndB, 8));
1049 //void DES_ede2_cbc_encrypt(const unsigned char *input,
1050 // unsigned char *output, long length, DES_key_schedule *ks1,
1051 // DES_key_schedule *ks2, DES_cblock *ivec, int enc);
1052 DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0);
1053
1054 PrintAndLog(" RndB:%s",sprint_hex(RndB, 8));
1055 rol(RndB,8);
1056 memcpy(RndARndB,RndA,8);
1057 memcpy(RndARndB+8,RndB,8);
1058 PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16));
1059 DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1);
1060 PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16));
1061
1062 }
1063 PrintAndLog("----------PolarSSL implementation----------");
1064 {
1065 uint8_t random_a[8] = { 0 };
1066 uint8_t enc_random_a[8] = { 0 };
1067 uint8_t random_b[8] = { 0 };
1068 uint8_t enc_random_b[8] = { 0 };
1069 uint8_t random_a_and_b[16] = { 0 };
1070 des3_context ctx = { 0 };
1071
1072 memcpy(random_a, RndA,8);
1073
1074 uint8_t output[8] = { 0 };
1075 uint8_t iv[8] = { 0 };
1076
1077 PrintAndLog(" RndA :%s",sprint_hex(random_a, 8));
1078 PrintAndLog(" e_RndB:%s",sprint_hex(enc_random_b, 8));
1079
1080 des3_set2key_dec(&ctx, key);
1081
1082 des3_crypt_cbc(&ctx // des3_context *ctx
1083 , DES_DECRYPT // int mode
1084 , sizeof(random_b) // size_t length
1085 , iv // unsigned char iv[8]
1086 , enc_random_b // const unsigned char *input
1087 , random_b // unsigned char *output
1088 );
1089
1090 PrintAndLog(" RndB:%s",sprint_hex(random_b, 8));
1091
1092 rol(random_b,8);
1093 memcpy(random_a_and_b ,random_a,8);
1094 memcpy(random_a_and_b+8,random_b,8);
1095
1096 PrintAndLog(" RA+B:%s",sprint_hex(random_a_and_b, 16));
1097
1098 des3_set2key_enc(&ctx, key);
1099
1100 des3_crypt_cbc(&ctx // des3_context *ctx
1101 , DES_ENCRYPT // int mode
1102 , sizeof(random_a_and_b) // size_t length
1103 , enc_random_b // unsigned char iv[8]
1104 , random_a_and_b // const unsigned char *input
1105 , random_a_and_b // unsigned char *output
1106 );
1107
1108 PrintAndLog("enc(RA+B):%s",sprint_hex(random_a_and_b, 16));
1109 }
1110 return 0;
1111 }
1112 **/
1113
1114 //
1115 // Ultralight C Read Single Block
1116 //
1117 int CmdHF14AMfUCRdBl(const char *Cmd)
1118 {
1119 UsbCommand resp;
1120 bool hasPwd = FALSE;
1121 uint8_t blockNo = -1;
1122 unsigned char key[16];
1123 char cmdp = param_getchar(Cmd, 0);
1124
1125 if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') {
1126 PrintAndLog("Usage: hf mfu crdbl <block number> <password>");
1127 PrintAndLog("");
1128 PrintAndLog("sample: hf mfu crdbl 0");
1129 PrintAndLog(" hf mfu crdbl 0 00112233445566778899AABBCCDDEEFF");
1130 return 0;
1131 }
1132
1133 blockNo = param_get8(Cmd, 0);
1134 if (blockNo < 0) {
1135 PrintAndLog("Wrong block number");
1136 return 1;
1137 }
1138
1139 if (blockNo > MAX_ULC_BLOCKS ){
1140 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C");
1141 return 1;
1142 }
1143
1144 // key
1145 if ( strlen(Cmd) > 3){
1146 if (param_gethex(Cmd, 1, key, 32)) {
1147 PrintAndLog("Key must include %d HEX symbols", 32);
1148 return 1;
1149 } else {
1150 hasPwd = TRUE;
1151 }
1152 }
1153
1154 //Read Block
1155 UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}};
1156 if ( hasPwd ) {
1157 c.arg[1] = 1;
1158 memcpy(c.d.asBytes,key,16);
1159 }
1160 SendCommand(&c);
1161
1162 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1163 uint8_t isOK = resp.arg[0] & 0xff;
1164 if (isOK) {
1165 uint8_t *data = resp.d.asBytes;
1166 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo, blockNo, sprint_hex(data, 4));
1167 }
1168 else {
1169 PrintAndLog("Failed reading block: (%02x)", isOK);
1170 }
1171 } else {
1172 PrintAndLog("Command execute time-out");
1173 }
1174 return 0;
1175 }
1176
1177 //
1178 // Mifare Ultralight C Write Single Block
1179 //
1180 int CmdHF14AMfUCWrBl(const char *Cmd){
1181
1182 uint8_t blockNo = -1;
1183 bool chinese_card = FALSE;
1184 uint8_t bldata[16] = {0x00};
1185 UsbCommand resp;
1186
1187 char cmdp = param_getchar(Cmd, 0);
1188
1189 if (strlen(Cmd) < 3 || cmdp == 'h' || cmdp == 'H') {
1190 PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]");
1191 PrintAndLog(" [block number]");
1192 PrintAndLog(" [block data] - (8 hex symbols)");
1193 PrintAndLog(" [w] - Chinese magic ultralight tag");
1194 PrintAndLog("");
1195 PrintAndLog(" sample: hf mfu cwrbl 0 01020304");
1196 PrintAndLog("");
1197 return 0;
1198 }
1199
1200 blockNo = param_get8(Cmd, 0);
1201 if (blockNo > MAX_ULC_BLOCKS ){
1202 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!");
1203 return 1;
1204 }
1205
1206 if (param_gethex(Cmd, 1, bldata, 8)) {
1207 PrintAndLog("Block data must include 8 HEX symbols");
1208 return 1;
1209 }
1210
1211 if (strchr(Cmd,'w') != 0 || strchr(Cmd,'W') != 0 ) {
1212 chinese_card = TRUE;
1213 }
1214
1215 if ( blockNo <= 3 ) {
1216 if (!chinese_card){
1217 PrintAndLog("Access Denied");
1218 } else {
1219 PrintAndLog("--Special block no: 0x%02x", blockNo);
1220 PrintAndLog("--Data: %s", sprint_hex(bldata, 4));
1221 UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};
1222 memcpy(d.d.asBytes,bldata, 4);
1223 SendCommand(&d);
1224 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1225 uint8_t isOK = resp.arg[0] & 0xff;
1226 PrintAndLog("isOk:%02x", isOK);
1227 } else {
1228 PrintAndLog("Command execute timeout");
1229 }
1230 }
1231 } else {
1232 PrintAndLog("--Block no : 0x%02x", blockNo);
1233 PrintAndLog("--Data: %s", sprint_hex(bldata, 4));
1234 UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}};
1235 memcpy(e.d.asBytes,bldata, 4);
1236 SendCommand(&e);
1237 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1238 uint8_t isOK = resp.arg[0] & 0xff;
1239 PrintAndLog("isOk : %02x", isOK);
1240 } else {
1241 PrintAndLog("Command execute timeout");
1242 }
1243 }
1244 return 0;
1245 }
1246
1247 //
1248 // Mifare Ultralight C - Set password
1249 //
1250 int CmdHF14AMfucSetPwd(const char *Cmd){
1251
1252 uint8_t pwd[16] = {0x00};
1253
1254 char cmdp = param_getchar(Cmd, 0);
1255
1256 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') {
1257 PrintAndLog("Usage: hf mfu setpwd <password (32 hex symbols)>");
1258 PrintAndLog(" [password] - (32 hex symbols)");
1259 PrintAndLog("");
1260 PrintAndLog("sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f");
1261 PrintAndLog("");
1262 return 0;
1263 }
1264
1265 if (param_gethex(Cmd, 0, pwd, 32)) {
1266 PrintAndLog("Password must include 32 HEX symbols");
1267 return 1;
1268 }
1269
1270 UsbCommand c = {CMD_MIFAREUC_SETPWD};
1271 memcpy( c.d.asBytes, pwd, 16);
1272 SendCommand(&c);
1273
1274 UsbCommand resp;
1275
1276 if (WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {
1277 if ( (resp.arg[0] & 0xff) == 1)
1278 PrintAndLog("Ultralight-C new password: %s", sprint_hex(pwd,16));
1279 else{
1280 PrintAndLog("Failed writing at block %d", resp.arg[1] & 0xff);
1281 return 1;
1282 }
1283 }
1284 else {
1285 PrintAndLog("command execution time out");
1286 return 1;
1287 }
1288
1289 return 0;
1290 }
1291
1292 //
1293 // Magic UL / UL-C tags - Set UID
1294 //
1295 int CmdHF14AMfucSetUid(const char *Cmd){
1296
1297 UsbCommand c;
1298 UsbCommand resp;
1299 uint8_t uid[7] = {0x00};
1300 char cmdp = param_getchar(Cmd, 0);
1301
1302 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') {
1303 PrintAndLog("Usage: hf mfu setuid <uid (14 hex symbols)>");
1304 PrintAndLog(" [uid] - (14 hex symbols)");
1305 PrintAndLog("\nThis only works for Magic Ultralight tags.");
1306 PrintAndLog("");
1307 PrintAndLog("sample: hf mfu setuid 11223344556677");
1308 PrintAndLog("");
1309 return 0;
1310 }
1311
1312 if (param_gethex(Cmd, 0, uid, 14)) {
1313 PrintAndLog("UID must include 14 HEX symbols");
1314 return 1;
1315 }
1316
1317 // read block2.
1318 c.cmd = CMD_MIFAREU_READBL;
1319 c.arg[0] = 2;
1320 SendCommand(&c);
1321 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1322 PrintAndLog("Command execute timeout");
1323 return 2;
1324 }
1325
1326 // save old block2.
1327 uint8_t oldblock2[4] = {0x00};
1328 memcpy(resp.d.asBytes, oldblock2, 4);
1329
1330 // block 0.
1331 c.cmd = CMD_MIFAREU_WRITEBL;
1332 c.arg[0] = 0;
1333 c.d.asBytes[0] = uid[0];
1334 c.d.asBytes[1] = uid[1];
1335 c.d.asBytes[2] = uid[2];
1336 c.d.asBytes[3] = 0x88 ^ uid[0] ^ uid[1] ^ uid[2];
1337 SendCommand(&c);
1338 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1339 PrintAndLog("Command execute timeout");
1340 return 3;
1341 }
1342
1343 // block 1.
1344 c.arg[0] = 1;
1345 c.d.asBytes[0] = uid[3];
1346 c.d.asBytes[1] = uid[4];
1347 c.d.asBytes[2] = uid[5];
1348 c.d.asBytes[3] = uid[6];
1349 SendCommand(&c);
1350 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {
1351 PrintAndLog("Command execute timeout");
1352 return 4;
1353 }
1354
1355 // block 2.
1356 c.arg[0] = 2;
1357 c.d.asBytes[0] = uid[3] ^ uid[4] ^ uid[5] ^ uid[6];
1358 c.d.asBytes[1] = oldblock2[1];
1359 c.d.asBytes[2] = oldblock2[2];
1360 c.d.asBytes[3] = oldblock2[3];
1361 SendCommand(&c);
1362 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {
1363 PrintAndLog("Command execute timeout");
1364 return 5;
1365 }
1366
1367 return 0;
1368 }
1369
1370 int CmdHF14AMfuGenDiverseKeys(const char *Cmd){
1371
1372 uint8_t iv[8] = { 0x00 };
1373 uint8_t block = 0x07;
1374
1375 // UL-EV1
1376 //04 57 b6 e2 05 3f 80 UID
1377 //4a f8 4b 19 PWD
1378 uint8_t uid[] = { 0xF4,0xEA, 0x54, 0x8E };
1379 uint8_t mifarekeyA[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 };
1380 uint8_t mifarekeyB[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 };
1381 uint8_t dkeyA[8] = { 0x00 };
1382 uint8_t dkeyB[8] = { 0x00 };
1383
1384 uint8_t masterkey[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff };
1385
1386 uint8_t mix[8] = { 0x00 };
1387 uint8_t divkey[8] = { 0x00 };
1388
1389 memcpy(mix, mifarekeyA, 4);
1390
1391 mix[4] = mifarekeyA[4] ^ uid[0];
1392 mix[5] = mifarekeyA[5] ^ uid[1];
1393 mix[6] = block ^ uid[2];
1394 mix[7] = uid[3];
1395
1396 des3_context ctx = { 0x00 };
1397 des3_set2key_enc(&ctx, masterkey);
1398
1399 des3_crypt_cbc(&ctx // des3_context
1400 , DES_ENCRYPT // int mode
1401 , sizeof(mix) // length
1402 , iv // iv[8]
1403 , mix // input
1404 , divkey // output
1405 );
1406
1407 PrintAndLog("3DES version");
1408 PrintAndLog("Masterkey :\t %s", sprint_hex(masterkey,sizeof(masterkey)));
1409 PrintAndLog("UID :\t %s", sprint_hex(uid, sizeof(uid)));
1410 PrintAndLog("Sector :\t %0d", block);
1411 PrintAndLog("Mifare key :\t %s", sprint_hex(mifarekeyA, sizeof(mifarekeyA)));
1412 PrintAndLog("Message :\t %s", sprint_hex(mix, sizeof(mix)));
1413 PrintAndLog("Diversified key: %s", sprint_hex(divkey+1, 6));
1414
1415 PrintAndLog("\n DES version");
1416
1417 for (int i=0; i < sizeof(mifarekeyA); ++i){
1418 dkeyA[i] = (mifarekeyA[i] << 1) & 0xff;
1419 dkeyA[6] |= ((mifarekeyA[i] >> 7) & 1) << (i+1);
1420 }
1421
1422 for (int i=0; i < sizeof(mifarekeyB); ++i){
1423 dkeyB[1] |= ((mifarekeyB[i] >> 7) & 1) << (i+1);
1424 dkeyB[2+i] = (mifarekeyB[i] << 1) & 0xff;
1425 }
1426
1427 uint8_t zeros[8] = {0x00};
1428 uint8_t newpwd[8] = {0x00};
1429 uint8_t dmkey[24] = {0x00};
1430 memcpy(dmkey, dkeyA, 8);
1431 memcpy(dmkey+8, dkeyB, 8);
1432 memcpy(dmkey+16, dkeyA, 8);
1433 memset(iv, 0x00, 8);
1434
1435 des3_set3key_enc(&ctx, dmkey);
1436
1437 des3_crypt_cbc(&ctx // des3_context
1438 , DES_ENCRYPT // int mode
1439 , sizeof(newpwd) // length
1440 , iv // iv[8]
1441 , zeros // input
1442 , newpwd // output
1443 );
1444
1445 PrintAndLog("Mifare dkeyA :\t %s", sprint_hex(dkeyA, sizeof(dkeyA)));
1446 PrintAndLog("Mifare dkeyB :\t %s", sprint_hex(dkeyB, sizeof(dkeyB)));
1447 PrintAndLog("Mifare ABA :\t %s", sprint_hex(dmkey, sizeof(dmkey)));
1448 PrintAndLog("Mifare Pwd :\t %s", sprint_hex(newpwd, sizeof(newpwd)));
1449
1450 return 0;
1451 }
1452
1453 // static uint8_t * diversify_key(uint8_t * key){
1454
1455 // for(int i=0; i<16; i++){
1456 // if(i<=6) key[i]^=cuid[i];
1457 // if(i>6) key[i]^=cuid[i%7];
1458 // }
1459 // return key;
1460 // }
1461
1462 // static void GenerateUIDe( uint8_t *uid, uint8_t len){
1463 // for (int i=0; i<len; ++i){
1464
1465 // }
1466 // return;
1467 // }
1468
1469 static command_t CommandTable[] =
1470 {
1471 {"help", CmdHelp, 1, "This help"},
1472 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},
1473 {"info", CmdHF14AMfUInfo, 0, "Tag information"},
1474 {"dump", CmdHF14AMfUDump, 0, "Dump Ultralight / Ultralight-C tag to binary file"},
1475 {"rdbl", CmdHF14AMfURdBl, 0, "Read block - Ultralight"},
1476 {"wrbl", CmdHF14AMfUWrBl, 0, "Write block - Ultralight"},
1477 {"crdbl", CmdHF14AMfUCRdBl, 0, "Read block - Ultralight C"},
1478 {"cwrbl", CmdHF14AMfUCWrBl, 0, "Write block - Ultralight C"},
1479 {"cauth", CmdHF14AMfucAuth, 0, "Authentication - Ultralight C"},
1480 {"setpwd", CmdHF14AMfucSetPwd, 1, "Set 3des password - Ultralight-C"},
1481 {"setuid", CmdHF14AMfucSetUid, 1, "Set UID - MAGIC tags only"},
1482 {"gen", CmdHF14AMfuGenDiverseKeys , 1, "Generate 3des mifare diversified keys"},
1483 {NULL, NULL, 0, NULL}
1484 };
1485
1486 int CmdHFMFUltra(const char *Cmd){
1487 WaitForResponseTimeout(CMD_ACK,NULL,100);
1488 CmdsParse(CommandTable, Cmd);
1489 return 0;
1490 }
1491
1492 int CmdHelp(const char *Cmd){
1493 CmdsHelp(CommandTable);
1494 return 0;
1495 }
Impressum, Datenschutz