]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmfu.c
1886fc3d8fecadba54d869e5cf84356345bf9e2c
[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
16 #define MAX_UL_BLOCKS 0x0f
17 #define MAX_ULC_BLOCKS 0x2f
18 #define MAX_ULEV1a_BLOCKS 0x0b;
19 #define MAX_ULEV1b_BLOCKS 0x20;
20
21 uint8_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
31 static int CmdHelp(const char *Cmd);
32
33 // return 1 if tag responded to 0x1A.
34 uint8_t requestAuthentication( uint8_t* nonce){
35
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.
42
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 }
55
56 typedef 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
68 uint8_t GetHF14AMfU_Type(uint8_t *data, uint8_t dataSize){
69 TagTypeUL_t tagtype = UNKNOWN;
70 uint8_t isOK = 0;
71
72 UsbCommand c = {CMD_MIFAREU_READCARD, {0, 4}};
73 SendCommand(&c);
74 UsbCommand resp;
75
76 if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
77 isOK = resp.arg[0] & 0xff;
78 memcpy(data, resp.d.asBytes, dataSize);
79
80 if (!isOK) {
81 PrintAndLog("Error reading from tag");
82 return UL_ERROR;
83 }
84 } else {
85 PrintAndLog("Command execute timed out");
86 return UL_ERROR;
87 }
88
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)) {
99
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
129 int 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
152 // UID
153 memcpy( datatemp, data, 3);
154 memcpy( datatemp+3, data+4, 4);
155
156 PrintAndLog(" UID : %s ", sprint_hex(datatemp, 7));
157 PrintAndLog(" UID[0] : (Manufacturer Byte) = %02x, Manufacturer: %s", datatemp[0], getTagInfo(datatemp[0]) );
158
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 )
163 PrintAndLog(" BCC0 : %02x - Ok", data[3]);
164 else
165 PrintAndLog(" BCC0 : %02x - crc should be %02x", data[3], crc0);
166
167 int crc1 = data[4] ^ data[5] ^ data[6] ^data[7];
168 if ( data[8] == crc1 )
169 PrintAndLog(" BCC1 : %02x - Ok", data[8]);
170 else
171 PrintAndLog(" BCC1 : %02x - crc should be %02x", data[8], crc1 );
172
173 PrintAndLog(" Internal : %s ", sprint_hex(data + 9, 1));
174
175 memcpy(datatemp, data+10, 2);
176 PrintAndLog(" Lock : %s - %s", sprint_hex(datatemp, 2),printBits( 2, &datatemp) );
177 PrintAndLog("OneTimePad : %s ", sprint_hex(data + 3*4, 4));
178 PrintAndLog("");
179
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 }
199 return 0;
200 }
201
202 //
203 // Mifare Ultralight Write Single Block
204 //
205 int CmdHF14AMfUWrBl(const char *Cmd){
206 uint8_t blockNo = -1;
207 bool chinese_card = FALSE;
208 uint8_t bldata[16] = {0x00};
209 UsbCommand resp;
210
211 char cmdp = param_getchar(Cmd, 0);
212 if (strlen(Cmd) < 3 || cmdp == 'h' || cmdp == 'H') {
213 PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]");
214 PrintAndLog(" [block number]");
215 PrintAndLog(" [block data] - (8 hex symbols)");
216 PrintAndLog(" [w] - Chinese magic ultralight tag");
217 PrintAndLog("");
218 PrintAndLog(" sample: hf mfu wrbl 0 01020304");
219 PrintAndLog("");
220 return 0;
221 }
222
223 blockNo = param_get8(Cmd, 0);
224
225 if (blockNo > MAX_UL_BLOCKS){
226 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
227 return 1;
228 }
229
230 if (param_gethex(Cmd, 1, bldata, 8)) {
231 PrintAndLog("Block data must include 8 HEX symbols");
232 return 1;
233 }
234
235 if (strchr(Cmd,'w') != 0 || strchr(Cmd,'W') != 0 ) {
236 chinese_card = TRUE;
237 }
238
239 if ( blockNo <= 3) {
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 {
256 PrintAndLog("--block no:%02x", blockNo);
257 PrintAndLog("--data: %s", sprint_hex(bldata, 4));
258 UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}};
259 memcpy(e.d.asBytes,bldata, 4);
260 SendCommand(&e);
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;
269 }
270
271 //
272 // Mifare Ultralight Read Single Block
273 //
274 int CmdHF14AMfURdBl(const char *Cmd){
275
276 UsbCommand resp;
277 uint8_t blockNo = -1;
278 char cmdp = param_getchar(Cmd, 0);
279
280 if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') {
281 PrintAndLog("Usage: hf mfu rdbl <block number>");
282 PrintAndLog(" sample: hfu mfu rdbl 0");
283 return 0;
284 }
285
286 blockNo = param_get8(Cmd, 0);
287
288 if (blockNo > MAX_UL_BLOCKS){
289 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight");
290 return 1;
291 }
292
293 UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}};
294 SendCommand(&c);
295
296
297 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
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 }
306 } else {
307 PrintAndLog("Command execute time-out");
308 }
309
310 return 0;
311 }
312
313 //
314 // Mifare Ultralight / Ultralight-C; Read and Dump Card Contents
315 //
316 int CmdHF14AMfUDump(const char *Cmd){
317
318 FILE *fout;
319 char filename[FILE_PATH_SIZE] = {0x00};
320 char * fnameptr = filename;
321
322 uint8_t *lockbytes_t = NULL;
323 uint8_t lockbytes[2] = {0x00};
324
325 uint8_t *lockbytes_t2 = NULL;
326 uint8_t lockbytes2[2] = {0x00};
327
328 bool bit[16] = {0x00};
329 bool bit2[16] = {0x00};
330
331 int i;
332 uint8_t BlockNo = 0;
333 int Pages = 16;
334
335 bool tmplockbit = false;
336 uint8_t isOK = 0;
337 uint8_t *data = NULL;
338
339 char cmdp = param_getchar(Cmd, 0);
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>");
345 PrintAndLog(" <c> optional cardtype c == Ultralight-C, Defaults to Ultralight");
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
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");
385
386 UsbCommand c = {CMD_MIFAREU_READCARD, {BlockNo,Pages}};
387 SendCommand(&c);
388 UsbCommand resp;
389
390 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
391 isOK = resp.arg[0] & 0xff;
392 if (!isOK) {
393 PrintAndLog("Command error");
394 return 0;
395 }
396 data = resp.d.asBytes;
397 } else {
398 PrintAndLog("Command execute time-out");
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 }
429
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
483 if (len > FILE_PATH_SIZE-5) len = FILE_PATH_SIZE-5;
484
485 // user supplied filename?
486 if (len < 1) {
487
488 // UID = data 0-1-2 4-5-6-7 (skips a beat)
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]);
491
492 } else {
493 sprintf(fnameptr + len," .bin");
494 }
495
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);
505 return 0;
506 }
507
508 // Needed to Authenticate to Ultralight C tags
509 void rol (uint8_t *data, const size_t len){
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;
515 }
516
517 //-------------------------------------------------------------------------------
518 // Ultralight C Methods
519 //-------------------------------------------------------------------------------
520
521 //
522 // Ultralight C Authentication Demo {currently uses hard-coded key}
523 //
524 int CmdHF14AMfucAuth(const char *Cmd){
525
526 uint8_t keyNo = 0;
527 bool errors = false;
528
529 char cmdp = param_getchar(Cmd, 0);
530
531 //Change key to user defined one
532 if (cmdp == 'k' || cmdp == 'K'){
533 keyNo = param_get8(Cmd, 1);
534 if(keyNo > 6)
535 errors = true;
536 }
537
538 if (cmdp == 'h' || cmdp == 'H')
539 errors = true;
540
541 if (errors) {
542 PrintAndLog("Usage: hf mfu cauth k <key number>");
543 PrintAndLog(" 0 (default): 3DES standard key");
544 PrintAndLog(" 1 : all 0x00 key");
545 PrintAndLog(" 2 : 0x00-0x0F key");
546 PrintAndLog(" 3 : nfc key");
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");
551 PrintAndLog(" : hf mfu cauth k 3");
552 return 0;
553 }
554
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
564 int try3DesAuthentication( uint8_t *key){
565
566 uint8_t blockNo = 0;
567 uint32_t cuid = 0;
568
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
577 UsbCommand c = {CMD_MIFAREUC_AUTH1, {blockNo}};
578 SendCommand(&c);
579 UsbCommand resp;
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);
585
586 des3_set2key_dec(&ctx, key);
587 // context, mode, length, IV, input, output
588 des3_crypt_cbc( &ctx, DES_DECRYPT, sizeof(random_b), iv , enc_random_b , random_b);
589
590 rol(random_b,8);
591 memcpy(rnd_ab ,random_a,8);
592 memcpy(rnd_ab+8,random_b,8);
593
594 des3_set2key_enc(&ctx, key);
595 // context, mode, length, IV, input, output
596 des3_crypt_cbc(&ctx, DES_ENCRYPT, sizeof(rnd_ab), enc_random_b, rnd_ab, rnd_ab);
597
598 //Auth2
599 c.cmd = CMD_MIFAREUC_AUTH2;
600 c.arg[0] = cuid;
601 memcpy(c.d.asBytes, rnd_ab, 16);
602 SendCommand(&c);
603
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);
610
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;
617 return 0;
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));
625 }
626
627 /**
628 A test function to validate that the polarssl-function works the same
629 was as the openssl-implementation.
630 Commented out, since it requires openssl
631
632 int 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);
709
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 **/
723 //
724 // Ultralight C Read Single Block
725 //
726 int CmdHF14AMfUCRdBl(const char *Cmd)
727 {
728 UsbCommand resp;
729 bool hasPwd = FALSE;
730 uint8_t blockNo = -1;
731 unsigned char key[16];
732 char cmdp = param_getchar(Cmd, 0);
733
734 if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') {
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");
739 return 0;
740 }
741
742 blockNo = param_get8(Cmd, 0);
743 if (blockNo < 0) {
744 PrintAndLog("Wrong block number");
745 return 1;
746 }
747
748 if (blockNo > MAX_ULC_BLOCKS ){
749 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C");
750 return 1;
751 }
752
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 }
762
763 //Read Block
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 }
780 } else {
781 PrintAndLog("Command execute time-out");
782 }
783 return 0;
784 }
785
786 //
787 // Mifare Ultralight C Write Single Block
788 //
789 int CmdHF14AMfUCWrBl(const char *Cmd){
790
791 uint8_t blockNo = -1;
792 bool chinese_card = FALSE;
793 uint8_t bldata[16] = {0x00};
794 UsbCommand resp;
795
796 char cmdp = param_getchar(Cmd, 0);
797
798 if (strlen(Cmd) < 3 || cmdp == 'h' || cmdp == 'H') {
799 PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]");
800 PrintAndLog(" [block number]");
801 PrintAndLog(" [block data] - (8 hex symbols)");
802 PrintAndLog(" [w] - Chinese magic ultralight tag");
803 PrintAndLog("");
804 PrintAndLog(" sample: hf mfu cwrbl 0 01020304");
805 PrintAndLog("");
806 return 0;
807 }
808
809 blockNo = param_get8(Cmd, 0);
810 if (blockNo > MAX_ULC_BLOCKS ){
811 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!");
812 return 1;
813 }
814
815 if (param_gethex(Cmd, 1, bldata, 8)) {
816 PrintAndLog("Block data must include 8 HEX symbols");
817 return 1;
818 }
819
820 if (strchr(Cmd,'w') != 0 || strchr(Cmd,'W') != 0 ) {
821 chinese_card = TRUE;
822 }
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 {
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 }
852 }
853 return 0;
854 }
855
856 //
857 // Mifare Ultralight C - Set password
858 //
859 int 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 //
904 int 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
979 int 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
1078 //------------------------------------
1079 // Menu Stuff
1080 //------------------------------------
1081 static command_t CommandTable[] =
1082 {
1083 {"help", CmdHelp, 1,"This help"},
1084 {"dbg", CmdHF14AMfDbg, 0,"Set default debug mode"},
1085 {"info", CmdHF14AMfUInfo, 0,"Taginfo"},
1086 {"dump", CmdHF14AMfUDump, 0,"Dump MIFARE Ultralight / Ultralight-C tag to binary file"},
1087 {"rdbl", CmdHF14AMfURdBl, 0,"Read block - MIFARE Ultralight"},
1088 {"wrbl", CmdHF14AMfUWrBl, 0,"Write block - MIFARE Ultralight"},
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"},
1095 {NULL, NULL, 0, NULL}
1096 };
1097
1098 int CmdHFMFUltra(const char *Cmd){
1099 WaitForResponseTimeout(CMD_ACK,NULL,100);
1100 CmdsParse(CommandTable, Cmd);
1101 return 0;
1102 }
1103
1104 int CmdHelp(const char *Cmd){
1105 CmdsHelp(CommandTable);
1106 return 0;
1107 }
Impressum, Datenschutz