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