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