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