]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhficlass.c
Extended iClass support with Card and Reader emulation!
[proxmark3-svn] / client / cmdhficlass.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>, Hagen Fritsch
3 // Copyright (C) 2011 Gerhard de Koning Gans
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 iClass commands
10 //-----------------------------------------------------------------------------
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "iso14443crc.h" // Can also be used for iClass, using 0xE012 as CRC-type
16 #include "data.h"
17 #include "proxusb.h"
18 #include "ui.h"
19 #include "cmdparser.h"
20 #include "cmdhficlass.h"
21 #include "common.h"
22
23 static int CmdHelp(const char *Cmd);
24
25 int CmdHFiClassList(const char *Cmd)
26 {
27 uint8_t got[1920];
28 GetFromBigBuf(got, sizeof(got));
29
30 PrintAndLog("recorded activity:");
31 PrintAndLog(" ETU :rssi: who bytes");
32 PrintAndLog("---------+----+----+-----------");
33
34 int i = 0;
35 int prev = -1;
36
37 for (;;) {
38 if(i >= 1900) {
39 break;
40 }
41
42 bool isResponse;
43 int timestamp = *((uint32_t *)(got+i));
44 if (timestamp & 0x80000000) {
45 timestamp &= 0x7fffffff;
46 isResponse = 1;
47 } else {
48 isResponse = 0;
49 }
50
51 int metric = 0;
52 int parityBits = *((uint32_t *)(got+i+4));
53 // 4 bytes of additional information...
54 // maximum of 32 additional parity bit information
55 //
56 // TODO:
57 // at each quarter bit period we can send power level (16 levels)
58 // or each half bit period in 256 levels.
59
60
61 int len = got[i+8];
62
63 if (len > 100) {
64 break;
65 }
66 if (i + len >= 1900) {
67 break;
68 }
69
70 uint8_t *frame = (got+i+9);
71
72 // Break and stick with current result if buffer was not completely full
73 if (frame[0] == 0x44 && frame[1] == 0x44 && frame[3] == 0x44) { break; }
74
75 char line[1000] = "";
76 int j;
77 for (j = 0; j < len; j++) {
78 int oddparity = 0x01;
79 int k;
80
81 for (k=0;k<8;k++) {
82 oddparity ^= (((frame[j] & 0xFF) >> k) & 0x01);
83 }
84
85 //if((parityBits >> (len - j - 1)) & 0x01) {
86 if (isResponse && (oddparity != ((parityBits >> (len - j - 1)) & 0x01))) {
87 sprintf(line+(j*4), "%02x! ", frame[j]);
88 }
89 else {
90 sprintf(line+(j*4), "%02x ", frame[j]);
91 }
92 }
93
94 char *crc;
95 crc = "";
96 if (len > 2) {
97 uint8_t b1, b2;
98 for (j = 0; j < (len - 1); j++) {
99 // gives problems... search for the reason..
100 /*if(frame[j] == 0xAA) {
101 switch(frame[j+1]) {
102 case 0x01:
103 crc = "[1] Two drops close after each other";
104 break;
105 case 0x02:
106 crc = "[2] Potential SOC with a drop in second half of bitperiod";
107 break;
108 case 0x03:
109 crc = "[3] Segment Z after segment X is not possible";
110 break;
111 case 0x04:
112 crc = "[4] Parity bit of a fully received byte was wrong";
113 break;
114 default:
115 crc = "[?] Unknown error";
116 break;
117 }
118 break;
119 }*/
120 }
121
122 if (strlen(crc)==0) {
123 if(!isResponse && len == 4) {
124 // Rough guess that this is a command from the reader
125 // For iClass the command byte is not part of the CRC
126 ComputeCrc14443(CRC_ICLASS, &frame[1], len-3, &b1, &b2);
127 }
128 else {
129 // For other data.. CRC might not be applicable (UPDATE commands etc.)
130 ComputeCrc14443(CRC_ICLASS, frame, len-2, &b1, &b2);
131 }
132 //printf("%1x %1x",(unsigned)b1,(unsigned)b2);
133 if (b1 != frame[len-2] || b2 != frame[len-1]) {
134 crc = (isResponse & (len < 8)) ? "" : " !crc";
135 } else {
136 crc = "";
137 }
138 }
139 } else {
140 crc = ""; // SHORT
141 }
142
143 char metricString[100];
144 if (isResponse) {
145 sprintf(metricString, "%3d", metric);
146 } else {
147 strcpy(metricString, " ");
148 }
149
150 PrintAndLog(" +%7d: %s: %s %s %s",
151 (prev < 0 ? 0 : (timestamp - prev)),
152 metricString,
153 (isResponse ? "TAG" : " "), line, crc);
154
155 prev = timestamp;
156 i += (len + 9);
157 }
158 return 0;
159 }
160
161 /*void iso14a_set_timeout(uint32_t timeout) {
162 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_SET_TIMEOUT, 0, timeout}};
163 SendCommand(&c);
164 }*/
165
166 int CmdHFiClassSnoop(const char *Cmd)
167 {
168 UsbCommand c = {CMD_SNOOP_ICLASS};
169 SendCommand(&c);
170 return 0;
171 }
172
173 int CmdHFiClassSim(const char *Cmd)
174 {
175 uint8_t simType = 0;
176 uint8_t CSN[8] = {0, 0, 0, 0, 0, 0, 0, 0};
177
178 if (strlen(Cmd)<2) {
179 PrintAndLog("Usage: hf iclass sim <sim type> <CSN (16 hex symbols)>");
180 PrintAndLog(" sample: hf iclass sim 0 031FEC8AF7FF12E0");
181 return 0;
182 }
183
184 simType = param_get8(Cmd, 0);
185 if (param_gethex(Cmd, 1, CSN, 16)) {
186 PrintAndLog("A CSN should consist of 16 HEX symbols");
187 return 1;
188 }
189 PrintAndLog("--simtype:%02x csn:%s", simType, sprint_hex(CSN, 8));
190
191 UsbCommand c = {CMD_SIMULATE_TAG_ICLASS, {simType}};
192 memcpy(c.d.asBytes, CSN, 8);
193 SendCommand(&c);
194
195 /*UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);
196 if (resp != NULL) {
197 uint8_t isOK = resp->arg[0] & 0xff;
198 PrintAndLog("isOk:%02x", isOK);
199 } else {
200 PrintAndLog("Command execute timeout");
201 }*/
202
203 return 0;
204 }
205
206 int CmdHFiClassReader(const char *Cmd)
207 {
208 uint8_t readerType = 0;
209
210 if (strlen(Cmd)<1) {
211 PrintAndLog("Usage: hf iclass reader <reader type>");
212 PrintAndLog(" sample: hf iclass reader 0");
213 return 0;
214 }
215
216 readerType = param_get8(Cmd, 0);
217 PrintAndLog("--readertype:%02x", readerType);
218
219 UsbCommand c = {CMD_READER_ICLASS, {readerType}};
220 //memcpy(c.d.asBytes, CSN, 8);
221 SendCommand(&c);
222
223 /*UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);
224 if (resp != NULL) {
225 uint8_t isOK = resp->arg[0] & 0xff;
226 PrintAndLog("isOk:%02x", isOK);
227 } else {
228 PrintAndLog("Command execute timeout");
229 }*/
230
231 return 0;
232 }
233
234 static command_t CommandTable[] =
235 {
236 {"help", CmdHelp, 1, "This help"},
237 {"list", CmdHFiClassList, 0, "List iClass history"},
238 {"snoop", CmdHFiClassSnoop, 0, "Eavesdrop iClass communication"},
239 {"sim", CmdHFiClassSim, 0, "Simulate iClass tag"},
240 {"reader", CmdHFiClassReader, 0, "Read an iClass tag"},
241 {NULL, NULL, 0, NULL}
242 };
243
244 int CmdHFiClass(const char *Cmd)
245 {
246 CmdsParse(CommandTable, Cmd);
247 return 0;
248 }
249
250 int CmdHelp(const char *Cmd)
251 {
252 CmdsHelp(CommandTable);
253 return 0;
254 }
Impressum, Datenschutz