]>
Commit | Line | Data |
---|---|---|
f38a1528 | 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 <openssl/des.h> | |
5f2cb31b | 11 | #include "cmdhfmfu.h" |
f38a1528 | 12 | #include "cmdhfmf.h" |
99a71418 | 13 | #include "cmdhf14a.h" |
f38a1528 | 14 | |
5f2cb31b | 15 | |
c0e6c18b | 16 | #define MAX_ULTRA_BLOCKS 0x0f |
17 | #define MAX_ULTRAC_BLOCKS 0x2f | |
18 | //#define MAX_ULTRAC_BLOCKS 0x2c | |
99a71418 | 19 | uint8_t key1_blnk_data[16] = { 0x00 }; |
f38a1528 | 20 | uint8_t key2_defa_data[16] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f }; |
21 | uint8_t key3_3des_data[16] = { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 }; | |
22 | uint8_t key4_nfc_data[16] = { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 }; | |
99a71418 | 23 | uint8_t key5_ones_data[16] = { 0x01 }; |
f38a1528 | 24 | |
25 | static int CmdHelp(const char *Cmd); | |
26 | ||
e3ab50ca | 27 | int CmdHF14AMfUInfo(const char *Cmd){ |
28 | ||
29 | uint8_t datatemp[7] = {0x00}; | |
30 | uint8_t isOK = 0; | |
31 | uint8_t *data = NULL; | |
32 | ||
33 | UsbCommand c = {CMD_MIFAREU_READCARD, {0, 4}}; | |
34 | SendCommand(&c); | |
35 | UsbCommand resp; | |
36 | ||
37 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) { | |
38 | isOK = resp.arg[0] & 0xff; | |
39 | data = resp.d.asBytes; | |
40 | ||
41 | if (!isOK) { | |
42 | PrintAndLog("Error reading from tag"); | |
43 | return -1; | |
44 | } | |
45 | } else { | |
46 | PrintAndLog("Command execute timed out"); | |
47 | return -1; | |
48 | } | |
49 | ||
99a71418 | 50 | PrintAndLog(""); |
51 | PrintAndLog("-- Mifare Ultralight / Ultralight-C Tag Information ---------"); | |
52 | PrintAndLog("-------------------------------------------------------------"); | |
53 | ||
e3ab50ca | 54 | // UID |
99a71418 | 55 | memcpy( datatemp, data, 3); |
e3ab50ca | 56 | memcpy( datatemp+3, data+4, 4); |
99a71418 | 57 | |
58 | PrintAndLog("MANUFACTURER : %s", getTagInfo(datatemp[0])); | |
59 | PrintAndLog(" UID : %s ", sprint_hex(datatemp, 7)); | |
e3ab50ca | 60 | // BBC |
61 | // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2 | |
62 | int crc0 = 0x88 ^ data[0] ^ data[1] ^data[2]; | |
63 | if ( data[3] == crc0 ) | |
99a71418 | 64 | PrintAndLog(" BCC0 : %02x - Ok", data[3]); |
e3ab50ca | 65 | else |
99a71418 | 66 | PrintAndLog(" BCC0 : %02x - crc should be %02x", data[3], crc0); |
e3ab50ca | 67 | |
68 | int crc1 = data[4] ^ data[5] ^ data[6] ^data[7]; | |
69 | if ( data[8] == crc1 ) | |
99a71418 | 70 | PrintAndLog(" BCC1 : %02x - Ok", data[8]); |
e3ab50ca | 71 | else |
99a71418 | 72 | PrintAndLog(" BCC1 : %02x - crc should be %02x", data[8], crc1 ); |
e3ab50ca | 73 | |
99a71418 | 74 | PrintAndLog(" Internal : %s ", sprint_hex(data + 9, 1)); |
e3ab50ca | 75 | |
76 | memcpy(datatemp, data+10, 2); | |
99a71418 | 77 | PrintAndLog(" Lock : %s - %s", sprint_hex(datatemp, 2),printBits( 2, &datatemp) ); |
78 | PrintAndLog(" OneTimePad : %s ", sprint_hex(data + 3*4, 4)); | |
e3ab50ca | 79 | PrintAndLog(""); |
80 | ||
5f2cb31b | 81 | int len = CmdHF14AMfucAuth("K 0"); |
82 | // PrintAndLog("CODE: %d",len); | |
83 | ||
84 | PrintAndLog("Seems to be a Ultralight %s", (len==0) ? "-C" :""); | |
e3ab50ca | 85 | return 0; |
86 | } | |
87 | ||
f38a1528 | 88 | // |
89 | // Mifare Ultralight Write Single Block | |
90 | // | |
91 | int CmdHF14AMfUWrBl(const char *Cmd){ | |
c0e6c18b | 92 | uint8_t blockNo = -1; |
5f2cb31b | 93 | bool chinese_card = FALSE; |
149aeada | 94 | uint8_t bldata[16] = {0x00}; |
f38a1528 | 95 | UsbCommand resp; |
5f2cb31b | 96 | |
97 | char cmdp = param_getchar(Cmd, 0); | |
98 | if (strlen(Cmd) < 3 || cmdp == 'h' || cmdp == 'H') { | |
99 | PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]"); | |
100 | PrintAndLog(" [block number]"); | |
99a71418 | 101 | PrintAndLog(" [block data] - (8 hex symbols)"); |
5f2cb31b | 102 | PrintAndLog(" [w] - Chinese magic ultralight tag"); |
99a71418 | 103 | PrintAndLog(""); |
225ccb91 | 104 | PrintAndLog(" sample: hf mfu wrbl 0 01020304"); |
5f2cb31b | 105 | PrintAndLog(""); |
f38a1528 | 106 | return 0; |
107 | } | |
5f2cb31b | 108 | |
109 | blockNo = param_get8(Cmd, 0); | |
c0e6c18b | 110 | |
5f2cb31b | 111 | if (blockNo > MAX_ULTRA_BLOCKS){ |
f38a1528 | 112 | PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!"); |
113 | return 1; | |
114 | } | |
5f2cb31b | 115 | |
f38a1528 | 116 | if (param_gethex(Cmd, 1, bldata, 8)) { |
117 | PrintAndLog("Block data must include 8 HEX symbols"); | |
118 | return 1; | |
119 | } | |
5f2cb31b | 120 | |
121 | if (strchr(Cmd,'w') != 0 || strchr(Cmd,'W') != 0 ) { | |
122 | chinese_card = TRUE; | |
f38a1528 | 123 | } |
5f2cb31b | 124 | |
125 | if ( blockNo <= 3) { | |
126 | if (!chinese_card){ | |
127 | PrintAndLog("Access Denied"); | |
128 | } else { | |
129 | PrintAndLog("--specialblock no:%02x", blockNo); | |
130 | PrintAndLog("--data: %s", sprint_hex(bldata, 4)); | |
131 | UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}}; | |
132 | memcpy(d.d.asBytes,bldata, 4); | |
133 | SendCommand(&d); | |
134 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
135 | uint8_t isOK = resp.arg[0] & 0xff; | |
136 | PrintAndLog("isOk:%02x", isOK); | |
137 | } else { | |
138 | PrintAndLog("Command execute timeout"); | |
139 | } | |
140 | } | |
141 | } else { | |
f38a1528 | 142 | PrintAndLog("--block no:%02x", blockNo); |
143 | PrintAndLog("--data: %s", sprint_hex(bldata, 4)); | |
144 | UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}}; | |
5f2cb31b | 145 | memcpy(e.d.asBytes,bldata, 4); |
146 | SendCommand(&e); | |
f38a1528 | 147 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { |
148 | uint8_t isOK = resp.arg[0] & 0xff; | |
149 | PrintAndLog("isOk:%02x", isOK); | |
150 | } else { | |
151 | PrintAndLog("Command execute timeout"); | |
152 | } | |
f38a1528 | 153 | } |
154 | return 0; | |
155 | } | |
156 | ||
157 | // | |
158 | // Mifare Ultralight Read Single Block | |
159 | // | |
160 | int CmdHF14AMfURdBl(const char *Cmd){ | |
161 | ||
c0e6c18b | 162 | uint8_t blockNo = -1; |
5f2cb31b | 163 | |
164 | char cmdp = param_getchar(Cmd, 0); | |
165 | ||
166 | if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') { | |
225ccb91 | 167 | PrintAndLog("Usage: hf mfu rdbl <block number>"); |
168 | PrintAndLog(" sample: hfu mfu rdbl 0"); | |
f38a1528 | 169 | return 0; |
170 | } | |
171 | ||
172 | blockNo = param_get8(Cmd, 0); | |
c0e6c18b | 173 | |
174 | if (blockNo > MAX_ULTRA_BLOCKS){ | |
175 | PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!"); | |
176 | return 1; | |
177 | } | |
178 | ||
5f2cb31b | 179 | PrintAndLog("--block no:0x%02X (%d)", (int)blockNo, blockNo); |
f38a1528 | 180 | UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}}; |
181 | SendCommand(&c); | |
182 | ||
183 | UsbCommand resp; | |
184 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
185 | uint8_t isOK = resp.arg[0] & 0xff; | |
186 | uint8_t * data = resp.d.asBytes; | |
5f2cb31b | 187 | |
188 | PrintAndLog("isOk: %02x", isOK); | |
189 | ||
f38a1528 | 190 | if (isOK) |
5f2cb31b | 191 | PrintAndLog("Data: %s", sprint_hex(data, 4)); |
192 | } else { | |
d3499d36 | 193 | PrintAndLog("Command execute timeout"); |
194 | } | |
f38a1528 | 195 | return 0; |
196 | } | |
197 | ||
198 | // | |
99a71418 | 199 | // Mifare Ultralight / Ultralight-C; Read and Dump Card Contents |
f38a1528 | 200 | // |
99a71418 | 201 | int CmdHF14AMfUDump(const char *Cmd){ |
202 | ||
203 | FILE *fout; | |
204 | char filename[FILE_PATH_SIZE] = {0x00}; | |
205 | char * fnameptr = filename; | |
206 | ||
e3ab50ca | 207 | uint8_t *lockbytes_t = NULL; |
208 | uint8_t lockbytes[2] = {0x00}; | |
99a71418 | 209 | |
210 | uint8_t *lockbytes_t2 = NULL; | |
211 | uint8_t lockbytes2[2] = {0x00}; | |
f38a1528 | 212 | |
99a71418 | 213 | bool bit[16] = {0x00}; |
214 | bool bit2[16] = {0x00}; | |
215 | ||
f38a1528 | 216 | int i; |
149aeada | 217 | uint8_t BlockNo = 0; |
218 | int Pages = 16; | |
99a71418 | 219 | |
220 | bool tmplockbit = false; | |
149aeada | 221 | uint8_t isOK = 0; |
99a71418 | 222 | uint8_t *data = NULL; |
f38a1528 | 223 | |
99a71418 | 224 | char cmdp = param_getchar(Cmd, 0); |
225 | ||
226 | if (cmdp == 'h' || cmdp == 'H') { | |
227 | PrintAndLog("Reads all pages from Mifare Ultralight or Ultralight-C tag."); | |
228 | PrintAndLog("It saves binary dump into the file `filename.bin` or `cardUID.bin`"); | |
229 | PrintAndLog("Usage: hf mfu dump <c> <filename w/o .bin>"); | |
230 | PrintAndLog(" <c> optional cardtype c == Ultralight-C, if not defaults to Ultralight"); | |
231 | PrintAndLog(" sample: hf mfu dump"); | |
232 | PrintAndLog(" : hf mfu dump myfile"); | |
233 | PrintAndLog(" : hf mfu dump c myfile"); | |
234 | return 0; | |
235 | } | |
236 | ||
237 | // UL or UL-C? | |
238 | Pages = (cmdp == 'c' || cmdp == 'C') ? 44 : 16; | |
239 | ||
240 | PrintAndLog("Dumping Ultralight%s Card Data...", (Pages ==16)?"":"-C"); | |
f38a1528 | 241 | |
f38a1528 | 242 | UsbCommand c = {CMD_MIFAREU_READCARD, {BlockNo,Pages}}; |
243 | SendCommand(&c); | |
244 | UsbCommand resp; | |
245 | ||
246 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
99a71418 | 247 | isOK = resp.arg[0] & 0xff; |
248 | if (!isOK) { | |
249 | PrintAndLog("Command error"); | |
250 | return 0; | |
251 | } | |
252 | data = resp.d.asBytes; | |
253 | } else { | |
254 | PrintAndLog("Command execute timeout"); | |
255 | return 0; | |
256 | } | |
257 | ||
258 | // Load lock bytes. | |
259 | int j = 0; | |
260 | ||
261 | lockbytes_t = data + 8; | |
262 | lockbytes[0] = lockbytes_t[2]; | |
263 | lockbytes[1] = lockbytes_t[3]; | |
264 | for(j = 0; j < 16; j++){ | |
265 | bit[j] = lockbytes[j/8] & ( 1 <<(7-j%8)); | |
266 | } | |
267 | ||
268 | // Load bottom lockbytes if available | |
269 | if ( Pages == 44 ) { | |
270 | ||
271 | lockbytes_t2 = data + (40*4); | |
272 | lockbytes2[0] = lockbytes_t2[2]; | |
273 | lockbytes2[1] = lockbytes_t2[3]; | |
274 | for (j = 0; j < 16; j++) { | |
275 | bit2[j] = lockbytes2[j/8] & ( 1 <<(7-j%8)); | |
276 | } | |
f38a1528 | 277 | } |
99a71418 | 278 | |
279 | for (i = 0; i < Pages; ++i) { | |
280 | ||
281 | if ( i < 3 ) { | |
282 | PrintAndLog("Block %02x:%s ", i,sprint_hex(data + i * 4, 4)); | |
283 | continue; | |
284 | } | |
285 | ||
286 | switch(i){ | |
287 | case 3: tmplockbit = bit[4]; break; | |
288 | case 4: tmplockbit = bit[3]; break; | |
289 | case 5: tmplockbit = bit[2]; break; | |
290 | case 6: tmplockbit = bit[1]; break; | |
291 | case 7: tmplockbit = bit[0]; break; | |
292 | case 8: tmplockbit = bit[15]; break; | |
293 | case 9: tmplockbit = bit[14]; break; | |
294 | case 10: tmplockbit = bit[13]; break; | |
295 | case 11: tmplockbit = bit[12]; break; | |
296 | case 12: tmplockbit = bit[11]; break; | |
297 | case 13: tmplockbit = bit[10]; break; | |
298 | case 14: tmplockbit = bit[9]; break; | |
299 | case 15: tmplockbit = bit[8]; break; | |
300 | case 16: | |
301 | case 17: | |
302 | case 18: | |
303 | case 19: tmplockbit = bit2[6]; break; | |
304 | case 20: | |
305 | case 21: | |
306 | case 22: | |
307 | case 23: tmplockbit = bit2[5]; break; | |
308 | case 24: | |
309 | case 25: | |
310 | case 26: | |
311 | case 27: tmplockbit = bit2[4]; break; | |
312 | case 28: | |
313 | case 29: | |
314 | case 30: | |
315 | case 31: tmplockbit = bit2[2]; break; | |
316 | case 32: | |
317 | case 33: | |
318 | case 34: | |
319 | case 35: tmplockbit = bit2[1]; break; | |
320 | case 36: | |
321 | case 37: | |
322 | case 38: | |
323 | case 39: tmplockbit = bit2[0]; break; | |
324 | case 40: tmplockbit = bit2[12]; break; | |
325 | case 41: tmplockbit = bit2[11]; break; | |
326 | case 42: tmplockbit = bit2[10]; break; //auth0 | |
327 | case 43: tmplockbit = bit2[9]; break; //auth1 | |
328 | default: break; | |
329 | } | |
330 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),tmplockbit); | |
331 | } | |
332 | ||
333 | int len = 0; | |
334 | if ( Pages == 16 ) | |
335 | len = param_getstr(Cmd,0,filename); | |
336 | else | |
337 | len = param_getstr(Cmd,1,filename); | |
338 | ||
339 | if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE; | |
340 | ||
341 | // user supplied filename? | |
342 | if (len < 1) { | |
343 | ||
344 | // UID = data 0-1-2 4-5-6-7 (skips a beat) | |
345 | sprintf(fnameptr, "%02X", data[0]); | |
346 | fnameptr += 2; | |
347 | sprintf(fnameptr, "%02X", data[1]); | |
348 | fnameptr += 2; | |
349 | sprintf(fnameptr, "%02X", data[2]); | |
350 | fnameptr += 2; | |
351 | sprintf(fnameptr, "%02X", data[4]); | |
352 | fnameptr += 2; | |
353 | sprintf(fnameptr, "%02X", data[5]); | |
354 | fnameptr += 2; | |
355 | sprintf(fnameptr, "%02X", data[6]); | |
356 | fnameptr += 2; | |
357 | sprintf(fnameptr, "%02X", data[7]); | |
358 | fnameptr += 2; | |
359 | ||
360 | } else { | |
361 | fnameptr += len; | |
362 | } | |
363 | ||
364 | // add file extension | |
365 | sprintf(fnameptr, ".bin"); | |
366 | ||
367 | if ((fout = fopen(filename,"wb")) == NULL) { | |
368 | PrintAndLog("Could not create file name %s", filename); | |
369 | return 1; | |
370 | } | |
371 | fwrite( data, 1, Pages*4, fout ); | |
372 | fclose(fout); | |
373 | ||
374 | PrintAndLog("Dumped %d pages, wrote %d bytes to %s", Pages, Pages*4, filename); | |
f38a1528 | 375 | return 0; |
376 | } | |
377 | ||
378 | // Needed to Authenticate to Ultralight C tags | |
379 | void rol (uint8_t *data, const size_t len){ | |
380 | uint8_t first = data[0]; | |
381 | for (size_t i = 0; i < len-1; i++) { | |
382 | data[i] = data[i+1]; | |
383 | } | |
384 | data[len-1] = first; | |
385 | } | |
386 | ||
387 | //------------------------------------------------------------------------------- | |
388 | // Ultralight C Methods | |
389 | //------------------------------------------------------------------------------- | |
390 | ||
391 | // | |
392 | // Ultralight C Authentication Demo {currently uses hard-coded key} | |
393 | // | |
394 | int CmdHF14AMfucAuth(const char *Cmd){ | |
395 | ||
99a71418 | 396 | uint8_t blockNo = 0, keyNo = 0; |
149aeada | 397 | uint8_t e_RndB[8] = {0x00}; |
99a71418 | 398 | uint32_t cuid = 0; |
149aeada | 399 | unsigned char RndARndB[16] = {0x00}; |
400 | uint8_t key[16] = {0x00}; | |
f38a1528 | 401 | DES_cblock RndA, RndB; |
db297e69 | 402 | DES_cblock iv; |
f38a1528 | 403 | DES_key_schedule ks1,ks2; |
404 | DES_cblock key1,key2; | |
405 | ||
225ccb91 | 406 | char cmdp = param_getchar(Cmd, 0); |
db297e69 | 407 | // |
408 | memset(iv, 0, 8); | |
409 | ||
225ccb91 | 410 | if (cmdp == 'h' || cmdp == 'H') { |
5f2cb31b | 411 | PrintAndLog("Usage: hf mfu cauth k <key number>"); |
225ccb91 | 412 | PrintAndLog(" 1 = all zeros key"); |
413 | PrintAndLog(" 2 = 0x00-0x0F key"); | |
414 | PrintAndLog(" 3 = nfc key"); | |
415 | PrintAndLog(" 4 = all ones key"); | |
416 | PrintAndLog(" defaults to 3DES standard key"); | |
5f2cb31b | 417 | PrintAndLog(" sample : hf mfu cauth k"); |
418 | PrintAndLog(" : hf mfu cauth k 3"); | |
f38a1528 | 419 | return 0; |
420 | } | |
421 | ||
422 | //Change key to user defined one | |
225ccb91 | 423 | if (cmdp == 'k' || cmdp == 'K'){ |
424 | ||
425 | keyNo = param_get8(Cmd, 1); | |
426 | ||
427 | switch(keyNo){ | |
f38a1528 | 428 | case 0: |
429 | memcpy(key,key1_blnk_data,16); | |
430 | break; | |
431 | case 1: | |
432 | memcpy(key,key2_defa_data,16); | |
433 | break; | |
225ccb91 | 434 | case 2: |
f38a1528 | 435 | memcpy(key,key4_nfc_data,16); |
436 | break; | |
225ccb91 | 437 | case 3: |
f38a1528 | 438 | memcpy(key,key5_ones_data,16); |
439 | break; | |
440 | default: | |
441 | memcpy(key,key3_3des_data,16); | |
442 | break; | |
443 | } | |
225ccb91 | 444 | } else { |
f38a1528 | 445 | memcpy(key,key3_3des_data,16); |
446 | } | |
225ccb91 | 447 | |
f38a1528 | 448 | memcpy(key1,key,8); |
449 | memcpy(key2,key+8,8); | |
450 | DES_set_key((DES_cblock *)key1,&ks1); | |
451 | DES_set_key((DES_cblock *)key2,&ks2); | |
452 | ||
225ccb91 | 453 | //Auth1 |
454 | UsbCommand c = {CMD_MIFAREUC_AUTH1, {blockNo}}; | |
455 | SendCommand(&c); | |
456 | UsbCommand resp; | |
457 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
458 | uint8_t isOK = resp.arg[0] & 0xff; | |
459 | cuid = resp.arg[1]; | |
460 | uint8_t * data= resp.d.asBytes; | |
461 | ||
462 | if (isOK){ | |
463 | PrintAndLog("enc(RndB):%s", sprint_hex(data+1, 8)); | |
464 | memcpy(e_RndB,data+1,8); | |
5f2cb31b | 465 | } else { |
466 | return 2; // auth failed. | |
467 | } | |
225ccb91 | 468 | } else { |
469 | PrintAndLog("Command execute timeout"); | |
5f2cb31b | 470 | return 1; |
f38a1528 | 471 | } |
f38a1528 | 472 | |
473 | //Do crypto magic | |
474 | DES_random_key(&RndA); | |
475 | DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0); | |
476 | PrintAndLog(" RndB:%s",sprint_hex(RndB, 8)); | |
477 | PrintAndLog(" RndA:%s",sprint_hex(RndA, 8)); | |
478 | rol(RndB,8); | |
479 | memcpy(RndARndB,RndA,8); | |
480 | memcpy(RndARndB+8,RndB,8); | |
481 | PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16)); | |
482 | DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1); | |
483 | PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16)); | |
484 | ||
485 | //Auth2 | |
486 | UsbCommand d = {CMD_MIFAREUC_AUTH2, {cuid}}; | |
487 | memcpy(d.d.asBytes,RndARndB, 16); | |
488 | SendCommand(&d); | |
489 | ||
225ccb91 | 490 | UsbCommand respb; |
491 | if (WaitForResponseTimeout(CMD_ACK,&respb,1500)) { | |
492 | uint8_t isOK = respb.arg[0] & 0xff; | |
493 | uint8_t * data2= respb.d.asBytes; | |
f38a1528 | 494 | |
225ccb91 | 495 | if (isOK){ |
496 | PrintAndLog("enc(RndA'):%s", sprint_hex(data2+1, 8)); | |
5f2cb31b | 497 | } else { |
498 | return 2; | |
225ccb91 | 499 | } |
5f2cb31b | 500 | |
225ccb91 | 501 | } else { |
502 | PrintAndLog("Command execute timeout"); | |
5f2cb31b | 503 | return 1; |
225ccb91 | 504 | } |
5f2cb31b | 505 | return 0; |
f38a1528 | 506 | } |
507 | ||
508 | // | |
509 | // Ultralight C Read Single Block | |
510 | // | |
511 | int CmdHF14AMfUCRdBl(const char *Cmd) | |
512 | { | |
5f2cb31b | 513 | uint8_t blockNo = -1; |
514 | char cmdp = param_getchar(Cmd, 0); | |
515 | ||
516 | if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') { | |
225ccb91 | 517 | PrintAndLog("Usage: hf mfu crdbl <block number>"); |
518 | PrintAndLog(" sample: hf mfu crdbl 0"); | |
f38a1528 | 519 | return 0; |
520 | } | |
521 | ||
522 | blockNo = param_get8(Cmd, 0); | |
5f2cb31b | 523 | if (blockNo < 0) { |
524 | PrintAndLog("Wrong block number"); | |
525 | return 1; | |
526 | } | |
527 | ||
c0e6c18b | 528 | if (blockNo > MAX_ULTRAC_BLOCKS ){ |
5f2cb31b | 529 | PrintAndLog("Error: Maximum number of readable blocks is 47 for Ultralight-C Cards!"); |
f38a1528 | 530 | return 1; |
5f2cb31b | 531 | } |
532 | ||
533 | PrintAndLog("--block no: 0x%02X (%d)", (int)blockNo, blockNo); | |
f38a1528 | 534 | |
535 | //Read Block | |
536 | UsbCommand e = {CMD_MIFAREU_READBL, {blockNo}}; | |
537 | SendCommand(&e); | |
538 | UsbCommand resp_c; | |
539 | if (WaitForResponseTimeout(CMD_ACK,&resp_c,1500)) { | |
5f2cb31b | 540 | uint8_t isOK = resp_c.arg[0] & 0xff; |
541 | uint8_t *data = resp_c.d.asBytes; | |
542 | ||
543 | PrintAndLog("isOk: %02x", isOK); | |
f38a1528 | 544 | if (isOK) |
5f2cb31b | 545 | PrintAndLog("Data: %s", sprint_hex(data, 4)); |
546 | ||
547 | } else { | |
548 | PrintAndLog("Command execute timeout"); | |
549 | } | |
f38a1528 | 550 | return 0; |
551 | } | |
552 | ||
f38a1528 | 553 | // |
554 | // Mifare Ultralight C Write Single Block | |
555 | // | |
556 | int CmdHF14AMfUCWrBl(const char *Cmd){ | |
557 | ||
c0e6c18b | 558 | uint8_t blockNo = -1; |
5f2cb31b | 559 | bool chinese_card = FALSE; |
149aeada | 560 | uint8_t bldata[16] = {0x00}; |
f38a1528 | 561 | UsbCommand resp; |
5f2cb31b | 562 | |
563 | char cmdp = param_getchar(Cmd, 0); | |
564 | ||
565 | if (strlen(Cmd) < 3 || cmdp == 'h' || cmdp == 'H') { | |
225ccb91 | 566 | PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]"); |
5f2cb31b | 567 | PrintAndLog(" [block number]"); |
568 | PrintAndLog(" [block data] - (8 hex symbols)"); | |
569 | PrintAndLog(" [w] - Chinese magic ultralight tag"); | |
570 | PrintAndLog(""); | |
571 | PrintAndLog(" sample: hf mfu cwrbl 0 01020304"); | |
572 | PrintAndLog(""); | |
f38a1528 | 573 | return 0; |
5f2cb31b | 574 | } |
575 | ||
f38a1528 | 576 | blockNo = param_get8(Cmd, 0); |
c0e6c18b | 577 | if (blockNo > MAX_ULTRAC_BLOCKS ){ |
5f2cb31b | 578 | PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!"); |
f38a1528 | 579 | return 1; |
580 | } | |
5f2cb31b | 581 | |
f38a1528 | 582 | if (param_gethex(Cmd, 1, bldata, 8)) { |
583 | PrintAndLog("Block data must include 8 HEX symbols"); | |
584 | return 1; | |
585 | } | |
5f2cb31b | 586 | |
587 | if (strchr(Cmd,'w') != 0 || strchr(Cmd,'W') != 0 ) { | |
588 | chinese_card = TRUE; | |
f38a1528 | 589 | } |
5f2cb31b | 590 | |
591 | if ( blockNo <= 3 ) { | |
592 | if (!chinese_card){ | |
593 | PrintAndLog("Access Denied"); | |
594 | } else { | |
595 | PrintAndLog("--Special block no: 0x%02x", blockNo); | |
596 | PrintAndLog("--Data: %s", sprint_hex(bldata, 4)); | |
597 | UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}}; | |
598 | memcpy(d.d.asBytes,bldata, 4); | |
599 | SendCommand(&d); | |
600 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
601 | uint8_t isOK = resp.arg[0] & 0xff; | |
602 | PrintAndLog("isOk:%02x", isOK); | |
603 | } else { | |
604 | PrintAndLog("Command execute timeout"); | |
605 | } | |
606 | } | |
607 | } else { | |
608 | PrintAndLog("--Block no : 0x%02x", blockNo); | |
609 | PrintAndLog("--Data: %s", sprint_hex(bldata, 4)); | |
f38a1528 | 610 | UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}}; |
611 | memcpy(e.d.asBytes,bldata, 4); | |
612 | SendCommand(&e); | |
613 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
614 | uint8_t isOK = resp.arg[0] & 0xff; | |
5f2cb31b | 615 | PrintAndLog("isOk : %02x", isOK); |
f38a1528 | 616 | } else { |
617 | PrintAndLog("Command execute timeout"); | |
618 | } | |
5f2cb31b | 619 | } |
620 | return 0; | |
f38a1528 | 621 | } |
622 | ||
623 | //------------------------------------ | |
624 | // Menu Stuff | |
625 | //------------------------------------ | |
626 | static command_t CommandTable[] = | |
627 | { | |
e3ab50ca | 628 | {"help", CmdHelp, 1,"This help"}, |
629 | {"dbg", CmdHF14AMfDbg, 0,"Set default debug mode"}, | |
630 | {"info", CmdHF14AMfUInfo, 0,"Taginfo"}, | |
5f2cb31b | 631 | {"dump", CmdHF14AMfUDump, 0,"Dump MIFARE Ultralight / Ultralight-C tag to binary file"}, |
e3ab50ca | 632 | {"rdbl", CmdHF14AMfURdBl, 0,"Read block - MIFARE Ultralight"}, |
5f2cb31b | 633 | {"wrbl", CmdHF14AMfUWrBl, 0,"Write block - MIFARE Ultralight"}, |
e3ab50ca | 634 | {"crdbl", CmdHF14AMfUCRdBl, 0,"Read block - MIFARE Ultralight C"}, |
5f2cb31b | 635 | {"cwrbl", CmdHF14AMfUCWrBl, 0,"Write MIFARE Ultralight C block"}, |
636 | {"cauth", CmdHF14AMfucAuth, 0,"try a Ultralight C Authentication"}, | |
f38a1528 | 637 | {NULL, NULL, 0, NULL} |
638 | }; | |
639 | ||
640 | int CmdHFMFUltra(const char *Cmd){ | |
f38a1528 | 641 | WaitForResponseTimeout(CMD_ACK,NULL,100); |
642 | CmdsParse(CommandTable, Cmd); | |
643 | return 0; | |
644 | } | |
645 | ||
646 | int CmdHelp(const char *Cmd){ | |
647 | CmdsHelp(CommandTable); | |
648 | return 0; | |
649 | } |