]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhfmfu.c
MF Ultralight - Iceman's updates + mine
[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"
81740aa5 15
f168b263 16#define MAX_UL_BLOCKS 0x0f
17#define MAX_ULC_BLOCKS 0x2f
18#define MAX_ULEV1a_BLOCKS 0x0b;
19#define MAX_ULEV1b_BLOCKS 0x20;
81740aa5 20
f168b263 21uint8_t default_3des_keys[7][16] = {
22 { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 },// 3des std key
23 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },// all zeroes
24 { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f },// 0x00-0x0F
25 { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 },// NFC-key
26 { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },// all ones
27 { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF },// all FF
28 { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF } // 11 22 33
29 };
30
31static int CmdHelp(const char *Cmd);
afceaf40 32
f168b263 33// return 1 if tag responded to 0x1A.
34uint8_t requestAuthentication( uint8_t* nonce){
81740aa5 35
f168b263 36 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_RAW | ISO14A_APPEND_CRC ,2 ,0}};
37 c.d.asBytes[0] = 0x1A;
38 c.d.asBytes[1] = 0x00;
39 SendCommand(&c);
40 UsbCommand resp;
41 WaitForResponse(CMD_ACK, &resp); // skip select answer.
81740aa5 42
f168b263 43 if ( !(resp.arg[0] & 0xff) )
44 return 0;
45
46 if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
47
48 if ( resp.arg[0] & 0xff ) {
49 memcpy(nonce, resp.d.asBytes+1, 8);
50 return 1;
51 }
52 }
53 return 0;
54}
81740aa5 55
f168b263 56typedef enum TAGTYPE_UL {
57 UNKNOWN = 0x00,
58 UL = 0x01,
59 UL_C = 0x02,
60 UL_EV1_48 = 0x04,
61 UL_EV1_128 = 0x08,
62 UL_MAGIC = 0x11,
63 UL_C_MAGIC = 0x12,
64 MAGIC = 0x10,
65 UL_ERROR = 0xFF,
66} TagTypeUL_t;
67
68uint8_t GetHF14AMfU_Type(uint8_t *data, uint8_t dataSize){
69 TagTypeUL_t tagtype = UNKNOWN;
afceaf40 70 uint8_t isOK = 0;
81740aa5 71
afceaf40
MHS
72 UsbCommand c = {CMD_MIFAREU_READCARD, {0, 4}};
73 SendCommand(&c);
74 UsbCommand resp;
81740aa5 75
afceaf40
MHS
76 if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
77 isOK = resp.arg[0] & 0xff;
f168b263 78 memcpy(data, resp.d.asBytes, dataSize);
81740aa5 79
afceaf40 80 if (!isOK) {
81740aa5 81 PrintAndLog("Error reading from tag");
f168b263 82 return UL_ERROR;
81740aa5 83 }
84 } else {
85 PrintAndLog("Command execute timed out");
f168b263 86 return UL_ERROR;
81740aa5 87 }
88
f168b263 89 c.cmd = CMD_READER_ISO_14443a;
90 c.arg[0] = ISO14A_CONNECT | ISO14A_RAW | ISO14A_APPEND_CRC;
91 c.arg[1] = 1;
92 c.arg[2] = 0;
93 c.d.asBytes[0] = 0x60;
94 SendCommand(&c);
95 WaitForResponse(CMD_ACK, &resp);
96
97 if ( resp.arg[0] ) {
98 if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
81740aa5 99
f168b263 100 uint8_t version[8] = {0,0,0,0,0,0,0,0};
101 memcpy(&version, resp.d.asBytes, sizeof(version));
102 uint8_t len = resp.arg[0] & 0xff;
103
104 if ( len == 0x0A && version[6] == 0x0B )
105 tagtype = UL_EV1_48;
106 else if ( len == 0x0A && version[6] != 0x0B )
107 tagtype = UL_EV1_128;
108 else if ( len == 0x01 )
109 tagtype = UL_C;
110 else if ( len == 0x00 )
111 tagtype = UL; //| UL_MAGIC | UL_C_MAGIC
112 }
113 }
114
115 // Magic UL-C, mine have a static nonce response to 0x1A command.
116 uint8_t nonce1[8] = {0,0,0,0,0,0,0,0};
117 uint8_t nonce2[8] = {0,0,0,0,0,0,0,0};
118 uint8_t status = requestAuthentication(nonce1);
119 if ( status ) {
120 requestAuthentication(nonce2);
121 if ( !memcmp(nonce1, nonce2, 8) )
122 tagtype ^= MAGIC;
123 } else {
124 // Magic Ultralight test here - TODO
125 }
126 return tagtype;
127}
128
129int CmdHF14AMfUInfo(const char *Cmd){
130
131 TagTypeUL_t tagtype = UNKNOWN;
132
133 uint8_t datatemp[7] = {0x00};
134 uint8_t data[16] = {0x00};
135
136 tagtype = GetHF14AMfU_Type(data, sizeof(data));
137 if (tagtype == UL_ERROR) return -1;
138
139 PrintAndLog("\n-- Tag Information ---------");
140 PrintAndLog("-------------------------------------------------------------");
141 switch(tagtype){
142 case UNKNOWN : PrintAndLog(" TYPE : Unknown"); return 0;
143 case UL : PrintAndLog(" TYPE : MIFARE Ultralight");break;
144 case UL_C : PrintAndLog(" TYPE : MIFARE Ultralight C");break;
145 case UL_EV1_48 : PrintAndLog(" TYPE : MIFARE Ultralight EV1 48 bytes"); break;
146 case UL_EV1_128 : PrintAndLog(" TYPE : MIFARE Ultralight EV1 128 bytes"); break;
147 case UL_MAGIC : PrintAndLog(" TYPE : MIFARE Ultralight (MAGIC)");break;
148 case UL_C_MAGIC : PrintAndLog(" TYPE : MIFARE Ultralight-C (MAGIC)");break;
149 default : PrintAndLog(" TYPE : Unknown %x",tagtype);break;
150 }
151
81740aa5 152 // UID
153 memcpy( datatemp, data, 3);
154 memcpy( datatemp+3, data+4, 4);
155
f168b263 156 PrintAndLog(" UID : %s ", sprint_hex(datatemp, 7));
157 PrintAndLog(" UID[0] : (Manufacturer Byte) = %02x, Manufacturer: %s", datatemp[0], getTagInfo(datatemp[0]) );
158
81740aa5 159 // BBC
160 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
161 int crc0 = 0x88 ^ data[0] ^ data[1] ^data[2];
162 if ( data[3] == crc0 )
f168b263 163 PrintAndLog(" BCC0 : %02x - Ok", data[3]);
81740aa5 164 else
f168b263 165 PrintAndLog(" BCC0 : %02x - crc should be %02x", data[3], crc0);
81740aa5 166
167 int crc1 = data[4] ^ data[5] ^ data[6] ^data[7];
168 if ( data[8] == crc1 )
f168b263 169 PrintAndLog(" BCC1 : %02x - Ok", data[8]);
81740aa5 170 else
f168b263 171 PrintAndLog(" BCC1 : %02x - crc should be %02x", data[8], crc1 );
81740aa5 172
f168b263 173 PrintAndLog(" Internal : %s ", sprint_hex(data + 9, 1));
81740aa5 174
175 memcpy(datatemp, data+10, 2);
f168b263 176 PrintAndLog(" Lock : %s - %s", sprint_hex(datatemp, 2),printBits( 2, &datatemp) );
177 PrintAndLog("OneTimePad : %s ", sprint_hex(data + 3*4, 4));
81740aa5 178 PrintAndLog("");
81740aa5 179
f168b263 180
181 PrintAndLog("--- ");
182 if ((tagtype & UL_C)){
183
184 PrintAndLog("Trying some default 3des keys");
185 uint8_t *key;
186
187 for (uint8_t i = 0; i < 5; ++i ){
188 key = default_3des_keys[i];
189 if (try3DesAuthentication(key)){
190 PrintAndLog("Found default 3des key: %s", sprint_hex(key,16));
191 return 0;
192 }
193 }
194 }
195 else if ((tagtype & (UL_EV1_48 | UL_EV1_128))) {
196 //********** TODO ********************************
197 //PrintAndLog("Trying some known EV1 passwords.");
198 }
81740aa5 199 return 0;
200}
201
202//
203// Mifare Ultralight Write Single Block
204//
205int CmdHF14AMfUWrBl(const char *Cmd){
afceaf40
MHS
206 uint8_t blockNo = -1;
207 bool chinese_card = FALSE;
208 uint8_t bldata[16] = {0x00};
209 UsbCommand resp;
81740aa5 210
afceaf40 211 char cmdp = param_getchar(Cmd, 0);
81740aa5 212 if (strlen(Cmd) < 3 || cmdp == 'h' || cmdp == 'H') {
afceaf40 213 PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]");
81740aa5 214 PrintAndLog(" [block number]");
215 PrintAndLog(" [block data] - (8 hex symbols)");
216 PrintAndLog(" [w] - Chinese magic ultralight tag");
217 PrintAndLog("");
afceaf40 218 PrintAndLog(" sample: hf mfu wrbl 0 01020304");
81740aa5 219 PrintAndLog("");
afceaf40
MHS
220 return 0;
221 }
222
81740aa5 223 blockNo = param_get8(Cmd, 0);
224
f168b263 225 if (blockNo > MAX_UL_BLOCKS){
afceaf40
MHS
226 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
227 return 1;
228 }
81740aa5 229
afceaf40
MHS
230 if (param_gethex(Cmd, 1, bldata, 8)) {
231 PrintAndLog("Block data must include 8 HEX symbols");
232 return 1;
233 }
81740aa5 234
afceaf40
MHS
235 if (strchr(Cmd,'w') != 0 || strchr(Cmd,'W') != 0 ) {
236 chinese_card = TRUE;
237 }
81740aa5 238
afceaf40 239 if ( blockNo <= 3) {
81740aa5 240 if (!chinese_card){
241 PrintAndLog("Access Denied");
242 } else {
243 PrintAndLog("--specialblock no:%02x", blockNo);
244 PrintAndLog("--data: %s", sprint_hex(bldata, 4));
245 UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};
246 memcpy(d.d.asBytes,bldata, 4);
247 SendCommand(&d);
248 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
249 uint8_t isOK = resp.arg[0] & 0xff;
250 PrintAndLog("isOk:%02x", isOK);
251 } else {
252 PrintAndLog("Command execute timeout");
253 }
254 }
255 } else {
afceaf40
MHS
256 PrintAndLog("--block no:%02x", blockNo);
257 PrintAndLog("--data: %s", sprint_hex(bldata, 4));
258 UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}};
81740aa5 259 memcpy(e.d.asBytes,bldata, 4);
260 SendCommand(&e);
afceaf40
MHS
261 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
262 uint8_t isOK = resp.arg[0] & 0xff;
263 PrintAndLog("isOk:%02x", isOK);
264 } else {
265 PrintAndLog("Command execute timeout");
266 }
267 }
268 return 0;
81740aa5 269}
270
271//
272// Mifare Ultralight Read Single Block
273//
274int CmdHF14AMfURdBl(const char *Cmd){
81740aa5 275
f168b263 276 UsbCommand resp;
277 uint8_t blockNo = -1;
afceaf40 278 char cmdp = param_getchar(Cmd, 0);
81740aa5 279
280 if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') {
afceaf40
MHS
281 PrintAndLog("Usage: hf mfu rdbl <block number>");
282 PrintAndLog(" sample: hfu mfu rdbl 0");
283 return 0;
f168b263 284 }
285
afceaf40
MHS
286 blockNo = param_get8(Cmd, 0);
287
f168b263 288 if (blockNo > MAX_UL_BLOCKS){
289 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight");
290 return 1;
afceaf40 291 }
f168b263 292
afceaf40
MHS
293 UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}};
294 SendCommand(&c);
295
f168b263 296
afceaf40 297 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
f168b263 298 uint8_t isOK = resp.arg[0] & 0xff;
299 if (isOK) {
300 uint8_t *data = resp.d.asBytes;
301 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo, blockNo, sprint_hex(data, 4));
302 }
303 else {
304 PrintAndLog("Failed reading block: (%02x)", isOK);
305 }
81740aa5 306 } else {
f168b263 307 PrintAndLog("Command execute time-out");
afceaf40 308 }
f168b263 309
afceaf40 310 return 0;
81740aa5 311}
312
313//
314// Mifare Ultralight / Ultralight-C; Read and Dump Card Contents
315//
316int CmdHF14AMfUDump(const char *Cmd){
317
afceaf40 318 FILE *fout;
81740aa5 319 char filename[FILE_PATH_SIZE] = {0x00};
320 char * fnameptr = filename;
321
afceaf40
MHS
322 uint8_t *lockbytes_t = NULL;
323 uint8_t lockbytes[2] = {0x00};
81740aa5 324
afceaf40
MHS
325 uint8_t *lockbytes_t2 = NULL;
326 uint8_t lockbytes2[2] = {0x00};
81740aa5 327
afceaf40 328 bool bit[16] = {0x00};
81740aa5 329 bool bit2[16] = {0x00};
330
afceaf40
MHS
331 int i;
332 uint8_t BlockNo = 0;
333 int Pages = 16;
81740aa5 334
335 bool tmplockbit = false;
afceaf40
MHS
336 uint8_t isOK = 0;
337 uint8_t *data = NULL;
81740aa5 338
afceaf40 339 char cmdp = param_getchar(Cmd, 0);
81740aa5 340
341 if (cmdp == 'h' || cmdp == 'H') {
342 PrintAndLog("Reads all pages from Mifare Ultralight or Ultralight-C tag.");
343 PrintAndLog("It saves binary dump into the file `filename.bin` or `cardUID.bin`");
344 PrintAndLog("Usage: hf mfu dump <c> <filename w/o .bin>");
f168b263 345 PrintAndLog(" <c> optional cardtype c == Ultralight-C, Defaults to Ultralight");
81740aa5 346 PrintAndLog(" sample: hf mfu dump");
347 PrintAndLog(" : hf mfu dump myfile");
348 PrintAndLog(" : hf mfu dump c myfile");
349 return 0;
350 }
351
352 // UL or UL-C?
353 Pages = (cmdp == 'c' || cmdp == 'C') ? 44 : 16;
354
f168b263 355 uint8_t data2[16] = {0x00};
356 TagTypeUL_t tagtype = GetHF14AMfU_Type(data2, sizeof(data2));
357 switch(tagtype){
358 case UL_C:
359 Pages = 44;
360 PrintAndLog("Dumping Ultralight_C Card Data...");
361 break;
362 case UL_EV1_48:
363 Pages = 18;
364 PrintAndLog("Dumping Ultralight EV1_48 Card Data...");
365 break;
366 case UL_EV1_128:
367 Pages = 32;
368 PrintAndLog("Dumping Ultralight EV1_128 Card Data...");
369 break;
370 case UL_MAGIC:
371 Pages = 16;
372 PrintAndLog("Dumping Ultralight (Magic) Card Data...");
373 break;
374 case UL_C_MAGIC:
375 Pages = 44;
376 PrintAndLog("Dumping Ultralight_C (Magic) Card Data...");
377 break;
378 case UL:
379 default:
380 Pages = 16;
381 PrintAndLog("Dumping Ultralight Card Data...");
382 break;
383 }
384 //PrintAndLog("Dumping Ultralight%s Card Data...", (Pages ==16)?"":"-C");
afceaf40
MHS
385
386 UsbCommand c = {CMD_MIFAREU_READCARD, {BlockNo,Pages}};
387 SendCommand(&c);
388 UsbCommand resp;
81740aa5 389
afceaf40 390 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
81740aa5 391 isOK = resp.arg[0] & 0xff;
afceaf40 392 if (!isOK) {
81740aa5 393 PrintAndLog("Command error");
394 return 0;
395 }
396 data = resp.d.asBytes;
397 } else {
f168b263 398 PrintAndLog("Command execute time-out");
81740aa5 399 return 0;
400 }
401
402 // Load lock bytes.
403 int j = 0;
404
405 lockbytes_t = data + 8;
406 lockbytes[0] = lockbytes_t[2];
407 lockbytes[1] = lockbytes_t[3];
408 for(j = 0; j < 16; j++){
409 bit[j] = lockbytes[j/8] & ( 1 <<(7-j%8));
410 }
411
412 // Load bottom lockbytes if available
413 if ( Pages == 44 ) {
414
415 lockbytes_t2 = data + (40*4);
416 lockbytes2[0] = lockbytes_t2[2];
417 lockbytes2[1] = lockbytes_t2[3];
418 for (j = 0; j < 16; j++) {
419 bit2[j] = lockbytes2[j/8] & ( 1 <<(7-j%8));
420 }
421 }
422
423 for (i = 0; i < Pages; ++i) {
424
425 if ( i < 3 ) {
426 PrintAndLog("Block %02x:%s ", i,sprint_hex(data + i * 4, 4));
427 continue;
428 }
afceaf40 429
81740aa5 430 switch(i){
431 case 3: tmplockbit = bit[4]; break;
432 case 4: tmplockbit = bit[3]; break;
433 case 5: tmplockbit = bit[2]; break;
434 case 6: tmplockbit = bit[1]; break;
435 case 7: tmplockbit = bit[0]; break;
436 case 8: tmplockbit = bit[15]; break;
437 case 9: tmplockbit = bit[14]; break;
438 case 10: tmplockbit = bit[13]; break;
439 case 11: tmplockbit = bit[12]; break;
440 case 12: tmplockbit = bit[11]; break;
441 case 13: tmplockbit = bit[10]; break;
442 case 14: tmplockbit = bit[9]; break;
443 case 15: tmplockbit = bit[8]; break;
444 case 16:
445 case 17:
446 case 18:
447 case 19: tmplockbit = bit2[6]; break;
448 case 20:
449 case 21:
450 case 22:
451 case 23: tmplockbit = bit2[5]; break;
452 case 24:
453 case 25:
454 case 26:
455 case 27: tmplockbit = bit2[4]; break;
456 case 28:
457 case 29:
458 case 30:
459 case 31: tmplockbit = bit2[2]; break;
460 case 32:
461 case 33:
462 case 34:
463 case 35: tmplockbit = bit2[1]; break;
464 case 36:
465 case 37:
466 case 38:
467 case 39: tmplockbit = bit2[0]; break;
468 case 40: tmplockbit = bit2[12]; break;
469 case 41: tmplockbit = bit2[11]; break;
470 case 42: tmplockbit = bit2[10]; break; //auth0
471 case 43: tmplockbit = bit2[9]; break; //auth1
472 default: break;
473 }
474 PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),tmplockbit);
475 }
476
477 int len = 0;
478 if ( Pages == 16 )
479 len = param_getstr(Cmd,0,filename);
480 else
481 len = param_getstr(Cmd,1,filename);
482
afceaf40 483 if (len > FILE_PATH_SIZE-5) len = FILE_PATH_SIZE-5;
81740aa5 484
485 // user supplied filename?
486 if (len < 1) {
487
488 // UID = data 0-1-2 4-5-6-7 (skips a beat)
afceaf40
MHS
489 sprintf(fnameptr,"%02X%02X%02X%02X%02X%02X%02X.bin",
490 data[0],data[1], data[2], data[4],data[5],data[6], data[7]);
81740aa5 491
492 } else {
afceaf40 493 sprintf(fnameptr + len," .bin");
81740aa5 494 }
495
81740aa5 496
497 if ((fout = fopen(filename,"wb")) == NULL) {
498 PrintAndLog("Could not create file name %s", filename);
499 return 1;
500 }
501 fwrite( data, 1, Pages*4, fout );
502 fclose(fout);
503
504 PrintAndLog("Dumped %d pages, wrote %d bytes to %s", Pages, Pages*4, filename);
afceaf40 505 return 0;
81740aa5 506}
507
508// Needed to Authenticate to Ultralight C tags
509void rol (uint8_t *data, const size_t len){
afceaf40
MHS
510 uint8_t first = data[0];
511 for (size_t i = 0; i < len-1; i++) {
512 data[i] = data[i+1];
513 }
514 data[len-1] = first;
81740aa5 515}
516
517//-------------------------------------------------------------------------------
518// Ultralight C Methods
519//-------------------------------------------------------------------------------
520
521//
522// Ultralight C Authentication Demo {currently uses hard-coded key}
523//
524int CmdHF14AMfucAuth(const char *Cmd){
afceaf40 525
afceaf40
MHS
526 uint8_t keyNo = 0;
527 bool errors = false;
f168b263 528
529 char cmdp = param_getchar(Cmd, 0);
530
afceaf40
MHS
531 //Change key to user defined one
532 if (cmdp == 'k' || cmdp == 'K'){
533 keyNo = param_get8(Cmd, 1);
f168b263 534 if(keyNo > 6)
535 errors = true;
afceaf40
MHS
536 }
537
f168b263 538 if (cmdp == 'h' || cmdp == 'H')
afceaf40 539 errors = true;
f168b263 540
afceaf40
MHS
541 if (errors) {
542 PrintAndLog("Usage: hf mfu cauth k <key number>");
543 PrintAndLog(" 0 (default): 3DES standard key");
f168b263 544 PrintAndLog(" 1 : all 0x00 key");
afceaf40
MHS
545 PrintAndLog(" 2 : 0x00-0x0F key");
546 PrintAndLog(" 3 : nfc key");
f168b263 547 PrintAndLog(" 4 : all 0x01 key");
548 PrintAndLog(" 5 : all 0xff key");
549 PrintAndLog(" 6 : 0x00-0xFF key");
550 PrintAndLog("\n sample : hf mfu cauth k");
81740aa5 551 PrintAndLog(" : hf mfu cauth k 3");
afceaf40
MHS
552 return 0;
553 }
554
f168b263 555 uint8_t *key = default_3des_keys[keyNo];
556 if (try3DesAuthentication(key))
557 PrintAndLog("Authentication successful. 3des key: %s",sprint_hex(key, 8));
558 else
559 PrintAndLog("Authentication failed");
560
561 return 0;
562}
563
564int try3DesAuthentication( uint8_t *key){
565
afceaf40
MHS
566 uint8_t blockNo = 0;
567 uint32_t cuid = 0;
81740aa5 568
f168b263 569 des3_context ctx = { 0 };
570
571 uint8_t random_a[8] = { 1,1,1,1,1,1,1,1 };
572 uint8_t random_b[8] = { 0 };
573 uint8_t enc_random_b[8] = { 0 };
574 uint8_t rnd_ab[16] = { 0 };
575 uint8_t iv[8] = { 0 };
576
81740aa5 577 UsbCommand c = {CMD_MIFAREUC_AUTH1, {blockNo}};
578 SendCommand(&c);
579 UsbCommand resp;
f168b263 580 if ( !WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) return -1;
581 if ( !(resp.arg[0] & 0xff) ) return -2;
582
583 cuid = resp.arg[1];
584 memcpy(enc_random_b,resp.d.asBytes+1,8);
afceaf40
MHS
585
586 des3_set2key_dec(&ctx, key);
f168b263 587 // context, mode, length, IV, input, output
588 des3_crypt_cbc( &ctx, DES_DECRYPT, sizeof(random_b), iv , enc_random_b , random_b);
afceaf40
MHS
589
590 rol(random_b,8);
f168b263 591 memcpy(rnd_ab ,random_a,8);
592 memcpy(rnd_ab+8,random_b,8);
afceaf40
MHS
593
594 des3_set2key_enc(&ctx, key);
f168b263 595 // context, mode, length, IV, input, output
596 des3_crypt_cbc(&ctx, DES_ENCRYPT, sizeof(rnd_ab), enc_random_b, rnd_ab, rnd_ab);
afceaf40
MHS
597
598 //Auth2
f168b263 599 c.cmd = CMD_MIFAREUC_AUTH2;
600 c.arg[0] = cuid;
601 memcpy(c.d.asBytes, rnd_ab, 16);
602 SendCommand(&c);
81740aa5 603
f168b263 604 if ( !WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
605 if ( !(resp.arg[0] & 0xff)) return -2;
606
607 uint8_t enc_resp[8] = { 0 };
608 uint8_t resp_random_a[8] = { 0 };
609 memcpy(enc_resp, resp.d.asBytes+1, 8);
81740aa5 610
f168b263 611 des3_set2key_dec(&ctx, key);
612 // context, mode, length, IV, input, output
613 des3_crypt_cbc( &ctx, DES_DECRYPT, 8, enc_random_b, enc_resp, resp_random_a);
614
615 if ( !memcmp(resp_random_a, random_a, 8))
616 return 1;
afceaf40 617 return 0;
f168b263 618
619 //PrintAndLog(" RndA :%s", sprint_hex(random_a, 8));
620 //PrintAndLog(" enc(RndB) :%s", sprint_hex(enc_random_b, 8));
621 //PrintAndLog(" RndB :%s", sprint_hex(random_b, 8));
622 //PrintAndLog(" A+B :%s", sprint_hex(random_a_and_b, 16));
623 //PrintAndLog(" enc(A+B) :%s", sprint_hex(random_a_and_b, 16));
624 //PrintAndLog(" enc(RndA') :%s", sprint_hex(data2+1, 8));
81740aa5 625}
f168b263 626
afceaf40
MHS
627/**
628A test function to validate that the polarssl-function works the same
629was as the openssl-implementation.
630Commented out, since it requires openssl
631
632int CmdTestDES(const char * cmd)
633{
634 uint8_t key[16] = {0x00};
635
636 memcpy(key,key3_3des_data,16);
637 DES_cblock RndA, RndB;
638
639 PrintAndLog("----------OpenSSL DES implementation----------");
640 {
641 uint8_t e_RndB[8] = {0x00};
642 unsigned char RndARndB[16] = {0x00};
643
644 DES_cblock iv = { 0 };
645 DES_key_schedule ks1,ks2;
646 DES_cblock key1,key2;
647
648 memcpy(key,key3_3des_data,16);
649 memcpy(key1,key,8);
650 memcpy(key2,key+8,8);
651
652
653 DES_set_key((DES_cblock *)key1,&ks1);
654 DES_set_key((DES_cblock *)key2,&ks2);
655
656 DES_random_key(&RndA);
657 PrintAndLog(" RndA:%s",sprint_hex(RndA, 8));
658 PrintAndLog(" e_RndB:%s",sprint_hex(e_RndB, 8));
659 //void DES_ede2_cbc_encrypt(const unsigned char *input,
660 // unsigned char *output, long length, DES_key_schedule *ks1,
661 // DES_key_schedule *ks2, DES_cblock *ivec, int enc);
662 DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0);
663
664 PrintAndLog(" RndB:%s",sprint_hex(RndB, 8));
665 rol(RndB,8);
666 memcpy(RndARndB,RndA,8);
667 memcpy(RndARndB+8,RndB,8);
668 PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16));
669 DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1);
670 PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16));
671
672 }
673 PrintAndLog("----------PolarSSL implementation----------");
674 {
675 uint8_t random_a[8] = { 0 };
676 uint8_t enc_random_a[8] = { 0 };
677 uint8_t random_b[8] = { 0 };
678 uint8_t enc_random_b[8] = { 0 };
679 uint8_t random_a_and_b[16] = { 0 };
680 des3_context ctx = { 0 };
681
682 memcpy(random_a, RndA,8);
683
684 uint8_t output[8] = { 0 };
685 uint8_t iv[8] = { 0 };
686
687 PrintAndLog(" RndA :%s",sprint_hex(random_a, 8));
688 PrintAndLog(" e_RndB:%s",sprint_hex(enc_random_b, 8));
689
690 des3_set2key_dec(&ctx, key);
691
692 des3_crypt_cbc(&ctx // des3_context *ctx
693 , DES_DECRYPT // int mode
694 , sizeof(random_b) // size_t length
695 , iv // unsigned char iv[8]
696 , enc_random_b // const unsigned char *input
697 , random_b // unsigned char *output
698 );
699
700 PrintAndLog(" RndB:%s",sprint_hex(random_b, 8));
701
702 rol(random_b,8);
703 memcpy(random_a_and_b ,random_a,8);
704 memcpy(random_a_and_b+8,random_b,8);
705
706 PrintAndLog(" RA+B:%s",sprint_hex(random_a_and_b, 16));
707
708 des3_set2key_enc(&ctx, key);
81740aa5 709
afceaf40
MHS
710 des3_crypt_cbc(&ctx // des3_context *ctx
711 , DES_ENCRYPT // int mode
712 , sizeof(random_a_and_b) // size_t length
713 , enc_random_b // unsigned char iv[8]
714 , random_a_and_b // const unsigned char *input
715 , random_a_and_b // unsigned char *output
716 );
717
718 PrintAndLog("enc(RA+B):%s",sprint_hex(random_a_and_b, 16));
719 }
720 return 0;
721}
722**/
81740aa5 723//
724// Ultralight C Read Single Block
725//
726int CmdHF14AMfUCRdBl(const char *Cmd)
727{
f168b263 728 UsbCommand resp;
729 bool hasPwd = FALSE;
afceaf40 730 uint8_t blockNo = -1;
f168b263 731 unsigned char key[16];
afceaf40 732 char cmdp = param_getchar(Cmd, 0);
81740aa5 733
734 if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') {
f168b263 735 PrintAndLog("Usage: hf mfu crdbl <block number> <password>");
736 PrintAndLog("");
737 PrintAndLog("sample: hf mfu crdbl 0");
738 PrintAndLog(" hf mfu crdbl 0 112233445566778899AABBCCDDEEFF");
afceaf40
MHS
739 return 0;
740 }
741
742 blockNo = param_get8(Cmd, 0);
81740aa5 743 if (blockNo < 0) {
744 PrintAndLog("Wrong block number");
745 return 1;
746 }
747
f168b263 748 if (blockNo > MAX_ULC_BLOCKS ){
749 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C");
afceaf40
MHS
750 return 1;
751 }
81740aa5 752
f168b263 753 // key
754 if ( strlen(Cmd) > 3){
755 if (param_gethex(Cmd, 1, key, 32)) {
756 PrintAndLog("Key must include %d HEX symbols", 32);
757 return 1;
758 } else {
759 hasPwd = TRUE;
760 }
761 }
afceaf40
MHS
762
763 //Read Block
f168b263 764 UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}};
765 if ( hasPwd ) {
766 c.arg[1] = 1;
767 memcpy(c.d.asBytes,key,16);
768 }
769 SendCommand(&c);
770
771 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
772 uint8_t isOK = resp.arg[0] & 0xff;
773 if (isOK) {
774 uint8_t *data = resp.d.asBytes;
775 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo, blockNo, sprint_hex(data, 4));
776 }
777 else {
778 PrintAndLog("Failed reading block: (%02x)", isOK);
779 }
81740aa5 780 } else {
f168b263 781 PrintAndLog("Command execute time-out");
81740aa5 782 }
afceaf40 783 return 0;
81740aa5 784}
785
786//
787// Mifare Ultralight C Write Single Block
788//
789int CmdHF14AMfUCWrBl(const char *Cmd){
afceaf40
MHS
790
791 uint8_t blockNo = -1;
792 bool chinese_card = FALSE;
793 uint8_t bldata[16] = {0x00};
794 UsbCommand resp;
81740aa5 795
afceaf40 796 char cmdp = param_getchar(Cmd, 0);
81740aa5 797
798 if (strlen(Cmd) < 3 || cmdp == 'h' || cmdp == 'H') {
afceaf40 799 PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]");
81740aa5 800 PrintAndLog(" [block number]");
801 PrintAndLog(" [block data] - (8 hex symbols)");
802 PrintAndLog(" [w] - Chinese magic ultralight tag");
803 PrintAndLog("");
afceaf40 804 PrintAndLog(" sample: hf mfu cwrbl 0 01020304");
81740aa5 805 PrintAndLog("");
afceaf40
MHS
806 return 0;
807 }
81740aa5 808
afceaf40 809 blockNo = param_get8(Cmd, 0);
f168b263 810 if (blockNo > MAX_ULC_BLOCKS ){
afceaf40
MHS
811 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!");
812 return 1;
813 }
81740aa5 814
afceaf40
MHS
815 if (param_gethex(Cmd, 1, bldata, 8)) {
816 PrintAndLog("Block data must include 8 HEX symbols");
817 return 1;
818 }
81740aa5 819
afceaf40
MHS
820 if (strchr(Cmd,'w') != 0 || strchr(Cmd,'W') != 0 ) {
821 chinese_card = TRUE;
822 }
81740aa5 823
824 if ( blockNo <= 3 ) {
825 if (!chinese_card){
826 PrintAndLog("Access Denied");
827 } else {
828 PrintAndLog("--Special block no: 0x%02x", blockNo);
829 PrintAndLog("--Data: %s", sprint_hex(bldata, 4));
830 UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};
831 memcpy(d.d.asBytes,bldata, 4);
832 SendCommand(&d);
833 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
834 uint8_t isOK = resp.arg[0] & 0xff;
835 PrintAndLog("isOk:%02x", isOK);
836 } else {
837 PrintAndLog("Command execute timeout");
838 }
839 }
840 } else {
afceaf40
MHS
841 PrintAndLog("--Block no : 0x%02x", blockNo);
842 PrintAndLog("--Data: %s", sprint_hex(bldata, 4));
843 UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}};
844 memcpy(e.d.asBytes,bldata, 4);
845 SendCommand(&e);
846 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
847 uint8_t isOK = resp.arg[0] & 0xff;
848 PrintAndLog("isOk : %02x", isOK);
849 } else {
850 PrintAndLog("Command execute timeout");
851 }
81740aa5 852 }
853 return 0;
854}
855
f168b263 856//
857// Mifare Ultralight C - Set password
858//
859int CmdHF14AMfucSetPwd(const char *Cmd){
860
861 uint8_t pwd[16] = {0x00};
862
863 char cmdp = param_getchar(Cmd, 0);
864
865 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') {
866 PrintAndLog("Usage: hf mfu setpwd <password (32 hex symbols)>");
867 PrintAndLog(" [password] - (32 hex symbols)");
868 PrintAndLog("");
869 PrintAndLog("sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f");
870 PrintAndLog("");
871 return 0;
872 }
873
874 if (param_gethex(Cmd, 0, pwd, 32)) {
875 PrintAndLog("Password must include 32 HEX symbols");
876 return 1;
877 }
878
879 UsbCommand c = {CMD_MIFAREUC_SETPWD};
880 memcpy( c.d.asBytes, pwd, 16);
881 SendCommand(&c);
882
883 UsbCommand resp;
884
885 if (WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {
886 if ( (resp.arg[0] & 0xff) == 1)
887 PrintAndLog("Ultralight-C new password: %s", sprint_hex(pwd,16));
888 else{
889 PrintAndLog("Failed writing at block %d", resp.arg[1] & 0xff);
890 return 1;
891 }
892 }
893 else {
894 PrintAndLog("command execution time out");
895 return 1;
896 }
897
898 return 0;
899}
900
901//
902// Mifare Ultraligh - Set UID
903//
904int CmdHF14AMfucSetUid(const char *Cmd){
905
906 UsbCommand c;
907 UsbCommand resp;
908 uint8_t uid[7] = {0x00};
909 char cmdp = param_getchar(Cmd, 0);
910
911 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') {
912 PrintAndLog("Usage: hf mfu setuid <uid (14 hex symbols)>");
913 PrintAndLog(" [uid] - (14 hex symbols)");
914 PrintAndLog("\nThis only works for Magic Ultralight tags.");
915 PrintAndLog("");
916 PrintAndLog("sample: hf mfu setuid 11223344556677");
917 PrintAndLog("");
918 return 0;
919 }
920
921 if (param_gethex(Cmd, 0, uid, 14)) {
922 PrintAndLog("UID must include 14 HEX symbols");
923 return 1;
924 }
925
926 // read block2.
927 c.cmd = CMD_MIFAREU_READBL;
928 c.arg[0] = 2;
929 SendCommand(&c);
930 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
931 PrintAndLog("Command execute timeout");
932 return 2;
933 }
934
935 // save old block2.
936 uint8_t oldblock2[4] = {0x00};
937 memcpy(resp.d.asBytes, oldblock2, 4);
938
939 // block 0.
940 c.cmd = CMD_MIFAREU_WRITEBL;
941 c.arg[0] = 0;
942 c.d.asBytes[0] = uid[0];
943 c.d.asBytes[1] = uid[1];
944 c.d.asBytes[2] = uid[2];
945 c.d.asBytes[3] = 0x88 ^ uid[0] ^ uid[1] ^ uid[2];
946 SendCommand(&c);
947 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
948 PrintAndLog("Command execute timeout");
949 return 3;
950 }
951
952 // block 1.
953 c.arg[0] = 1;
954 c.d.asBytes[0] = uid[3];
955 c.d.asBytes[1] = uid[4];
956 c.d.asBytes[2] = uid[5];
957 c.d.asBytes[3] = uid[6];
958 SendCommand(&c);
959 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {
960 PrintAndLog("Command execute timeout");
961 return 4;
962 }
963
964 // block 2.
965 c.arg[0] = 2;
966 c.d.asBytes[0] = uid[3] ^ uid[4] ^ uid[5] ^ uid[6];
967 c.d.asBytes[1] = oldblock2[1];
968 c.d.asBytes[2] = oldblock2[2];
969 c.d.asBytes[3] = oldblock2[3];
970 SendCommand(&c);
971 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {
972 PrintAndLog("Command execute timeout");
973 return 5;
974 }
975
976 return 0;
977}
978
979int CmdHF14AMfuGenDiverseKeys(const char *Cmd){
980
981 uint8_t iv[8] = { 0x00 };
982 uint8_t block = 0x07;
983
984 // UL-EV1
985 //04 57 b6 e2 05 3f 80 UID
986 //4a f8 4b 19 PWD
987 uint8_t uid[] = { 0xF4,0xEA, 0x54, 0x8E };
988 uint8_t mifarekeyA[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 };
989 uint8_t mifarekeyB[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 };
990 uint8_t dkeyA[8] = { 0x00 };
991 uint8_t dkeyB[8] = { 0x00 };
992
993 uint8_t masterkey[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff };
994
995 uint8_t mix[8] = { 0x00 };
996 uint8_t divkey[8] = { 0x00 };
997
998 memcpy(mix, mifarekeyA, 4);
999
1000 mix[4] = mifarekeyA[4] ^ uid[0];
1001 mix[5] = mifarekeyA[5] ^ uid[1];
1002 mix[6] = block ^ uid[2];
1003 mix[7] = uid[3];
1004
1005 des3_context ctx = { 0x00 };
1006 des3_set2key_enc(&ctx, masterkey);
1007
1008 des3_crypt_cbc(&ctx // des3_context
1009 , DES_ENCRYPT // int mode
1010 , sizeof(mix) // length
1011 , iv // iv[8]
1012 , mix // input
1013 , divkey // output
1014 );
1015
1016 PrintAndLog("3DES version");
1017 PrintAndLog("Masterkey :\t %s", sprint_hex(masterkey,sizeof(masterkey)));
1018 PrintAndLog("UID :\t %s", sprint_hex(uid, sizeof(uid)));
1019 PrintAndLog("Sector :\t %0d", block);
1020 PrintAndLog("Mifare key :\t %s", sprint_hex(mifarekeyA, sizeof(mifarekeyA)));
1021 PrintAndLog("Message :\t %s", sprint_hex(mix, sizeof(mix)));
1022 PrintAndLog("Diversified key: %s", sprint_hex(divkey+1, 6));
1023
1024 PrintAndLog("\n DES version");
1025
1026 for (int i=0; i < sizeof(mifarekeyA); ++i){
1027 dkeyA[i] = (mifarekeyA[i] << 1) & 0xff;
1028 dkeyA[6] |= ((mifarekeyA[i] >> 7) & 1) << (i+1);
1029 }
1030
1031 for (int i=0; i < sizeof(mifarekeyB); ++i){
1032 dkeyB[1] |= ((mifarekeyB[i] >> 7) & 1) << (i+1);
1033 dkeyB[2+i] = (mifarekeyB[i] << 1) & 0xff;
1034 }
1035
1036 uint8_t zeros[8] = {0x00};
1037 uint8_t newpwd[8] = {0x00};
1038 uint8_t dmkey[24] = {0x00};
1039 memcpy(dmkey, dkeyA, 8);
1040 memcpy(dmkey+8, dkeyB, 8);
1041 memcpy(dmkey+16, dkeyA, 8);
1042 memset(iv, 0x00, 8);
1043
1044 des3_set3key_enc(&ctx, dmkey);
1045
1046 des3_crypt_cbc(&ctx // des3_context
1047 , DES_ENCRYPT // int mode
1048 , sizeof(newpwd) // length
1049 , iv // iv[8]
1050 , zeros // input
1051 , newpwd // output
1052 );
1053
1054 PrintAndLog("Mifare dkeyA :\t %s", sprint_hex(dkeyA, sizeof(dkeyA)));
1055 PrintAndLog("Mifare dkeyB :\t %s", sprint_hex(dkeyB, sizeof(dkeyB)));
1056 PrintAndLog("Mifare ABA :\t %s", sprint_hex(dmkey, sizeof(dmkey)));
1057 PrintAndLog("Mifare Pwd :\t %s", sprint_hex(newpwd, sizeof(newpwd)));
1058
1059 return 0;
1060}
1061
1062// static uint8_t * diversify_key(uint8_t * key){
1063
1064 // for(int i=0; i<16; i++){
1065 // if(i<=6) key[i]^=cuid[i];
1066 // if(i>6) key[i]^=cuid[i%7];
1067 // }
1068 // return key;
1069// }
1070
1071// static void GenerateUIDe( uint8_t *uid, uint8_t len){
1072 // for (int i=0; i<len; ++i){
1073
1074 // }
1075 // return;
1076// }
1077
81740aa5 1078//------------------------------------
1079// Menu Stuff
1080//------------------------------------
1081static command_t CommandTable[] =
1082{
afceaf40
MHS
1083 {"help", CmdHelp, 1,"This help"},
1084 {"dbg", CmdHF14AMfDbg, 0,"Set default debug mode"},
81740aa5 1085 {"info", CmdHF14AMfUInfo, 0,"Taginfo"},
1086 {"dump", CmdHF14AMfUDump, 0,"Dump MIFARE Ultralight / Ultralight-C tag to binary file"},
f168b263 1087 {"rdbl", CmdHF14AMfURdBl, 0,"Read block - MIFARE Ultralight"},
afceaf40 1088 {"wrbl", CmdHF14AMfUWrBl, 0,"Write block - MIFARE Ultralight"},
f168b263 1089 {"crdbl", CmdHF14AMfUCRdBl, 0,"Read block - MIFARE Ultralight C"},
1090 {"cwrbl", CmdHF14AMfUCWrBl, 0,"Write block - MIFARE Ultralight C"},
1091 {"cauth", CmdHF14AMfucAuth, 0,"Ultralight C Authentication"},
1092 {"setpwd", CmdHF14AMfucSetPwd , 1, "Set 3des password [Ultralight-C only]"},
1093 {"setuid", CmdHF14AMfucSetUid , 1, "Set UID"},
1094 {"gen", CmdHF14AMfuGenDiverseKeys , 1, "Generate 3des mifare diversified keys"},
afceaf40 1095 {NULL, NULL, 0, NULL}
81740aa5 1096};
1097
1098int CmdHFMFUltra(const char *Cmd){
afceaf40
MHS
1099 WaitForResponseTimeout(CMD_ACK,NULL,100);
1100 CmdsParse(CommandTable, Cmd);
1101 return 0;
81740aa5 1102}
1103
1104int CmdHelp(const char *Cmd){
afceaf40
MHS
1105 CmdsHelp(CommandTable);
1106 return 0;
f168b263 1107}
Impressum, Datenschutz