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