]>
Commit | Line | Data |
---|---|---|
a553f267 | 1 | //----------------------------------------------------------------------------- |
2 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> | |
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 commands | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
7fe9b0b7 | 11 | #include <stdio.h> |
d3a22c7d | 12 | #include <string.h> |
902cb3c0 | 13 | #include "proxmark3.h" |
7fe9b0b7 | 14 | #include "graph.h" |
15 | #include "ui.h" | |
16 | #include "cmdparser.h" | |
17 | #include "cmdhf.h" | |
18 | #include "cmdhf14a.h" | |
19 | #include "cmdhf14b.h" | |
20 | #include "cmdhf15.h" | |
5acd09bd | 21 | #include "cmdhfepa.h" |
7fe9b0b7 | 22 | #include "cmdhflegic.h" |
cee5a30d | 23 | #include "cmdhficlass.h" |
9ca155ba | 24 | #include "cmdhfmf.h" |
f38a1528 | 25 | #include "cmdhfmfu.h" |
26 | #include "cmdhfmfdes.h" | |
27 | #include "cmdhfdes.h" | |
7fe9b0b7 | 28 | |
29 | static int CmdHelp(const char *Cmd); | |
30 | ||
31 | int CmdHFTune(const char *Cmd) | |
32 | { | |
33 | UsbCommand c={CMD_MEASURE_ANTENNA_TUNING_HF}; | |
34 | SendCommand(&c); | |
35 | return 0; | |
36 | } | |
d3a22c7d | 37 | // for the time being. Need better Bigbuf handling. |
38 | #define TRACE_SIZE 3000 | |
39 | ||
40 | #define ICLASS_CMD_ACTALL 0x0A | |
41 | #define ICLASS_CMD_IDENTIFY 0x0C | |
42 | #define ICLASS_CMD_READ 0x0C | |
43 | #define ICLASS_CMD_SELECT 0x81 | |
44 | #define ICLASS_CMD_PAGESEL 0x84 | |
45 | #define ICLASS_CMD_READCHECK 0x88 | |
46 | #define ICLASS_CMD_CHECK 0x05 | |
47 | #define ICLASS_CMD_SOF 0x0F | |
48 | #define ICLASS_CMD_HALT 0x00 | |
49 | ||
50 | #define iso14443_CMD_WUPA 0x52 | |
51 | #define iso14443_CMD_SELECT 0x93 | |
52 | #define iso14443_CMD_SELECT_2 0x95 | |
53 | #define iso14443_CMD_REQ 0x26 | |
54 | #define iso14443_CMD_READBLOCK 0x30 | |
55 | #define iso14443_CMD_WRITEBLOCK 0xA0 | |
56 | #define iso14443_CMD_INC 0xC0 | |
57 | #define iso14443_CMD_DEC 0xC1 | |
58 | #define iso14443_CMD_RESTORE 0xC2 | |
59 | #define iso14443_CMD_TRANSFER 0xB0 | |
60 | #define iso14443_CMD_HALT 0x50 | |
61 | #define iso14443_CMD_RATS 0xE0 | |
62 | ||
63 | ||
64 | void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) | |
65 | { | |
66 | switch(cmd[0]) | |
67 | { | |
68 | case iso14443_CMD_WUPA: snprintf(exp,size,"WUPA"); break; | |
69 | case iso14443_CMD_SELECT:{ | |
70 | if(cmdsize > 2) | |
71 | { | |
72 | snprintf(exp,size,"SELECT_UID"); break; | |
73 | }else | |
74 | { | |
75 | snprintf(exp,size,"SELECT_ALL"); break; | |
76 | } | |
77 | } | |
78 | case iso14443_CMD_SELECT_2: snprintf(exp,size,"SELECT_2"); break; | |
79 | case iso14443_CMD_REQ: snprintf(exp,size,"REW"); break; | |
80 | case iso14443_CMD_READBLOCK: snprintf(exp,size,"READBLOCK(%d)",cmd[1]); break; | |
81 | case iso14443_CMD_WRITEBLOCK: snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); break; | |
82 | case iso14443_CMD_INC: snprintf(exp,size,"INC(%d)",cmd[1]); break; | |
83 | case iso14443_CMD_DEC: snprintf(exp,size,"DEC(%d)",cmd[1]); break; | |
84 | case iso14443_CMD_RESTORE: snprintf(exp,size,"RESTORE(%d)",cmd[1]); break; | |
85 | case iso14443_CMD_TRANSFER: snprintf(exp,size,"TRANSFER(%d)",cmd[1]); break; | |
86 | case iso14443_CMD_HALT: snprintf(exp,size,"HALT"); break; | |
87 | case iso14443_CMD_RATS: snprintf(exp,size,"RATS"); break; | |
88 | default: snprintf(exp,size,"?"); break; | |
89 | } | |
90 | return; | |
91 | } | |
92 | ||
93 | void annotateIclass(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) | |
94 | { | |
95 | ||
96 | if(cmdsize > 1 && cmd[0] == ICLASS_CMD_READ) | |
97 | { | |
98 | snprintf(exp,size,"READ(%d)",cmd[1]); | |
99 | return; | |
100 | } | |
101 | ||
102 | switch(cmd[0]) | |
103 | { | |
104 | case ICLASS_CMD_ACTALL: snprintf(exp,size,"ACTALL"); break; | |
105 | case ICLASS_CMD_IDENTIFY: snprintf(exp,size,"IDENTIFY"); break; | |
106 | case ICLASS_CMD_SELECT: snprintf(exp,size,"SELECT"); break; | |
107 | case ICLASS_CMD_PAGESEL: snprintf(exp,size,"PAGESEL"); break; | |
108 | case ICLASS_CMD_READCHECK: snprintf(exp,size,"READCHECK"); break; | |
109 | case ICLASS_CMD_CHECK: snprintf(exp,size,"CHECK"); break; | |
110 | case ICLASS_CMD_SOF: snprintf(exp,size,"SOF"); break; | |
111 | case ICLASS_CMD_HALT: snprintf(exp,size,"HALT"); break; | |
112 | default: snprintf(exp,size,"?"); break; | |
113 | } | |
114 | return; | |
115 | } | |
116 | ||
117 | ||
118 | ||
119 | uint16_t printTraceLine(uint16_t tracepos, uint8_t* trace, bool iclass, bool showWaitCycles) | |
120 | { | |
121 | bool isResponse; | |
122 | uint16_t duration, data_len,parity_len; | |
123 | ||
124 | uint32_t timestamp, first_timestamp, EndOfTransmissionTimestamp; | |
125 | char explanation[30] = {0}; | |
126 | ||
127 | first_timestamp = *((uint32_t *)(trace)); | |
128 | timestamp = *((uint32_t *)(trace + tracepos)); | |
129 | // Break and stick with current result if buffer was not completely full | |
130 | if (timestamp == 0x44444444) return TRACE_SIZE; | |
131 | ||
132 | tracepos += 4; | |
133 | duration = *((uint16_t *)(trace + tracepos)); | |
134 | tracepos += 2; | |
135 | data_len = *((uint16_t *)(trace + tracepos)); | |
136 | tracepos += 2; | |
137 | ||
138 | if (data_len & 0x8000) { | |
139 | data_len &= 0x7fff; | |
140 | isResponse = true; | |
141 | } else { | |
142 | isResponse = false; | |
143 | } | |
144 | parity_len = (data_len-1)/8 + 1; | |
145 | ||
146 | if (tracepos + data_len + parity_len >= TRACE_SIZE) { | |
147 | return TRACE_SIZE; | |
148 | } | |
149 | ||
150 | uint8_t *frame = trace + tracepos; | |
151 | tracepos += data_len; | |
152 | uint8_t *parityBytes = trace + tracepos; | |
153 | tracepos += parity_len; | |
154 | ||
155 | //--- Draw the data column | |
156 | char line[16][110]; | |
157 | for (int j = 0; j < data_len; j++) { | |
158 | int oddparity = 0x01; | |
159 | int k; | |
160 | ||
161 | for (k=0 ; k<8 ; k++) { | |
162 | oddparity ^= (((frame[j] & 0xFF) >> k) & 0x01); | |
163 | } | |
164 | ||
165 | uint8_t parityBits = parityBytes[j>>3]; | |
166 | ||
167 | if (isResponse && (oddparity != ((parityBits >> (7-(j&0x0007))) & 0x01))) { | |
168 | sprintf(line[j/16]+((j%16)*4), "%02x! ", frame[j]); | |
169 | } else { | |
170 | sprintf(line[j/16]+((j%16)*4), "%02x ", frame[j]); | |
171 | } | |
172 | } | |
173 | //--- Draw the CRC column | |
174 | bool crcError = false; | |
175 | ||
176 | if (data_len > 2) { | |
177 | uint8_t b1, b2; | |
178 | if(iclass) | |
179 | { | |
180 | if(!isResponse && data_len == 4 ) { | |
181 | // Rough guess that this is a command from the reader | |
182 | // For iClass the command byte is not part of the CRC | |
183 | ComputeCrc14443(CRC_ICLASS, &frame[1], data_len-3, &b1, &b2); | |
184 | } | |
185 | else { | |
186 | // For other data.. CRC might not be applicable (UPDATE commands etc.) | |
187 | ComputeCrc14443(CRC_ICLASS, frame, data_len-2, &b1, &b2); | |
188 | } | |
189 | ||
190 | if (b1 != frame[data_len-2] || b2 != frame[data_len-1]) { | |
191 | crcError = true; | |
192 | } | |
193 | ||
194 | }else{//Iso 14443a | |
195 | ||
196 | ComputeCrc14443(CRC_14443_A, frame, data_len-2, &b1, &b2); | |
197 | ||
198 | if (b1 != frame[data_len-2] || b2 != frame[data_len-1]) { | |
199 | if(!(isResponse & (data_len < 6))) | |
200 | { | |
201 | crcError = true; | |
202 | } | |
203 | } | |
204 | } | |
205 | ||
206 | } | |
207 | char *crc = crcError ? "!crc" :" "; | |
208 | ||
209 | EndOfTransmissionTimestamp = timestamp + duration; | |
210 | ||
211 | if(!isResponse) | |
212 | { | |
213 | if(iclass) annotateIclass(explanation,sizeof(explanation),frame,data_len); | |
214 | else annotateIso14443a(explanation,sizeof(explanation),frame,data_len); | |
215 | } | |
216 | ||
217 | int num_lines = (data_len - 1)/16 + 1; | |
218 | for (int j = 0; j < num_lines; j++) { | |
219 | if (j == 0) { | |
220 | PrintAndLog(" %9d | %9d | %s | %-64s| %s| %s", | |
221 | (timestamp - first_timestamp), | |
222 | (EndOfTransmissionTimestamp - first_timestamp), | |
223 | (isResponse ? "Tag" : "Rdr"), | |
224 | line[j], | |
225 | (j == num_lines-1) ? crc : " ", | |
226 | (j == num_lines-1) ? explanation : ""); | |
227 | } else { | |
228 | PrintAndLog(" | | | %-64s| %s| %s", | |
229 | line[j], | |
230 | (j == num_lines-1)?crc:" ", | |
231 | (j == num_lines-1) ? explanation : ""); | |
232 | } | |
233 | } | |
234 | ||
235 | bool next_isResponse = *((uint16_t *)(trace + tracepos + 6)) & 0x8000; | |
236 | ||
237 | if (showWaitCycles && !isResponse && next_isResponse) { | |
238 | uint32_t next_timestamp = *((uint32_t *)(trace + tracepos)); | |
239 | if (next_timestamp != 0x44444444) { | |
240 | PrintAndLog(" %9d | %9d | %s | fdt (Frame Delay Time): %d", | |
241 | (EndOfTransmissionTimestamp - first_timestamp), | |
242 | (next_timestamp - first_timestamp), | |
243 | " ", | |
244 | (next_timestamp - EndOfTransmissionTimestamp)); | |
245 | } | |
246 | } | |
247 | return tracepos; | |
248 | } | |
249 | ||
250 | int CmdHFList(const char *Cmd) | |
251 | { | |
252 | bool showWaitCycles = false; | |
253 | char type[40] = {0}; | |
254 | int tlen = param_getstr(Cmd,0,type); | |
255 | char param = param_getchar(Cmd, 1); | |
256 | bool errors = false; | |
257 | bool iclass = false; | |
258 | //Validate params | |
259 | if(tlen == 0 || (strcmp(type, "iclass") != 0 && strcmp(type,"14a") != 0)) | |
260 | { | |
261 | errors = true; | |
262 | } | |
263 | if(param == 'h' || (param !=0 && param != 'f')) | |
264 | { | |
265 | errors = true; | |
266 | } | |
267 | ||
268 | if (errors) { | |
269 | PrintAndLog("List protocol data in trace buffer."); | |
270 | PrintAndLog("Usage: hf list [14a|iclass] [f]"); | |
271 | PrintAndLog(" 14a - interpret data as iso14443a communications"); | |
272 | PrintAndLog(" iclass - interpret data as iclass communications"); | |
273 | PrintAndLog(" f - show frame delay times as well"); | |
274 | PrintAndLog(""); | |
275 | PrintAndLog("example: hf list 14a f"); | |
276 | PrintAndLog("example: hf list iclass"); | |
277 | return 0; | |
278 | } | |
279 | if(strcmp(type, "iclass") == 0) | |
280 | { | |
281 | iclass = true; | |
282 | } | |
283 | ||
284 | if (param == 'f') { | |
285 | showWaitCycles = true; | |
286 | } | |
287 | ||
288 | ||
289 | uint8_t trace[TRACE_SIZE]; | |
290 | uint16_t tracepos = 0; | |
291 | GetFromBigBuf(trace, TRACE_SIZE, 0); | |
292 | WaitForResponse(CMD_ACK, NULL); | |
293 | ||
294 | PrintAndLog("Recorded Activity"); | |
295 | PrintAndLog(""); | |
296 | PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer"); | |
297 | PrintAndLog("iso14443a - All times are in carrier periods (1/13.56Mhz)"); | |
298 | PrintAndLog("iClass - Timings are not as accurate"); | |
299 | PrintAndLog(""); | |
300 | PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |"); | |
301 | PrintAndLog("-----------|-----------|-----|-----------------------------------------------------------------|-----|--------------------|"); | |
302 | ||
303 | while(tracepos < TRACE_SIZE) | |
304 | { | |
305 | tracepos = printTraceLine(tracepos, trace, iclass, showWaitCycles); | |
306 | } | |
307 | return 0; | |
308 | } | |
309 | ||
7fe9b0b7 | 310 | |
311 | static command_t CommandTable[] = | |
312 | { | |
313 | {"help", CmdHelp, 1, "This help"}, | |
37239a7c | 314 | {"14a", CmdHF14A, 1, "{ ISO14443A RFIDs... }"}, |
315 | {"14b", CmdHF14B, 1, "{ ISO14443B RFIDs... }"}, | |
316 | {"15", CmdHF15, 1, "{ ISO15693 RFIDs... }"}, | |
5acd09bd | 317 | {"epa", CmdHFEPA, 1, "{ German Identification Card... }"}, |
8e220a91 | 318 | {"legic", CmdHFLegic, 0, "{ LEGIC RFIDs... }"}, |
cee5a30d | 319 | {"iclass", CmdHFiClass, 1, "{ ICLASS RFIDs... }"}, |
9ca155ba | 320 | {"mf", CmdHFMF, 1, "{ MIFARE RFIDs... }"}, |
d3a22c7d | 321 | {"mfu", CmdHFMFUltra, 1, "{ MIFARE Ultralight RFIDs... }"}, |
322 | {"mfdes", CmdHFMFDes, 1, "{ MIFARE Desfire RFIDs... }"}, | |
f38a1528 | 323 | {"des", CmdHFDES, 0, "{ MIFARE DESfire}"}, |
c59c3405 | 324 | {"tune", CmdHFTune, 0, "Continuously measure HF antenna tuning"}, |
d3a22c7d | 325 | {"list", CmdHFList, 1, "List protocol data in trace buffer"}, |
7fe9b0b7 | 326 | {NULL, NULL, 0, NULL} |
327 | }; | |
328 | ||
329 | int CmdHF(const char *Cmd) | |
330 | { | |
331 | CmdsParse(CommandTable, Cmd); | |
332 | return 0; | |
333 | } | |
334 | ||
335 | int CmdHelp(const char *Cmd) | |
336 | { | |
337 | CmdsHelp(CommandTable); | |
338 | return 0; | |
339 | } |