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