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