]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhf14a.c
Add two missing bitflip state tables. Update .gitignore
[proxmark3-svn] / client / cmdhf14a.c
1 //-----------------------------------------------------------------------------
2 // 2011, Merlok
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>, Hagen Fritsch
4 //
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
7 // the license.
8 //-----------------------------------------------------------------------------
9 // High frequency ISO14443A commands
10 //-----------------------------------------------------------------------------
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <inttypes.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include "util.h"
18 #include "util_posix.h"
19 #include "iso14443crc.h"
20 #include "data.h"
21 #include "proxmark3.h"
22 #include "ui.h"
23 #include "cmdparser.h"
24 #include "cmdhf14a.h"
25 #include "common.h"
26 #include "cmdmain.h"
27 #include "mifare.h"
28 #include "cmdhfmfu.h"
29
30 static int CmdHelp(const char *Cmd);
31 static void waitCmd(uint8_t iLen);
32
33 // structure and database for uid -> tagtype lookups
34 typedef struct {
35 uint8_t uid;
36 char* desc;
37 } manufactureName;
38
39 const manufactureName manufactureMapping[] = {
40 // ID, "Vendor Country"
41 { 0x01, "Motorola UK" },
42 { 0x02, "ST Microelectronics SA France" },
43 { 0x03, "Hitachi, Ltd Japan" },
44 { 0x04, "NXP Semiconductors Germany" },
45 { 0x05, "Infineon Technologies AG Germany" },
46 { 0x06, "Cylink USA" },
47 { 0x07, "Texas Instrument France" },
48 { 0x08, "Fujitsu Limited Japan" },
49 { 0x09, "Matsushita Electronics Corporation, Semiconductor Company Japan" },
50 { 0x0A, "NEC Japan" },
51 { 0x0B, "Oki Electric Industry Co. Ltd Japan" },
52 { 0x0C, "Toshiba Corp. Japan" },
53 { 0x0D, "Mitsubishi Electric Corp. Japan" },
54 { 0x0E, "Samsung Electronics Co. Ltd Korea" },
55 { 0x0F, "Hynix / Hyundai, Korea" },
56 { 0x10, "LG-Semiconductors Co. Ltd Korea" },
57 { 0x11, "Emosyn-EM Microelectronics USA" },
58 { 0x12, "INSIDE Technology France" },
59 { 0x13, "ORGA Kartensysteme GmbH Germany" },
60 { 0x14, "SHARP Corporation Japan" },
61 { 0x15, "ATMEL France" },
62 { 0x16, "EM Microelectronic-Marin SA Switzerland" },
63 { 0x17, "KSW Microtec GmbH Germany" },
64 { 0x18, "ZMD AG Germany" },
65 { 0x19, "XICOR, Inc. USA" },
66 { 0x1A, "Sony Corporation Japan Identifier Company Country" },
67 { 0x1B, "Malaysia Microelectronic Solutions Sdn. Bhd Malaysia" },
68 { 0x1C, "Emosyn USA" },
69 { 0x1D, "Shanghai Fudan Microelectronics Co. Ltd. P.R. China" },
70 { 0x1E, "Magellan Technology Pty Limited Australia" },
71 { 0x1F, "Melexis NV BO Switzerland" },
72 { 0x20, "Renesas Technology Corp. Japan" },
73 { 0x21, "TAGSYS France" },
74 { 0x22, "Transcore USA" },
75 { 0x23, "Shanghai belling corp., ltd. China" },
76 { 0x24, "Masktech Germany Gmbh Germany" },
77 { 0x25, "Innovision Research and Technology Plc UK" },
78 { 0x26, "Hitachi ULSI Systems Co., Ltd. Japan" },
79 { 0x27, "Cypak AB Sweden" },
80 { 0x28, "Ricoh Japan" },
81 { 0x29, "ASK France" },
82 { 0x2A, "Unicore Microsystems, LLC Russian Federation" },
83 { 0x2B, "Dallas Semiconductor/Maxim USA" },
84 { 0x2C, "Impinj, Inc. USA" },
85 { 0x2D, "RightPlug Alliance USA" },
86 { 0x2E, "Broadcom Corporation USA" },
87 { 0x2F, "MStar Semiconductor, Inc Taiwan, ROC" },
88 { 0x30, "BeeDar Technology Inc. USA" },
89 { 0x31, "RFIDsec Denmark" },
90 { 0x32, "Schweizer Electronic AG Germany" },
91 { 0x33, "AMIC Technology Corp Taiwan" },
92 { 0x34, "Mikron JSC Russia" },
93 { 0x35, "Fraunhofer Institute for Photonic Microsystems Germany" },
94 { 0x36, "IDS Microchip AG Switzerland" },
95 { 0x37, "Kovio USA" },
96 { 0x38, "HMT Microelectronic Ltd Switzerland Identifier Company Country" },
97 { 0x39, "Silicon Craft Technology Thailand" },
98 { 0x3A, "Advanced Film Device Inc. Japan" },
99 { 0x3B, "Nitecrest Ltd UK" },
100 { 0x3C, "Verayo Inc. USA" },
101 { 0x3D, "HID Global USA" },
102 { 0x3E, "Productivity Engineering Gmbh Germany" },
103 { 0x3F, "Austriamicrosystems AG (reserved) Austria" },
104 { 0x40, "Gemalto SA France" },
105 { 0x41, "Renesas Electronics Corporation Japan" },
106 { 0x42, "3Alogics Inc Korea" },
107 { 0x43, "Top TroniQ Asia Limited Hong Kong" },
108 { 0x44, "Gentag Inc (USA) USA" },
109 { 0x00, "no tag-info available" } // must be the last entry
110 };
111
112
113 // get a product description based on the UID
114 // uid[8] tag uid
115 // returns description of the best match
116 char* getTagInfo(uint8_t uid) {
117
118 int i;
119 int len = sizeof(manufactureMapping) / sizeof(manufactureName);
120
121 for ( i = 0; i < len; ++i )
122 if ( uid == manufactureMapping[i].uid)
123 return manufactureMapping[i].desc;
124
125 //No match, return default
126 return manufactureMapping[len-1].desc;
127 }
128
129 int CmdHF14AList(const char *Cmd)
130 {
131 PrintAndLog("Deprecated command, use 'hf list 14a' instead");
132 return 0;
133 }
134
135 int CmdHF14AReader(const char *Cmd)
136 {
137 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}};
138 SendCommand(&c);
139
140 UsbCommand resp;
141 WaitForResponse(CMD_ACK,&resp);
142
143 iso14a_card_select_t card;
144 memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));
145
146 uint64_t select_status = resp.arg[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
147
148 if(select_status == 0) {
149 if (Cmd[0] != 's') PrintAndLog("iso14443a card select failed");
150 // disconnect
151 c.arg[0] = 0;
152 c.arg[1] = 0;
153 c.arg[2] = 0;
154 SendCommand(&c);
155 return 0;
156 }
157
158 if(select_status == 3) {
159 PrintAndLog("Card doesn't support standard iso14443-3 anticollision");
160 PrintAndLog("ATQA : %02x %02x", card.atqa[1], card.atqa[0]);
161 // disconnect
162 c.arg[0] = 0;
163 c.arg[1] = 0;
164 c.arg[2] = 0;
165 SendCommand(&c);
166 return 0;
167 }
168
169 PrintAndLog(" UID : %s", sprint_hex(card.uid, card.uidlen));
170 PrintAndLog("ATQA : %02x %02x", card.atqa[1], card.atqa[0]);
171 PrintAndLog(" SAK : %02x [%d]", card.sak, resp.arg[0]);
172
173 switch (card.sak) {
174 case 0x00:
175
176 //***************************************test****************
177 // disconnect
178 c.arg[0] = 0;
179 c.arg[1] = 0;
180 c.arg[2] = 0;
181 SendCommand(&c);
182
183 uint32_t tagT = GetHF14AMfU_Type();
184 ul_print_type(tagT, 0);
185
186 //reconnect for further tests
187 c.arg[0] = ISO14A_CONNECT | ISO14A_NO_DISCONNECT;
188 c.arg[1] = 0;
189 c.arg[2] = 0;
190
191 SendCommand(&c);
192
193 UsbCommand resp;
194 WaitForResponse(CMD_ACK,&resp);
195
196 memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));
197
198 select_status = resp.arg[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS
199
200 if(select_status == 0) {
201 //PrintAndLog("iso14443a card select failed");
202 // disconnect
203 c.arg[0] = 0;
204 c.arg[1] = 0;
205 c.arg[2] = 0;
206 SendCommand(&c);
207 return 0;
208 }
209
210 /* orig
211 // check if the tag answers to GETVERSION (0x60)
212 c.arg[0] = ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT;
213 c.arg[1] = 1;
214 c.arg[2] = 0;
215 c.d.asBytes[0] = 0x60;
216 SendCommand(&c);
217 WaitForResponse(CMD_ACK,&resp);
218
219 uint8_t version[10] = {0};
220 memcpy(version, resp.d.asBytes, resp.arg[0] < sizeof(version) ? resp.arg[0] : sizeof(version));
221 uint8_t len = resp.arg[0] & 0xff;
222 switch ( len ){
223 // todo, identify "Magic UL-C tags". // they usually have a static nonce response to 0x1A command.
224 // UL-EV1, size, check version[6] == 0x0b (smaller) 0x0b * 4 == 48
225 case 0x0A:PrintAndLog("TYPE : NXP MIFARE Ultralight EV1 %d bytes", (version[6] == 0xB) ? 48 : 128);break;
226 case 0x01:PrintAndLog("TYPE : NXP MIFARE Ultralight C");break;
227 case 0x00:PrintAndLog("TYPE : NXP MIFARE Ultralight");break;
228 }
229 */
230 break;
231 case 0x01: PrintAndLog("TYPE : NXP TNP3xxx Activision Game Appliance"); break;
232 case 0x04: PrintAndLog("TYPE : NXP MIFARE (various !DESFire !DESFire EV1)"); break;
233 case 0x08: PrintAndLog("TYPE : NXP MIFARE CLASSIC 1k | Plus 2k SL1"); break;
234 case 0x09: PrintAndLog("TYPE : NXP MIFARE Mini 0.3k"); break;
235 case 0x10: PrintAndLog("TYPE : NXP MIFARE Plus 2k SL2"); break;
236 case 0x11: PrintAndLog("TYPE : NXP MIFARE Plus 4k SL2"); break;
237 case 0x18: PrintAndLog("TYPE : NXP MIFARE Classic 4k | Plus 4k SL1"); break;
238 case 0x20: PrintAndLog("TYPE : NXP MIFARE DESFire 4k | DESFire EV1 2k/4k/8k | Plus 2k/4k SL3 | JCOP 31/41"); break;
239 case 0x24: PrintAndLog("TYPE : NXP MIFARE DESFire | DESFire EV1"); break;
240 case 0x28: PrintAndLog("TYPE : JCOP31 or JCOP41 v2.3.1"); break;
241 case 0x38: PrintAndLog("TYPE : Nokia 6212 or 6131 MIFARE CLASSIC 4K"); break;
242 case 0x88: PrintAndLog("TYPE : Infineon MIFARE CLASSIC 1K"); break;
243 case 0x98: PrintAndLog("TYPE : Gemplus MPCOS"); break;
244 default: ;
245 }
246
247 // Double & triple sized UID, can be mapped to a manufacturer.
248 // HACK: does this apply for Ultralight cards?
249 if ( card.uidlen > 4 ) {
250 PrintAndLog("MANUFACTURER : %s", getTagInfo(card.uid[0]));
251 }
252
253 // try to request ATS even if tag claims not to support it
254 if (select_status == 2) {
255 uint8_t rats[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0
256 c.arg[0] = ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT;
257 c.arg[1] = 2;
258 c.arg[2] = 0;
259 memcpy(c.d.asBytes, rats, 2);
260 SendCommand(&c);
261 WaitForResponse(CMD_ACK,&resp);
262
263 memcpy(card.ats, resp.d.asBytes, resp.arg[0]);
264 card.ats_len = resp.arg[0]; // note: ats_len includes CRC Bytes
265 }
266
267 if(card.ats_len >= 3) { // a valid ATS consists of at least the length byte (TL) and 2 CRC bytes
268 bool ta1 = 0, tb1 = 0, tc1 = 0;
269 int pos;
270
271 if (select_status == 2) {
272 PrintAndLog("SAK incorrectly claims that card doesn't support RATS");
273 }
274 PrintAndLog(" ATS : %s", sprint_hex(card.ats, card.ats_len));
275 PrintAndLog(" - TL : length is %d bytes", card.ats[0]);
276 if (card.ats[0] != card.ats_len - 2) {
277 PrintAndLog("ATS may be corrupted. Length of ATS (%d bytes incl. 2 Bytes CRC) doesn't match TL", card.ats_len);
278 }
279
280 if (card.ats[0] > 1) { // there is a format byte (T0)
281 ta1 = (card.ats[1] & 0x10) == 0x10;
282 tb1 = (card.ats[1] & 0x20) == 0x20;
283 tc1 = (card.ats[1] & 0x40) == 0x40;
284 int16_t fsci = card.ats[1] & 0x0f;
285 PrintAndLog(" - T0 : TA1 is%s present, TB1 is%s present, "
286 "TC1 is%s present, FSCI is %d (FSC = %ld)",
287 (ta1 ? "" : " NOT"), (tb1 ? "" : " NOT"), (tc1 ? "" : " NOT"),
288 fsci,
289 fsci < 5 ? (fsci - 2) * 8 :
290 fsci < 8 ? (fsci - 3) * 32 :
291 fsci == 8 ? 256 :
292 -1
293 );
294 }
295 pos = 2;
296 if (ta1) {
297 char dr[16], ds[16];
298 dr[0] = ds[0] = '\0';
299 if (card.ats[pos] & 0x10) strcat(ds, "2, ");
300 if (card.ats[pos] & 0x20) strcat(ds, "4, ");
301 if (card.ats[pos] & 0x40) strcat(ds, "8, ");
302 if (card.ats[pos] & 0x01) strcat(dr, "2, ");
303 if (card.ats[pos] & 0x02) strcat(dr, "4, ");
304 if (card.ats[pos] & 0x04) strcat(dr, "8, ");
305 if (strlen(ds) != 0) ds[strlen(ds) - 2] = '\0';
306 if (strlen(dr) != 0) dr[strlen(dr) - 2] = '\0';
307 PrintAndLog(" - TA1 : different divisors are%s supported, "
308 "DR: [%s], DS: [%s]",
309 (card.ats[pos] & 0x80 ? " NOT" : ""), dr, ds);
310 pos++;
311 }
312 if (tb1) {
313 uint32_t sfgi = card.ats[pos] & 0x0F;
314 uint32_t fwi = card.ats[pos] >> 4;
315 PrintAndLog(" - TB1 : SFGI = %d (SFGT = %s%ld/fc), FWI = %d (FWT = %ld/fc)",
316 (sfgi),
317 sfgi ? "" : "(not needed) ",
318 sfgi ? (1 << 12) << sfgi : 0,
319 fwi,
320 (1 << 12) << fwi
321 );
322 pos++;
323 }
324 if (tc1) {
325 PrintAndLog(" - TC1 : NAD is%s supported, CID is%s supported",
326 (card.ats[pos] & 0x01) ? "" : " NOT",
327 (card.ats[pos] & 0x02) ? "" : " NOT");
328 pos++;
329 }
330 if (card.ats[0] > pos) {
331 char *tip = "";
332 if (card.ats[0] - pos >= 7) {
333 if (memcmp(card.ats + pos, "\xC1\x05\x2F\x2F\x01\xBC\xD6", 7) == 0) {
334 tip = "-> MIFARE Plus X 2K or 4K";
335 } else if (memcmp(card.ats + pos, "\xC1\x05\x2F\x2F\x00\x35\xC7", 7) == 0) {
336 tip = "-> MIFARE Plus S 2K or 4K";
337 }
338 }
339 PrintAndLog(" - HB : %s%s", sprint_hex(card.ats + pos, card.ats[0] - pos), tip);
340 if (card.ats[pos] == 0xC1) {
341 PrintAndLog(" c1 -> Mifare or (multiple) virtual cards of various type");
342 PrintAndLog(" %02x -> Length is %d bytes",
343 card.ats[pos + 1], card.ats[pos + 1]);
344 switch (card.ats[pos + 2] & 0xf0) {
345 case 0x10:
346 PrintAndLog(" 1x -> MIFARE DESFire");
347 break;
348 case 0x20:
349 PrintAndLog(" 2x -> MIFARE Plus");
350 break;
351 }
352 switch (card.ats[pos + 2] & 0x0f) {
353 case 0x00:
354 PrintAndLog(" x0 -> <1 kByte");
355 break;
356 case 0x01:
357 PrintAndLog(" x1 -> 1 kByte");
358 break;
359 case 0x02:
360 PrintAndLog(" x2 -> 2 kByte");
361 break;
362 case 0x03:
363 PrintAndLog(" x3 -> 4 kByte");
364 break;
365 case 0x04:
366 PrintAndLog(" x4 -> 8 kByte");
367 break;
368 }
369 switch (card.ats[pos + 3] & 0xf0) {
370 case 0x00:
371 PrintAndLog(" 0x -> Engineering sample");
372 break;
373 case 0x20:
374 PrintAndLog(" 2x -> Released");
375 break;
376 }
377 switch (card.ats[pos + 3] & 0x0f) {
378 case 0x00:
379 PrintAndLog(" x0 -> Generation 1");
380 break;
381 case 0x01:
382 PrintAndLog(" x1 -> Generation 2");
383 break;
384 case 0x02:
385 PrintAndLog(" x2 -> Generation 3");
386 break;
387 }
388 switch (card.ats[pos + 4] & 0x0f) {
389 case 0x00:
390 PrintAndLog(" x0 -> Only VCSL supported");
391 break;
392 case 0x01:
393 PrintAndLog(" x1 -> VCS, VCSL, and SVC supported");
394 break;
395 case 0x0E:
396 PrintAndLog(" xE -> no VCS command supported");
397 break;
398 }
399 }
400 }
401 } else {
402 PrintAndLog("proprietary non iso14443-4 card found, RATS not supported");
403 }
404
405
406 // try to see if card responses to "chinese magic backdoor" commands.
407 c.cmd = CMD_MIFARE_CIDENT;
408 c.arg[0] = 0;
409 c.arg[1] = 0;
410 c.arg[2] = 0;
411 SendCommand(&c);
412 WaitForResponse(CMD_ACK,&resp);
413 uint8_t isOK = resp.arg[0] & 0xff;
414 PrintAndLog("Answers to chinese magic backdoor commands: %s", (isOK ? "YES" : "NO") );
415
416 // disconnect
417 c.cmd = CMD_READER_ISO_14443a;
418 c.arg[0] = 0;
419 c.arg[1] = 0;
420 c.arg[2] = 0;
421 SendCommand(&c);
422
423 return select_status;
424 }
425
426 // Collect ISO14443 Type A UIDs
427 int CmdHF14ACUIDs(const char *Cmd)
428 {
429 // requested number of UIDs
430 int n = atoi(Cmd);
431 // collect at least 1 (e.g. if no parameter was given)
432 n = n > 0 ? n : 1;
433
434 PrintAndLog("Collecting %d UIDs", n);
435 PrintAndLog("Start: %" PRIu64, msclock()/1000);
436 // repeat n times
437 for (int i = 0; i < n; i++) {
438 // execute anticollision procedure
439 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT, 0, 0}};
440 SendCommand(&c);
441
442 UsbCommand resp;
443 WaitForResponse(CMD_ACK,&resp);
444
445 iso14a_card_select_t *card = (iso14a_card_select_t *) resp.d.asBytes;
446
447 // check if command failed
448 if (resp.arg[0] == 0) {
449 PrintAndLog("Card select failed.");
450 } else {
451 char uid_string[20];
452 for (uint16_t i = 0; i < card->uidlen; i++) {
453 sprintf(&uid_string[2*i], "%02X", card->uid[i]);
454 }
455 PrintAndLog("%s", uid_string);
456 }
457 }
458 PrintAndLog("End: %" PRIu64, msclock()/1000);
459
460 return 1;
461 }
462
463 // ## simulate iso14443a tag
464 // ## greg - added ability to specify tag UID
465 int CmdHF14ASim(const char *Cmd)
466 {
467 UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,{0,0,0}};
468
469 // Retrieve the tag type
470 uint8_t tagtype = param_get8ex(Cmd,0,0,10);
471
472 // When no argument was given, just print help message
473 if (tagtype == 0) {
474 PrintAndLog("");
475 PrintAndLog(" Emulating ISO/IEC 14443 type A tag with 4 or 7 byte UID");
476 PrintAndLog("");
477 PrintAndLog(" syntax: hf 14a sim <type> <uid>");
478 PrintAndLog(" types: 1 = MIFARE Classic");
479 PrintAndLog(" 2 = MIFARE Ultralight");
480 PrintAndLog(" 3 = MIFARE Desfire");
481 PrintAndLog(" 4 = ISO/IEC 14443-4");
482 PrintAndLog(" 5 = MIFARE Tnp3xxx");
483 PrintAndLog("");
484 return 1;
485 }
486
487 // Store the tag type
488 c.arg[0] = tagtype;
489
490 // Retrieve the full 4 or 7 byte long uid
491 uint64_t long_uid = param_get64ex(Cmd,1,0,16);
492
493 // Are we handling the (optional) second part uid?
494 if (long_uid > 0xffffffff) {
495 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 7 byte UID (%014" PRIx64 ")",long_uid);
496 // Store the second part
497 c.arg[2] = (long_uid & 0xffffffff);
498 long_uid >>= 32;
499 // Store the first part, ignore the first byte, it is replaced by cascade byte (0x88)
500 c.arg[1] = (long_uid & 0xffffff);
501 } else {
502 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 4 byte UID (%08x)",long_uid);
503 // Only store the first part
504 c.arg[1] = long_uid & 0xffffffff;
505 }
506 /*
507 // At lease save the mandatory first part of the UID
508 c.arg[0] = long_uid & 0xffffffff;
509
510 if (c.arg[1] == 0) {
511 PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
512 }
513
514 switch (c.arg[0]) {
515 case 1: {
516 PrintAndLog("Emulating ISO/IEC 14443-3 type A tag with 4 byte UID");
517 UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,param_get32ex(Cmd,0,0,10),param_get32ex(Cmd,1,0,16),param_get32ex(Cmd,2,0,16)};
518 } break;
519 case 2: {
520 PrintAndLog("Emulating ISO/IEC 14443-4 type A tag with 7 byte UID");
521 } break;
522 default: {
523 PrintAndLog("Error: unkown tag type (%d)",c.arg[0]);
524 PrintAndLog("syntax: hf 14a sim <uid>",c.arg[0]);
525 PrintAndLog(" type1: 4 ",c.arg[0]);
526
527 return 1;
528 } break;
529 }
530 */
531 /*
532 unsigned int hi = 0, lo = 0;
533 int n = 0, i = 0;
534 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
535 hi= (hi << 4) | (lo >> 28);
536 lo= (lo << 4) | (n & 0xf);
537 }
538 */
539 // UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,param_get32ex(Cmd,0,0,10),param_get32ex(Cmd,1,0,16),param_get32ex(Cmd,2,0,16)};
540 // PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
541 SendCommand(&c);
542 return 0;
543 }
544
545 int CmdHF14ASnoop(const char *Cmd) {
546 int param = 0;
547
548 uint8_t ctmp = param_getchar(Cmd, 0) ;
549 if (ctmp == 'h' || ctmp == 'H') {
550 PrintAndLog("It get data from the field and saves it into command buffer.");
551 PrintAndLog("Buffer accessible from command hf list 14a.");
552 PrintAndLog("Usage: hf 14a snoop [c][r]");
553 PrintAndLog("c - triggered by first data from card");
554 PrintAndLog("r - triggered by first 7-bit request from reader (REQ,WUP,...)");
555 PrintAndLog("sample: hf 14a snoop c r");
556 return 0;
557 }
558
559 for (int i = 0; i < 2; i++) {
560 ctmp = param_getchar(Cmd, i);
561 if (ctmp == 'c' || ctmp == 'C') param |= 0x01;
562 if (ctmp == 'r' || ctmp == 'R') param |= 0x02;
563 }
564
565 UsbCommand c = {CMD_SNOOP_ISO_14443a, {param, 0, 0}};
566 SendCommand(&c);
567 return 0;
568 }
569
570
571 int CmdHF14ACmdRaw(const char *cmd) {
572 UsbCommand c = {CMD_READER_ISO_14443a, {0, 0, 0}};
573 bool reply=1;
574 bool crc = false;
575 bool power = false;
576 bool active = false;
577 bool active_select = false;
578 uint16_t numbits = 0;
579 bool bTimeout = false;
580 uint32_t timeout = 0;
581 bool topazmode = false;
582 char buf[5]="";
583 int i = 0;
584 uint8_t data[USB_CMD_DATA_SIZE];
585 uint16_t datalen = 0;
586 uint32_t temp;
587
588 if (strlen(cmd)<2) {
589 PrintAndLog("Usage: hf 14a raw [-r] [-c] [-p] [-f] [-b] [-t] <number of bits> <0A 0B 0C ... hex>");
590 PrintAndLog(" -r do not read response");
591 PrintAndLog(" -c calculate and append CRC");
592 PrintAndLog(" -p leave the signal field ON after receive");
593 PrintAndLog(" -a active signal field ON without select");
594 PrintAndLog(" -s active signal field ON with select");
595 PrintAndLog(" -b number of bits to send. Useful for send partial byte");
596 PrintAndLog(" -t timeout in ms");
597 PrintAndLog(" -T use Topaz protocol to send command");
598 return 0;
599 }
600
601
602 // strip
603 while (*cmd==' ' || *cmd=='\t') cmd++;
604
605 while (cmd[i]!='\0') {
606 if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; }
607 if (cmd[i]=='-') {
608 switch (cmd[i+1]) {
609 case 'r':
610 reply = false;
611 break;
612 case 'c':
613 crc = true;
614 break;
615 case 'p':
616 power = true;
617 break;
618 case 'a':
619 active = true;
620 break;
621 case 's':
622 active_select = true;
623 break;
624 case 'b':
625 sscanf(cmd+i+2,"%d",&temp);
626 numbits = temp & 0xFFFF;
627 i+=3;
628 while(cmd[i]!=' ' && cmd[i]!='\0') { i++; }
629 i-=2;
630 break;
631 case 't':
632 bTimeout = true;
633 sscanf(cmd+i+2,"%d",&temp);
634 timeout = temp;
635 i+=3;
636 while(cmd[i]!=' ' && cmd[i]!='\0') { i++; }
637 i-=2;
638 break;
639 case 'T':
640 topazmode = true;
641 break;
642 default:
643 PrintAndLog("Invalid option");
644 return 0;
645 }
646 i+=2;
647 continue;
648 }
649 if ((cmd[i]>='0' && cmd[i]<='9') ||
650 (cmd[i]>='a' && cmd[i]<='f') ||
651 (cmd[i]>='A' && cmd[i]<='F') ) {
652 buf[strlen(buf)+1]=0;
653 buf[strlen(buf)]=cmd[i];
654 i++;
655
656 if (strlen(buf)>=2) {
657 sscanf(buf,"%x",&temp);
658 data[datalen]=(uint8_t)(temp & 0xff);
659 *buf=0;
660 if (datalen > sizeof(data)-1) {
661 if (crc)
662 PrintAndLog("Buffer is full, we can't add CRC to your data");
663 break;
664 } else {
665 datalen++;
666 }
667 }
668 continue;
669 }
670 PrintAndLog("Invalid char on input");
671 return 0;
672 }
673
674 if(crc && datalen>0 && datalen<sizeof(data)-2)
675 {
676 uint8_t first, second;
677 if (topazmode) {
678 ComputeCrc14443(CRC_14443_B, data, datalen, &first, &second);
679 } else {
680 ComputeCrc14443(CRC_14443_A, data, datalen, &first, &second);
681 }
682 data[datalen++] = first;
683 data[datalen++] = second;
684 }
685
686 if(active || active_select)
687 {
688 c.arg[0] |= ISO14A_CONNECT;
689 if(active)
690 c.arg[0] |= ISO14A_NO_SELECT;
691 }
692
693 if(bTimeout){
694 #define MAX_TIMEOUT 40542464 // = (2^32-1) * (8*16) / 13560000Hz * 1000ms/s
695 c.arg[0] |= ISO14A_SET_TIMEOUT;
696 if(timeout > MAX_TIMEOUT) {
697 timeout = MAX_TIMEOUT;
698 PrintAndLog("Set timeout to 40542 seconds (11.26 hours). The max we can wait for response");
699 }
700 c.arg[2] = 13560000 / 1000 / (8*16) * timeout; // timeout in ETUs (time to transfer 1 bit, approx. 9.4 us)
701 }
702
703 if(power) {
704 c.arg[0] |= ISO14A_NO_DISCONNECT;
705 }
706
707 if(datalen > 0) {
708 c.arg[0] |= ISO14A_RAW;
709 }
710
711 if(topazmode) {
712 c.arg[0] |= ISO14A_TOPAZMODE;
713 }
714
715 // Max buffer is USB_CMD_DATA_SIZE (512)
716 c.arg[1] = (datalen & 0xFFFF) | ((uint32_t)numbits << 16);
717 memcpy(c.d.asBytes,data,datalen);
718
719 SendCommand(&c);
720
721 if (reply) {
722 if(active_select)
723 waitCmd(1);
724 if(datalen>0)
725 waitCmd(0);
726 } // if reply
727 return 0;
728 }
729
730
731 static void waitCmd(uint8_t iSelect)
732 {
733 uint8_t *recv;
734 UsbCommand resp;
735 char *hexout;
736
737 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
738 recv = resp.d.asBytes;
739 uint8_t iLen = iSelect ? resp.arg[1] : resp.arg[0];
740 PrintAndLog("received %i octets", iLen);
741 if(!iLen)
742 return;
743 hexout = (char *)malloc(iLen * 3 + 1);
744 if (hexout != NULL) {
745 for (int i = 0; i < iLen; i++) { // data in hex
746 sprintf(&hexout[i * 3], "%02X ", recv[i]);
747 }
748 PrintAndLog("%s", hexout);
749 free(hexout);
750 } else {
751 PrintAndLog("malloc failed your client has low memory?");
752 }
753 } else {
754 PrintAndLog("timeout while waiting for reply.");
755 }
756 }
757
758 static command_t CommandTable[] =
759 {
760 {"help", CmdHelp, 1, "This help"},
761 {"list", CmdHF14AList, 0, "[Deprecated] List ISO 14443a history"},
762 {"reader", CmdHF14AReader, 0, "Act like an ISO14443 Type A reader"},
763 {"cuids", CmdHF14ACUIDs, 0, "<n> Collect n>0 ISO14443 Type A UIDs in one go"},
764 {"sim", CmdHF14ASim, 0, "<UID> -- Simulate ISO 14443a tag"},
765 {"snoop", CmdHF14ASnoop, 0, "Eavesdrop ISO 14443 Type A"},
766 {"raw", CmdHF14ACmdRaw, 0, "Send raw hex data to tag"},
767 {NULL, NULL, 0, NULL}
768 };
769
770 int CmdHF14A(const char *Cmd) {
771 // flush
772 WaitForResponseTimeout(CMD_ACK,NULL,100);
773
774 // parse
775 CmdsParse(CommandTable, Cmd);
776 return 0;
777 }
778
779 int CmdHelp(const char *Cmd)
780 {
781 CmdsHelp(CommandTable);
782 return 0;
783 }
Impressum, Datenschutz