]>
Commit | Line | Data |
---|---|---|
a553f267 | 1 | //----------------------------------------------------------------------------- |
2 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> | |
4f131b53 | 3 | // Merlok - 2017 |
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 commands | |
10 | //----------------------------------------------------------------------------- | |
11 | ||
acf0582d | 12 | #include <stdlib.h> |
7fe9b0b7 | 13 | #include <stdio.h> |
4c3de57a | 14 | #include <string.h> |
902cb3c0 | 15 | #include "proxmark3.h" |
7cb8516c | 16 | #include "util.h" |
7fe9b0b7 | 17 | #include "ui.h" |
7cb8516c | 18 | #include "iso14443crc.h" |
1f065e1d | 19 | #include "parity.h" |
7cb8516c | 20 | #include "cmdmain.h" |
7fe9b0b7 | 21 | #include "cmdparser.h" |
22 | #include "cmdhf.h" | |
23 | #include "cmdhf14a.h" | |
24 | #include "cmdhf14b.h" | |
25 | #include "cmdhf15.h" | |
5acd09bd | 26 | #include "cmdhfepa.h" |
7fe9b0b7 | 27 | #include "cmdhflegic.h" |
cee5a30d | 28 | #include "cmdhficlass.h" |
9ca155ba | 29 | #include "cmdhfmf.h" |
5ee70129 | 30 | #include "cmdhfmfu.h" |
05ddb52c | 31 | #include "cmdhftopaz.h" |
b67f7ec3 | 32 | #include "protocols.h" |
3c5fce2b | 33 | #include "emv/cmdemv.h" |
4f131b53 | 34 | #include "cmdhflist.h" |
7fe9b0b7 | 35 | |
36 | static int CmdHelp(const char *Cmd); | |
37 | ||
38 | int CmdHFTune(const char *Cmd) | |
39 | { | |
40 | UsbCommand c={CMD_MEASURE_ANTENNA_TUNING_HF}; | |
41 | SendCommand(&c); | |
42 | return 0; | |
43 | } | |
4c3de57a | 44 | |
ef00343c | 45 | /** |
46 | * @brief iso14443B_CRC_check Checks CRC in command or response | |
41fdd0f0 MHS |
47 | * @param isResponse |
48 | * @param data | |
49 | * @param len | |
50 | * @return 0 : CRC-command, CRC not ok | |
51 | * 1 : CRC-command, CRC ok | |
52 | * 2 : Not crc-command | |
53 | */ | |
54 | ||
55 | uint8_t iso14443B_CRC_check(bool isResponse, uint8_t* data, uint8_t len) | |
56 | { | |
57 | uint8_t b1,b2; | |
58 | ||
59 | if(len <= 2) return 2; | |
60 | ||
61 | ComputeCrc14443(CRC_14443_B, data, len-2, &b1, &b2); | |
62 | if(b1 != data[len-2] || b2 != data[len-1]) { | |
ef00343c | 63 | return 0; |
64 | } else { | |
65 | return 1; | |
41fdd0f0 | 66 | } |
41fdd0f0 MHS |
67 | } |
68 | ||
49726b40 MHS |
69 | /** |
70 | * @brief iclass_CRC_Ok Checks CRC in command or response | |
71 | * @param isResponse | |
72 | * @param data | |
73 | * @param len | |
74 | * @return 0 : CRC-command, CRC not ok | |
75 | * 1 : CRC-command, CRC ok | |
76 | * 2 : Not crc-command | |
77 | */ | |
41fdd0f0 | 78 | uint8_t iclass_CRC_check(bool isResponse, uint8_t* data, uint8_t len) |
49726b40 MHS |
79 | { |
80 | if(len < 4) return 2;//CRC commands (and responses) are all at least 4 bytes | |
81 | ||
82 | uint8_t b1, b2; | |
83 | ||
84 | if(!isResponse)//Commands to tag | |
85 | { | |
86 | /** | |
87 | These commands should have CRC. Total length leftmost | |
88 | 4 READ | |
89 | 4 READ4 | |
90 | 12 UPDATE - unsecured, ends with CRC16 | |
91 | 14 UPDATE - secured, ends with signature instead | |
92 | 4 PAGESEL | |
93 | **/ | |
94 | if(len == 4 || len == 12)//Covers three of them | |
95 | { | |
96 | //Don't include the command byte | |
97 | ComputeCrc14443(CRC_ICLASS, (data+1), len-3, &b1, &b2); | |
98 | return b1 == data[len -2] && b2 == data[len-1]; | |
99 | } | |
100 | return 2; | |
101 | }else{ | |
102 | /** | |
103 | These tag responses should have CRC. Total length leftmost | |
104 | ||
105 | 10 READ data[8] crc[2] | |
106 | 34 READ4 data[32]crc[2] | |
107 | 10 UPDATE data[8] crc[2] | |
108 | 10 SELECT csn[8] crc[2] | |
109 | 10 IDENTIFY asnb[8] crc[2] | |
110 | 10 PAGESEL block1[8] crc[2] | |
111 | 10 DETECT csn[8] crc[2] | |
112 | ||
113 | These should not | |
114 | ||
115 | 4 CHECK chip_response[4] | |
116 | 8 READCHECK data[8] | |
117 | 1 ACTALL sof[1] | |
118 | 1 ACT sof[1] | |
119 | ||
120 | In conclusion, without looking at the command; any response | |
121 | of length 10 or 34 should have CRC | |
122 | **/ | |
123 | if(len != 10 && len != 34) return true; | |
124 | ||
125 | ComputeCrc14443(CRC_ICLASS, data, len-2, &b1, &b2); | |
126 | return b1 == data[len -2] && b2 == data[len-1]; | |
127 | } | |
128 | } | |
4c3de57a | 129 | |
ee1eadee | 130 | |
a8904ebd | 131 | bool is_last_record(uint16_t tracepos, uint8_t *trace, uint16_t traceLen) |
4c3de57a | 132 | { |
a8904ebd | 133 | return(tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) >= traceLen); |
134 | } | |
135 | ||
136 | ||
137 | bool next_record_is_response(uint16_t tracepos, uint8_t *trace) | |
138 | { | |
139 | uint16_t next_records_datalen = *((uint16_t *)(trace + tracepos + sizeof(uint32_t) + sizeof(uint16_t))); | |
140 | ||
141 | return(next_records_datalen & 0x8000); | |
142 | } | |
143 | ||
144 | ||
ef00343c | 145 | bool merge_topaz_reader_frames(uint32_t timestamp, uint32_t *duration, uint16_t *tracepos, uint16_t traceLen, uint8_t *trace, uint8_t *frame, uint8_t *topaz_reader_command, uint16_t *data_len) |
a8904ebd | 146 | { |
147 | ||
48ece4a7 | 148 | #define MAX_TOPAZ_READER_CMD_LEN 16 |
a8904ebd | 149 | |
150 | uint32_t last_timestamp = timestamp + *duration; | |
151 | ||
ef00343c | 152 | if ((*data_len != 1) || (frame[0] == TOPAZ_WUPA) || (frame[0] == TOPAZ_REQA)) return false; |
4c3de57a | 153 | |
ef00343c | 154 | memcpy(topaz_reader_command, frame, *data_len); |
a8904ebd | 155 | |
156 | while (!is_last_record(*tracepos, trace, traceLen) && !next_record_is_response(*tracepos, trace)) { | |
157 | uint32_t next_timestamp = *((uint32_t *)(trace + *tracepos)); | |
158 | *tracepos += sizeof(uint32_t); | |
ef00343c | 159 | uint16_t next_duration = *((uint16_t *)(trace + *tracepos)); |
a8904ebd | 160 | *tracepos += sizeof(uint16_t); |
161 | uint16_t next_data_len = *((uint16_t *)(trace + *tracepos)) & 0x7FFF; | |
162 | *tracepos += sizeof(uint16_t); | |
163 | uint8_t *next_frame = (trace + *tracepos); | |
164 | *tracepos += next_data_len; | |
ef00343c | 165 | if ((next_data_len == 1) && (*data_len + next_data_len <= MAX_TOPAZ_READER_CMD_LEN)) { |
a8904ebd | 166 | memcpy(topaz_reader_command + *data_len, next_frame, next_data_len); |
167 | *data_len += next_data_len; | |
ef00343c | 168 | last_timestamp = next_timestamp + next_duration; |
169 | } else { | |
170 | // rewind and exit | |
171 | *tracepos = *tracepos - next_data_len - sizeof(uint16_t) - sizeof(uint16_t) - sizeof(uint32_t); | |
172 | break; | |
173 | } | |
a8904ebd | 174 | uint16_t next_parity_len = (next_data_len-1)/8 + 1; |
175 | *tracepos += next_parity_len; | |
176 | } | |
177 | ||
178 | *duration = last_timestamp - timestamp; | |
ef00343c | 179 | |
180 | return true; | |
ee1eadee | 181 | } |
182 | ||
183 | ||
05ddb52c | 184 | uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace, uint8_t protocol, bool showWaitCycles, bool markCRCBytes) |
4c3de57a MHS |
185 | { |
186 | bool isResponse; | |
a8904ebd | 187 | uint16_t data_len, parity_len; |
188 | uint32_t duration; | |
ee1eadee | 189 | uint8_t topaz_reader_command[9]; |
4c3de57a MHS |
190 | uint32_t timestamp, first_timestamp, EndOfTransmissionTimestamp; |
191 | char explanation[30] = {0}; | |
7b215d14 OM |
192 | uint8_t mfData[32] = {0}; |
193 | size_t mfDataLen = 0; | |
4c3de57a | 194 | |
f71f4deb | 195 | if (tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen) return traceLen; |
196 | ||
4c3de57a MHS |
197 | first_timestamp = *((uint32_t *)(trace)); |
198 | timestamp = *((uint32_t *)(trace + tracepos)); | |
4c3de57a MHS |
199 | |
200 | tracepos += 4; | |
201 | duration = *((uint16_t *)(trace + tracepos)); | |
202 | tracepos += 2; | |
203 | data_len = *((uint16_t *)(trace + tracepos)); | |
204 | tracepos += 2; | |
205 | ||
206 | if (data_len & 0x8000) { | |
207 | data_len &= 0x7fff; | |
208 | isResponse = true; | |
209 | } else { | |
210 | isResponse = false; | |
211 | } | |
212 | parity_len = (data_len-1)/8 + 1; | |
213 | ||
f71f4deb | 214 | if (tracepos + data_len + parity_len > traceLen) { |
215 | return traceLen; | |
4c3de57a | 216 | } |
4c3de57a MHS |
217 | uint8_t *frame = trace + tracepos; |
218 | tracepos += data_len; | |
219 | uint8_t *parityBytes = trace + tracepos; | |
220 | tracepos += parity_len; | |
221 | ||
ee1eadee | 222 | if (protocol == TOPAZ && !isResponse) { |
ef00343c | 223 | // topaz reader commands come in 1 or 9 separate frames with 7 or 8 Bits each. |
ee1eadee | 224 | // merge them: |
ef00343c | 225 | if (merge_topaz_reader_frames(timestamp, &duration, &tracepos, traceLen, trace, frame, topaz_reader_command, &data_len)) { |
226 | frame = topaz_reader_command; | |
227 | } | |
ee1eadee | 228 | } |
229 | ||
27eabcdc MHS |
230 | //Check the CRC status |
231 | uint8_t crcStatus = 2; | |
232 | ||
233 | if (data_len > 2) { | |
ee1eadee | 234 | switch (protocol) { |
235 | case ICLASS: | |
236 | crcStatus = iclass_CRC_check(isResponse, frame, data_len); | |
237 | break; | |
238 | case ISO_14443B: | |
ef00343c | 239 | case TOPAZ: |
a8904ebd | 240 | crcStatus = iso14443B_CRC_check(isResponse, frame, data_len); |
ee1eadee | 241 | break; |
4f131b53 | 242 | case PROTO_MIFARE: |
6612a5a2 | 243 | crcStatus = mifare_CRC_check(isResponse, frame, data_len); |
244 | break; | |
ee1eadee | 245 | case ISO_14443A: |
ef00343c | 246 | crcStatus = iso14443A_CRC_check(isResponse, frame, data_len); |
ee1eadee | 247 | break; |
248 | default: | |
249 | break; | |
27eabcdc MHS |
250 | } |
251 | } | |
252 | //0 CRC-command, CRC not ok | |
253 | //1 CRC-command, CRC ok | |
254 | //2 Not crc-command | |
9e8255d4 | 255 | |
4c3de57a | 256 | //--- Draw the data column |
9e8255d4 | 257 | //char line[16][110]; |
4c3de57a | 258 | char line[16][110]; |
9e8255d4 MHS |
259 | |
260 | for (int j = 0; j < data_len && j/16 < 16; j++) { | |
261 | ||
4c3de57a | 262 | uint8_t parityBits = parityBytes[j>>3]; |
1f065e1d | 263 | if (protocol != ISO_14443B && (isResponse || protocol == ISO_14443A) && (oddparity8(frame[j]) != ((parityBits >> (7-(j&0x0007))) & 0x01))) { |
9e8255d4 | 264 | snprintf(line[j/16]+(( j % 16) * 4),110, "%02x! ", frame[j]); |
4c3de57a | 265 | } else { |
ef00343c | 266 | snprintf(line[j/16]+(( j % 16) * 4), 110, " %02x ", frame[j]); |
9e8255d4 | 267 | } |
27eabcdc MHS |
268 | |
269 | } | |
05ddb52c | 270 | |
271 | if (markCRCBytes) { | |
272 | if(crcStatus == 0 || crcStatus == 1) | |
273 | {//CRC-command | |
274 | char *pos1 = line[(data_len-2)/16]+(((data_len-2) % 16) * 4); | |
275 | (*pos1) = '['; | |
276 | char *pos2 = line[(data_len)/16]+(((data_len) % 16) * 4); | |
277 | sprintf(pos2, "%c", ']'); | |
278 | } | |
9e8255d4 | 279 | } |
05ddb52c | 280 | |
9e8255d4 MHS |
281 | if(data_len == 0) |
282 | { | |
283 | if(data_len == 0){ | |
284 | sprintf(line[0],"<empty trace - possible error>"); | |
4c3de57a MHS |
285 | } |
286 | } | |
287 | //--- Draw the CRC column | |
4c3de57a | 288 | |
49726b40 | 289 | char *crc = (crcStatus == 0 ? "!crc" : (crcStatus == 1 ? " ok " : " ")); |
4c3de57a MHS |
290 | |
291 | EndOfTransmissionTimestamp = timestamp + duration; | |
292 | ||
6612a5a2 | 293 | if (protocol == PROTO_MIFARE) |
b957bcd3 | 294 | annotateMifare(explanation, sizeof(explanation), frame, data_len, parityBytes, parity_len, isResponse); |
6612a5a2 | 295 | |
4c3de57a MHS |
296 | if(!isResponse) |
297 | { | |
ee1eadee | 298 | switch(protocol) { |
299 | case ICLASS: annotateIclass(explanation,sizeof(explanation),frame,data_len); break; | |
300 | case ISO_14443A: annotateIso14443a(explanation,sizeof(explanation),frame,data_len); break; | |
301 | case ISO_14443B: annotateIso14443b(explanation,sizeof(explanation),frame,data_len); break; | |
302 | case TOPAZ: annotateTopaz(explanation,sizeof(explanation),frame,data_len); break; | |
303 | default: break; | |
304 | } | |
4c3de57a MHS |
305 | } |
306 | ||
9e8255d4 MHS |
307 | int num_lines = MIN((data_len - 1)/16 + 1, 16); |
308 | for (int j = 0; j < num_lines ; j++) { | |
4c3de57a | 309 | if (j == 0) { |
48ece4a7 | 310 | PrintAndLog(" %10d | %10d | %s |%-64s | %s| %s", |
4c3de57a MHS |
311 | (timestamp - first_timestamp), |
312 | (EndOfTransmissionTimestamp - first_timestamp), | |
313 | (isResponse ? "Tag" : "Rdr"), | |
314 | line[j], | |
315 | (j == num_lines-1) ? crc : " ", | |
316 | (j == num_lines-1) ? explanation : ""); | |
317 | } else { | |
48ece4a7 | 318 | PrintAndLog(" | | |%-64s | %s| %s", |
4c3de57a | 319 | line[j], |
ee1eadee | 320 | (j == num_lines-1) ? crc : " ", |
4c3de57a MHS |
321 | (j == num_lines-1) ? explanation : ""); |
322 | } | |
323 | } | |
b957bcd3 | 324 | |
2d7bdee3 | 325 | if (DecodeMifareData(frame, data_len, parityBytes, isResponse, mfData, &mfDataLen)) { |
747885a6 OM |
326 | memset(explanation, 0x00, sizeof(explanation)); |
327 | if (!isResponse) { | |
328 | explanation[0] = '>'; | |
329 | annotateIso14443a(&explanation[1], sizeof(explanation) - 1, mfData, mfDataLen); | |
330 | } | |
331 | uint8_t crcc = iso14443A_CRC_check(isResponse, mfData, mfDataLen); | |
28ee794f | 332 | PrintAndLog(" | * | dec |%-64s | %-4s| %s", |
7b215d14 | 333 | sprint_hex(mfData, mfDataLen), |
747885a6 | 334 | (crcc == 0 ? "!crc" : (crcc == 1 ? " ok " : " ")), |
28ee794f | 335 | (true) ? explanation : ""); |
7b215d14 | 336 | }; |
4c3de57a | 337 | |
a8904ebd | 338 | if (is_last_record(tracepos, trace, traceLen)) return traceLen; |
f71f4deb | 339 | |
a8904ebd | 340 | if (showWaitCycles && !isResponse && next_record_is_response(tracepos, trace)) { |
4c3de57a | 341 | uint32_t next_timestamp = *((uint32_t *)(trace + tracepos)); |
275d9e61 | 342 | PrintAndLog(" %10d | %10d | %s | fdt (Frame Delay Time): %d", |
a8904ebd | 343 | (EndOfTransmissionTimestamp - first_timestamp), |
344 | (next_timestamp - first_timestamp), | |
345 | " ", | |
346 | (next_timestamp - EndOfTransmissionTimestamp)); | |
4c3de57a | 347 | } |
f71f4deb | 348 | |
4c3de57a MHS |
349 | return tracepos; |
350 | } | |
351 | ||
f71f4deb | 352 | |
4c3de57a MHS |
353 | int CmdHFList(const char *Cmd) |
354 | { | |
355 | bool showWaitCycles = false; | |
05ddb52c | 356 | bool markCRCBytes = false; |
3bcc4d77 | 357 | bool loadFromFile = false; |
358 | bool saveToFile = false; | |
359 | char param1 = '\0'; | |
360 | char param2 = '\0'; | |
361 | char param3 = '\0'; | |
4c3de57a | 362 | char type[40] = {0}; |
3bcc4d77 | 363 | char filename[FILE_PATH_SIZE]; |
b689b842 | 364 | uint8_t protocol = 0; |
3bcc4d77 | 365 | |
366 | // parse command line | |
367 | int tlen = param_getstr(Cmd, 0, type, sizeof(type)); | |
368 | if (param_getlength(Cmd, 1) == 1) { | |
369 | param1 = param_getchar(Cmd, 1); | |
370 | } else { | |
371 | param_getstr(Cmd, 1, filename, sizeof(filename)); | |
372 | } | |
373 | if (param_getlength(Cmd, 2) == 1) { | |
374 | param2 = param_getchar(Cmd, 2); | |
375 | } else if (strlen(filename) == 0) { | |
376 | param_getstr(Cmd, 2, filename, sizeof(filename)); | |
377 | } | |
378 | if (param_getlength(Cmd, 3) == 1) { | |
379 | param3 = param_getchar(Cmd, 3); | |
380 | } else if (strlen(filename) == 0) { | |
381 | param_getstr(Cmd, 3, filename, sizeof(filename)); | |
382 | } | |
383 | ||
384 | // Validate param1 | |
385 | bool errors = false; | |
05ddb52c | 386 | |
387 | if(tlen == 0) { | |
4c3de57a MHS |
388 | errors = true; |
389 | } | |
05ddb52c | 390 | |
391 | if(param1 == 'h' | |
3bcc4d77 | 392 | || (param1 != 0 && param1 != 'f' && param1 != 'c' && param1 != 'l') |
393 | || (param2 != 0 && param2 != 'f' && param2 != 'c' && param2 != 'l') | |
394 | || (param3 != 0 && param3 != 'f' && param3 != 'c' && param3 != 'l')) { | |
4c3de57a MHS |
395 | errors = true; |
396 | } | |
05ddb52c | 397 | |
398 | if(!errors) { | |
ee1eadee | 399 | if(strcmp(type, "iclass") == 0) { |
b689b842 | 400 | protocol = ICLASS; |
4f131b53 | 401 | } else if(strcmp(type, "mf") == 0) { |
402 | protocol = PROTO_MIFARE; | |
ee1eadee | 403 | } else if(strcmp(type, "14a") == 0) { |
b689b842 | 404 | protocol = ISO_14443A; |
ee1eadee | 405 | } else if(strcmp(type, "14b") == 0) { |
b689b842 | 406 | protocol = ISO_14443B; |
3bcc4d77 | 407 | } else if(strcmp(type,"topaz") == 0) { |
ee1eadee | 408 | protocol = TOPAZ; |
3bcc4d77 | 409 | } else if(strcmp(type,"raw") == 0) { |
410 | protocol = -1; //No crc, no annotations | |
411 | } else if (strcmp(type, "save") == 0) { | |
412 | saveToFile = true; | |
ee1eadee | 413 | } else { |
b689b842 MHS |
414 | errors = true; |
415 | } | |
416 | } | |
3bcc4d77 | 417 | |
418 | if (param1 == 'f' || param2 == 'f' || param3 == 'f') { | |
419 | showWaitCycles = true; | |
420 | } | |
4c3de57a | 421 | |
3bcc4d77 | 422 | if (param1 == 'c' || param2 == 'c' || param3 == 'c') { |
423 | markCRCBytes = true; | |
424 | } | |
425 | ||
426 | if (param1 == 'l' || param2 == 'l' || param3 == 'l') { | |
427 | loadFromFile = true; | |
428 | } | |
429 | ||
430 | if ((loadFromFile || saveToFile) && strlen(filename) == 0) { | |
431 | errors = true; | |
432 | } | |
433 | ||
434 | if (loadFromFile && saveToFile) { | |
435 | errors = true; | |
436 | } | |
437 | ||
4c3de57a | 438 | if (errors) { |
3bcc4d77 | 439 | PrintAndLog("List or save protocol data."); |
440 | PrintAndLog("Usage: hf list <protocol> [f] [c] [l <filename>]"); | |
441 | PrintAndLog(" hf list save <filename>"); | |
92623113 | 442 | PrintAndLog(" f - show frame delay times as well"); |
05ddb52c | 443 | PrintAndLog(" c - mark CRC bytes"); |
3bcc4d77 | 444 | PrintAndLog(" l - load data from file instead of trace buffer"); |
445 | PrintAndLog(" save - save data to file"); | |
92623113 MHS |
446 | PrintAndLog("Supported <protocol> values:"); |
447 | PrintAndLog(" raw - just show raw data without annotations"); | |
337818f7 | 448 | PrintAndLog(" 14a - interpret data as iso14443a communications"); |
4f131b53 | 449 | PrintAndLog(" mf - interpret data as iso14443a communications and decrypt crypto1 stream"); |
41fdd0f0 | 450 | PrintAndLog(" 14b - interpret data as iso14443b communications"); |
4c3de57a | 451 | PrintAndLog(" iclass - interpret data as iclass communications"); |
ee1eadee | 452 | PrintAndLog(" topaz - interpret data as topaz communications"); |
4c3de57a MHS |
453 | PrintAndLog(""); |
454 | PrintAndLog("example: hf list 14a f"); | |
455 | PrintAndLog("example: hf list iclass"); | |
3bcc4d77 | 456 | PrintAndLog("example: hf list save myCardTrace.trc"); |
457 | PrintAndLog("example: hf list 14a l myCardTrace.trc"); | |
4c3de57a MHS |
458 | return 0; |
459 | } | |
b689b842 | 460 | |
4c3de57a | 461 | |
f71f4deb | 462 | uint8_t *trace; |
3bcc4d77 | 463 | uint32_t tracepos = 0; |
464 | uint32_t traceLen = 0; | |
465 | ||
466 | if (loadFromFile) { | |
467 | #define TRACE_CHUNK_SIZE (1<<16) // 64K to start with. Will be enough for BigBuf and some room for future extensions | |
468 | FILE *tracefile = NULL; | |
469 | size_t bytes_read; | |
470 | trace = malloc(TRACE_CHUNK_SIZE); | |
471 | if (trace == NULL) { | |
f71f4deb | 472 | PrintAndLog("Cannot allocate memory for trace"); |
f71f4deb | 473 | return 2; |
474 | } | |
3bcc4d77 | 475 | if ((tracefile = fopen(filename,"rb")) == NULL) { |
476 | PrintAndLog("Could not open file %s", filename); | |
477 | free(trace); | |
478 | return 0; | |
479 | } | |
480 | while (!feof(tracefile)) { | |
481 | bytes_read = fread(trace+traceLen, 1, TRACE_CHUNK_SIZE, tracefile); | |
482 | traceLen += bytes_read; | |
483 | if (!feof(tracefile)) { | |
484 | uint8_t *p = realloc(trace, traceLen + TRACE_CHUNK_SIZE); | |
485 | if (p == NULL) { | |
486 | PrintAndLog("Cannot allocate memory for trace"); | |
487 | free(trace); | |
488 | fclose(tracefile); | |
489 | return 2; | |
490 | } | |
491 | trace = p; | |
492 | } | |
493 | } | |
494 | fclose(tracefile); | |
495 | } else { | |
496 | trace = malloc(USB_CMD_DATA_SIZE); | |
497 | // Query for the size of the trace | |
498 | UsbCommand response; | |
babca445 | 499 | GetFromBigBuf(trace, USB_CMD_DATA_SIZE, 0, &response, -1, false); |
3bcc4d77 | 500 | traceLen = response.arg[2]; |
501 | if (traceLen > USB_CMD_DATA_SIZE) { | |
502 | uint8_t *p = realloc(trace, traceLen); | |
503 | if (p == NULL) { | |
504 | PrintAndLog("Cannot allocate memory for trace"); | |
505 | free(trace); | |
506 | return 2; | |
507 | } | |
508 | trace = p; | |
babca445 | 509 | GetFromBigBuf(trace, traceLen, 0, NULL, -1, false); |
3bcc4d77 | 510 | } |
f71f4deb | 511 | } |
4c3de57a | 512 | |
3bcc4d77 | 513 | if (saveToFile) { |
514 | FILE *tracefile = NULL; | |
515 | if ((tracefile = fopen(filename,"wb")) == NULL) { | |
516 | PrintAndLog("Could not create file %s", filename); | |
517 | return 1; | |
518 | } | |
519 | fwrite(trace, 1, traceLen, tracefile); | |
520 | PrintAndLog("Recorded Activity (TraceLen = %d bytes) written to file %s", traceLen, filename); | |
521 | fclose(tracefile); | |
522 | } else { | |
523 | PrintAndLog("Recorded Activity (TraceLen = %d bytes)", traceLen); | |
524 | PrintAndLog(""); | |
525 | PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer"); | |
526 | PrintAndLog("iso14443a - All times are in carrier periods (1/13.56Mhz)"); | |
527 | PrintAndLog("iClass - Timings are not as accurate"); | |
528 | PrintAndLog(""); | |
529 | PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |"); | |
530 | PrintAndLog("------------|------------|-----|-----------------------------------------------------------------|-----|--------------------|"); | |
531 | ||
532 | ClearAuthData(); | |
533 | while(tracepos < traceLen) | |
534 | { | |
535 | tracepos = printTraceLine(tracepos, traceLen, trace, protocol, showWaitCycles, markCRCBytes); | |
536 | } | |
4c3de57a | 537 | } |
f71f4deb | 538 | |
539 | free(trace); | |
4c3de57a MHS |
540 | return 0; |
541 | } | |
542 | ||
8ceb6b03 | 543 | int CmdHFSearch(const char *Cmd){ |
544 | int ans = 0; | |
6ce0e538 | 545 | PrintAndLog(""); |
fe842bed | 546 | ans = CmdHF14AInfo("s"); |
6ce0e538 | 547 | if (ans > 0) { |
548 | PrintAndLog("\nValid ISO14443A Tag Found - Quiting Search\n"); | |
549 | return ans; | |
ff4fdb32 | 550 | } |
aa53efc3 | 551 | ans = HFiClassReader("", false, false); |
ff4fdb32 | 552 | if (ans) { |
aa53efc3 | 553 | PrintAndLog("\nValid iClass Tag (or PicoPass Tag) Found - Quiting Search\n"); |
ff4fdb32 | 554 | return ans; |
555 | } | |
979c7655 | 556 | ans = HF15Reader("", false); |
6ce0e538 | 557 | if (ans) { |
979c7655 | 558 | PrintAndLog("\nValid ISO15693 Tag Found - Quiting Search\n"); |
6ce0e538 | 559 | return ans; |
560 | } | |
979c7655 | 561 | //14b is longest test currently (and rarest chip type) ... put last |
562 | ans = HF14BInfo(false); | |
6ce0e538 | 563 | if (ans) { |
979c7655 | 564 | PrintAndLog("\nValid ISO14443B Tag Found - Quiting Search\n"); |
6ce0e538 | 565 | return ans; |
566 | } | |
ff4fdb32 | 567 | PrintAndLog("\nno known/supported 13.56 MHz tags found\n"); |
8ceb6b03 | 568 | return 0; |
569 | } | |
7fe9b0b7 | 570 | |
0472d76d | 571 | int CmdHFSnoop(const char *Cmd) |
572 | { | |
573 | char * pEnd; | |
574 | UsbCommand c = {CMD_HF_SNIFFER, {strtol(Cmd, &pEnd,0),strtol(pEnd, &pEnd,0),0}}; | |
575 | SendCommand(&c); | |
576 | return 0; | |
577 | } | |
578 | ||
7fe9b0b7 | 579 | static command_t CommandTable[] = |
580 | { | |
05ddb52c | 581 | {"help", CmdHelp, 1, "This help"}, |
582 | {"14a", CmdHF14A, 1, "{ ISO14443A RFIDs... }"}, | |
583 | {"14b", CmdHF14B, 1, "{ ISO14443B RFIDs... }"}, | |
584 | {"15", CmdHF15, 1, "{ ISO15693 RFIDs... }"}, | |
585 | {"epa", CmdHFEPA, 1, "{ German Identification Card... }"}, | |
3c5fce2b | 586 | {"emv", CmdHFEMV, 1, "{ EMV cards... }"}, |
05ddb52c | 587 | {"legic", CmdHFLegic, 0, "{ LEGIC RFIDs... }"}, |
588 | {"iclass", CmdHFiClass, 1, "{ ICLASS RFIDs... }"}, | |
589 | {"mf", CmdHFMF, 1, "{ MIFARE RFIDs... }"}, | |
590 | {"mfu", CmdHFMFUltra, 1, "{ MIFARE Ultralight RFIDs... }"}, | |
591 | {"topaz", CmdHFTopaz, 1, "{ TOPAZ (NFC Type 1) RFIDs... }"}, | |
592 | {"tune", CmdHFTune, 0, "Continuously measure HF antenna tuning"}, | |
593 | {"list", CmdHFList, 1, "List protocol data in trace buffer"}, | |
7624e8b2 | 594 | {"search", CmdHFSearch, 1, "Search for known HF tags [preliminary]"}, |
b2fe0e77 | 595 | {"snoop", CmdHFSnoop, 0, "<samples to skip (10000)> <triggers to skip (1)> Generic HF Snoop"}, |
05ddb52c | 596 | {NULL, NULL, 0, NULL} |
7fe9b0b7 | 597 | }; |
598 | ||
599 | int CmdHF(const char *Cmd) | |
600 | { | |
601 | CmdsParse(CommandTable, Cmd); | |
602 | return 0; | |
603 | } | |
604 | ||
605 | int CmdHelp(const char *Cmd) | |
606 | { | |
607 | CmdsHelp(CommandTable); | |
608 | return 0; | |
609 | } |