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