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