]>
Commit | Line | Data |
---|---|---|
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 <string.h> | |
15 | #include <unistd.h> | |
16 | #include "util.h" | |
17 | #include "iso14443crc.h" | |
18 | #include "data.h" | |
19 | #include "proxusb.h" | |
20 | #include "ui.h" | |
21 | #include "cmdparser.h" | |
22 | #include "cmdhf14a.h" | |
23 | #include "common.h" | |
24 | #include "cmdmain.h" | |
25 | ||
26 | static int CmdHelp(const char *Cmd); | |
27 | ||
28 | int CmdHF14AList(const char *Cmd) | |
29 | { | |
30 | uint8_t got[1920]; | |
31 | GetFromBigBuf(got, sizeof(got)); | |
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 | } | |
152 | return 0; | |
153 | } | |
154 | ||
155 | void iso14a_set_timeout(uint32_t timeout) { | |
156 | UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_SET_TIMEOUT, 0, timeout}}; | |
157 | SendCommand(&c); | |
158 | } | |
159 | ||
160 | int CmdHF14AReader(const char *Cmd) | |
161 | { | |
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; | |
166 | iso14a_card_select_t * card = (iso14a_card_select_t *)(uid + 12); | |
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]); | |
176 | switch (card->sak) { | |
177 | case 0: PrintAndLog(" SAK : MIFARE ultralight?"); break; | |
178 | case 8: PrintAndLog(" SAK : MIFARE CLASSIC 1K"); break; | |
179 | case 9: PrintAndLog(" SAK : MIFARE MINI"); break; | |
180 | case 18: PrintAndLog(" SAK : MIFARE CLASSIC 4K"); break; | |
181 | case 20: PrintAndLog(" SAK : MIFARE DESFIRE or JCOP 31/41"); break; | |
182 | case 28: PrintAndLog(" SAK : JCOP31 or JCOP41 v2.3.1"); break; | |
183 | case 38: PrintAndLog(" SAK : Nokia 6212 or 6131 MIFARE CLASSIC 4K"); break; | |
184 | case 88: PrintAndLog(" SAK : Infineon MIFARE CLASSIC 1K"); break; | |
185 | case 98: PrintAndLog(" SAK : Gemplus MPCOS"); break; | |
186 | default: ; | |
187 | } | |
188 | if(resp->arg[0] == 1) | |
189 | PrintAndLog(" ATS : %s", sprint_hex(card->ats, card->ats_len)); | |
190 | else | |
191 | PrintAndLog("proprietary non-iso14443a card found, RATS not supported"); | |
192 | ||
193 | return resp->arg[0]; | |
194 | } | |
195 | ||
196 | // ## simulate iso14443a tag | |
197 | // ## greg - added ability to specify tag UID | |
198 | int CmdHF14ASim(const char *Cmd) | |
199 | { | |
200 | ||
201 | unsigned int hi = 0, lo = 0; | |
202 | int n = 0, i = 0; | |
203 | while (sscanf(&Cmd[i++], "%1x", &n ) == 1) { | |
204 | hi= (hi << 4) | (lo >> 28); | |
205 | lo= (lo << 4) | (n & 0xf); | |
206 | } | |
207 | ||
208 | // c.arg should be set to *Cmd or convert *Cmd to the correct format for a uid | |
209 | UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a, {hi, lo, 0}}; | |
210 | PrintAndLog("Emulating 14443A TAG with UID %x%16x", hi, lo); | |
211 | SendCommand(&c); | |
212 | return 0; | |
213 | } | |
214 | ||
215 | int CmdHF14ASnoop(const char *Cmd) | |
216 | { | |
217 | UsbCommand c = {CMD_SNOOP_ISO_14443a}; | |
218 | SendCommand(&c); | |
219 | return 0; | |
220 | } | |
221 | ||
222 | static command_t CommandTable[] = | |
223 | { | |
224 | {"help", CmdHelp, 1, "This help"}, | |
225 | {"list", CmdHF14AList, 0, "List ISO 14443a history"}, | |
226 | {"reader", CmdHF14AReader, 0, "Act like an ISO14443 Type A reader"}, | |
227 | {"sim", CmdHF14ASim, 0, "<UID> -- Fake ISO 14443a tag"}, | |
228 | {"snoop", CmdHF14ASnoop, 0, "Eavesdrop ISO 14443 Type A"}, | |
229 | {NULL, NULL, 0, NULL} | |
230 | }; | |
231 | ||
232 | int CmdHF14A(const char *Cmd) | |
233 | { | |
234 | // flush | |
235 | while (WaitForResponseTimeout(CMD_ACK, 500) != NULL) ; | |
236 | ||
237 | // parse | |
238 | CmdsParse(CommandTable, Cmd); | |
239 | return 0; | |
240 | } | |
241 | ||
242 | int CmdHelp(const char *Cmd) | |
243 | { | |
244 | CmdsHelp(CommandTable); | |
245 | return 0; | |
246 | } |