]>
Commit | Line | Data |
---|---|---|
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" |
f38a1528 | 17 | #include "../common/iso14443crc.h" |
7fe9b0b7 | 18 | #include "data.h" |
902cb3c0 | 19 | #include "proxmark3.h" |
7fe9b0b7 | 20 | #include "ui.h" |
21 | #include "cmdparser.h" | |
22 | #include "cmdhf14a.h" | |
f38a1528 | 23 | #include "../include/common.h" |
20f9a2a1 | 24 | #include "cmdmain.h" |
f38a1528 | 25 | #include "../include/mifare.h" |
7fe9b0b7 | 26 | |
27 | static int CmdHelp(const char *Cmd); | |
5f6d6c90 | 28 | static void waitCmd(uint8_t iLen); |
7fe9b0b7 | 29 | |
30 | int CmdHF14AList(const char *Cmd) | |
31 | { | |
7bc95e2e | 32 | bool ShowWaitCycles = false; |
33 | char param = param_getchar(Cmd, 0); | |
34 | ||
35 | if (param == 'h' || (param != 0 && param != 'f')) { | |
36 | PrintAndLog("List data in trace buffer."); | |
37 | PrintAndLog("Usage: hf 14a list [f]"); | |
38 | PrintAndLog("f - show frame delay times as well"); | |
39 | PrintAndLog("sample: hf 14a list f"); | |
40 | return 0; | |
41 | } | |
42 | ||
43 | if (param == 'f') { | |
44 | ShowWaitCycles = true; | |
45 | } | |
46 | ||
95e63594 | 47 | uint8_t got[TRACE_BUFFER_SIZE]; |
7bc95e2e | 48 | GetFromBigBuf(got,sizeof(got),0); |
49 | WaitForResponse(CMD_ACK,NULL); | |
50 | ||
51 | PrintAndLog("Recorded Activity"); | |
52 | PrintAndLog(""); | |
53 | PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer"); | |
54 | PrintAndLog("All times are in carrier periods (1/13.56Mhz)"); | |
55 | PrintAndLog(""); | |
56 | PrintAndLog(" Start | End | Src | Data"); | |
57 | PrintAndLog("-----------|-----------|-----|--------"); | |
58 | ||
59 | int i = 0; | |
60 | uint32_t first_timestamp = 0; | |
61 | uint32_t timestamp; | |
62 | uint32_t EndOfTransmissionTimestamp = 0; | |
63 | ||
64 | for (;;) { | |
95e63594 | 65 | if(i >= TRACE_BUFFER_SIZE) { |
7bc95e2e | 66 | break; |
e691fc45 | 67 | } |
7bc95e2e | 68 | |
69 | bool isResponse; | |
70 | timestamp = *((uint32_t *)(got+i)); | |
71 | if (timestamp & 0x80000000) { | |
72 | timestamp &= 0x7fffffff; | |
73 | isResponse = true; | |
74 | } else { | |
75 | isResponse = false; | |
76 | } | |
77 | ||
78 | if(i==0) { | |
79 | first_timestamp = timestamp; | |
80 | } | |
81 | ||
82 | int parityBits = *((uint32_t *)(got+i+4)); | |
83 | ||
84 | int len = got[i+8]; | |
85 | ||
86 | if (len > 100) { | |
87 | break; | |
88 | } | |
95e63594 | 89 | if (i + len >= TRACE_BUFFER_SIZE) { |
7bc95e2e | 90 | break; |
91 | } | |
92 | ||
93 | uint8_t *frame = (got+i+9); | |
94 | ||
95 | // Break and stick with current result if buffer was not completely full | |
96 | if (frame[0] == 0x44 && frame[1] == 0x44 && frame[2] == 0x44 && frame[3] == 0x44) break; | |
97 | ||
98 | char line[1000] = ""; | |
99 | int j; | |
100 | if (len) { | |
101 | for (j = 0; j < len; j++) { | |
102 | int oddparity = 0x01; | |
103 | int k; | |
104 | ||
105 | for (k=0;k<8;k++) { | |
106 | oddparity ^= (((frame[j] & 0xFF) >> k) & 0x01); | |
107 | } | |
108 | ||
109 | //if((parityBits >> (len - j - 1)) & 0x01) { | |
110 | if (isResponse && (oddparity != ((parityBits >> (len - j - 1)) & 0x01))) { | |
111 | sprintf(line+(j*4), "%02x! ", frame[j]); | |
112 | } else { | |
113 | sprintf(line+(j*4), "%02x ", frame[j]); | |
114 | } | |
115 | } | |
116 | } else { | |
117 | if (ShowWaitCycles) { | |
118 | uint32_t next_timestamp = (*((uint32_t *)(got+i+9))) & 0x7fffffff; | |
119 | sprintf(line, "fdt (Frame Delay Time): %d", (next_timestamp - timestamp)); | |
120 | } | |
121 | } | |
122 | ||
123 | char *crc; | |
124 | crc = ""; | |
125 | if (len > 2) { | |
126 | uint8_t b1, b2; | |
127 | for (j = 0; j < (len - 1); j++) { | |
128 | // gives problems... search for the reason.. | |
129 | /*if(frame[j] == 0xAA) { | |
130 | switch(frame[j+1]) { | |
131 | case 0x01: | |
132 | crc = "[1] Two drops close after each other"; | |
133 | break; | |
134 | case 0x02: | |
135 | crc = "[2] Potential SOC with a drop in second half of bitperiod"; | |
136 | break; | |
137 | case 0x03: | |
138 | crc = "[3] Segment Z after segment X is not possible"; | |
139 | break; | |
140 | case 0x04: | |
141 | crc = "[4] Parity bit of a fully received byte was wrong"; | |
142 | break; | |
143 | default: | |
144 | crc = "[?] Unknown error"; | |
145 | break; | |
146 | } | |
147 | break; | |
148 | }*/ | |
149 | } | |
150 | ||
151 | if (strlen(crc)==0) { | |
152 | ComputeCrc14443(CRC_14443_A, frame, len-2, &b1, &b2); | |
153 | if (b1 != frame[len-2] || b2 != frame[len-1]) { | |
154 | crc = (isResponse & (len < 6)) ? "" : " !crc"; | |
155 | } else { | |
156 | crc = ""; | |
157 | } | |
158 | } | |
159 | } else { | |
160 | crc = ""; // SHORT | |
161 | } | |
162 | ||
163 | i += (len + 9); | |
164 | ||
165 | EndOfTransmissionTimestamp = (*((uint32_t *)(got+i))) & 0x7fffffff; | |
166 | ||
167 | if (!ShowWaitCycles) i += 9; | |
168 | ||
169 | PrintAndLog(" %9d | %9d | %s | %s %s", | |
170 | (timestamp - first_timestamp), | |
171 | (EndOfTransmissionTimestamp - first_timestamp), | |
172 | (len?(isResponse ? "Tag" : "Rdr"):" "), | |
173 | line, crc); | |
174 | ||
175 | } | |
f89c7050 | 176 | return 0; |
7fe9b0b7 | 177 | } |
178 | ||
534983d7 | 179 | void iso14a_set_timeout(uint32_t timeout) { |
180 | UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_SET_TIMEOUT, 0, timeout}}; | |
181 | SendCommand(&c); | |
182 | } | |
183 | ||
7fe9b0b7 | 184 | int CmdHF14AReader(const char *Cmd) |
185 | { | |
f38a1528 | 186 | UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT, 0, 0}}; |
534983d7 | 187 | SendCommand(&c); |
902cb3c0 | 188 | |
189 | UsbCommand resp; | |
7bc95e2e | 190 | WaitForResponse(CMD_ACK,&resp); |
902cb3c0 | 191 | |
f38a1528 | 192 | iso14a_card_select_t *card = (iso14a_card_select_t *)resp.d.asBytes; |
534983d7 | 193 | |
f38a1528 | 194 | if(resp.arg[0] == 0) { |
534983d7 | 195 | PrintAndLog("iso14443a card select failed"); |
196 | return 0; | |
197 | } | |
198 | ||
f38a1528 | 199 | PrintAndLog("ATQA : %02x %02x", card->atqa[0], card->atqa[1]); |
200 | PrintAndLog(" UID : %s", sprint_hex(card->uid, card->uidlen)); | |
201 | PrintAndLog(" SAK : %02x [%d]", card->sak, resp.arg[0]); | |
713e7ffb | 202 | |
f38a1528 | 203 | switch (card->sak) { |
79a73ab2 | 204 | case 0x00: PrintAndLog("TYPE : NXP MIFARE Ultralight | Ultralight C"); break; |
c15d2bdc | 205 | case 0x01: PrintAndLog("TYPE : NXP TNP3xxx Activision Game Appliance"); break; |
79a73ab2 | 206 | case 0x04: PrintAndLog("TYPE : NXP MIFARE (various !DESFire !DESFire EV1)"); break; |
e691fc45 | 207 | case 0x08: PrintAndLog("TYPE : NXP MIFARE CLASSIC 1k | Plus 2k SL1"); break; |
79a73ab2 | 208 | case 0x09: PrintAndLog("TYPE : NXP MIFARE Mini 0.3k"); break; |
e691fc45 | 209 | case 0x10: PrintAndLog("TYPE : NXP MIFARE Plus 2k SL2"); break; |
210 | case 0x11: PrintAndLog("TYPE : NXP MIFARE Plus 4k SL2"); break; | |
211 | case 0x18: PrintAndLog("TYPE : NXP MIFARE Classic 4k | Plus 4k SL1"); break; | |
212 | case 0x20: PrintAndLog("TYPE : NXP MIFARE DESFire 4k | DESFire EV1 2k/4k/8k | Plus 2k/4k SL3 | JCOP 31/41"); break; | |
79a73ab2 | 213 | case 0x24: PrintAndLog("TYPE : NXP MIFARE DESFire | DESFire EV1"); break; |
214 | case 0x28: PrintAndLog("TYPE : JCOP31 or JCOP41 v2.3.1"); break; | |
215 | case 0x38: PrintAndLog("TYPE : Nokia 6212 or 6131 MIFARE CLASSIC 4K"); break; | |
216 | case 0x88: PrintAndLog("TYPE : Infineon MIFARE CLASSIC 1K"); break; | |
217 | case 0x98: PrintAndLog("TYPE : Gemplus MPCOS"); break; | |
9ca155ba M |
218 | default: ; |
219 | } | |
f38a1528 | 220 | if(resp.arg[0] == 1) { |
561f7c11 | 221 | bool ta1 = 0, tb1 = 0, tc1 = 0; |
222 | int pos; | |
223 | ||
f38a1528 | 224 | PrintAndLog(" ATS : %s", sprint_hex(card->ats, card->ats_len)); |
225 | if (card->ats_len > 0) { | |
226 | PrintAndLog(" - TL : length is %d bytes", card->ats[0]); | |
19d6d91f | 227 | } |
f38a1528 | 228 | if (card->ats_len > 1) { |
229 | ta1 = (card->ats[1] & 0x10) == 0x10; | |
230 | tb1 = (card->ats[1] & 0x20) == 0x20; | |
231 | tc1 = (card->ats[1] & 0x40) == 0x40; | |
561f7c11 | 232 | PrintAndLog(" - T0 : TA1 is%s present, TB1 is%s present, " |
f38a1528 | 233 | "TC1 is%s present, FSCI is %d", |
561f7c11 | 234 | (ta1 ? "" : " NOT"), (tb1 ? "" : " NOT"), (tc1 ? "" : " NOT"), |
f38a1528 | 235 | (card->ats[1] & 0x0f)); |
561f7c11 | 236 | } |
237 | pos = 2; | |
f38a1528 | 238 | if (ta1 && card->ats_len > pos) { |
561f7c11 | 239 | char dr[16], ds[16]; |
240 | dr[0] = ds[0] = '\0'; | |
f38a1528 | 241 | if (card->ats[pos] & 0x10) strcat(ds, "2, "); |
242 | if (card->ats[pos] & 0x20) strcat(ds, "4, "); | |
243 | if (card->ats[pos] & 0x40) strcat(ds, "8, "); | |
244 | if (card->ats[pos] & 0x01) strcat(dr, "2, "); | |
245 | if (card->ats[pos] & 0x02) strcat(dr, "4, "); | |
246 | if (card->ats[pos] & 0x04) strcat(dr, "8, "); | |
561f7c11 | 247 | if (strlen(ds) != 0) ds[strlen(ds) - 2] = '\0'; |
248 | if (strlen(dr) != 0) dr[strlen(dr) - 2] = '\0'; | |
249 | PrintAndLog(" - TA1 : different divisors are%s supported, " | |
250 | "DR: [%s], DS: [%s]", | |
f38a1528 | 251 | (card->ats[pos] & 0x80 ? " NOT" : ""), dr, ds); |
561f7c11 | 252 | pos++; |
253 | } | |
f38a1528 | 254 | if (tb1 && card->ats_len > pos) { |
255 | PrintAndLog(" - TB1 : SFGI = %d, FWI = %d", | |
256 | (card->ats[pos] & 0x08), | |
257 | (card->ats[pos] & 0x80) >> 4); | |
561f7c11 | 258 | pos++; |
259 | } | |
f38a1528 | 260 | if (tc1 && card->ats_len > pos) { |
561f7c11 | 261 | PrintAndLog(" - TC1 : NAD is%s supported, CID is%s supported", |
f38a1528 | 262 | (card->ats[pos] & 0x01) ? "" : " NOT", |
263 | (card->ats[pos] & 0x02) ? "" : " NOT"); | |
561f7c11 | 264 | pos++; |
265 | } | |
f38a1528 | 266 | if (card->ats_len > pos) { |
561f7c11 | 267 | char *tip = ""; |
f38a1528 | 268 | if (card->ats_len - pos > 7) { |
269 | if (memcmp(card->ats + pos, "\xC1\x05\x2F\x2F\x01\xBC\xD6", 7) == 0) { | |
561f7c11 | 270 | tip = "-> MIFARE Plus X 2K or 4K"; |
f38a1528 | 271 | } else if (memcmp(card->ats + pos, "\xC1\x05\x2F\x2F\x00\x35\xC7", 7) == 0) { |
561f7c11 | 272 | tip = "-> MIFARE Plus S 2K or 4K"; |
273 | } | |
274 | } | |
f38a1528 | 275 | PrintAndLog(" - HB : %s%s", sprint_hex(card->ats + pos, card->ats_len - pos - 2), tip); |
276 | if (card->ats[pos] == 0xC1) { | |
561f7c11 | 277 | PrintAndLog(" c1 -> Mifare or (multiple) virtual cards of various type"); |
278 | PrintAndLog(" %02x -> Length is %d bytes", | |
f38a1528 | 279 | card->ats[pos + 1], card->ats[pos + 1]); |
280 | switch (card->ats[pos + 2] & 0xf0) { | |
561f7c11 | 281 | case 0x10: |
282 | PrintAndLog(" 1x -> MIFARE DESFire"); | |
283 | break; | |
284 | case 0x20: | |
285 | PrintAndLog(" 2x -> MIFARE Plus"); | |
286 | break; | |
287 | } | |
f38a1528 | 288 | switch (card->ats[pos + 2] & 0x0f) { |
561f7c11 | 289 | case 0x00: |
290 | PrintAndLog(" x0 -> <1 kByte"); | |
291 | break; | |
292 | case 0x01: | |
293 | PrintAndLog(" x0 -> 1 kByte"); | |
294 | break; | |
295 | case 0x02: | |
296 | PrintAndLog(" x0 -> 2 kByte"); | |
297 | break; | |
298 | case 0x03: | |
299 | PrintAndLog(" x0 -> 4 kByte"); | |
300 | break; | |
301 | case 0x04: | |
302 | PrintAndLog(" x0 -> 8 kByte"); | |
303 | break; | |
304 | } | |
f38a1528 | 305 | switch (card->ats[pos + 3] & 0xf0) { |
561f7c11 | 306 | case 0x00: |
307 | PrintAndLog(" 0x -> Engineering sample"); | |
308 | break; | |
309 | case 0x20: | |
310 | PrintAndLog(" 2x -> Released"); | |
311 | break; | |
312 | } | |
f38a1528 | 313 | switch (card->ats[pos + 3] & 0x0f) { |
561f7c11 | 314 | case 0x00: |
315 | PrintAndLog(" x0 -> Generation 1"); | |
316 | break; | |
317 | case 0x01: | |
318 | PrintAndLog(" x1 -> Generation 2"); | |
319 | break; | |
320 | case 0x02: | |
321 | PrintAndLog(" x2 -> Generation 3"); | |
322 | break; | |
323 | } | |
f38a1528 | 324 | switch (card->ats[pos + 4] & 0x0f) { |
561f7c11 | 325 | case 0x00: |
326 | PrintAndLog(" x0 -> Only VCSL supported"); | |
327 | break; | |
328 | case 0x01: | |
329 | PrintAndLog(" x1 -> VCS, VCSL, and SVC supported"); | |
330 | break; | |
331 | case 0x0E: | |
332 | PrintAndLog(" xE -> no VCS command supported"); | |
333 | break; | |
334 | } | |
335 | } | |
336 | } | |
79a73ab2 | 337 | } else { |
f38a1528 | 338 | PrintAndLog("proprietary non iso14443a-4 card found, RATS not supported"); |
19d6d91f | 339 | } |
534983d7 | 340 | |
f38a1528 | 341 | return resp.arg[0]; |
7fe9b0b7 | 342 | } |
343 | ||
db22dfe6 | 344 | // Collect ISO14443 Type A UIDs |
345 | int CmdHF14ACUIDs(const char *Cmd) | |
346 | { | |
347 | // requested number of UIDs | |
348 | int n = atoi(Cmd); | |
349 | // collect at least 1 (e.g. if no parameter was given) | |
350 | n = n > 0 ? n : 1; | |
351 | ||
352 | PrintAndLog("Collecting %d UIDs", n); | |
353 | PrintAndLog("Start: %u", time(NULL)); | |
354 | // repeat n times | |
355 | for (int i = 0; i < n; i++) { | |
356 | // execute anticollision procedure | |
357 | UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT, 0, 0}}; | |
358 | SendCommand(&c); | |
902cb3c0 | 359 | |
72b1090a | 360 | UsbCommand resp; |
361 | WaitForResponse(CMD_ACK,&resp); | |
902cb3c0 | 362 | |
f38a1528 | 363 | uint8_t *uid = resp.d.asBytes; |
364 | iso14a_card_select_t *card = (iso14a_card_select_t *)(uid + 12); | |
db22dfe6 | 365 | |
366 | // check if command failed | |
902cb3c0 | 367 | if (resp.arg[0] == 0) { |
db22dfe6 | 368 | PrintAndLog("Card select failed."); |
369 | } else { | |
f38a1528 | 370 | // check if UID is 4 bytes |
371 | if ((card->atqa[1] & 0xC0) == 0) { | |
372 | PrintAndLog("%02X%02X%02X%02X", | |
373 | *uid, *(uid + 1), *(uid + 2), *(uid + 3)); | |
374 | } else { | |
375 | PrintAndLog("UID longer than 4 bytes"); | |
db22dfe6 | 376 | } |
377 | } | |
378 | } | |
379 | PrintAndLog("End: %u", time(NULL)); | |
380 | ||
381 | return 1; | |
382 | } | |
383 | ||
7fe9b0b7 | 384 | // ## simulate iso14443a tag |
385 | // ## greg - added ability to specify tag UID | |
386 | int CmdHF14ASim(const char *Cmd) | |
81cd0474 | 387 | { |
388 | UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,{0,0,0}}; | |
389 | ||
390 | // Retrieve the tag type | |
391 | uint8_t tagtype = param_get8ex(Cmd,0,0,10); | |
392 | ||
393 | // When no argument was given, just print help message | |
394 | if (tagtype == 0) { | |
395 | PrintAndLog(""); | |
396 | PrintAndLog(" Emulating ISO/IEC 14443 type A tag with 4 or 7 byte UID"); | |
397 | PrintAndLog(""); | |
398 | PrintAndLog(" syntax: hf 14a sim <type> <uid>"); | |
399 | PrintAndLog(" types: 1 = MIFARE Classic"); | |
400 | PrintAndLog(" 2 = MIFARE Ultralight"); | |
401 | PrintAndLog(" 3 = MIFARE DESFIRE"); | |
402 | PrintAndLog(" 4 = ISO/IEC 14443-4"); | |
95e63594 | 403 | PrintAndLog(" 5 = MIFARE TNP3XXX"); |
81cd0474 | 404 | PrintAndLog(""); |
405 | return 1; | |
406 | } | |
407 | ||
408 | // Store the tag type | |
409 | c.arg[0] = tagtype; | |
410 | ||
411 | // Retrieve the full 4 or 7 byte long uid | |
412 | uint64_t long_uid = param_get64ex(Cmd,1,0,16); | |
413 | ||
414 | // Are we handling the (optional) second part uid? | |
415 | if (long_uid > 0xffffffff) { | |
125a98a1 | 416 | PrintAndLog("Emulating ISO/IEC 14443 type A tag with 7 byte UID (%014"llx")",long_uid); |
81cd0474 | 417 | // Store the second part |
418 | c.arg[2] = (long_uid & 0xffffffff); | |
419 | long_uid >>= 32; | |
420 | // Store the first part, ignore the first byte, it is replaced by cascade byte (0x88) | |
421 | c.arg[1] = (long_uid & 0xffffff); | |
422 | } else { | |
423 | PrintAndLog("Emulating ISO/IEC 14443 type A tag with 4 byte UID (%08x)",long_uid); | |
424 | // Only store the first part | |
425 | c.arg[1] = long_uid & 0xffffffff; | |
426 | } | |
427 | /* | |
428 | // At lease save the mandatory first part of the UID | |
429 | c.arg[0] = long_uid & 0xffffffff; | |
430 | ||
81cd0474 | 431 | if (c.arg[1] == 0) { |
432 | PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]); | |
433 | } | |
434 | ||
435 | switch (c.arg[0]) { | |
436 | case 1: { | |
437 | PrintAndLog("Emulating ISO/IEC 14443-3 type A tag with 4 byte UID"); | |
438 | 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)}; | |
439 | } break; | |
440 | case 2: { | |
441 | PrintAndLog("Emulating ISO/IEC 14443-4 type A tag with 7 byte UID"); | |
442 | } break; | |
443 | default: { | |
444 | PrintAndLog("Error: unkown tag type (%d)",c.arg[0]); | |
445 | PrintAndLog("syntax: hf 14a sim <uid>",c.arg[0]); | |
446 | PrintAndLog(" type1: 4 ",c.arg[0]); | |
447 | ||
448 | return 1; | |
449 | } break; | |
450 | } | |
451 | */ | |
452 | /* | |
7fe9b0b7 | 453 | unsigned int hi = 0, lo = 0; |
454 | int n = 0, i = 0; | |
455 | while (sscanf(&Cmd[i++], "%1x", &n ) == 1) { | |
456 | hi= (hi << 4) | (lo >> 28); | |
457 | lo= (lo << 4) | (n & 0xf); | |
458 | } | |
81cd0474 | 459 | */ |
460 | // 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)}; | |
461 | // PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]); | |
7fe9b0b7 | 462 | SendCommand(&c); |
463 | return 0; | |
464 | } | |
465 | ||
5cd9ec01 M |
466 | int CmdHF14ASnoop(const char *Cmd) { |
467 | int param = 0; | |
468 | ||
469 | if (param_getchar(Cmd, 0) == 'h') { | |
470 | PrintAndLog("It get data from the field and saves it into command buffer."); | |
471 | PrintAndLog("Buffer accessible from command hf 14a list."); | |
472 | PrintAndLog("Usage: hf 14a snoop [c][r]"); | |
473 | PrintAndLog("c - triggered by first data from card"); | |
474 | PrintAndLog("r - triggered by first 7-bit request from reader (REQ,WUP,...)"); | |
475 | PrintAndLog("sample: hf 14a snoop c r"); | |
476 | return 0; | |
477 | } | |
478 | ||
479 | for (int i = 0; i < 2; i++) { | |
480 | char ctmp = param_getchar(Cmd, i); | |
481 | if (ctmp == 'c' || ctmp == 'C') param |= 0x01; | |
482 | if (ctmp == 'r' || ctmp == 'R') param |= 0x02; | |
483 | } | |
484 | ||
485 | UsbCommand c = {CMD_SNOOP_ISO_14443a, {param, 0, 0}}; | |
7fe9b0b7 | 486 | SendCommand(&c); |
487 | return 0; | |
488 | } | |
489 | ||
5f6d6c90 | 490 | int CmdHF14ACmdRaw(const char *cmd) { |
491 | UsbCommand c = {CMD_READER_ISO_14443a, {0, 0, 0}}; | |
492 | uint8_t reply=1; | |
493 | uint8_t crc=0; | |
494 | uint8_t power=0; | |
495 | uint8_t active=0; | |
496 | uint8_t active_select=0; | |
497 | uint16_t numbits=0; | |
f38a1528 | 498 | uint16_t timeout=0; |
499 | uint8_t bTimeout=0; | |
5f6d6c90 | 500 | char buf[5]=""; |
501 | int i=0; | |
f38a1528 | 502 | uint8_t data[USB_CMD_DATA_SIZE]; |
5f6d6c90 | 503 | unsigned int datalen=0, temp; |
504 | ||
505 | if (strlen(cmd)<2) { | |
f38a1528 | 506 | PrintAndLog("Usage: hf 14a raw [-r] [-c] [-p] [-f] [-b] [-t] <number of bits> <0A 0B 0C ... hex>"); |
5f6d6c90 | 507 | PrintAndLog(" -r do not read response"); |
508 | PrintAndLog(" -c calculate and append CRC"); | |
509 | PrintAndLog(" -p leave the signal field ON after receive"); | |
510 | PrintAndLog(" -a active signal field ON without select"); | |
511 | PrintAndLog(" -s active signal field ON with select"); | |
512 | PrintAndLog(" -b number of bits to send. Useful for send partial byte"); | |
f38a1528 | 513 | PrintAndLog(" -t timeout"); |
5f6d6c90 | 514 | return 0; |
515 | } | |
516 | ||
517 | // strip | |
518 | while (*cmd==' ' || *cmd=='\t') cmd++; | |
519 | ||
520 | while (cmd[i]!='\0') { | |
521 | if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; } | |
522 | if (cmd[i]=='-') { | |
523 | switch (cmd[i+1]) { | |
524 | case 'r': | |
525 | reply=0; | |
526 | break; | |
527 | case 'c': | |
528 | crc=1; | |
529 | break; | |
530 | case 'p': | |
531 | power=1; | |
532 | break; | |
533 | case 'a': | |
534 | active=1; | |
535 | break; | |
536 | case 's': | |
537 | active_select=1; | |
538 | break; | |
539 | case 'b': | |
540 | sscanf(cmd+i+2,"%d",&temp); | |
541 | numbits = temp & 0xFFFF; | |
542 | i+=3; | |
543 | while(cmd[i]!=' ' && cmd[i]!='\0') { i++; } | |
544 | i-=2; | |
545 | break; | |
f38a1528 | 546 | case 't': |
547 | bTimeout=1; | |
548 | sscanf(cmd+i+2,"%d",&temp); | |
549 | timeout = temp & 0xFFFF; | |
550 | i+=3; | |
551 | while(cmd[i]!=' ' && cmd[i]!='\0') { i++; } | |
552 | i+=2; | |
553 | break; | |
5f6d6c90 | 554 | default: |
555 | PrintAndLog("Invalid option"); | |
556 | return 0; | |
557 | } | |
558 | i+=2; | |
559 | continue; | |
560 | } | |
561 | if ((cmd[i]>='0' && cmd[i]<='9') || | |
562 | (cmd[i]>='a' && cmd[i]<='f') || | |
563 | (cmd[i]>='A' && cmd[i]<='F') ) { | |
564 | buf[strlen(buf)+1]=0; | |
565 | buf[strlen(buf)]=cmd[i]; | |
566 | i++; | |
567 | ||
568 | if (strlen(buf)>=2) { | |
569 | sscanf(buf,"%x",&temp); | |
570 | data[datalen]=(uint8_t)(temp & 0xff); | |
5f6d6c90 | 571 | *buf=0; |
f38a1528 | 572 | if (++datalen>sizeof(data)){ |
573 | if (crc) | |
574 | PrintAndLog("Buffer is full, we can't add CRC to your data"); | |
575 | break; | |
576 | } | |
5f6d6c90 | 577 | } |
578 | continue; | |
579 | } | |
580 | PrintAndLog("Invalid char on input"); | |
581 | return 0; | |
582 | } | |
f38a1528 | 583 | if(crc && datalen>0 && datalen<sizeof(data)-2) |
5f6d6c90 | 584 | { |
585 | uint8_t first, second; | |
586 | ComputeCrc14443(CRC_14443_A, data, datalen, &first, &second); | |
587 | data[datalen++] = first; | |
588 | data[datalen++] = second; | |
589 | } | |
590 | ||
591 | if(active || active_select) | |
592 | { | |
593 | c.arg[0] |= ISO14A_CONNECT; | |
594 | if(active) | |
595 | c.arg[0] |= ISO14A_NO_SELECT; | |
596 | } | |
f38a1528 | 597 | if(bTimeout){ |
598 | #define MAX_TIMEOUT 624*105 // max timeout is 624 ms | |
599 | c.arg[0] |= ISO14A_SET_TIMEOUT; | |
600 | c.arg[2] = timeout * 105; // each bit is about 9.4 us | |
601 | if(c.arg[2]>MAX_TIMEOUT) { | |
602 | c.arg[2] = MAX_TIMEOUT; | |
603 | PrintAndLog("Set timeout to 624 ms. The max we can wait for response"); | |
604 | } | |
605 | } | |
5f6d6c90 | 606 | if(power) |
607 | c.arg[0] |= ISO14A_NO_DISCONNECT; | |
608 | if(datalen>0) | |
609 | c.arg[0] |= ISO14A_RAW; | |
610 | ||
f38a1528 | 611 | // Max buffer is USB_CMD_DATA_SIZE |
612 | c.arg[1] = (datalen & 0xFFFF) | (numbits << 16); | |
5f6d6c90 | 613 | memcpy(c.d.asBytes,data,datalen); |
614 | ||
615 | SendCommand(&c); | |
616 | ||
617 | if (reply) { | |
618 | if(active_select) | |
619 | waitCmd(1); | |
620 | if(datalen>0) | |
621 | waitCmd(0); | |
622 | } // if reply | |
623 | return 0; | |
624 | } | |
625 | ||
626 | static void waitCmd(uint8_t iSelect) | |
627 | { | |
628 | uint8_t *recv; | |
629 | UsbCommand resp; | |
630 | char *hexout; | |
631 | ||
95e63594 | 632 | if (WaitForResponseTimeout(CMD_ACK,&resp,10000)) { |
5f6d6c90 | 633 | recv = resp.d.asBytes; |
634 | uint8_t iLen = iSelect ? resp.arg[1] : resp.arg[0]; | |
635 | PrintAndLog("received %i octets",iLen); | |
636 | if(!iLen) | |
637 | return; | |
638 | hexout = (char *)malloc(iLen * 3 + 1); | |
639 | if (hexout != NULL) { | |
5f6d6c90 | 640 | for (int i = 0; i < iLen; i++) { // data in hex |
f66021cf | 641 | sprintf(&hexout[i * 3], "%02X ", recv[i]); |
5f6d6c90 | 642 | } |
643 | PrintAndLog("%s", hexout); | |
644 | free(hexout); | |
645 | } else { | |
646 | PrintAndLog("malloc failed your client has low memory?"); | |
647 | } | |
648 | } else { | |
649 | PrintAndLog("timeout while waiting for reply."); | |
650 | } | |
651 | } | |
652 | ||
7fe9b0b7 | 653 | static command_t CommandTable[] = |
654 | { | |
5acd09bd | 655 | {"help", CmdHelp, 1, "This help"}, |
656 | {"list", CmdHF14AList, 0, "List ISO 14443a history"}, | |
657 | {"reader", CmdHF14AReader, 0, "Act like an ISO14443 Type A reader"}, | |
658 | {"cuids", CmdHF14ACUIDs, 0, "<n> Collect n>0 ISO14443 Type A UIDs in one go"}, | |
659 | {"sim", CmdHF14ASim, 0, "<UID> -- Fake ISO 14443a tag"}, | |
660 | {"snoop", CmdHF14ASnoop, 0, "Eavesdrop ISO 14443 Type A"}, | |
5f6d6c90 | 661 | {"raw", CmdHF14ACmdRaw, 0, "Send raw hex data to tag"}, |
7fe9b0b7 | 662 | {NULL, NULL, 0, NULL} |
663 | }; | |
664 | ||
902cb3c0 | 665 | int CmdHF14A(const char *Cmd) { |
f397b5cc | 666 | // flush |
902cb3c0 | 667 | WaitForResponseTimeout(CMD_ACK,NULL,100); |
f397b5cc M |
668 | |
669 | // parse | |
7fe9b0b7 | 670 | CmdsParse(CommandTable, Cmd); |
671 | return 0; | |
672 | } | |
673 | ||
674 | int CmdHelp(const char *Cmd) | |
675 | { | |
676 | CmdsHelp(CommandTable); | |
677 | return 0; | |
678 | } |