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