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