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