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