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