]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhf14a.c
making USB CDC branch
[proxmark3-svn] / client / cmdhf14a.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
f89c7050 2// 2011, Merlok
534983d7 3// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>, Hagen Fritsch
a553f267 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
7fe9b0b7 12#include <stdio.h>
590f8ff9 13#include <stdlib.h>
7fe9b0b7 14#include <string.h>
f397b5cc 15#include <unistd.h>
20f9a2a1 16#include "util.h"
7fe9b0b7 17#include "iso14443crc.h"
18#include "data.h"
19#include "proxusb.h"
20#include "ui.h"
21#include "cmdparser.h"
22#include "cmdhf14a.h"
534983d7 23#include "common.h"
20f9a2a1 24#include "cmdmain.h"
7fe9b0b7 25
26static int CmdHelp(const char *Cmd);
27
28int CmdHF14AList(const char *Cmd)
29{
30 uint8_t got[1920];
db09cb3a 31 GetFromBigBuf(got,sizeof(got),0);
7fe9b0b7 32
33 PrintAndLog("recorded activity:");
34 PrintAndLog(" ETU :rssi: who bytes");
35 PrintAndLog("---------+----+----+-----------");
36
37 int i = 0;
38 int prev = -1;
39
40 for (;;) {
41 if(i >= 1900) {
42 break;
43 }
44
45 bool isResponse;
46 int timestamp = *((uint32_t *)(got+i));
47 if (timestamp & 0x80000000) {
48 timestamp &= 0x7fffffff;
49 isResponse = 1;
50 } else {
51 isResponse = 0;
52 }
53
54 int metric = 0;
55 int parityBits = *((uint32_t *)(got+i+4));
56 // 4 bytes of additional information...
57 // maximum of 32 additional parity bit information
58 //
59 // TODO:
60 // at each quarter bit period we can send power level (16 levels)
61 // or each half bit period in 256 levels.
62
63
64 int len = got[i+8];
65
66 if (len > 100) {
67 break;
68 }
69 if (i + len >= 1900) {
70 break;
71 }
72
73 uint8_t *frame = (got+i+9);
74
75 // Break and stick with current result if buffer was not completely full
76 if (frame[0] == 0x44 && frame[1] == 0x44 && frame[3] == 0x44) { break; }
77
78 char line[1000] = "";
79 int j;
80 for (j = 0; j < len; j++) {
81 int oddparity = 0x01;
82 int k;
83
84 for (k=0;k<8;k++) {
85 oddparity ^= (((frame[j] & 0xFF) >> k) & 0x01);
86 }
87
88 //if((parityBits >> (len - j - 1)) & 0x01) {
89 if (isResponse && (oddparity != ((parityBits >> (len - j - 1)) & 0x01))) {
90 sprintf(line+(j*4), "%02x! ", frame[j]);
91 }
92 else {
93 sprintf(line+(j*4), "%02x ", frame[j]);
94 }
95 }
96
97 char *crc;
98 crc = "";
99 if (len > 2) {
100 uint8_t b1, b2;
101 for (j = 0; j < (len - 1); j++) {
102 // gives problems... search for the reason..
103 /*if(frame[j] == 0xAA) {
104 switch(frame[j+1]) {
105 case 0x01:
106 crc = "[1] Two drops close after each other";
107 break;
108 case 0x02:
109 crc = "[2] Potential SOC with a drop in second half of bitperiod";
110 break;
111 case 0x03:
112 crc = "[3] Segment Z after segment X is not possible";
113 break;
114 case 0x04:
115 crc = "[4] Parity bit of a fully received byte was wrong";
116 break;
117 default:
118 crc = "[?] Unknown error";
119 break;
120 }
121 break;
122 }*/
123 }
124
125 if (strlen(crc)==0) {
126 ComputeCrc14443(CRC_14443_A, frame, len-2, &b1, &b2);
127 if (b1 != frame[len-2] || b2 != frame[len-1]) {
128 crc = (isResponse & (len < 6)) ? "" : " !crc";
129 } else {
130 crc = "";
131 }
132 }
133 } else {
134 crc = ""; // SHORT
135 }
136
137 char metricString[100];
138 if (isResponse) {
139 sprintf(metricString, "%3d", metric);
140 } else {
141 strcpy(metricString, " ");
142 }
143
144 PrintAndLog(" +%7d: %s: %s %s %s",
145 (prev < 0 ? 0 : (timestamp - prev)),
146 metricString,
147 (isResponse ? "TAG" : " "), line, crc);
148
149 prev = timestamp;
150 i += (len + 9);
151 }
f89c7050 152 return 0;
7fe9b0b7 153}
154
534983d7 155void iso14a_set_timeout(uint32_t timeout) {
156 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_SET_TIMEOUT, 0, timeout}};
157 SendCommand(&c);
158}
159
7fe9b0b7 160int CmdHF14AReader(const char *Cmd)
161{
534983d7 162 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT, 0, 0}};
163 SendCommand(&c);
164 UsbCommand * resp = WaitForResponse(CMD_ACK);
165 uint8_t * uid = resp->d.asBytes;
9ca155ba 166 iso14a_card_select_t * card = (iso14a_card_select_t *)(uid + 12);
534983d7 167
168 if(resp->arg[0] == 0) {
169 PrintAndLog("iso14443a card select failed");
170 return 0;
171 }
172
173 PrintAndLog("ATQA : %02x %02x", card->atqa[0], card->atqa[1]);
174 PrintAndLog(" UID : %s", sprint_hex(uid, 12));
175 PrintAndLog(" SAK : %02x [%d]", card->sak, resp->arg[0]);
713e7ffb 176
9ca155ba 177 switch (card->sak) {
713e7ffb
W
178 case 0x00: PrintAndLog(" SAK : NXP MIFARE Ultralight | Ultralight C"); break;
179 case 0x04: PrintAndLog(" SAK : NXP MIFARE (various !DESFire !DESFire EV1)"); break;
180
181 case 0x08: PrintAndLog(" SAK : NXP MIFARE CLASSIC 1k | Plus 2k"); break;
182 case 0x09: PrintAndLog(" SAK : NXP MIFARE Mini 0.3k"); break;
183 case 0x10: PrintAndLog(" SAK : NXP MIFARE Plus 2k"); break;
184 case 0x11: PrintAndLog(" SAK : NXP MIFARE Plus 4k"); break;
185 case 0x18: PrintAndLog(" SAK : NXP MIFARE Classic 4k | Plus 4k"); break;
186 case 0x20: PrintAndLog(" SAK : NXP MIFARE DESFire 4k | DESFire EV1 2k/4k/8k | Plus 2k/4k | JCOP 31/41"); break;
187 case 0x24: PrintAndLog(" SAK : NXP MIFARE DESFire | DESFire EV1"); break;
23487cd2
M
188 case 0x28: PrintAndLog(" SAK : JCOP31 or JCOP41 v2.3.1"); break;
189 case 0x38: PrintAndLog(" SAK : Nokia 6212 or 6131 MIFARE CLASSIC 4K"); break;
190 case 0x88: PrintAndLog(" SAK : Infineon MIFARE CLASSIC 1K"); break;
191 case 0x98: PrintAndLog(" SAK : Gemplus MPCOS"); break;
9ca155ba
M
192 default: ;
193 }
561f7c11 194 if(resp->arg[0] == 1) {
195 bool ta1 = 0, tb1 = 0, tc1 = 0;
196 int pos;
197
534983d7 198 PrintAndLog(" ATS : %s", sprint_hex(card->ats, card->ats_len));
561f7c11 199 if (card->ats_len > 0) {
200 PrintAndLog(" - TL : length is %d bytes", card->ats[0]);
201 }
202 if (card->ats_len > 1) {
203 ta1 = (card->ats[1] & 0x10) == 0x10;
204 tb1 = (card->ats[1] & 0x20) == 0x20;
205 tc1 = (card->ats[1] & 0x40) == 0x40;
206 PrintAndLog(" - T0 : TA1 is%s present, TB1 is%s present, "
207 "TC1 is%s present, FSCI is %d",
208 (ta1 ? "" : " NOT"), (tb1 ? "" : " NOT"), (tc1 ? "" : " NOT"),
209 (card->ats[1] & 0x0f));
210 }
211 pos = 2;
212 if (ta1 && card->ats_len > pos) {
213 char dr[16], ds[16];
214 dr[0] = ds[0] = '\0';
215 if (card->ats[pos] & 0x10) strcat(ds, "2, ");
216 if (card->ats[pos] & 0x20) strcat(ds, "4, ");
217 if (card->ats[pos] & 0x40) strcat(ds, "8, ");
218 if (card->ats[pos] & 0x01) strcat(dr, "2, ");
219 if (card->ats[pos] & 0x02) strcat(dr, "4, ");
220 if (card->ats[pos] & 0x04) strcat(dr, "8, ");
221 if (strlen(ds) != 0) ds[strlen(ds) - 2] = '\0';
222 if (strlen(dr) != 0) dr[strlen(dr) - 2] = '\0';
223 PrintAndLog(" - TA1 : different divisors are%s supported, "
224 "DR: [%s], DS: [%s]",
225 (card->ats[pos] & 0x80 ? " NOT" : ""), dr, ds);
226 pos++;
227 }
228 if (tb1 && card->ats_len > pos) {
229 PrintAndLog(" - TB1 : SFGI = %d, FWI = %d",
230 (card->ats[pos] & 0x08),
231 (card->ats[pos] & 0x80) >> 4);
232 pos++;
233 }
234 if (tc1 && card->ats_len > pos) {
235 PrintAndLog(" - TC1 : NAD is%s supported, CID is%s supported",
236 (card->ats[pos] & 0x01) ? "" : " NOT",
237 (card->ats[pos] & 0x02) ? "" : " NOT");
238 pos++;
239 }
240 if (card->ats_len > pos) {
241 char *tip = "";
242 if (card->ats_len - pos > 7) {
243 if (memcmp(card->ats + pos, "\xC1\x05\x2F\x2F\x01\xBC\xD6", 7) == 0) {
244 tip = "-> MIFARE Plus X 2K or 4K";
245 } else if (memcmp(card->ats + pos, "\xC1\x05\x2F\x2F\x00\x35\xC7", 7) == 0) {
246 tip = "-> MIFARE Plus S 2K or 4K";
247 }
248 }
249 PrintAndLog(" - HB : %s%s", sprint_hex(card->ats + pos, card->ats_len - pos - 2), tip);
250 if (card->ats[pos] == 0xC1) {
251 PrintAndLog(" c1 -> Mifare or (multiple) virtual cards of various type");
252 PrintAndLog(" %02x -> Length is %d bytes",
253 card->ats[pos + 1], card->ats[pos + 1]);
254 switch (card->ats[pos + 2] & 0xf0) {
255 case 0x10:
256 PrintAndLog(" 1x -> MIFARE DESFire");
257 break;
258 case 0x20:
259 PrintAndLog(" 2x -> MIFARE Plus");
260 break;
261 }
262 switch (card->ats[pos + 2] & 0x0f) {
263 case 0x00:
264 PrintAndLog(" x0 -> <1 kByte");
265 break;
266 case 0x01:
267 PrintAndLog(" x0 -> 1 kByte");
268 break;
269 case 0x02:
270 PrintAndLog(" x0 -> 2 kByte");
271 break;
272 case 0x03:
273 PrintAndLog(" x0 -> 4 kByte");
274 break;
275 case 0x04:
276 PrintAndLog(" x0 -> 8 kByte");
277 break;
278 }
279 switch (card->ats[pos + 3] & 0xf0) {
280 case 0x00:
281 PrintAndLog(" 0x -> Engineering sample");
282 break;
283 case 0x20:
284 PrintAndLog(" 2x -> Released");
285 break;
286 }
287 switch (card->ats[pos + 3] & 0x0f) {
288 case 0x00:
289 PrintAndLog(" x0 -> Generation 1");
290 break;
291 case 0x01:
292 PrintAndLog(" x1 -> Generation 2");
293 break;
294 case 0x02:
295 PrintAndLog(" x2 -> Generation 3");
296 break;
297 }
298 switch (card->ats[pos + 4] & 0x0f) {
299 case 0x00:
300 PrintAndLog(" x0 -> Only VCSL supported");
301 break;
302 case 0x01:
303 PrintAndLog(" x1 -> VCS, VCSL, and SVC supported");
304 break;
305 case 0x0E:
306 PrintAndLog(" xE -> no VCS command supported");
307 break;
308 }
309 }
310 }
311 }
534983d7 312 else
313 PrintAndLog("proprietary non-iso14443a card found, RATS not supported");
314
315 return resp->arg[0];
7fe9b0b7 316}
317
db22dfe6 318// Collect ISO14443 Type A UIDs
319int CmdHF14ACUIDs(const char *Cmd)
320{
321 // requested number of UIDs
322 int n = atoi(Cmd);
323 // collect at least 1 (e.g. if no parameter was given)
324 n = n > 0 ? n : 1;
325
326 PrintAndLog("Collecting %d UIDs", n);
327 PrintAndLog("Start: %u", time(NULL));
328 // repeat n times
329 for (int i = 0; i < n; i++) {
330 // execute anticollision procedure
331 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT, 0, 0}};
332 SendCommand(&c);
333 UsbCommand *resp = WaitForResponse(CMD_ACK);
334 uint8_t *uid = resp->d.asBytes;
335 iso14a_card_select_t *card = (iso14a_card_select_t *)(uid + 12);
336
337 // check if command failed
338 if (resp->arg[0] == 0) {
339 PrintAndLog("Card select failed.");
340 } else {
341 // check if UID is 4 bytes
342 if ((card->atqa[1] & 0xC0) == 0) {
343 PrintAndLog("%02X%02X%02X%02X",
344 *uid, *(uid + 1), *(uid + 2), *(uid + 3));
345 } else {
346 PrintAndLog("UID longer than 4 bytes");
347 }
348 }
349 }
350 PrintAndLog("End: %u", time(NULL));
351
352 return 1;
353}
354
7fe9b0b7 355// ## simulate iso14443a tag
356// ## greg - added ability to specify tag UID
357int CmdHF14ASim(const char *Cmd)
81cd0474 358{
359 UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,{0,0,0}};
360
361 // Retrieve the tag type
362 uint8_t tagtype = param_get8ex(Cmd,0,0,10);
363
364 // When no argument was given, just print help message
365 if (tagtype == 0) {
366 PrintAndLog("");
367 PrintAndLog(" Emulating ISO/IEC 14443 type A tag with 4 or 7 byte UID");
368 PrintAndLog("");
369 PrintAndLog(" syntax: hf 14a sim <type> <uid>");
370 PrintAndLog(" types: 1 = MIFARE Classic");
371 PrintAndLog(" 2 = MIFARE Ultralight");
372 PrintAndLog(" 3 = MIFARE DESFIRE");
373 PrintAndLog(" 4 = ISO/IEC 14443-4");
374 PrintAndLog("");
375 return 1;
376 }
377
378 // Store the tag type
379 c.arg[0] = tagtype;
380
381 // Retrieve the full 4 or 7 byte long uid
382 uint64_t long_uid = param_get64ex(Cmd,1,0,16);
383
384 // Are we handling the (optional) second part uid?
385 if (long_uid > 0xffffffff) {
386 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 7 byte UID (%014llx)",long_uid);
387 // Store the second part
388 c.arg[2] = (long_uid & 0xffffffff);
389 long_uid >>= 32;
390 // Store the first part, ignore the first byte, it is replaced by cascade byte (0x88)
391 c.arg[1] = (long_uid & 0xffffff);
392 } else {
393 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 4 byte UID (%08x)",long_uid);
394 // Only store the first part
395 c.arg[1] = long_uid & 0xffffffff;
396 }
397/*
398 // At lease save the mandatory first part of the UID
399 c.arg[0] = long_uid & 0xffffffff;
400
401
402 // At lease save the mandatory first part of the UID
403 c.arg[0] = long_uid & 0xffffffff;
404
405 if (c.arg[1] == 0) {
406 PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
407 }
408
409 switch (c.arg[0]) {
410 case 1: {
411 PrintAndLog("Emulating ISO/IEC 14443-3 type A tag with 4 byte UID");
412 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)};
413 } break;
414 case 2: {
415 PrintAndLog("Emulating ISO/IEC 14443-4 type A tag with 7 byte UID");
416 } break;
417 default: {
418 PrintAndLog("Error: unkown tag type (%d)",c.arg[0]);
419 PrintAndLog("syntax: hf 14a sim <uid>",c.arg[0]);
420 PrintAndLog(" type1: 4 ",c.arg[0]);
421
422 return 1;
423 } break;
424 }
425*/
426/*
7fe9b0b7 427 unsigned int hi = 0, lo = 0;
428 int n = 0, i = 0;
429 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
430 hi= (hi << 4) | (lo >> 28);
431 lo= (lo << 4) | (n & 0xf);
432 }
81cd0474 433*/
434// 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)};
435// PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
7fe9b0b7 436 SendCommand(&c);
437 return 0;
438}
439
5cd9ec01
M
440int CmdHF14ASnoop(const char *Cmd) {
441 int param = 0;
442
443 if (param_getchar(Cmd, 0) == 'h') {
444 PrintAndLog("It get data from the field and saves it into command buffer.");
445 PrintAndLog("Buffer accessible from command hf 14a list.");
446 PrintAndLog("Usage: hf 14a snoop [c][r]");
447 PrintAndLog("c - triggered by first data from card");
448 PrintAndLog("r - triggered by first 7-bit request from reader (REQ,WUP,...)");
449 PrintAndLog("sample: hf 14a snoop c r");
450 return 0;
451 }
452
453 for (int i = 0; i < 2; i++) {
454 char ctmp = param_getchar(Cmd, i);
455 if (ctmp == 'c' || ctmp == 'C') param |= 0x01;
456 if (ctmp == 'r' || ctmp == 'R') param |= 0x02;
457 }
458
459 UsbCommand c = {CMD_SNOOP_ISO_14443a, {param, 0, 0}};
7fe9b0b7 460 SendCommand(&c);
461 return 0;
462}
463
464static command_t CommandTable[] =
465{
5acd09bd 466 {"help", CmdHelp, 1, "This help"},
467 {"list", CmdHF14AList, 0, "List ISO 14443a history"},
468 {"reader", CmdHF14AReader, 0, "Act like an ISO14443 Type A reader"},
469 {"cuids", CmdHF14ACUIDs, 0, "<n> Collect n>0 ISO14443 Type A UIDs in one go"},
470 {"sim", CmdHF14ASim, 0, "<UID> -- Fake ISO 14443a tag"},
471 {"snoop", CmdHF14ASnoop, 0, "Eavesdrop ISO 14443 Type A"},
7fe9b0b7 472 {NULL, NULL, 0, NULL}
473};
474
475int CmdHF14A(const char *Cmd)
476{
f397b5cc
M
477 // flush
478 while (WaitForResponseTimeout(CMD_ACK, 500) != NULL) ;
479
480 // parse
7fe9b0b7 481 CmdsParse(CommandTable, Cmd);
482 return 0;
483}
484
485int CmdHelp(const char *Cmd)
486{
487 CmdsHelp(CommandTable);
488 return 0;
489}
Impressum, Datenschutz