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